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.DB_ACCESSION;
25 import static jalview.util.UrlConstants.DELIM;
26 import static jalview.util.UrlConstants.SEP;
28 import jalview.util.MessageManager;
30 import java.io.BufferedWriter;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.List;
37 import javax.swing.event.TableModelListener;
39 import org.testng.Assert;
40 import org.testng.annotations.BeforeMethod;
41 import org.testng.annotations.Test;
43 public class UrlLinkTableModelTest {
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";
51 private static final String notinmenu = "Not1|http://not.in.menu/$DB_ACCESSION$|"
52 + "Not2|http://not.in.menu.either/$DB_ACCESSION$";
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\"}]";
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("MIR:00000005", provlist);
101 * Test that the table model is correctly initialised
102 * Display columns and default row are set; data provider listening event set up
105 public void testInitialisation()
110 UrlLinkTableModel m = new UrlLinkTableModel(prov);
112 // exactly one table model listener
113 TableModelListener[] listeners = m
114 .getListeners(TableModelListener.class);
115 Assert.assertEquals(listeners.length, 1);
117 // default row exists, there is exactly 1, and it matches the supplied
120 for (int row = 0; row < m.getRowCount(); row++)
122 boolean isDefault = (boolean) m.getValueAt(row, defaultCol);
126 String defaultName = (String) m.getValueAt(row, nameCol);
127 Assert.assertEquals(defaultName, "UniProt Knowledgebase");
130 Assert.assertEquals(count, 1);
134 * Test row and column counts
137 public void testCounts()
139 UrlLinkTableModel m = new UrlLinkTableModel(prov);
141 // correct numbers of column and rows
142 Assert.assertEquals(m.getColumnCount(), 4);
143 Assert.assertEquals(m.getRowCount(), 10);
150 public void testColumns()
152 UrlLinkTableModel m = new UrlLinkTableModel(prov);
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"));
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);
175 public void testRowInsert()
177 UrlLinkTableModel m = new UrlLinkTableModel(prov);
179 m.insertRow("newname", "newurl");
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);
187 // check data source has new row insrte
188 Assert.assertTrue(prov.getLinksForMenu().contains(
189 "newname" + SEP + "newurl"));
196 public void testRowDelete()
198 UrlLinkTableModel m = new UrlLinkTableModel(prov);
200 // get name and url at row 0
201 String name = (String) m.getValueAt(0, 0);
202 String url = (String) m.getValueAt(0, 1);
206 // check table no longer has row 0 elements in it
207 for (int row = 0; row < m.getRowCount(); row++)
209 Assert.assertNotEquals(m.getValueAt(row, 0), name);
212 // check data source likewise
213 Assert.assertFalse(prov.getLinksForMenu().contains(name + SEP + url));
217 * Test value setting and getting
220 public void testValues()
222 UrlLinkTableModel m = new UrlLinkTableModel(prov);
224 // get original default
226 boolean isDefault = false;
227 for (olddefault = 0; olddefault < m.getRowCount() && !isDefault; olddefault++)
229 isDefault = (boolean) m.getValueAt(olddefault, 3);
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);
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));
245 // check default row is exactly one row still
246 for (int row = 0; row < m.getRowCount(); row++)
248 isDefault = (boolean) m.getValueAt(row, 3);
250 // if isDefault is true, row is 9
251 // if isDefault is false, row is not 9
252 Assert.assertFalse(isDefault && !(row == 9));
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"));
266 * Test cell editability
269 public void testEditable()
271 UrlLinkTableModel m = new UrlLinkTableModel(prov);
273 for (int row = 0; row < m.getRowCount(); row++)
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));