1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.urls

File CustomUrlProviderTest.java

 

Code metrics

0
34
4
1
161
94
4
0.12
8.5
4
1

Classes

Class
Line #
Actions
CustomUrlProviderTest 36 34 4
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

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  4 toggle {
50  4 put("TEST", "http://someurl.blah/$DB_ACCESSION$");
51  4 put("ANOTHER", "http://test/t$SEQUENCE_ID$");
52  4 put("TEST2", "http://address/$SEQUENCE_ID$");
53  4 put("SRS", "http://theSRSlink/$SEQUENCE_ID$");
54    }
55    };
56   
57    private static final HashMap<String, String> unselUrlMap = new HashMap<String, String>()
58    {
 
59  4 toggle {
60  4 put("NON1", "http://x/y/$DB_ACCESSION$");
61  4 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$", 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 = {
80    "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  1 toggle @Test(groups = { "Functional" })
99    public void testDefaultUrl()
100    {
101  1 UrlProviderI customProv = new CustomUrlProvider(cachedList,
102    unselectedList);
103   
104    // default url can be set
105  1 assertTrue(customProv.setPrimaryUrl("ANOTHER"));
106   
107    // supplied replacement id must be more than 4 chars
108  1 String result = customProv.getPrimaryUrl("123");
109  1 assertEquals(null, result);
110   
111    // default url can be retrieved given a sequence id
112  1 result = customProv.getPrimaryUrl("seqid");
113  1 assertEquals("http://test/tseqid", result);
114   
115    // if there is no default url it sets the default to null
116  1 assertFalse(customProv.setPrimaryUrl("No default"));
117  1 result = customProv.getPrimaryUrl("testid");
118  1 assertEquals(null, result);
119   
120    // choosing the default picks the DEFAULT_STRING option
121  1 customProv.choosePrimaryUrl();
122  1 result = customProv.getPrimaryUrl("seqid");
123  1 assertEquals(UrlConstants.DEFAULT_STRING.split("\\|")[1].split("\\$")[0]
124    + "seqid", result);
125    }
126   
127    /*
128    * Test urls are set and returned correctly
129    */
 
130  1 toggle @Test(groups = { "Functional" })
131    public void testUrlLinks()
132    {
133    // creation from cached url list works + old links upgraded
134  1 UrlProviderI customProv = new CustomUrlProvider(cachedList,
135    unselectedList);
136  1 assertTrue(displayLinks.containsAll(customProv.getLinksForMenu()));
137   
138    // creation from map works + old links upgraded
139  1 UrlProviderI customProv2 = new CustomUrlProvider(urlMap, unselUrlMap);
140  1 assertTrue(displayLinks.containsAll(customProv2.getLinksForMenu()));
141   
142    // writing url links as a string works
143    // because UrlProvider does not guarantee order of links, we can't just
144    // compare the output of writeUrlsAsString to a string, hence the hoops here
145  1 String result = customProv.writeUrlsAsString(true);
146  1 UrlProviderI up = new CustomUrlProvider(result, "");
147  1 assertTrue(displayLinks.containsAll(up.getLinksForMenu()));
148   
149  1 result = customProv.writeUrlsAsString(false);
150  1 up = new CustomUrlProvider("", result);
151  1 assertTrue(unselDisplayLinks.containsAll(up.getLinksForMenu()));
152   
153  1 result = customProv2.writeUrlsAsString(true);
154  1 UrlProviderI up2 = new CustomUrlProvider(result, "");
155  1 assertTrue(displayLinks.containsAll(up2.getLinksForMenu()));
156   
157  1 result = customProv2.writeUrlsAsString(false);
158  1 up2 = new CustomUrlProvider("", result);
159  1 assertTrue(displayLinks.containsAll(up2.getLinksForMenu()));
160    }
161    }