--- /dev/null
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+
+package jalview.urls;
+
+import static jalview.util.UrlConstants.DB_ACCESSION;
+import static jalview.util.UrlConstants.DELIM;
+import static jalview.util.UrlConstants.SEP;
+
+import jalview.util.MessageManager;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.event.TableModelListener;
+
+import org.testng.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class UrlLinkTableModelTest {
+
+ private static final String inmenu = "TEST|http://someurl.blah/$DB_ACCESSION$|"
+ + "ANOTHER|http://test/t$SEQUENCE_ID$|"
+ + "TEST2|http://address/$SEQUENCE_ID$|SRS|"
+ + "http://theSRSlink/$SEQUENCE_ID$|"
+ + "MIR:00000005|MIR:00000011|MIR:00000372";
+
+ private static final String notinmenu = "Not1|http://not.in.menu/$DB_ACCESSION$|"
+ + "Not2|http://not.in.menu.either/$DB_ACCESSION$";
+
+ // Test identifiers.org download file
+ private static final String testIdOrgString = "[{\"id\":\"MIR:00000002\",\"name\":\"ChEBI\",\"pattern\":\"^CHEBI:\\d+$\","
+ + "\"definition\":\"Chemical Entities of Biological Interest (ChEBI)\",\"prefix\":\"chebi\","
+ + "\"url\":\"http://identifiers.org/chebi\"},{\"id\":\"MIR:00000005\",\"name\":\"UniProt Knowledgebase\","
+ + "\"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+)?$\","
+ + "\"definition\":\"The UniProt Knowledgebase (UniProtKB)\",\"prefix\":\"uniprot\",\"url\":\"http://identifiers.org/uniprot\"},"
+ + "{\"id\":\"MIR:00000011\",\"name\":\"InterPro\",\"pattern\":\"^IPR\\d{6}$\",\"definition\":\"InterPro\",\"prefix\":\"interpro\","
+ + "\"url\":\"http://identifiers.org/interpro\"},"
+ + "{\"id\":\"MIR:00000372\",\"name\":\"ENA\",\"pattern\":\"^[A-Z]+[0-9]+(\\.\\d+)?$\",\"definition\":\"The European Nucleotide Archive (ENA),\""
+ + "\"prefix\":\"ena.embl\",\"url\":\"http://identifiers.org/ena.embl\"}]";
+
+ private UrlProviderI prov;
+
+ @BeforeMethod(alwaysRun = true)
+ public void setup()
+ {
+ // set up UrlProvider data as the source for the TableModel
+ // the data gets updated by the TableModel, so needs to be reinitialised for
+ // each test
+
+ // make a dummy identifiers.org download file
+ File temp = null;
+ try
+ {
+ temp = File.createTempFile("tempfile", ".tmp");
+ temp.deleteOnExit();
+ BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
+ bw.write(testIdOrgString);
+ bw.close();
+ } catch (IOException e)
+ {
+ System.out.println("Error initialising UrlLinkTableModel test: "
+ + e.getMessage());
+ }
+
+ // set up custom and identifiers.org url providers
+ IdOrgSettings.setDownloadLocation(temp.getPath());
+ IdentifiersUrlProvider idprov = new IdentifiersUrlProvider(inmenu);
+ CustomUrlProvider cprov = new CustomUrlProvider(inmenu, notinmenu);
+ List<UrlProviderI> provlist = new ArrayList<UrlProviderI>();
+ provlist.add(idprov);
+ provlist.add(cprov);
+
+ prov = new UrlProvider("MIR:00000005", provlist);
+ }
+
+ /*
+ * Test that the table model is correctly initialised
+ * Display columns and default row are set; data provider listening event set up
+ */
+ @Test
+ public void testInitialisation()
+ {
+ int defaultCol = 3;
+ int nameCol = 0;
+
+ UrlLinkTableModel m = new UrlLinkTableModel(prov);
+
+ // exactly one table model listener
+ TableModelListener[] listeners = m
+ .getListeners(TableModelListener.class);
+ Assert.assertEquals(listeners.length, 1);
+
+ // default row exists, there is exactly 1, and it matches the supplied
+ // default
+ int count = 0;
+ for (int row = 0; row < m.getRowCount(); row++)
+ {
+ boolean isDefault = (boolean) m.getValueAt(row, defaultCol);
+ if (isDefault)
+ {
+ count++;
+ String defaultName = (String) m.getValueAt(row, nameCol);
+ Assert.assertEquals(defaultName, "UniProt Knowledgebase");
+ }
+ }
+ Assert.assertEquals(count, 1);
+ }
+
+ /*
+ * Test row and column counts
+ */
+ @Test
+ public void testCounts()
+ {
+ UrlLinkTableModel m = new UrlLinkTableModel(prov);
+
+ // correct numbers of column and rows
+ Assert.assertEquals(m.getColumnCount(), 4);
+ Assert.assertEquals(m.getRowCount(), 10);
+ }
+
+ /*
+ * Test column access
+ */
+ @Test
+ public void testColumns()
+ {
+ UrlLinkTableModel m = new UrlLinkTableModel(prov);
+
+ // check column names
+ Assert.assertEquals(m.getColumnName(0),
+ MessageManager.formatMessage("label.name"));
+ Assert.assertEquals(m.getColumnName(1),
+ MessageManager.formatMessage("label.url"));
+ Assert.assertEquals(m.getColumnName(2),
+ MessageManager.formatMessage("label.inmenu"));
+ Assert.assertEquals(m.getColumnName(3),
+ MessageManager.formatMessage("label.default"));
+
+ // check column classes
+ Assert.assertEquals(m.getColumnClass(0), String.class);
+ Assert.assertEquals(m.getColumnClass(1), String.class);
+ Assert.assertEquals(m.getColumnClass(2), Boolean.class);
+ Assert.assertEquals(m.getColumnClass(3), Boolean.class);
+ }
+
+ /*
+ * Test row insertion
+ */
+ @Test
+ public void testRowInsert()
+ {
+ UrlLinkTableModel m = new UrlLinkTableModel(prov);
+
+ m.insertRow("newname", "newurl");
+
+ // check table has new row inserted
+ Assert.assertEquals(m.getValueAt(10, 0), "newname");
+ Assert.assertEquals(m.getValueAt(10, 1), "newurl");
+ Assert.assertEquals(m.getValueAt(10, 2), true);
+ Assert.assertEquals(m.getValueAt(10, 3), false);
+
+ // check data source has new row insrte
+ Assert.assertTrue(prov.getLinksForMenu().contains(
+ "newname" + SEP + "newurl"));
+ }
+
+ /*
+ * Test row deletion
+ */
+ @Test
+ public void testRowDelete()
+ {
+ UrlLinkTableModel m = new UrlLinkTableModel(prov);
+
+ // get name and url at row 0
+ String name = (String) m.getValueAt(0, 0);
+ String url = (String) m.getValueAt(0, 1);
+
+ m.removeRow(0);
+
+ // check table no longer has row 0 elements in it
+ for (int row = 0; row < m.getRowCount(); row++)
+ {
+ Assert.assertNotEquals(m.getValueAt(row, 0), name);
+ }
+
+ // check data source likewise
+ Assert.assertFalse(prov.getLinksForMenu().contains(name + SEP + url));
+ }
+
+ /*
+ * Test value setting and getting
+ */
+ @Test
+ public void testValues()
+ {
+ UrlLinkTableModel m = new UrlLinkTableModel(prov);
+
+ // get original default
+ int olddefault;
+ boolean isDefault = false;
+ for (olddefault = 0; olddefault < m.getRowCount() && !isDefault; olddefault++)
+ {
+ isDefault = (boolean) m.getValueAt(olddefault, 3);
+ }
+
+ // set new values, one in each row - uneditable
+ m.setValueAt("namechanged", 6, 0);
+ m.setValueAt("urlchanged", 7, 1);
+ m.setValueAt(false, 8, 2);
+ m.setValueAt(true, 9, 3);
+
+ // check values updated in table
+ Assert.assertEquals(m.getValueAt(6, 0), "namechanged");
+ Assert.assertEquals(m.getValueAt(7, 1), "urlchanged");
+ Assert.assertFalse((boolean) m.getValueAt(8, 2));
+ Assert.assertTrue((boolean) m.getValueAt(9, 3));
+ Assert.assertFalse((boolean) m.getValueAt(olddefault, 3));
+
+ // check default row is exactly one row still
+ for (int row = 0; row < m.getRowCount(); row++)
+ {
+ isDefault = (boolean) m.getValueAt(row, 3);
+
+ // if isDefault is true, row is 9
+ // if isDefault is false, row is not 9
+ Assert.assertFalse(isDefault && !(row == 9));
+ }
+
+ // check table updated
+ Assert.assertTrue(prov.writeUrlsAsString(true).contains("namechanged" +SEP + m.getValueAt(6, 1)));
+ Assert.assertTrue(prov.writeUrlsAsString(true).contains(m.getValueAt(7,0) + SEP + "urlchanged"));
+ Assert.assertTrue(prov.writeUrlsAsString(false).contains(
+ (String) m.getValueAt(8, 0)));
+ Assert.assertEquals(prov.getDefaultUrl("seqid"), m.getValueAt(9, 1)
+ .toString().replace(DELIM + DB_ACCESSION + DELIM, "seqid"));
+
+ }
+
+ /*
+ * Test cell editability
+ */
+ @Test
+ public void testEditable()
+ {
+ UrlLinkTableModel m = new UrlLinkTableModel(prov);
+
+ for (int row = 0; row < m.getRowCount(); row++)
+ {
+ Assert.assertFalse(m.isCellEditable(row, 0));
+ Assert.assertFalse(m.isCellEditable(row, 1));
+ Assert.assertTrue(m.isCellEditable(row, 2));
+ Assert.assertTrue(m.isCellEditable(row, 3));
+ }
+ }
+}