JAL-2316 Restructure identifiers.org download
[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 String unselectedList = "NON1|http://x/y/$DB_ACCESSION$|"
44           + "NON2|http://a/b/c/$DB_ACCESSION";
45
46   private static final HashMap<String, String> urlMap = new HashMap<String, String>()
47   {
48     {
49       put("TEST","http://someurl.blah/$DB_ACCESSION$");
50       put("ANOTHER","http://test/t$SEQUENCE_ID$");
51       put("TEST2", "http://address/$SEQUENCE_ID$");
52       put("SRS", "http://theSRSlink/$SEQUENCE_ID$");
53     }
54   };
55   
56   private static final HashMap<String, String> unselUrlMap = new HashMap<String, String>()
57   {
58     {
59       put("NON1", "http://x/y/$DB_ACCESSION$");
60       put("NON2", "http://a/b/c/$DB_ACCESSION");
61     }
62   };
63
64   private static final String[] dlinks = {
65       "TEST|http://someurl.blah/$DB_ACCESSION$",
66       "ANOTHER|http://test/t$SEQUENCE_ID$",
67       "TEST2|http://address/$SEQUENCE_ID$",
68  UrlConstants.DEFAULT_STRING };
69
70   private static final String[] unselDlinks = {
71       "NON1|http://x/y/$DB_ACCESSION$", "NON2|http://a/b/c/$DB_ACCESSION" };
72
73   private static final Vector<String> displayLinks = new Vector<String>(
74           Arrays.asList(dlinks));
75
76   private static final Vector<String> unselDisplayLinks = new Vector<String>(
77           Arrays.asList(unselDlinks));
78
79   private static final String[] dlinks2 = { "a|http://x.y.z/$SEQUENCE_ID$" };
80
81   private static final Vector<String> displayLinks2 = new Vector<String>(
82           Arrays.asList(dlinks2));
83
84   private static final String[] list1 = { "a" };
85
86   private static final String[] list2 = { "http://x.y.z/$SEQUENCE_ID$" };
87
88   private static final Vector<String> names = new Vector<String>(
89           Arrays.asList(list1));
90
91   private static final Vector<String> urls = new Vector<String>(
92           Arrays.asList(list2));
93
94   /*
95    * Test default url is set and returned correctly
96    */
97   @Test(groups = { "Functional" })
98   public void testDefaultUrl()
99   {
100     UrlProviderI customProv = new CustomUrlProvider(cachedList,
101             unselectedList);
102     
103     // default url can be set
104     assertTrue(customProv.setDefaultUrl("ANOTHER"));
105
106     // supplied replacement id must be more than 4 chars
107     String result = customProv.getDefaultUrl("123");
108     assertEquals(null, result);
109
110     // default url can be retrieved given a sequence id
111     result = customProv.getDefaultUrl("seqid");
112     assertEquals("http://test/tseqid", result);
113
114     // if there is no default url it sets the default to null
115     assertFalse(customProv.setDefaultUrl("No default"));
116     result = customProv.getDefaultUrl("testid");
117     assertEquals(null, result);
118     
119     // choosing the default picks the DEFAULT_STRING option
120     customProv.chooseDefaultUrl();
121     result = customProv.getDefaultUrl("seqid");
122     assertEquals(
123             UrlConstants.DEFAULT_STRING.split("\\|")[1].split("\\$")[0]
124             + "seqid",
125             result);
126   }
127
128   /*
129    * Test urls are set and returned correctly
130    */
131   @Test(groups = { "Functional" })
132   public void testUrlLinks()
133   {
134     // creation from cached url list works + old links upgraded
135     UrlProviderI customProv = new CustomUrlProvider(cachedList,
136             unselectedList);
137     assertTrue(displayLinks.containsAll(customProv.getLinksForMenu()));
138
139     // creation from map works + old links upgraded
140     UrlProviderI customProv2 = new CustomUrlProvider(urlMap, unselUrlMap);
141     assertTrue(displayLinks.containsAll(customProv2.getLinksForMenu()));
142
143     // writing url links as a string works
144     // because UrlProvider does not guarantee order of links, we can't just
145     // compare the output of writeUrlsAsString to a string, hence the hoops here
146     String result = customProv.writeUrlsAsString(true);
147     UrlProviderI up = new CustomUrlProvider(result, "");
148     assertTrue(displayLinks.containsAll(up.getLinksForMenu()));
149
150     result = customProv.writeUrlsAsString(false);
151     up = new CustomUrlProvider("", result);
152     assertTrue(unselDisplayLinks.containsAll(up.getLinksForMenu()));
153
154     result = customProv2.writeUrlsAsString(true);
155     UrlProviderI up2 = new CustomUrlProvider(result, "");
156     assertTrue(displayLinks.containsAll(up2.getLinksForMenu()));
157
158     result = customProv2.writeUrlsAsString(false);
159     up2 = new CustomUrlProvider("", result);
160     assertTrue(displayLinks.containsAll(up2.getLinksForMenu()));
161   }
162 }