From 717ed17ca6afa23d73e2afb55cc4d46d83c678ed Mon Sep 17 00:00:00 2001 From: kiramt Date: Wed, 2 Nov 2016 09:28:50 +0000 Subject: [PATCH] JAL-2282 URLLink unit tests updated --- src/jalview/util/UrlLink.java | 29 ++++--- test/jalview/util/UrlLinkTest.java | 158 +++++++++++++++++++++++++++++++++++- 2 files changed, 175 insertions(+), 12 deletions(-) diff --git a/src/jalview/util/UrlLink.java b/src/jalview/util/UrlLink.java index e14ff63..218da6c 100644 --- a/src/jalview/util/UrlLink.java +++ b/src/jalview/util/UrlLink.java @@ -83,7 +83,7 @@ public class UrlLink target = link.substring(0, sep); sep = link.lastIndexOf("|"); label = link.substring(0, sep); - url_prefix = link.substring(sep + 1); + url_prefix = link.substring(sep + 1).trim(); regexReplace = null; // implies we trim any prefix if necessary // // regexReplace=".*\\|?(.*)"; url_suffix = null; @@ -331,7 +331,7 @@ public class UrlLink */ protected void parseUrl(String link, String varName, int sqidPos, int sep) { - url_prefix = link.substring(sep + 1, sqidPos); + url_prefix = link.substring(sep + 1, sqidPos).trim(); // delimiter at start of regex: e.g. $SEQUENCE_ID=/ String startDelimiter = "$" + varName + "=/"; @@ -384,10 +384,12 @@ public class UrlLink } /** + * Create a set of URL links for a sequence * - * @param urlLink * @param seq + * The sequence to create links for * @param linkset + * Map of links: key = id | link, value = [target, label, id, link] */ public void createLinksFromSeq(final SequenceI seq, Map> linkset) @@ -406,8 +408,9 @@ public class UrlLink * Create a static URL link * * @param linkset + * Map of links: key = id | link, value = [target, label, id, link] */ - public void createStaticLink(Map> linkset) + protected void createStaticLink(Map> linkset) { if (!linkset.containsKey(label + "|" + getUrl_prefix())) { @@ -418,12 +421,14 @@ public class UrlLink } /** - * Create a dynamic URL link + * Create dynamic URL links * * @param seq + * The sequence to create links for * @param linkset + * Map of links: key = id | link, value = [target, label, id, link] */ - public void createDynamicLinks(final SequenceI seq, + protected void createDynamicLinks(final SequenceI seq, Map> linkset) { // collect id string too @@ -446,14 +451,14 @@ public class UrlLink for (int r = 0; r < dbr.length; r++) { // create Bare ID link for this URL - createBareURLLink(dbr[r].getAccessionId(), linkset, true); + createBareURLLink(dbr[r].getAccessionId(), true, linkset); } } } else if (!usesSeqId() && id != null) // link is name { // create Bare ID link for this URL - createBareURLLink(id, linkset, false); + createBareURLLink(id, false, linkset); } // Create urls from description but only for URL links which are regex @@ -461,15 +466,16 @@ public class UrlLink if (descr != null && getRegexReplace() != null) { // create link for this URL from description where regex matches - createBareURLLink(descr, linkset, false); + createBareURLLink(descr, false, linkset); } } /* * Create a bare URL Link + * Returns map where key = id | link, and value = [target, label, id, link] */ - protected void createBareURLLink(String id, - Map> linkset, Boolean combineLabel) + protected void createBareURLLink(String id, Boolean combineLabel, + Map> linkset) { String[] urls = makeUrls(id, true); if (urls != null) @@ -481,6 +487,7 @@ public class UrlLink String thisLabel = label; if (combineLabel) { + // incorporate label with idstring thisLabel = label + "|" + urls[u]; } diff --git a/test/jalview/util/UrlLinkTest.java b/test/jalview/util/UrlLinkTest.java index 45ef6af..875dd7f 100644 --- a/test/jalview/util/UrlLinkTest.java +++ b/test/jalview/util/UrlLinkTest.java @@ -27,6 +27,15 @@ import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertTrue; +import jalview.datamodel.DBRefEntry; +import jalview.datamodel.DBRefSource; +import jalview.datamodel.Sequence; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + import org.testng.annotations.Test; public class UrlLinkTest @@ -167,7 +176,8 @@ public class UrlLinkTest } /** - * Test construction of link by substituting sequence id or name + * Test construction of link by substituting sequence id or name using regular + * expression */ @Test(groups = { "Functional" }) public void testMakeUrlWithRegex() @@ -243,4 +253,150 @@ public class UrlLinkTest assertNull(ul.getInvalidMessage()); } + /** + * Test creating links with null sequence + */ + @Test(groups = { "Functional" }) + public void testCreateLinksFromNullSequence() + { + UrlLink ul = new UrlLink(DB + SEP + URL_PREFIX + DELIM + SEQUENCE_NAME + + DELIM + URL_SUFFIX); + + Map> linkset = new LinkedHashMap>(); + ul.createLinksFromSeq(null, linkset); + + String key = DB + SEP + URL_PREFIX; + assertEquals(1, linkset.size()); + assertTrue(linkset.containsKey(key)); + assertEquals(linkset.get(key).get(0), DB.toUpperCase()); + assertEquals(linkset.get(key).get(1), DB); + assertEquals(linkset.get(key).get(2), null); + assertEquals(linkset.get(key).get(3), URL_PREFIX); + } + + /** + * Test creating links with non-dynamic urlLink + */ + @Test(groups = { "Functional" }) + public void testCreateLinksForNonDynamic() + { + UrlLink ul = new UrlLink(DB + SEP + URL_PREFIX + URL_SUFFIX); + + Map> linkset = new LinkedHashMap>(); + ul.createLinksFromSeq(null, linkset); + + String key = DB + SEP + URL_PREFIX + URL_SUFFIX; + assertEquals(1, linkset.size()); + assertTrue(linkset.containsKey(key)); + assertEquals(linkset.get(key).get(0), DB.toUpperCase()); + assertEquals(linkset.get(key).get(1), DB); + assertEquals(linkset.get(key).get(2), null); + assertEquals(linkset.get(key).get(3), URL_PREFIX + URL_SUFFIX); + } + + /** + * Test creating links + */ + @Test(groups = { "Functional" }) + public void testCreateLinksFromSequence() + { + + // create list of links and list of DBRefs + List links = new ArrayList(); + List refs = new ArrayList(); + + // links as might be added into Preferences | Connections dialog + links.add("EMBL-EBI Search | http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$" + + SEQUENCE_NAME + "$"); + links.add("UNIPROT | http://www.uniprot.org/uniprot/$" + SEQUENCE_ID + + "$"); + links.add("INTERPRO | http://www.ebi.ac.uk/interpro/entry/$" + + SEQUENCE_ID + "$"); + + // make seq0 dbrefs + refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "P83527")); + refs.add(new DBRefEntry("INTERPRO", "1", "IPR001041")); + refs.add(new DBRefEntry("INTERPRO", "1", "IPR006058")); + refs.add(new DBRefEntry("INTERPRO", "1", "IPR012675")); + + Sequence seq0 = new Sequence("FER1", "AKPNGVL"); + + // add all the dbrefs to the sequence + seq0.addDBRef(refs.get(0)); + seq0.addDBRef(refs.get(1)); + seq0.addDBRef(refs.get(2)); + seq0.addDBRef(refs.get(3)); + seq0.createDatasetSequence(); + + // Test where link takes a sequence id as replacement + UrlLink ul = new UrlLink(DB + SEP + URL_PREFIX + DELIM + SEQUENCE_NAME + + DELIM + URL_SUFFIX); + + Map> linkset = new LinkedHashMap>(); + ul.createLinksFromSeq(seq0, linkset); + + String key = seq0.getName() + SEP + URL_PREFIX + seq0.getName() + + URL_SUFFIX; + assertEquals(1, linkset.size()); + assertTrue(linkset.containsKey(key)); + assertEquals(linkset.get(key).get(0), DB.toUpperCase()); + assertEquals(linkset.get(key).get(1), DB); + assertEquals(linkset.get(key).get(2), seq0.getName()); + assertEquals(linkset.get(key).get(3), URL_PREFIX + seq0.getName() + + URL_SUFFIX); + + // Test where link takes a db annotation id and only has one dbref + ul = new UrlLink(links.get(1)); + linkset = new LinkedHashMap>(); + ul.createLinksFromSeq(seq0, linkset); + + key = "P83527|http://www.uniprot.org/uniprot/P83527"; + assertEquals(1, linkset.size()); + assertTrue(linkset.containsKey(key)); + assertEquals(linkset.get(key).get(0), DBRefSource.UNIPROT); + assertEquals(linkset.get(key).get(1), DBRefSource.UNIPROT + SEP + + "P83527"); + assertEquals(linkset.get(key).get(2), "P83527"); + assertEquals(linkset.get(key).get(3), + "http://www.uniprot.org/uniprot/P83527"); + + // Test where link takes a db annotation id and has multiple dbrefs + ul = new UrlLink(links.get(2)); + linkset = new LinkedHashMap>(); + ul.createLinksFromSeq(seq0, linkset); + assertEquals(3, linkset.size()); + + // check each link made it in correctly + key = "IPR001041|http://www.ebi.ac.uk/interpro/entry/IPR001041"; + assertTrue(linkset.containsKey(key)); + assertEquals(linkset.get(key).get(0), "INTERPRO"); + assertEquals(linkset.get(key).get(1), "INTERPRO" + SEP + "IPR001041"); + assertEquals(linkset.get(key).get(2), "IPR001041"); + assertEquals(linkset.get(key).get(3), + "http://www.ebi.ac.uk/interpro/entry/IPR001041"); + + key = "IPR006058|http://www.ebi.ac.uk/interpro/entry/IPR006058"; + assertTrue(linkset.containsKey(key)); + assertEquals(linkset.get(key).get(0), "INTERPRO"); + assertEquals(linkset.get(key).get(1), "INTERPRO" + SEP + "IPR006058"); + assertEquals(linkset.get(key).get(2), "IPR006058"); + assertEquals(linkset.get(key).get(3), + "http://www.ebi.ac.uk/interpro/entry/IPR006058"); + + key = "IPR012675|http://www.ebi.ac.uk/interpro/entry/IPR012675"; + assertTrue(linkset.containsKey(key)); + assertEquals(linkset.get(key).get(0), "INTERPRO"); + assertEquals(linkset.get(key).get(1), "INTERPRO" + SEP + "IPR012675"); + assertEquals(linkset.get(key).get(2), "IPR012675"); + assertEquals(linkset.get(key).get(3), + "http://www.ebi.ac.uk/interpro/entry/IPR012675"); + + // Test where there are no matching dbrefs for the link + ul = new UrlLink(DB + SEP + URL_PREFIX + DELIM + SEQUENCE_ID + DELIM + + URL_SUFFIX); + linkset = new LinkedHashMap>(); + ul.createLinksFromSeq(seq0, linkset); + assertTrue(linkset.isEmpty()); + } + } -- 1.7.10.2