From: kiramt Date: Mon, 24 Oct 2016 15:46:59 +0000 (+0100) Subject: Unit tests for UrlLink X-Git-Tag: Release_2_10_1^2~30 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=f7061cf12f703969bf3e22fc7e37f5712b9df10a;p=jalview.git Unit tests for UrlLink --- diff --git a/src/jalview/util/UrlLink.java b/src/jalview/util/UrlLink.java index f9dda09..872f432 100644 --- a/src/jalview/util/UrlLink.java +++ b/src/jalview/util/UrlLink.java @@ -86,6 +86,7 @@ public class UrlLink label = label.trim(); target = target.trim(); target = target.toUpperCase(); // DBRefEntry uppercases DB names + // NB getCanonicalName might be better but does not currently change case } /** @@ -261,13 +262,14 @@ public class UrlLink @Override public String toString() { + String var = (uses_seq_id ? SEQUENCE_ID : SEQUENCE_NAME); + return label + "|" + url_prefix - + (dynamic ? ("$" + SEQUENCE_ID + ((regexReplace != null) ? "=" + + (dynamic ? ("$" + var + ((regexReplace != null) ? "=" + regexReplace + "=$" : "$")) : "") + ((url_suffix == null) ? "" : url_suffix); - } /** diff --git a/test/jalview/util/UrlLinkTest.java b/test/jalview/util/UrlLinkTest.java new file mode 100644 index 0000000..45ef6af --- /dev/null +++ b/test/jalview/util/UrlLinkTest.java @@ -0,0 +1,246 @@ +/* + * 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.util; + +import static jalview.util.UrlConstants.SEQUENCE_ID; +import static jalview.util.UrlConstants.SEQUENCE_NAME; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertNull; +import static org.testng.AssertJUnit.assertTrue; + +import org.testng.annotations.Test; + +public class UrlLinkTest +{ + + final static String DB = "Test"; + + final static String URL_PREFIX = "http://www.jalview.org/"; + + final static String URL_SUFFIX = "/blah"; + + final static String SEP = "|"; + + final static String DELIM = "$"; + + final static String REGEX_NESTED = "=/^(?:Label:)?(?:(?:gi\\|(\\d+))|([^:]+))/="; + + final static String REGEX_RUBBISH = "=/[0-9]++/="; + + /** + * Test URL link creation when the input string has no regex + */ + @Test(groups = { "Functional" }) + public void testUrlLinkCreationNoRegex() + { + // SEQUENCE_NAME + UrlLink ul = new UrlLink(DB + SEP + URL_PREFIX + DELIM + SEQUENCE_NAME + + DELIM + URL_SUFFIX); + assertEquals(DB.toUpperCase(), ul.getTarget()); + assertEquals(DB, ul.getLabel()); + assertEquals(URL_PREFIX, ul.getUrl_prefix()); + assertEquals(URL_SUFFIX, ul.getUrl_suffix()); + assertTrue(ul.isDynamic()); + assertFalse(ul.usesSeqId()); + assertNull(ul.getRegexReplace()); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + + // SEQUENCE_ID + ul = new UrlLink(DB + SEP + URL_PREFIX + DELIM + SEQUENCE_ID + DELIM + + URL_SUFFIX); + assertEquals(DB.toUpperCase(), ul.getTarget()); + assertEquals(DB, ul.getLabel()); + assertEquals(URL_PREFIX, ul.getUrl_prefix()); + assertEquals(URL_SUFFIX, ul.getUrl_suffix()); + assertTrue(ul.isDynamic()); + assertTrue(ul.usesSeqId()); + assertNull(ul.getRegexReplace()); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + + // Not dynamic + ul = new UrlLink(DB + SEP + URL_PREFIX + URL_SUFFIX.substring(1)); + assertEquals(DB.toUpperCase(), ul.getTarget()); + assertEquals(DB, ul.getLabel()); + assertEquals(URL_PREFIX + URL_SUFFIX.substring(1), ul.getUrl_prefix()); + assertFalse(ul.isDynamic()); + assertFalse(ul.usesSeqId()); + assertNull(ul.getRegexReplace()); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + } + + /** + * Test URL link creation when the input string has regex + */ + @Test(groups = { "Functional" }) + public void testUrlLinkCreationWithRegex() + { + // SEQUENCE_NAME + UrlLink ul = new UrlLink(DB + SEP + URL_PREFIX + DELIM + SEQUENCE_NAME + + REGEX_NESTED + DELIM + URL_SUFFIX); + assertEquals(DB.toUpperCase(), ul.getTarget()); + assertEquals(DB, ul.getLabel()); + assertEquals(URL_PREFIX, ul.getUrl_prefix()); + assertEquals(URL_SUFFIX, ul.getUrl_suffix()); + assertTrue(ul.isDynamic()); + assertFalse(ul.usesSeqId()); + assertEquals(REGEX_NESTED.substring(2, REGEX_NESTED.length() - 2), + ul.getRegexReplace()); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + + // SEQUENCE_ID + ul = new UrlLink(DB + SEP + URL_PREFIX + DELIM + SEQUENCE_ID + + REGEX_NESTED + DELIM + URL_SUFFIX); + assertEquals(DB.toUpperCase(), ul.getTarget()); + assertEquals(DB, ul.getLabel()); + assertEquals(URL_PREFIX, ul.getUrl_prefix()); + assertEquals(URL_SUFFIX, ul.getUrl_suffix()); + assertTrue(ul.isDynamic()); + assertTrue(ul.usesSeqId()); + assertEquals(REGEX_NESTED.substring(2, REGEX_NESTED.length() - 2), + ul.getRegexReplace()); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + + // invalid regex + ul = new UrlLink(DB + SEP + URL_PREFIX + DELIM + SEQUENCE_ID + + REGEX_RUBBISH + DELIM + URL_SUFFIX); + assertEquals(DB.toUpperCase(), ul.getTarget()); + assertEquals(DB, ul.getLabel()); + assertEquals(URL_PREFIX, ul.getUrl_prefix()); + assertEquals(URL_SUFFIX, ul.getUrl_suffix()); + assertTrue(ul.isDynamic()); + assertTrue(ul.usesSeqId()); + assertEquals(REGEX_RUBBISH.substring(2, REGEX_RUBBISH.length() - 2), + ul.getRegexReplace()); + assertFalse(ul.isValid()); + assertEquals( + "Invalid Regular Expression : '" + + REGEX_RUBBISH.substring(2, REGEX_RUBBISH.length() - 2) + + "'\n", + ul.getInvalidMessage()); + } + + /** + * Test construction of link by substituting sequence id or name + */ + @Test(groups = { "Functional" }) + public void testMakeUrlNoRegex() + { + // Single non-regex + UrlLink ul = new UrlLink(DB + SEP + URL_PREFIX + DELIM + SEQUENCE_NAME + + DELIM + URL_SUFFIX); + String idstring = "FER_CAPAA"; + String[] urls = ul.makeUrls(idstring, true); + + assertEquals(2, urls.length); + assertEquals(idstring, urls[0]); + assertEquals(URL_PREFIX + idstring + URL_SUFFIX, urls[1]); + + urls = ul.makeUrls(idstring, false); + + assertEquals(2, urls.length); + assertEquals(idstring, urls[0]); + assertEquals(URL_PREFIX + idstring + URL_SUFFIX, urls[1]); + } + + /** + * Test construction of link by substituting sequence id or name + */ + @Test(groups = { "Functional" }) + public void testMakeUrlWithRegex() + { + // Unused regex + UrlLink ul = new UrlLink(DB + SEP + URL_PREFIX + DELIM + SEQUENCE_ID + + REGEX_NESTED + DELIM + URL_SUFFIX); + String idstring = "FER_CAPAA"; + String[] urls = ul.makeUrls(idstring, true); + + assertEquals(2, urls.length); + assertEquals(idstring, urls[0]); + assertEquals(URL_PREFIX + idstring + URL_SUFFIX, urls[1]); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + + urls = ul.makeUrls(idstring, false); + + assertEquals(2, urls.length); + assertEquals(idstring, urls[0]); + assertEquals(URL_PREFIX + idstring + URL_SUFFIX, urls[1]); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + + // nested regex + idstring = "Label:gi|9234|pdb|102L|A"; + urls = ul.makeUrls(idstring, true); + + assertEquals(2, urls.length); + assertEquals("9234", urls[0]); + assertEquals(URL_PREFIX + "9234" + URL_SUFFIX, urls[1]); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + + urls = ul.makeUrls(idstring, false); + + assertEquals(2, urls.length); + assertEquals("9234", urls[0]); + assertEquals(URL_PREFIX + "9234" + URL_SUFFIX, urls[1]); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + + // unmatched regex + idstring = "this does not match"; + urls = ul.makeUrls(idstring, true); + + assertEquals(2, urls.length); + assertEquals(idstring, urls[0]); + assertEquals(URL_PREFIX + idstring + URL_SUFFIX, urls[1]); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + + urls = ul.makeUrls(idstring, false); + + assertEquals(2, urls.length); + assertEquals(idstring, urls[0]); + assertEquals(URL_PREFIX + idstring + URL_SUFFIX, urls[1]); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + + // empty idstring + idstring = ""; + urls = ul.makeUrls(idstring, true); + + assertNull(urls); + + urls = ul.makeUrls(idstring, false); + + assertEquals(2, urls.length); + assertEquals("", urls[0]); + assertEquals(URL_PREFIX + URL_SUFFIX, urls[1]); + assertTrue(ul.isValid()); + assertNull(ul.getInvalidMessage()); + } + +}