ab190ef46d7ccc4c5624cad576ed4abb39b816e1
[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   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   @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     File temp = null;
76     try
77     {
78       temp = File.createTempFile("tempfile", ".tmp");
79       temp.deleteOnExit();
80       BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
81       bw.write(testIdOrgString);
82       bw.close();
83     } catch (IOException e)
84     {
85       System.out.println("Error initialising UrlLinkTableModel test: "
86               + e.getMessage());
87     }
88
89     // set up custom and identifiers.org url providers
90     IdOrgSettings.setDownloadLocation(temp.getPath());
91     IdentifiersUrlProvider idprov = new IdentifiersUrlProvider(inmenu);
92     CustomUrlProvider cprov = new CustomUrlProvider(inmenu, notinmenu);
93     List<UrlProviderI> provlist = new ArrayList<UrlProviderI>();
94     provlist.add(idprov);
95     provlist.add(cprov);
96
97     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   @Test(groups = { "Functional" })
105   public void testInitialisation()
106   {
107     int defaultCol = 4;
108     int dbCol = 0;
109     int descCol = 1;
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 defaultDBName = (String) m.getValueAt(row, dbCol);
128         Assert.assertEquals(defaultDBName, "TEST2");
129
130         String defaultDesc = (String) m.getValueAt(row, descCol);
131         Assert.assertEquals(defaultDesc, "TEST2");
132       }
133     }
134     Assert.assertEquals(count, 1);
135   }
136
137   /*
138    * Test row and column counts
139    */
140   @Test(groups = { "Functional" })
141   public void testCounts()
142   {
143     UrlLinkTableModel m = new UrlLinkTableModel(prov);
144
145     // correct numbers of column and rows
146     Assert.assertEquals(m.getColumnCount(), 5);
147     Assert.assertEquals(m.getRowCount(), 10);
148   }
149
150   /*
151    * Test column access
152    */
153   @Test(groups = { "Functional" })
154   public void testColumns()
155   {
156     UrlLinkTableModel m = new UrlLinkTableModel(prov);
157
158     // check column names
159     Assert.assertEquals(m.getColumnName(0),
160             MessageManager.formatMessage("label.database"));
161     Assert.assertEquals(m.getColumnName(1),
162             MessageManager.formatMessage("label.name"));
163     Assert.assertEquals(m.getColumnName(2),
164             MessageManager.formatMessage("label.url"));
165     Assert.assertEquals(m.getColumnName(3),
166             MessageManager.formatMessage("label.inmenu"));
167     Assert.assertEquals(m.getColumnName(4),
168             MessageManager.formatMessage("label.primary"));
169
170     // check column classes
171     Assert.assertEquals(m.getColumnClass(0), String.class);
172     Assert.assertEquals(m.getColumnClass(1), String.class);
173     Assert.assertEquals(m.getColumnClass(2), String.class);
174     Assert.assertEquals(m.getColumnClass(3), Boolean.class);
175     Assert.assertEquals(m.getColumnClass(4), Boolean.class);
176   }
177
178   /*
179    * Test row insertion
180    */
181   @Test(groups = { "Functional" })
182   public void testRowInsert()
183   {
184     UrlLinkTableModel m = new UrlLinkTableModel(prov);
185
186     m.insertRow("newname", "newurl");
187
188     // check table has new row inserted
189     Assert.assertEquals(m.getValueAt(10, 0), "newname");
190     Assert.assertEquals(m.getValueAt(10, 1), "newname");
191     Assert.assertEquals(m.getValueAt(10, 2), "newurl");
192     Assert.assertEquals(m.getValueAt(10, 3), true);
193     Assert.assertEquals(m.getValueAt(10, 4), false);
194
195     // check data source has new row insrte
196     Assert.assertTrue(prov.getLinksForMenu().contains(
197             "newname" + SEP + "newurl"));
198   }
199
200   /*
201    * Test row deletion
202    */
203   @Test(groups = { "Functional" })
204   public void testRowDelete()
205   {
206     UrlLinkTableModel m = new UrlLinkTableModel(prov);
207
208     // get name and url at row 0
209     String name = (String) m.getValueAt(0, 0);
210     String url = (String) m.getValueAt(0, 1);
211
212     m.removeRow(0);
213
214     // check table no longer has row 0 elements in it
215     for (int row = 0; row < m.getRowCount(); row++)
216     {
217       Assert.assertNotEquals(m.getValueAt(row, 0), name);
218     }
219
220     // check data source likewise
221     Assert.assertFalse(prov.getLinksForMenu().contains(name + SEP + url));
222   }
223
224   /*
225    * Test value setting and getting
226    */
227   @Test(groups = { "Functional" })
228   public void testValues()
229   {
230     UrlLinkTableModel m = new UrlLinkTableModel(prov);
231
232     // get original default
233     int olddefault;
234     boolean isDefault = false;
235     for (olddefault = 0; olddefault < m.getRowCount() && !isDefault; olddefault++)
236     {
237       isDefault = (boolean) m.getValueAt(olddefault, 3);
238     }
239
240     // set new values, one in each row
241     m.setValueAt("dbnamechanged", 6, 0);
242     m.setValueAt("descchanged", 6, 1);
243     m.setValueAt("urlchanged", 7, 2);
244     m.setValueAt(false, 8, 3);
245     m.setValueAt(true, 6, 4);
246
247     m.setValueAt("dbnamechanged", 5, 0);
248
249     // check values updated in table
250     Assert.assertEquals(m.getValueAt(6, 0), "descchanged"); // custom url can't
251                                                             // change db name
252     Assert.assertEquals(m.getValueAt(6, 1), "descchanged");
253     Assert.assertEquals(m.getValueAt(7, 2), "urlchanged");
254     Assert.assertFalse((boolean) m.getValueAt(8, 3));
255     Assert.assertTrue((boolean) m.getValueAt(6, 4));
256     Assert.assertFalse((boolean) m.getValueAt(olddefault, 4));
257
258     Assert.assertEquals(m.getValueAt(5, 0), "dbnamechanged");
259
260     // check default row is exactly one row still
261     for (int row = 0; row < m.getRowCount(); row++)
262     {
263       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       Assert.assertFalse(isDefault && !(row == 6));
268     }
269
270     // check table updated
271     Assert.assertTrue(prov.writeUrlsAsString(true).contains(
272             "descchanged" + SEP + m.getValueAt(6, 2)));
273     Assert.assertTrue(prov.writeUrlsAsString(true).contains(
274             m.getValueAt(7, 1) + SEP + "urlchanged"));
275     Assert.assertTrue(prov.writeUrlsAsString(false).contains(
276             (String) m.getValueAt(8, 1)));
277     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   @Test(groups = { "Functional" })
285   public void testEditable()
286   {
287     UrlLinkTableModel m = new UrlLinkTableModel(prov);
288
289     for (int row = 0; row < m.getRowCount(); row++)
290     {
291       Assert.assertFalse(m.isCellEditable(row, 0));
292       Assert.assertFalse(m.isCellEditable(row, 1));
293       Assert.assertFalse(m.isCellEditable(row, 2));
294       Assert.assertTrue(m.isCellEditable(row, 3));
295
296       if ((row == 4) || (row == 6) || (row == 7))
297       {
298         Assert.assertTrue(m.isCellEditable(row, 4));
299       }
300       else
301       {
302         Assert.assertFalse(m.isCellEditable(row, 4));
303       }
304     }
305   }
306
307   /*
308    * Test row 'deletability'
309    */
310   @Test(groups = { "Functional" })
311   public void testDeletable()
312   {
313     UrlLinkTableModel m = new UrlLinkTableModel(prov);
314
315     for (int row = 0; row < m.getRowCount(); row++)
316     {
317       if (row > 4)
318       {
319         Assert.assertTrue(m.isRowDeletable(row));
320       }
321       else
322       {
323         Assert.assertFalse(m.isRowDeletable(row));
324       }
325     }
326   }
327
328   /*
329    * Test indirect row editability
330    */
331   @Test(groups = { "Functional" })
332   public void testRowEditable()
333   {
334     UrlLinkTableModel m = new UrlLinkTableModel(prov);
335
336     for (int row = 0; row < m.getRowCount(); row++)
337     {
338       if (row > 3)
339       {
340         Assert.assertTrue(m.isRowEditable(row));
341       }
342       else
343       {
344         Assert.assertFalse(m.isRowEditable(row));
345       }
346     }
347   }
348 }