35ec5ff8a9bb3c5fa91c86b20634491136ecd077
[jalview.git] / test / jalview / urls / CustomUrlProviderTest.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 static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.util.UrlConstants;
28
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.Vector;
32
33 import org.testng.annotations.Test;
34
35 public class CustomUrlProviderTest
36 {
37
38   private static final String cachedList = "TEST|http://someurl.blah/$DB_ACCESSION$|"
39           + "ANOTHER|http://test/t$SEQUENCE_ID$|"
40           + "TEST2|http://address/$SEQUENCE_ID$|SRS|"
41           + "http://theSRSlink/$SEQUENCE_ID$";
42
43   private static final HashMap<String, String> urlMap = new HashMap<String, String>()
44   {
45     {
46       put("TEST","http://someurl.blah/$DB_ACCESSION$");
47       put("ANOTHER","http://test/t$SEQUENCE_ID$");
48       put("TEST2", "http://address/$SEQUENCE_ID$");
49       put("SRS", "http://theSRSlink/$SEQUENCE_ID$");
50     }
51   };
52   
53   private static final String[] dlinks = {
54       "TEST|http://someurl.blah/$DB_ACCESSION$",
55       "ANOTHER|http://test/t$SEQUENCE_ID$",
56       "TEST2|http://address/$SEQUENCE_ID$",
57  UrlConstants.DEFAULT_STRING };
58
59   private static final Vector<String> displayLinks = new Vector<String>(
60           Arrays.asList(dlinks));
61
62   private static final String[] dlinks2 = { "a|http://x.y.z/$SEQUENCE_ID$" };
63
64   private static final Vector<String> displayLinks2 = new Vector<String>(
65           Arrays.asList(dlinks2));
66
67   private static final String[] list1 = { "a" };
68
69   private static final String[] list2 = { "http://x.y.z/$SEQUENCE_ID$" };
70
71   private static final Vector<String> names = new Vector<String>(
72           Arrays.asList(list1));
73
74   private static final Vector<String> urls = new Vector<String>(
75           Arrays.asList(list2));
76
77   /*
78    * Test default url is set and returned correctly
79    */
80   @Test(groups = { "Functional" })
81   public void testDefaultUrl()
82   {
83     UrlProviderI customProv = new CustomUrlProvider(cachedList);
84     
85     // default url can be set
86     assertTrue(customProv.setDefaultUrl("ANOTHER"));
87
88     // supplied replacement id must be more than 4 chars
89     String result = customProv.getDefaultUrl("123");
90     assertEquals(null, result);
91
92     // default url can be retrieved given a sequence id
93     result = customProv.getDefaultUrl("seqid");
94     assertEquals("http://test/tseqid", result);
95
96     // if there is no default url it sets the default to null
97     assertFalse(customProv.setDefaultUrl("No default"));
98     result = customProv.getDefaultUrl("testid");
99     assertEquals(null, result);
100     
101     // choosing the default picks the DEFAULT_STRING option
102     customProv.chooseDefaultUrl();
103     result = customProv.getDefaultUrl("seqid");
104     assertEquals(
105             UrlConstants.DEFAULT_STRING.split("\\|")[1].split("\\$")[0]
106             + "seqid",
107             result);
108   }
109
110   /*
111    * Test urls are set and returned correctly
112    */
113   @Test(groups = { "Functional" })
114   public void testUrlLinks()
115   {
116     // creation from cached url list works + old links upgraded
117     UrlProviderI customProv = new CustomUrlProvider(cachedList);
118     assertTrue(displayLinks.containsAll(customProv.getLinksForMenu()));
119
120     // creation from map works + old links upgraded
121     UrlProviderI customProv2 = new CustomUrlProvider(urlMap);
122     assertTrue(displayLinks.containsAll(customProv2.getLinksForMenu()));
123
124     // writing url links as a string works
125     // because UrlProvider does not guarantee order of links, we can't just
126     // compare the output of writeUrlsAsString to a string, hence the hoops here
127     String result = customProv.writeUrlsAsString();
128     UrlProviderI up = new CustomUrlProvider(result);
129     assertTrue(displayLinks.containsAll(up.getLinksForMenu()));
130
131     result = customProv2.writeUrlsAsString();
132     UrlProviderI up2 = new CustomUrlProvider(result);
133     assertTrue(displayLinks.containsAll(up2.getLinksForMenu()));
134   }
135 }