Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.urls

File UrlLinkTableModelTest.java

 

Code metrics

22
104
10
1
348
226
25
0.24
10.4
10
2.5

Classes

Class Line # Actions
UrlLinkTableModelTest 44 104 25 1
0.9926470599.3%
 

Contributing tests

This file is covered by 9 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   
22    package jalview.urls;
23   
24    import static jalview.util.UrlConstants.DELIM;
25    import static jalview.util.UrlConstants.SEP;
26    import static jalview.util.UrlConstants.SEQUENCE_ID;
27   
28    import jalview.urls.api.UrlProviderI;
29    import jalview.util.MessageManager;
30   
31    import java.io.BufferedWriter;
32    import java.io.File;
33    import java.io.FileWriter;
34    import java.io.IOException;
35    import java.util.ArrayList;
36    import java.util.List;
37   
38    import javax.swing.event.TableModelListener;
39   
40    import org.testng.Assert;
41    import org.testng.annotations.BeforeMethod;
42    import org.testng.annotations.Test;
43   
 
44    public class UrlLinkTableModelTest {
45   
46    private static final String inmenu = "TEST|http://someurl.blah/$DB_ACCESSION$|"
47    + "ANOTHER|http://test/t$SEQUENCE_ID$|"
48    + "TEST2|http://address/$SEQUENCE_ID$|SRS|"
49    + "http://theSRSlink/$SEQUENCE_ID$|"
50    + "MIR:00000005|MIR:00000011|MIR:00000372";
51   
52    private static final String notinmenu = "Not1|http://not.in.menu/$DB_ACCESSION$|"
53    + "Not2|http://not.in.menu.either/$DB_ACCESSION$";
54   
55    private static final String testIdOrgString = "{\"Local\": [{\"id\":\"MIR:00000002\",\"name\":\"ChEBI\",\"pattern\":\"^CHEBI:\\d+$\","
56    + "\"definition\":\"Chemical Entities of Biological Interest (ChEBI)\",\"prefix\":\"chebi\","
57    + "\"url\":\"http://identifiers.org/chebi\"},{\"id\":\"MIR:00000005\",\"name\":\"UniProt Knowledgebase\","
58    + "\"pattern\":\"^([A-N,R-Z][0-9]([A-Z][A-Z, 0-9][A-Z, 0-9][0-9]){1,2})|([O,P,Q][0-9][A-Z, 0-9][A-Z, 0-9][A-Z, 0-9][0-9])(\\.\\d+)?$\","
59    + "\"definition\":\"The UniProt Knowledgebase (UniProtKB)\",\"prefix\":\"uniprot\",\"url\":\"http://identifiers.org/uniprot\"},"
60    + "{\"id\":\"MIR:00000011\",\"name\":\"InterPro\",\"pattern\":\"^IPR\\d{6}$\",\"definition\":\"InterPro\",\"prefix\":\"interpro\","
61    + "\"url\":\"http://identifiers.org/interpro\"},"
62    + "{\"id\":\"MIR:00000372\",\"name\":\"ENA\",\"pattern\":\"^[A-Z]+[0-9]+(\\.\\d+)?$\",\"definition\":\"The European Nucleotide Archive (ENA),\""
63    + "\"prefix\":\"ena.embl\",\"url\":\"http://identifiers.org/ena.embl\"}]}";
64   
65    private UrlProviderI prov;
66   
 
67  9 toggle @BeforeMethod(alwaysRun = true)
68    public void setup()
69    {
70    // set up UrlProvider data as the source for the TableModel
71    // the data gets updated by the TableModel, so needs to be reinitialised for
72    // each test
73   
74    // make a dummy identifiers.org download file
75  9 File temp = null;
76  9 try
77    {
78  9 temp = File.createTempFile("tempfile", ".tmp");
79  9 temp.deleteOnExit();
80  9 BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
81  9 bw.write(testIdOrgString);
82  9 bw.close();
83    } catch (IOException e)
84    {
85  0 System.out.println("Error initialising UrlLinkTableModel test: "
86    + e.getMessage());
87    }
88   
89    // set up custom and identifiers.org url providers
90  9 IdOrgSettings.setDownloadLocation(temp.getPath());
91  9 IdentifiersUrlProvider idprov = new IdentifiersUrlProvider(inmenu);
92  9 CustomUrlProvider cprov = new CustomUrlProvider(inmenu, notinmenu);
93  9 List<UrlProviderI> provlist = new ArrayList<UrlProviderI>();
94  9 provlist.add(idprov);
95  9 provlist.add(cprov);
96   
97  9 prov = new UrlProvider("TEST2", provlist);
98    }
99   
100    /*
101    * Test that the table model is correctly initialised
102    * Display columns and default row are set; data provider listening event set up
103    */
 
104  1 toggle @Test(groups = { "Functional" })
105    public void testInitialisation()
106    {
107  1 int defaultCol = 4;
108  1 int dbCol = 0;
109  1 int descCol = 1;
110   
111  1 UrlLinkTableModel m = new UrlLinkTableModel(prov);
112   
113    // exactly one table model listener
114  1 TableModelListener[] listeners = m
115    .getListeners(TableModelListener.class);
116  1 Assert.assertEquals(listeners.length, 1);
117   
118    // default row exists, there is exactly 1, and it matches the supplied
119    // default
120  1 int count = 0;
121  11 for (int row = 0; row < m.getRowCount(); row++)
122    {
123  10 boolean isDefault = (boolean) m.getValueAt(row, defaultCol);
124  10 if (isDefault)
125    {
126  1 count++;
127  1 String defaultDBName = (String) m.getValueAt(row, dbCol);
128  1 Assert.assertEquals(defaultDBName, "TEST2");
129   
130  1 String defaultDesc = (String) m.getValueAt(row, descCol);
131  1 Assert.assertEquals(defaultDesc, "TEST2");
132    }
133    }
134  1 Assert.assertEquals(count, 1);
135    }
136   
137    /*
138    * Test row and column counts
139    */
 
140  1 toggle @Test(groups = { "Functional" })
141    public void testCounts()
142    {
143  1 UrlLinkTableModel m = new UrlLinkTableModel(prov);
144   
145    // correct numbers of column and rows
146  1 Assert.assertEquals(m.getColumnCount(), 5);
147  1 Assert.assertEquals(m.getRowCount(), 10);
148    }
149   
150    /*
151    * Test column access
152    */
 
153  1 toggle @Test(groups = { "Functional" })
154    public void testColumns()
155    {
156  1 UrlLinkTableModel m = new UrlLinkTableModel(prov);
157   
158    // check column names
159  1 Assert.assertEquals(m.getColumnName(0),
160    MessageManager.formatMessage("label.database"));
161  1 Assert.assertEquals(m.getColumnName(1),
162    MessageManager.formatMessage("label.name"));
163  1 Assert.assertEquals(m.getColumnName(2),
164    MessageManager.formatMessage("label.url"));
165  1 Assert.assertEquals(m.getColumnName(3),
166    MessageManager.formatMessage("label.inmenu"));
167  1 Assert.assertEquals(m.getColumnName(4),
168    MessageManager.formatMessage("label.primary"));
169   
170    // check column classes
171  1 Assert.assertEquals(m.getColumnClass(0), String.class);
172  1 Assert.assertEquals(m.getColumnClass(1), String.class);
173  1 Assert.assertEquals(m.getColumnClass(2), String.class);
174  1 Assert.assertEquals(m.getColumnClass(3), Boolean.class);
175  1 Assert.assertEquals(m.getColumnClass(4), Boolean.class);
176    }
177   
178    /*
179    * Test row insertion
180    */
 
181  1 toggle @Test(groups = { "Functional" })
182    public void testRowInsert()
183    {
184  1 UrlLinkTableModel m = new UrlLinkTableModel(prov);
185   
186  1 m.insertRow("newname", "newurl");
187   
188    // check table has new row inserted
189  1 Assert.assertEquals(m.getValueAt(10, 0), "newname");
190  1 Assert.assertEquals(m.getValueAt(10, 1), "newname");
191  1 Assert.assertEquals(m.getValueAt(10, 2), "newurl");
192  1 Assert.assertEquals(m.getValueAt(10, 3), true);
193  1 Assert.assertEquals(m.getValueAt(10, 4), false);
194   
195    // check data source has new row insrte
196  1 Assert.assertTrue(prov.getLinksForMenu().contains(
197    "newname" + SEP + "newurl"));
198    }
199   
200    /*
201    * Test row deletion
202    */
 
203  1 toggle @Test(groups = { "Functional" })
204    public void testRowDelete()
205    {
206  1 UrlLinkTableModel m = new UrlLinkTableModel(prov);
207   
208    // get name and url at row 0
209  1 String name = (String) m.getValueAt(0, 0);
210  1 String url = (String) m.getValueAt(0, 1);
211   
212  1 m.removeRow(0);
213   
214    // check table no longer has row 0 elements in it
215  10 for (int row = 0; row < m.getRowCount(); row++)
216    {
217  9 Assert.assertNotEquals(m.getValueAt(row, 0), name);
218    }
219   
220    // check data source likewise
221  1 Assert.assertFalse(prov.getLinksForMenu().contains(name + SEP + url));
222    }
223   
224    /*
225    * Test value setting and getting
226    */
 
227  1 toggle @Test(groups = { "Functional" })
228    public void testValues()
229    {
230  1 UrlLinkTableModel m = new UrlLinkTableModel(prov);
231   
232    // get original default
233  1 int olddefault;
234  1 boolean isDefault = false;
235  2 for (olddefault = 0; olddefault < m.getRowCount() && !isDefault; olddefault++)
236    {
237  1 isDefault = (boolean) m.getValueAt(olddefault, 3);
238    }
239   
240    // set new values, one in each row
241  1 m.setValueAt("dbnamechanged", 6, 0);
242  1 m.setValueAt("descchanged", 6, 1);
243  1 m.setValueAt("urlchanged", 7, 2);
244  1 m.setValueAt(false, 8, 3);
245  1 m.setValueAt(true, 6, 4);
246   
247  1 m.setValueAt("dbnamechanged", 5, 0);
248   
249    // check values updated in table
250  1 Assert.assertEquals(m.getValueAt(6, 0), "descchanged"); // custom url can't
251    // change db name
252  1 Assert.assertEquals(m.getValueAt(6, 1), "descchanged");
253  1 Assert.assertEquals(m.getValueAt(7, 2), "urlchanged");
254  1 Assert.assertFalse((boolean) m.getValueAt(8, 3));
255  1 Assert.assertTrue((boolean) m.getValueAt(6, 4));
256  1 Assert.assertFalse((boolean) m.getValueAt(olddefault, 4));
257   
258  1 Assert.assertEquals(m.getValueAt(5, 0), "dbnamechanged");
259   
260    // check default row is exactly one row still
261  11 for (int row = 0; row < m.getRowCount(); row++)
262    {
263  10 isDefault = (boolean) m.getValueAt(row, 4);
264   
265    // if isDefault is true, row is 9
266    // if isDefault is false, row is not 9
267  10 Assert.assertFalse(isDefault && !(row == 6));
268    }
269   
270    // check table updated
271  1 Assert.assertTrue(prov.writeUrlsAsString(true).contains(
272    "descchanged" + SEP + m.getValueAt(6, 2)));
273  1 Assert.assertTrue(prov.writeUrlsAsString(true).contains(
274    m.getValueAt(7, 1) + SEP + "urlchanged"));
275  1 Assert.assertTrue(prov.writeUrlsAsString(false).contains(
276    (String) m.getValueAt(8, 1)));
277  1 Assert.assertEquals(prov.getPrimaryUrl("seqid"), m.getValueAt(6, 2)
278    .toString().replace(DELIM + SEQUENCE_ID + DELIM, "seqid"));
279    }
280   
281    /*
282    * Test cell editability
283    */
 
284  1 toggle @Test(groups = { "Functional" })
285    public void testEditable()
286    {
287  1 UrlLinkTableModel m = new UrlLinkTableModel(prov);
288   
289  11 for (int row = 0; row < m.getRowCount(); row++)
290    {
291  10 Assert.assertFalse(m.isCellEditable(row, 0));
292  10 Assert.assertFalse(m.isCellEditable(row, 1));
293  10 Assert.assertFalse(m.isCellEditable(row, 2));
294  10 Assert.assertTrue(m.isCellEditable(row, 3));
295   
296  10 if ((row == 4) || (row == 6) || (row == 7))
297    {
298  3 Assert.assertTrue(m.isCellEditable(row, 4));
299    }
300    else
301    {
302  7 Assert.assertFalse(m.isCellEditable(row, 4));
303    }
304    }
305    }
306   
307    /*
308    * Test row 'deletability'
309    */
 
310  1 toggle @Test(groups = { "Functional" })
311    public void testDeletable()
312    {
313  1 UrlLinkTableModel m = new UrlLinkTableModel(prov);
314   
315  11 for (int row = 0; row < m.getRowCount(); row++)
316    {
317  10 if (row > 4)
318    {
319  5 Assert.assertTrue(m.isRowDeletable(row));
320    }
321    else
322    {
323  5 Assert.assertFalse(m.isRowDeletable(row));
324    }
325    }
326    }
327   
328    /*
329    * Test indirect row editability
330    */
 
331  1 toggle @Test(groups = { "Functional" })
332    public void testRowEditable()
333    {
334  1 UrlLinkTableModel m = new UrlLinkTableModel(prov);
335   
336  11 for (int row = 0; row < m.getRowCount(); row++)
337    {
338  10 if (row > 3)
339    {
340  6 Assert.assertTrue(m.isRowEditable(row));
341    }
342    else
343    {
344  4 Assert.assertFalse(m.isRowEditable(row));
345    }
346    }
347    }
348    }