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