c7717c98b9f4b3a2c06b126a46c6275f75aadcf9
[jalview.git] / test / jalview / urls / UrlProviderTest.java
1 package jalview.urls;
2
3 import jalview.util.UrlConstants;
4
5 import java.io.BufferedWriter;
6 import java.io.File;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.util.List;
10 import java.util.Vector;
11
12 import org.testng.Assert;
13 import org.testng.annotations.BeforeMethod;
14 import org.testng.annotations.Test;
15
16
17 public class UrlProviderTest {
18   
19   // Test identifiers.org download file
20   private static final String testIdOrgString = "[{\"id\":\"MIR:00000002\",\"name\":\"ChEBI\",\"pattern\":\"^CHEBI:\\d+$\","
21          + "\"definition\":\"Chemical Entities of Biological Interest (ChEBI)\",\"prefix\":\"chebi\","
22          + "\"url\":\"http://identifiers.org/chebi\"},{\"id\":\"MIR:00000005\",\"name\":\"UniProt Knowledgebase\","
23          + "\"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+)?$\","
24          + "\"definition\":\"The UniProt Knowledgebase (UniProtKB)\",\"prefix\":\"uniprot\",\"url\":\"http://identifiers.org/uniprot\"},"
25          + "{\"id\":\"MIR:00000011\",\"name\":\"InterPro\",\"pattern\":\"^IPR\\d{6}$\",\"definition\":\"InterPro\",\"prefix\":\"interpro\","
26          + "\"url\":\"http://identifiers.org/interpro\"},"
27          + "{\"id\":\"MIR:00000372\",\"name\":\"ENA\",\"pattern\":\"^[A-Z]+[0-9]+(\\.\\d+)?$\",\"definition\":\"The European Nucleotide Archive (ENA),\""
28          + "\"prefix\":\"ena.embl\",\"url\":\"http://identifiers.org/ena.embl\"}]";
29
30   private UrlProviderI prov;
31
32   @BeforeMethod(alwaysRun = true)
33   public void setup()
34   {
35    // make a dummy identifiers.org download file
36    File temp = null;
37
38    try
39    {
40      temp = File.createTempFile("tempfile", ".tmp");
41      temp.deleteOnExit();
42      BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
43      bw.write(testIdOrgString);
44      bw.close();
45    } catch (IOException e)
46    {
47       System.out.println("Error initialising UrlProviderTest test: "
48               + e.getMessage());
49    }
50
51    IdOrgSettings.setDownloadLocation(temp.getPath());
52
53     String defaultUrlString = "No default";
54     String cachedUrlList = "MIR:00000005|MIR:00000011|Test1|http://blah.blah/$SEQUENCE_ID$|"
55             + "Test2|http://test2/$DB_ACCESSION$|Test3|http://test3/$SEQUENCE_ID$";
56     String userUrlList = "MIR:00000372|Test4|httpL//another.url/$SEQUENCE_ID$";
57
58     DesktopUrlProviderFactory factory = new DesktopUrlProviderFactory(
59             defaultUrlString, cachedUrlList, userUrlList);
60     prov = factory.createUrlProvider();
61  }
62
63   @Test(groups = { "Functional" })
64   public void testInitUrlProvider()
65   {
66     String emblUrl = UrlConstants.DEFAULT_STRING.substring(
67             UrlConstants.DEFAULT_STRING.indexOf(UrlConstants.SEP) + 1,
68             UrlConstants.DEFAULT_STRING.length());
69
70     // chooses EMBL url when default Url id does not exist in provided url lists
71     Assert.assertEquals(prov.getDefaultUrlId(), UrlConstants.DEFAULT_LABEL);
72     Assert.assertEquals(prov.getDefaultUrl("FER_CAPAN"),
73             emblUrl.replace("$SEQUENCE_ID$", "FER_CAPAN"));
74
75     Vector<String> menulinks = prov.getLinksForMenu();
76     List<UrlLinkDisplay> allLinks = prov.getLinksForTable();
77
78     // 9 links in provider - 4 from id file, 4 custom links, 1 additional
79     // default
80     Assert.assertEquals(allLinks.size(), 9);
81
82     // 6 links in menu (cachedUrlList) + new default
83     Assert.assertEquals(menulinks.size(), 6);
84
85     Assert.assertTrue(menulinks
86             .contains("Test1|http://blah.blah/$SEQUENCE_ID$"));
87     Assert.assertTrue(menulinks
88             .contains("Test2|http://test2/$DB_ACCESSION$"));
89     Assert.assertTrue(menulinks
90             .contains("Test3|http://test3/$SEQUENCE_ID$"));
91     Assert.assertTrue(menulinks
92             .contains("UniProt Knowledgebase|http://identifiers.org/uniprot/$DB_ACCESSION$"));
93     Assert.assertTrue(menulinks
94             .contains("InterPro|http://identifiers.org/interpro/$DB_ACCESSION$"));
95     Assert.assertTrue(menulinks.contains(UrlConstants.DEFAULT_LABEL
96             + UrlConstants.SEP + emblUrl));
97   }
98
99   @Test(groups = { "Functional" })
100   public void testSetDefaultUrl()
101   {
102     // set custom url as default
103     Assert.assertTrue(prov.setDefaultUrl("Test1"));
104     Assert.assertEquals(prov.getDefaultUrlId(), "Test1");
105
106     // set identifiers url as default
107     Assert.assertTrue(prov.setDefaultUrl("MIR:00000011"));
108     Assert.assertEquals(prov.getDefaultUrlId(), "MIR:00000011");
109   }
110
111   @Test(
112     groups = { "Functional" },
113     expectedExceptions = { IllegalArgumentException.class })
114   public void testSetDefaultUrlWrongly()
115   {
116     // don't allow default to be a non-key
117     prov.setDefaultUrl("not-a-key");
118   }
119 }