JAL-2316 Unit testing, relax constraint that default url is in menu
[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.DB_ACCESSION;
25 import static jalview.util.UrlConstants.DELIM;
26 import static jalview.util.UrlConstants.SEP;
27
28 import jalview.util.MessageManager;
29
30 import java.io.BufferedWriter;
31 import java.io.File;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import javax.swing.event.TableModelListener;
38
39 import org.testng.Assert;
40 import org.testng.annotations.BeforeMethod;
41 import org.testng.annotations.Test;
42
43 public class UrlLinkTableModelTest {
44
45   private static final String inmenu = "TEST|http://someurl.blah/$DB_ACCESSION$|"
46           + "ANOTHER|http://test/t$SEQUENCE_ID$|"
47           + "TEST2|http://address/$SEQUENCE_ID$|SRS|"
48           + "http://theSRSlink/$SEQUENCE_ID$|"
49           + "MIR:00000005|MIR:00000011|MIR:00000372";
50
51   private static final String notinmenu = "Not1|http://not.in.menu/$DB_ACCESSION$|"
52           + "Not2|http://not.in.menu.either/$DB_ACCESSION$";
53
54   // Test identifiers.org download file
55   private static final String testIdOrgString = "[{\"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("MIR:00000005", 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
105   public void testInitialisation()
106   {
107     int defaultCol = 3;
108     int nameCol = 0;
109
110     UrlLinkTableModel m = new UrlLinkTableModel(prov);
111
112     // exactly one table model listener
113     TableModelListener[] listeners = m
114             .getListeners(TableModelListener.class);
115     Assert.assertEquals(listeners.length, 1);
116
117     // default row exists, there is exactly 1, and it matches the supplied
118     // default
119     int count = 0;
120     for (int row = 0; row < m.getRowCount(); row++)
121     {
122       boolean isDefault = (boolean) m.getValueAt(row, defaultCol);
123       if (isDefault)
124       {
125         count++;
126         String defaultName = (String) m.getValueAt(row, nameCol);
127         Assert.assertEquals(defaultName, "UniProt Knowledgebase");
128       }
129     }
130     Assert.assertEquals(count, 1);
131   }
132
133   /*
134    * Test row and column counts
135    */
136   @Test
137   public void testCounts()
138   {
139     UrlLinkTableModel m = new UrlLinkTableModel(prov);
140
141     // correct numbers of column and rows
142     Assert.assertEquals(m.getColumnCount(), 4);
143     Assert.assertEquals(m.getRowCount(), 10);
144   }
145
146   /*
147    * Test column access
148    */
149   @Test
150   public void testColumns()
151   {
152     UrlLinkTableModel m = new UrlLinkTableModel(prov);
153
154     // check column names
155     Assert.assertEquals(m.getColumnName(0),
156             MessageManager.formatMessage("label.name"));
157     Assert.assertEquals(m.getColumnName(1),
158             MessageManager.formatMessage("label.url"));
159     Assert.assertEquals(m.getColumnName(2),
160             MessageManager.formatMessage("label.inmenu"));
161     Assert.assertEquals(m.getColumnName(3),
162             MessageManager.formatMessage("label.default"));
163
164     // check column classes
165     Assert.assertEquals(m.getColumnClass(0), String.class);
166     Assert.assertEquals(m.getColumnClass(1), String.class);
167     Assert.assertEquals(m.getColumnClass(2), Boolean.class);
168     Assert.assertEquals(m.getColumnClass(3), Boolean.class);
169   }
170
171   /*
172    * Test row insertion
173    */
174   @Test
175   public void testRowInsert()
176   {
177     UrlLinkTableModel m = new UrlLinkTableModel(prov);
178
179     m.insertRow("newname", "newurl");
180
181     // check table has new row inserted
182     Assert.assertEquals(m.getValueAt(10, 0), "newname");
183     Assert.assertEquals(m.getValueAt(10, 1), "newurl");
184     Assert.assertEquals(m.getValueAt(10, 2), true);
185     Assert.assertEquals(m.getValueAt(10, 3), false);
186
187     // check data source has new row insrte
188     Assert.assertTrue(prov.getLinksForMenu().contains(
189             "newname" + SEP + "newurl"));
190   }
191
192   /*
193    * Test row deletion
194    */
195   @Test
196   public void testRowDelete()
197   {
198     UrlLinkTableModel m = new UrlLinkTableModel(prov);
199
200     // get name and url at row 0
201     String name = (String) m.getValueAt(0, 0);
202     String url = (String) m.getValueAt(0, 1);
203
204     m.removeRow(0);
205
206     // check table no longer has row 0 elements in it
207     for (int row = 0; row < m.getRowCount(); row++)
208     {
209       Assert.assertNotEquals(m.getValueAt(row, 0), name);
210     }
211
212     // check data source likewise
213     Assert.assertFalse(prov.getLinksForMenu().contains(name + SEP + url));
214   }
215
216   /*
217    * Test value setting and getting
218    */
219   @Test
220   public void testValues()
221   {
222     UrlLinkTableModel m = new UrlLinkTableModel(prov);
223
224     // get original default
225     int olddefault;
226     boolean isDefault = false;
227     for (olddefault = 0; olddefault < m.getRowCount() && !isDefault; olddefault++)
228     {
229       isDefault = (boolean) m.getValueAt(olddefault, 3);
230     }
231
232     // set new values, one in each row - uneditable
233     m.setValueAt("namechanged", 6, 0);
234     m.setValueAt("urlchanged", 7, 1);
235     m.setValueAt(false, 8, 2);
236     m.setValueAt(true, 9, 3);
237
238     // check values updated in table
239     Assert.assertEquals(m.getValueAt(6, 0), "namechanged");
240     Assert.assertEquals(m.getValueAt(7, 1), "urlchanged");
241     Assert.assertFalse((boolean) m.getValueAt(8, 2));
242     Assert.assertTrue((boolean) m.getValueAt(9, 3));
243     Assert.assertFalse((boolean) m.getValueAt(olddefault, 3));
244
245     // check default row is exactly one row still
246     for (int row = 0; row < m.getRowCount(); row++)
247     {
248       isDefault = (boolean) m.getValueAt(row, 3);
249
250       // if isDefault is true, row is 9
251       // if isDefault is false, row is not 9
252       Assert.assertFalse(isDefault && !(row == 9));
253     }
254
255     // check table updated
256     Assert.assertTrue(prov.writeUrlsAsString(true).contains("namechanged" +SEP + m.getValueAt(6, 1)));
257     Assert.assertTrue(prov.writeUrlsAsString(true).contains(m.getValueAt(7,0) + SEP + "urlchanged"));
258     Assert.assertTrue(prov.writeUrlsAsString(false).contains(
259             (String) m.getValueAt(8, 0)));
260     Assert.assertEquals(prov.getDefaultUrl("seqid"), m.getValueAt(9, 1)
261             .toString().replace(DELIM + DB_ACCESSION + DELIM, "seqid"));
262
263   }
264
265   /*
266    * Test cell editability
267    */
268   @Test
269   public void testEditable()
270   {
271     UrlLinkTableModel m = new UrlLinkTableModel(prov);
272
273     for (int row = 0; row < m.getRowCount(); row++)
274     {
275       Assert.assertFalse(m.isCellEditable(row, 0));
276       Assert.assertFalse(m.isCellEditable(row, 1));
277       Assert.assertTrue(m.isCellEditable(row, 2));
278       Assert.assertTrue(m.isCellEditable(row, 3));
279     }
280   }
281 }