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