4b2ea6ecd4d56751885880c4187638202c32d412
[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 = 4;
109     int dbCol = 0;
110     int descCol = 1;
111
112     UrlLinkTableModel m = new UrlLinkTableModel(prov);
113
114     // exactly one table model listener
115     TableModelListener[] listeners = m
116             .getListeners(TableModelListener.class);
117     Assert.assertEquals(listeners.length, 1);
118
119     // default row exists, there is exactly 1, and it matches the supplied
120     // default
121     int count = 0;
122     for (int row = 0; row < m.getRowCount(); row++)
123     {
124       boolean isDefault = (boolean) m.getValueAt(row, defaultCol);
125       if (isDefault)
126       {
127         count++;
128         String defaultDBName = (String) m.getValueAt(row, dbCol);
129         Assert.assertEquals(defaultDBName, "TEST2");
130
131         String defaultDesc = (String) m.getValueAt(row, descCol);
132         Assert.assertEquals(defaultDesc, "TEST2");
133       }
134     }
135     Assert.assertEquals(count, 1);
136   }
137
138   /*
139    * Test row and column counts
140    */
141   @Test(groups = { "Functional" })
142   public void testCounts()
143   {
144     UrlLinkTableModel m = new UrlLinkTableModel(prov);
145
146     // correct numbers of column and rows
147     Assert.assertEquals(m.getColumnCount(), 5);
148     Assert.assertEquals(m.getRowCount(), 10);
149   }
150
151   /*
152    * Test column access
153    */
154   @Test(groups = { "Functional" })
155   public void testColumns()
156   {
157     UrlLinkTableModel m = new UrlLinkTableModel(prov);
158
159     // check column names
160     Assert.assertEquals(m.getColumnName(0),
161             MessageManager.formatMessage("label.database"));
162     Assert.assertEquals(m.getColumnName(1),
163             MessageManager.formatMessage("label.name"));
164     Assert.assertEquals(m.getColumnName(2),
165             MessageManager.formatMessage("label.url"));
166     Assert.assertEquals(m.getColumnName(3),
167             MessageManager.formatMessage("label.inmenu"));
168     Assert.assertEquals(m.getColumnName(4),
169             MessageManager.formatMessage("label.primary"));
170
171     // check column classes
172     Assert.assertEquals(m.getColumnClass(0), String.class);
173     Assert.assertEquals(m.getColumnClass(1), String.class);
174     Assert.assertEquals(m.getColumnClass(2), String.class);
175     Assert.assertEquals(m.getColumnClass(3), Boolean.class);
176     Assert.assertEquals(m.getColumnClass(4), Boolean.class);
177   }
178
179   /*
180    * Test row insertion
181    */
182   @Test(groups = { "Functional" })
183   public void testRowInsert()
184   {
185     UrlLinkTableModel m = new UrlLinkTableModel(prov);
186
187     m.insertRow("newname", "newurl");
188
189     // check table has new row inserted
190     Assert.assertEquals(m.getValueAt(10, 0), "newname");
191     Assert.assertEquals(m.getValueAt(10, 1), "newname");
192     Assert.assertEquals(m.getValueAt(10, 2), "newurl");
193     Assert.assertEquals(m.getValueAt(10, 3), true);
194     Assert.assertEquals(m.getValueAt(10, 4), false);
195
196     // check data source has new row insrte
197     Assert.assertTrue(prov.getLinksForMenu().contains(
198             "newname" + SEP + "newurl"));
199   }
200
201   /*
202    * Test row deletion
203    */
204   @Test(groups = { "Functional" })
205   public void testRowDelete()
206   {
207     UrlLinkTableModel m = new UrlLinkTableModel(prov);
208
209     // get name and url at row 0
210     String name = (String) m.getValueAt(0, 0);
211     String url = (String) m.getValueAt(0, 1);
212
213     m.removeRow(0);
214
215     // check table no longer has row 0 elements in it
216     for (int row = 0; row < m.getRowCount(); row++)
217     {
218       Assert.assertNotEquals(m.getValueAt(row, 0), name);
219     }
220
221     // check data source likewise
222     Assert.assertFalse(prov.getLinksForMenu().contains(name + SEP + url));
223   }
224
225   /*
226    * Test value setting and getting
227    */
228   @Test(groups = { "Functional" })
229   public void testValues()
230   {
231     UrlLinkTableModel m = new UrlLinkTableModel(prov);
232
233     // get original default
234     int olddefault;
235     boolean isDefault = false;
236     for (olddefault = 0; olddefault < m.getRowCount() && !isDefault; olddefault++)
237     {
238       isDefault = (boolean) m.getValueAt(olddefault, 3);
239     }
240
241     // set new values, one in each row
242     m.setValueAt("dbnamechanged", 6, 0);
243     m.setValueAt("descchanged", 6, 1);
244     m.setValueAt("urlchanged", 7, 2);
245     m.setValueAt(false, 8, 3);
246     m.setValueAt(true, 6, 4);
247
248     m.setValueAt("dbnamechanged", 5, 0);
249
250     // check values updated in table
251     Assert.assertEquals(m.getValueAt(6, 0), "descchanged"); // custom url can't
252                                                             // change db name
253     Assert.assertEquals(m.getValueAt(6, 1), "descchanged");
254     Assert.assertEquals(m.getValueAt(7, 2), "urlchanged");
255     Assert.assertFalse((boolean) m.getValueAt(8, 3));
256     Assert.assertTrue((boolean) m.getValueAt(6, 4));
257     Assert.assertFalse((boolean) m.getValueAt(olddefault, 4));
258
259     Assert.assertEquals(m.getValueAt(5, 0), "dbnamechanged");
260
261     // check default row is exactly one row still
262     for (int row = 0; row < m.getRowCount(); row++)
263     {
264       isDefault = (boolean) m.getValueAt(row, 4);
265
266       // if isDefault is true, row is 9
267       // if isDefault is false, row is not 9
268       Assert.assertFalse(isDefault && !(row == 6));
269     }
270
271     // check table updated
272     Assert.assertTrue(prov.writeUrlsAsString(true).contains(
273             "descchanged" + SEP + m.getValueAt(6, 2)));
274     Assert.assertTrue(prov.writeUrlsAsString(true).contains(
275             m.getValueAt(7, 1) + SEP + "urlchanged"));
276     Assert.assertTrue(prov.writeUrlsAsString(false).contains(
277             (String) m.getValueAt(8, 1)));
278     Assert.assertEquals(prov.getPrimaryUrl("seqid"), m.getValueAt(6, 2)
279             .toString().replace(DELIM + SEQUENCE_ID + DELIM, "seqid"));
280   }
281
282   /*
283    * Test cell editability
284    */
285   @Test(groups = { "Functional" })
286   public void testEditable()
287   {
288     UrlLinkTableModel m = new UrlLinkTableModel(prov);
289
290     for (int row = 0; row < m.getRowCount(); row++)
291     {
292       Assert.assertFalse(m.isCellEditable(row, 0));
293       Assert.assertFalse(m.isCellEditable(row, 1));
294       Assert.assertFalse(m.isCellEditable(row, 2));
295       Assert.assertTrue(m.isCellEditable(row, 3));
296
297       if ((row == 4) || (row == 6) || (row == 7))
298       {
299         Assert.assertTrue(m.isCellEditable(row, 4));
300       }
301       else
302       {
303         Assert.assertFalse(m.isCellEditable(row, 4));
304       }
305     }
306   }
307
308   /*
309    * Test row 'deletability'
310    */
311   @Test(groups = { "Functional" })
312   public void testDeletable()
313   {
314     UrlLinkTableModel m = new UrlLinkTableModel(prov);
315
316     for (int row = 0; row < m.getRowCount(); row++)
317     {
318       if (row > 4)
319       {
320         Assert.assertTrue(m.isRowDeletable(row));
321       }
322       else
323       {
324         Assert.assertFalse(m.isRowDeletable(row));
325       }
326     }
327   }
328
329   /*
330    * Test indirect row editability
331    */
332   @Test(groups = { "Functional" })
333   public void testRowEditable()
334   {
335     UrlLinkTableModel m = new UrlLinkTableModel(prov);
336
337     for (int row = 0; row < m.getRowCount(); row++)
338     {
339       if (row > 3)
340       {
341         Assert.assertTrue(m.isRowEditable(row));
342       }
343       else
344       {
345         Assert.assertFalse(m.isRowEditable(row));
346       }
347     }
348   }
349 }