JAL-3746 apply copyright to tests
[jalview.git] / test / jalview / urls / UrlProviderTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.urls;
22
23 import jalview.urls.api.UrlProviderI;
24 import jalview.urls.desktop.DesktopUrlProviderFactory;
25 import jalview.util.UrlConstants;
26
27 import java.io.BufferedWriter;
28 import java.io.File;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.util.List;
32
33 import org.testng.Assert;
34 import org.testng.annotations.BeforeMethod;
35 import org.testng.annotations.Test;
36
37 public class UrlProviderTest
38 {
39
40   // Test identifiers.org download file
41   private static final String testIdOrgString = "{\"Local\": [{\"id\":\"MIR:00000002\",\"name\":\"ChEBI\",\"pattern\":\"^CHEBI:\\d+$\","
42           + "\"definition\":\"Chemical Entities of Biological Interest (ChEBI)\",\"prefix\":\"chebi\","
43           + "\"url\":\"http://identifiers.org/chebi\"},{\"id\":\"MIR:00000005\",\"name\":\"UniProt Knowledgebase\","
44           + "\"pattern\":\"^([A-N,R-Z][0-9]([A-Z][A-Z, 0-9][A-Z, 0-9][0-9]){1,2})|([O,P,Q][0-9][A-Z, 0-9][A-Z, 0-9][A-Z, 0-9][0-9])(\\.\\d+)?$\","
45           + "\"definition\":\"The UniProt Knowledgebase (UniProtKB)\",\"prefix\":\"uniprot\",\"url\":\"http://identifiers.org/uniprot\"},"
46           + "{\"id\":\"MIR:00000011\",\"name\":\"InterPro\",\"pattern\":\"^IPR\\d{6}$\",\"definition\":\"InterPro\",\"prefix\":\"interpro\","
47           + "\"url\":\"http://identifiers.org/interpro\"},"
48           + "{\"id\":\"MIR:00000372\",\"name\":\"ENA\",\"pattern\":\"^[A-Z]+[0-9]+(\\.\\d+)?$\",\"definition\":\"The European Nucleotide Archive (ENA),\""
49           + "\"prefix\":\"ena.embl\",\"url\":\"http://identifiers.org/ena.embl\"}]}";
50
51   private UrlProviderI prov;
52
53   @BeforeMethod(alwaysRun = true)
54   public void setup()
55   {
56     // make a dummy identifiers.org download file
57     File temp = null;
58
59     try
60     {
61       temp = File.createTempFile("tempfile", ".tmp");
62       temp.deleteOnExit();
63       BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
64       bw.write(testIdOrgString);
65       bw.close();
66     } catch (IOException e)
67     {
68       System.out.println(
69               "Error initialising UrlProviderTest test: " + e.getMessage());
70     }
71
72     IdOrgSettings.setDownloadLocation(temp.getPath());
73
74     String defaultUrlString = "No default";
75     String cachedUrlList = "MIR:00000005|MIR:00000011|Test1|http://blah.blah/$SEQUENCE_ID$|"
76             + "Test2|http://test2/$DB_ACCESSION$|Test3|http://test3/$SEQUENCE_ID$";
77     String userUrlList = "MIR:00000372|Test4|httpL//another.url/$SEQUENCE_ID$";
78
79     DesktopUrlProviderFactory factory = new DesktopUrlProviderFactory(
80             defaultUrlString, cachedUrlList, userUrlList);
81     prov = factory.createUrlProvider();
82   }
83
84   @Test(groups = { "Functional" })
85   public void testInitUrlProvider()
86   {
87     String emblUrl = UrlConstants.DEFAULT_STRING.substring(
88             UrlConstants.DEFAULT_STRING.indexOf(UrlConstants.SEP) + 1,
89             UrlConstants.DEFAULT_STRING.length());
90
91     // chooses EMBL url when default Url id does not exist in provided url lists
92     Assert.assertEquals(prov.getPrimaryUrlId(), UrlConstants.DEFAULT_LABEL);
93     Assert.assertEquals(prov.getPrimaryUrl("FER_CAPAN"),
94             emblUrl.replace("$SEQUENCE_ID$", "FER_CAPAN"));
95
96     List<String> menulinks = prov.getLinksForMenu();
97     List<UrlLinkDisplay> allLinks = prov.getLinksForTable();
98
99     // 9 links in provider - 4 from id file, 4 custom links, 1 additional
100     // default
101     Assert.assertEquals(allLinks.size(), 9);
102
103     // 6 links in menu (cachedUrlList) + new default
104     Assert.assertEquals(menulinks.size(), 6);
105
106     Assert.assertTrue(
107             menulinks.contains("Test1|http://blah.blah/$SEQUENCE_ID$"));
108     Assert.assertTrue(
109             menulinks.contains("Test2|http://test2/$DB_ACCESSION$"));
110     Assert.assertTrue(
111             menulinks.contains("Test3|http://test3/$SEQUENCE_ID$"));
112     Assert.assertTrue(menulinks.contains(
113             "UniProt Knowledgebase|http://identifiers.org/uniprot/$DB_ACCESSION$|uniprot"));
114     Assert.assertTrue(menulinks.contains(
115             "InterPro|http://identifiers.org/interpro/$DB_ACCESSION$|interpro"));
116     Assert.assertTrue(menulinks.contains(
117             UrlConstants.DEFAULT_LABEL + UrlConstants.SEP + emblUrl));
118   }
119
120   @Test(groups = { "Functional" })
121   public void testSetDefaultUrl()
122   {
123     // set custom url as default
124     Assert.assertTrue(prov.setPrimaryUrl("Test1"));
125     Assert.assertEquals(prov.getPrimaryUrlId(), "Test1");
126
127     // set identifiers url as default
128     Assert.assertTrue(prov.setPrimaryUrl("MIR:00000011"));
129     Assert.assertEquals(prov.getPrimaryUrlId(), "MIR:00000011");
130   }
131
132   @Test(
133     groups =
134     { "Functional" },
135     expectedExceptions =
136     { IllegalArgumentException.class })
137   public void testSetDefaultUrlWrongly()
138   {
139     // don't allow default to be a non-key
140     prov.setPrimaryUrl("not-a-key");
141   }
142 }