--- /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.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());
+ }
+
+}