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