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