2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
24 import static jalview.util.UrlConstants.DELIM;
25 import static jalview.util.UrlConstants.SEP;
26 import static jalview.util.UrlConstants.SEQUENCE_ID;
28 import jalview.urls.api.UrlProviderI;
29 import jalview.util.MessageManager;
31 import java.io.BufferedWriter;
33 import java.io.FileWriter;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.List;
38 import javax.swing.event.TableModelListener;
40 import org.testng.Assert;
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
44 public class UrlLinkTableModelTest {
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";
52 private static final String notinmenu = "Not1|http://not.in.menu/$DB_ACCESSION$|"
53 + "Not2|http://not.in.menu.either/$DB_ACCESSION$";
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\"}]}";
65 private UrlProviderI prov;
67 @BeforeMethod(alwaysRun = true)
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
74 // make a dummy identifiers.org download file
78 temp = File.createTempFile("tempfile", ".tmp");
80 BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
81 bw.write(testIdOrgString);
83 } catch (IOException e)
85 System.out.println("Error initialising UrlLinkTableModel test: "
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>();
97 prov = new UrlProvider("TEST2", provlist);
101 * Test that the table model is correctly initialised
102 * Display columns and default row are set; data provider listening event set up
104 @Test(groups = { "Functional" })
105 public void testInitialisation()
111 UrlLinkTableModel m = new UrlLinkTableModel(prov);
113 // exactly one table model listener
114 TableModelListener[] listeners = m
115 .getListeners(TableModelListener.class);
116 Assert.assertEquals(listeners.length, 1);
118 // default row exists, there is exactly 1, and it matches the supplied
121 for (int row = 0; row < m.getRowCount(); row++)
123 boolean isDefault = (boolean) m.getValueAt(row, defaultCol);
127 String defaultDBName = (String) m.getValueAt(row, dbCol);
128 Assert.assertEquals(defaultDBName, "TEST2");
130 String defaultDesc = (String) m.getValueAt(row, descCol);
131 Assert.assertEquals(defaultDesc, "TEST2");
134 Assert.assertEquals(count, 1);
138 * Test row and column counts
140 @Test(groups = { "Functional" })
141 public void testCounts()
143 UrlLinkTableModel m = new UrlLinkTableModel(prov);
145 // correct numbers of column and rows
146 Assert.assertEquals(m.getColumnCount(), 5);
147 Assert.assertEquals(m.getRowCount(), 10);
153 @Test(groups = { "Functional" })
154 public void testColumns()
156 UrlLinkTableModel m = new UrlLinkTableModel(prov);
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"));
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);
181 @Test(groups = { "Functional" })
182 public void testRowInsert()
184 UrlLinkTableModel m = new UrlLinkTableModel(prov);
186 m.insertRow("newname", "newurl");
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);
195 // check data source has new row insrte
196 Assert.assertTrue(prov.getLinksForMenu().contains(
197 "newname" + SEP + "newurl"));
203 @Test(groups = { "Functional" })
204 public void testRowDelete()
206 UrlLinkTableModel m = new UrlLinkTableModel(prov);
208 // get name and url at row 0
209 String name = (String) m.getValueAt(0, 0);
210 String url = (String) m.getValueAt(0, 1);
214 // check table no longer has row 0 elements in it
215 for (int row = 0; row < m.getRowCount(); row++)
217 Assert.assertNotEquals(m.getValueAt(row, 0), name);
220 // check data source likewise
221 Assert.assertFalse(prov.getLinksForMenu().contains(name + SEP + url));
225 * Test value setting and getting
227 @Test(groups = { "Functional" })
228 public void testValues()
230 UrlLinkTableModel m = new UrlLinkTableModel(prov);
232 // get original default
234 boolean isDefault = false;
235 for (olddefault = 0; olddefault < m.getRowCount() && !isDefault; olddefault++)
237 isDefault = (boolean) m.getValueAt(olddefault, 3);
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);
247 m.setValueAt("dbnamechanged", 5, 0);
249 // check values updated in table
250 Assert.assertEquals(m.getValueAt(6, 0), "descchanged"); // custom url can't
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));
258 Assert.assertEquals(m.getValueAt(5, 0), "dbnamechanged");
260 // check default row is exactly one row still
261 for (int row = 0; row < m.getRowCount(); row++)
263 isDefault = (boolean) m.getValueAt(row, 4);
265 // if isDefault is true, row is 9
266 // if isDefault is false, row is not 9
267 Assert.assertFalse(isDefault && !(row == 6));
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"));
282 * Test cell editability
284 @Test(groups = { "Functional" })
285 public void testEditable()
287 UrlLinkTableModel m = new UrlLinkTableModel(prov);
289 for (int row = 0; row < m.getRowCount(); row++)
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));
296 if ((row == 4) || (row == 6) || (row == 7))
298 Assert.assertTrue(m.isCellEditable(row, 4));
302 Assert.assertFalse(m.isCellEditable(row, 4));
308 * Test row 'deletability'
310 @Test(groups = { "Functional" })
311 public void testDeletable()
313 UrlLinkTableModel m = new UrlLinkTableModel(prov);
315 for (int row = 0; row < m.getRowCount(); row++)
319 Assert.assertTrue(m.isRowDeletable(row));
323 Assert.assertFalse(m.isRowDeletable(row));
329 * Test indirect row editability
331 @Test(groups = { "Functional" })
332 public void testRowEditable()
334 UrlLinkTableModel m = new UrlLinkTableModel(prov);
336 for (int row = 0; row < m.getRowCount(); row++)
340 Assert.assertTrue(m.isRowEditable(row));
344 Assert.assertFalse(m.isRowEditable(row));