From ab658ca1052ede3491f4bf93630103a7c82776dc Mon Sep 17 00:00:00 2001 From: kiramt Date: Mon, 12 Dec 2016 13:42:47 +0000 Subject: [PATCH] JAL-2316 More unit testing and adjustments --- src/jalview/urls/IdentifiersUrlProvider.java | 6 + src/jalview/urls/UrlProvider.java | 25 +++- src/jalview/urls/UrlProviderI.java | 19 +++- .../jalview/urls/AppletUrlProviderFactoryTest.java | 78 +++++++++++++ .../urls/DesktopUrlProviderFactoryTest.java | 108 ++++++++++++++++++ test/jalview/urls/UrlLinkTableModelTest.java | 28 +++-- test/jalview/urls/UrlProviderTest.java | 119 ++++++++++++++++++++ 7 files changed, 367 insertions(+), 16 deletions(-) create mode 100644 test/jalview/urls/AppletUrlProviderFactoryTest.java create mode 100644 test/jalview/urls/DesktopUrlProviderFactoryTest.java create mode 100644 test/jalview/urls/UrlProviderTest.java diff --git a/src/jalview/urls/IdentifiersUrlProvider.java b/src/jalview/urls/IdentifiersUrlProvider.java index 84da2d7..6c01f97 100644 --- a/src/jalview/urls/IdentifiersUrlProvider.java +++ b/src/jalview/urls/IdentifiersUrlProvider.java @@ -237,4 +237,10 @@ public class IdentifiersUrlProvider extends UrlProviderImpl { return null; } + + @Override + public boolean contains(String id) + { + return (urls.containsKey(id)); + } } diff --git a/src/jalview/urls/UrlProvider.java b/src/jalview/urls/UrlProvider.java index 7009e70..383c5d1 100644 --- a/src/jalview/urls/UrlProvider.java +++ b/src/jalview/urls/UrlProvider.java @@ -57,12 +57,16 @@ public class UrlProvider implements UrlProviderI customProvider = findCustomProvider(); // check that the defaultUrl still exists - if (!setDefaultUrl(defaultUrlString)) + if (!contains(defaultUrlString)) { // if the defaultUrl can't be found in any of the providers // set up a custom default url chooseDefaultUrl(); } + else + { + setDefaultUrl(defaultUrlString); + } } /* @@ -94,6 +98,24 @@ public class UrlProvider implements UrlProviderI outcome = true; } } + if (!outcome) + { + throw new IllegalArgumentException(); + } + return outcome; + } + + @Override + public boolean contains(String id) + { + boolean outcome = false; + for (UrlProviderI p : providers) + { + if (p.contains(id)) + { + outcome = true; + } + } return outcome; } @@ -151,7 +173,6 @@ public class UrlProvider implements UrlProviderI { for (UrlProviderI p : providers) { - p.setDefaultUrl(null); p.setUrlData(links); } } diff --git a/src/jalview/urls/UrlProviderI.java b/src/jalview/urls/UrlProviderI.java index 595f98d..de52391 100644 --- a/src/jalview/urls/UrlProviderI.java +++ b/src/jalview/urls/UrlProviderI.java @@ -33,7 +33,7 @@ public interface UrlProviderI { /** - * Get names and urls in as strings for display + * Get names and urls in the UrlProvider as strings for display * */ Vector getLinksForMenu(); @@ -65,7 +65,7 @@ public interface UrlProviderI String getDefaultUrlId(); /** - * Get the target of thelink for the default URL + * Get the target of the link for the default URL * * @seqid sequence id for which to build link * @return target of link for the default URL @@ -77,9 +77,20 @@ public interface UrlProviderI * * @param id * the id of the URL to set as default - * @return true if setting was successful, false otherwise + * @return true if setting is successful + * @throws IllegalArgumentException + * if id does not exist as a url in the UrlProvider */ - boolean setDefaultUrl(String id); + boolean setDefaultUrl(String id) throws IllegalArgumentException; + + /** + * Test if UrlProvider contains a url + * + * @param id + * to test for + * @return true of UrlProvider contains this id, false otherwise + */ + boolean contains(String id); /** * Write out all URLs as a string suitable for serialising diff --git a/test/jalview/urls/AppletUrlProviderFactoryTest.java b/test/jalview/urls/AppletUrlProviderFactoryTest.java new file mode 100644 index 0000000..baf9c63 --- /dev/null +++ b/test/jalview/urls/AppletUrlProviderFactoryTest.java @@ -0,0 +1,78 @@ +/* + * 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 . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.urls; + +import jalview.util.UrlConstants; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.testng.Assert; +import org.testng.annotations.Test; + +public class AppletUrlProviderFactoryTest { + + @Test(groups = { "Functional" }) + public void testCreateUrlProvider() + { + final String defaultUrl = UrlConstants.DEFAULT_STRING.substring( + UrlConstants.DEFAULT_STRING.indexOf(UrlConstants.SEP) + 1, + UrlConstants.DEFAULT_STRING.length()); + Map urlList = new HashMap() + { + { + put("Test1", "http://identifiers.org/uniprot/$DB_ACCESSION$"); + put("Test2", defaultUrl); + } + }; + + UrlProviderFactoryI factory = new AppletUrlProviderFactory("Test2", + urlList); + UrlProviderI prov = factory.createUrlProvider(); + + // default url correctly set + Assert.assertEquals(prov.getDefaultUrlId(), "Test2"); + Assert.assertEquals(prov.getDefaultUrl("FER_CAPAN"), + defaultUrl.replace("$SEQUENCE_ID$", + "FER_CAPAN")); + + List allLinks = prov.getLinksForTable(); + + // 2 links in provider + Assert.assertEquals(allLinks.size(), 2); + + // first link set correctly + Assert.assertEquals(allLinks.get(0).getId(), "Test1"); + Assert.assertEquals(allLinks.get(0).getName(), "Test1"); + Assert.assertEquals(allLinks.get(0).getUrl(), + "http://identifiers.org/uniprot/$DB_ACCESSION$"); + Assert.assertFalse(allLinks.get(0).getIsDefault()); + Assert.assertTrue(allLinks.get(0).getIsSelected()); + + // second link set correctly + Assert.assertEquals(allLinks.get(1).getId(), "Test2"); + Assert.assertEquals(allLinks.get(1).getName(), "Test2"); + Assert.assertEquals(allLinks.get(1).getUrl(), defaultUrl); + Assert.assertTrue(allLinks.get(1).getIsDefault()); + Assert.assertTrue(allLinks.get(1).getIsSelected()); + } +} diff --git a/test/jalview/urls/DesktopUrlProviderFactoryTest.java b/test/jalview/urls/DesktopUrlProviderFactoryTest.java new file mode 100644 index 0000000..d224c2e --- /dev/null +++ b/test/jalview/urls/DesktopUrlProviderFactoryTest.java @@ -0,0 +1,108 @@ +/* + * 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 . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.urls; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; +import java.util.Vector; + +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class DesktopUrlProviderFactoryTest +{ + // 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\"}]"; + + @BeforeMethod(alwaysRun = true) + public void setup() + { + // 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 DesktopUrlProviderFactoryTest test: " + + e.getMessage()); + } + + IdOrgSettings.setDownloadLocation(temp.getPath()); + } + + @Test(groups = { "Functional" }) + public void testCreateUrlProvider() + { + String defaultUrlString = "Test1"; + String defaultUrl = "http://blah.blah/$SEQUENCE_ID$"; + String cachedUrlList = "MIR:00000005|MIR:00000011|Test1|http://blah.blah/$SEQUENCE_ID$|" + + "Test2|http://test2/$DB_ACCESSION$|Test3|http://test3/$SEQUENCE_ID$"; + String userUrlList = "MIR:00000372|Test4|httpL//another.url/$SEQUENCE_ID$"; + + DesktopUrlProviderFactory factory = new DesktopUrlProviderFactory( + defaultUrlString, cachedUrlList, userUrlList); + UrlProviderI prov = factory.createUrlProvider(); + + // default url correctly set + Assert.assertEquals(prov.getDefaultUrlId(), "Test1"); + Assert.assertEquals(prov.getDefaultUrl("FER_CAPAN"), + defaultUrl.replace("$SEQUENCE_ID$", "FER_CAPAN")); + + Vector menulinks = prov.getLinksForMenu(); + List allLinks = prov.getLinksForTable(); + + // 8 links in provider - 4 from id file, 4 custom links + Assert.assertEquals(allLinks.size(), 8); + + // 5 links in menu (cachedUrlList) + Assert.assertEquals(menulinks.size(), 5); + + Assert.assertTrue(menulinks + .contains("Test1|http://blah.blah/$SEQUENCE_ID$")); + Assert.assertTrue(menulinks + .contains("Test2|http://test2/$DB_ACCESSION$")); + Assert.assertTrue(menulinks + .contains("Test3|http://test3/$SEQUENCE_ID$")); + Assert.assertTrue(menulinks + .contains("UniProt Knowledgebase|http://identifiers.org/uniprot/$DB_ACCESSION$")); + Assert.assertTrue(menulinks + .contains("InterPro|http://identifiers.org/interpro/$DB_ACCESSION$")); + } +} diff --git a/test/jalview/urls/UrlLinkTableModelTest.java b/test/jalview/urls/UrlLinkTableModelTest.java index bf13784..1304d1a 100644 --- a/test/jalview/urls/UrlLinkTableModelTest.java +++ b/test/jalview/urls/UrlLinkTableModelTest.java @@ -21,9 +21,9 @@ package jalview.urls; -import static jalview.util.UrlConstants.DB_ACCESSION; import static jalview.util.UrlConstants.DELIM; import static jalview.util.UrlConstants.SEP; +import static jalview.util.UrlConstants.SEQUENCE_ID; import jalview.util.MessageManager; @@ -94,7 +94,7 @@ public class UrlLinkTableModelTest { provlist.add(idprov); provlist.add(cprov); - prov = new UrlProvider("MIR:00000005", provlist); + prov = new UrlProvider("TEST2", provlist); } /* @@ -124,7 +124,7 @@ public class UrlLinkTableModelTest { { count++; String defaultName = (String) m.getValueAt(row, nameCol); - Assert.assertEquals(defaultName, "UniProt Knowledgebase"); + Assert.assertEquals(defaultName, "TEST2"); } } Assert.assertEquals(count, 1); @@ -229,17 +229,17 @@ public class UrlLinkTableModelTest { isDefault = (boolean) m.getValueAt(olddefault, 3); } - // set new values, one in each row - uneditable + // set new values, one in each row m.setValueAt("namechanged", 6, 0); m.setValueAt("urlchanged", 7, 1); m.setValueAt(false, 8, 2); - m.setValueAt(true, 9, 3); + m.setValueAt(true, 6, 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.assertTrue((boolean) m.getValueAt(6, 3)); Assert.assertFalse((boolean) m.getValueAt(olddefault, 3)); // check default row is exactly one row still @@ -249,7 +249,7 @@ public class UrlLinkTableModelTest { // if isDefault is true, row is 9 // if isDefault is false, row is not 9 - Assert.assertFalse(isDefault && !(row == 9)); + Assert.assertFalse(isDefault && !(row == 6)); } // check table updated @@ -257,8 +257,8 @@ public class UrlLinkTableModelTest { 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")); + Assert.assertEquals(prov.getDefaultUrl("seqid"), m.getValueAt(6, 1) + .toString().replace(DELIM + SEQUENCE_ID + DELIM, "seqid")); } @@ -275,7 +275,15 @@ public class UrlLinkTableModelTest { Assert.assertFalse(m.isCellEditable(row, 0)); Assert.assertFalse(m.isCellEditable(row, 1)); Assert.assertTrue(m.isCellEditable(row, 2)); - Assert.assertTrue(m.isCellEditable(row, 3)); + + if ((row == 4) || (row == 6) || (row == 7)) + { + Assert.assertTrue(m.isCellEditable(row, 3)); + } + else + { + Assert.assertFalse(m.isCellEditable(row, 3)); + } } } } diff --git a/test/jalview/urls/UrlProviderTest.java b/test/jalview/urls/UrlProviderTest.java new file mode 100644 index 0000000..c7717c9 --- /dev/null +++ b/test/jalview/urls/UrlProviderTest.java @@ -0,0 +1,119 @@ +package jalview.urls; + +import jalview.util.UrlConstants; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; +import java.util.Vector; + +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + + +public class UrlProviderTest { + + // 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() + { + // 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 UrlProviderTest test: " + + e.getMessage()); + } + + IdOrgSettings.setDownloadLocation(temp.getPath()); + + String defaultUrlString = "No default"; + String cachedUrlList = "MIR:00000005|MIR:00000011|Test1|http://blah.blah/$SEQUENCE_ID$|" + + "Test2|http://test2/$DB_ACCESSION$|Test3|http://test3/$SEQUENCE_ID$"; + String userUrlList = "MIR:00000372|Test4|httpL//another.url/$SEQUENCE_ID$"; + + DesktopUrlProviderFactory factory = new DesktopUrlProviderFactory( + defaultUrlString, cachedUrlList, userUrlList); + prov = factory.createUrlProvider(); + } + + @Test(groups = { "Functional" }) + public void testInitUrlProvider() + { + String emblUrl = UrlConstants.DEFAULT_STRING.substring( + UrlConstants.DEFAULT_STRING.indexOf(UrlConstants.SEP) + 1, + UrlConstants.DEFAULT_STRING.length()); + + // chooses EMBL url when default Url id does not exist in provided url lists + Assert.assertEquals(prov.getDefaultUrlId(), UrlConstants.DEFAULT_LABEL); + Assert.assertEquals(prov.getDefaultUrl("FER_CAPAN"), + emblUrl.replace("$SEQUENCE_ID$", "FER_CAPAN")); + + Vector menulinks = prov.getLinksForMenu(); + List allLinks = prov.getLinksForTable(); + + // 9 links in provider - 4 from id file, 4 custom links, 1 additional + // default + Assert.assertEquals(allLinks.size(), 9); + + // 6 links in menu (cachedUrlList) + new default + Assert.assertEquals(menulinks.size(), 6); + + Assert.assertTrue(menulinks + .contains("Test1|http://blah.blah/$SEQUENCE_ID$")); + Assert.assertTrue(menulinks + .contains("Test2|http://test2/$DB_ACCESSION$")); + Assert.assertTrue(menulinks + .contains("Test3|http://test3/$SEQUENCE_ID$")); + Assert.assertTrue(menulinks + .contains("UniProt Knowledgebase|http://identifiers.org/uniprot/$DB_ACCESSION$")); + Assert.assertTrue(menulinks + .contains("InterPro|http://identifiers.org/interpro/$DB_ACCESSION$")); + Assert.assertTrue(menulinks.contains(UrlConstants.DEFAULT_LABEL + + UrlConstants.SEP + emblUrl)); + } + + @Test(groups = { "Functional" }) + public void testSetDefaultUrl() + { + // set custom url as default + Assert.assertTrue(prov.setDefaultUrl("Test1")); + Assert.assertEquals(prov.getDefaultUrlId(), "Test1"); + + // set identifiers url as default + Assert.assertTrue(prov.setDefaultUrl("MIR:00000011")); + Assert.assertEquals(prov.getDefaultUrlId(), "MIR:00000011"); + } + + @Test( + groups = { "Functional" }, + expectedExceptions = { IllegalArgumentException.class }) + public void testSetDefaultUrlWrongly() + { + // don't allow default to be a non-key + prov.setDefaultUrl("not-a-key"); + } +} -- 1.7.10.2