From: kiramt Date: Wed, 16 Nov 2016 15:34:12 +0000 (+0000) Subject: JAL-2316 Unit test for IdentifiersUrlProvider X-Git-Tag: Release_2_10_3b1~346^2~31 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=add9d4cc86b660530c72aae631a3f23aaad16451;p=jalview.git JAL-2316 Unit test for IdentifiersUrlProvider --- diff --git a/test/jalview/urls/CustomUrlProviderTest.java b/test/jalview/urls/CustomUrlProviderTest.java index b536566..62d7806 100644 --- a/test/jalview/urls/CustomUrlProviderTest.java +++ b/test/jalview/urls/CustomUrlProviderTest.java @@ -118,6 +118,8 @@ public class CustomUrlProviderTest assertTrue(displayLinks.containsAll(customProv2.getLinksForDisplay())); // writing url links as a string works + // because UrlProvider does not guarantee order of links, we can't just + // compare the output of writeUrlsAsString to a string, hence the hoops here String result = customProv.writeUrlsAsString(); UrlProviderI up = new CustomUrlProvider(result); assertTrue(displayLinks.containsAll(up.getLinksForDisplay())); diff --git a/test/jalview/urls/IdentifiersUrlProviderTest.java b/test/jalview/urls/IdentifiersUrlProviderTest.java new file mode 100644 index 0000000..d8776f4 --- /dev/null +++ b/test/jalview/urls/IdentifiersUrlProviderTest.java @@ -0,0 +1,143 @@ +/* + * 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 static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertTrue; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Vector; + +import org.testng.annotations.Test; + +public class IdentifiersUrlProviderTest +{ + // Test identifiers.org download file + private static final String testIdOrgFile = "[{\"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 static final String[] dlinks = { + "MIR:00000005|http://identifiers.org/uniprot/$DB_ACCESSION$", + "MIR:00000011|http://identifiers.org/interpro/$DB_ACCESSION$", + "MIR:00000372|http://identifiers.org/ena.embl/$DB_ACCESSION$"}; + + private static final String stringLinks = "MIR:00000005|http://identifiers.org/uniprot/$DB_ACCESSION$" + + "MIR:00000011|http://identifiers.org/interpro/$DB_ACCESSION$" + + "MIR:00000372|http://identifiers.org/ena.embl/$DB_ACCESSION$"; + + + private static final Vector displayLinks = new Vector( + Arrays.asList(dlinks)); + + private static final HashMap urlMap = new HashMap() + { + { + put("MIR:00000005", "http://identifiers.org/uniprot/$DB_ACCESSION$"); + put("MIR:00000011", "http://identifiers.org/interpro/$DB_ACCESSION$"); + put("MIR:00000372", "http://identifiers.org/ena.embl/$DB_ACCESSION$"); + } + }; + + /* + * Test urls are set and returned correctly + */ + @Test(groups = { "Functional" }) + public void testUrlLinks() + { + // creation from cached id list + String idList = "MIR:00000005|MIR:00000011|MIR:00000372"; + UrlProviderI idProv = new IdentifiersUrlProvider(idList, testIdOrgFile); + + assertTrue(displayLinks.containsAll(idProv.getLinksForDisplay())); + + // because UrlProvider does not guarantee order of links, we can't just + // compare the output of writeUrlsAsString to a string, hence the hoops here + String result = idProv.writeUrlsAsString(); + UrlProviderI up = new IdentifiersUrlProvider(result, testIdOrgFile); + assertTrue(displayLinks.containsAll(up.getLinksForDisplay())); + + } + + /* + * Test default is set and returned correctly + */ + @Test(groups = { "Functional" }) + public void testDefaultUrl() + { + // creation from cached id list + String idList = "MIR:00000005|MIR:00000011|MIR:00000372"; + UrlProviderI idProv = new IdentifiersUrlProvider(idList, testIdOrgFile); + + // initially no default + assertEquals(null, idProv.getDefaultUrl()); + + // set and then retrieve default + assertTrue(idProv.setDefaultUrl("MIR:00000005")); + assertEquals("MIR:00000005", idProv.getDefaultUrl()); + assertEquals("http://identifiers.org/uniprot/id", + idProv.getDefaultUrl("id")); + + // attempt to set bad default + assertFalse(idProv.setDefaultUrl("MIR:00001234")); + // default set to null (as default should have been set elsewhere) + assertEquals(null, idProv.getDefaultUrl()); + + // chooseDefaultUrl not implemented for IdentifiersUrlProvider + assertEquals(null, idProv.chooseDefaultUrl()); + } + + /* + * Test url setting works + */ + @Test(groups = { "Functional" }) + public void testSetUrlLinks() + { + // creation from cached id list + String idList = "MIR:00000005|MIR:00000011|MIR:00000372"; + UrlProviderI idProv = new IdentifiersUrlProvider(idList, testIdOrgFile); + + // set url links + String[] ids = { "MIR:00000372", "MIR:00000011" }; + Vector names = new Vector(Arrays.asList(ids)); + Vector urls = null; + idProv.setUrlLinks(names, urls); + assertEquals(names, idProv.getLinksForDisplay()); + + //set default url then reset url links, default should be unset if not present + String[] moreids = { "MIR:00000005", "MIR:00000011" }; + names = new Vector(Arrays.asList(moreids)); + idProv.setDefaultUrl("MIR:00000372"); + idProv.setUrlLinks(names, urls); + assertEquals(names, idProv.getLinksForDisplay()); + assertEquals(null, idProv.getDefaultUrl()); + + } +}