From a2f83ec9e48dc2c9ebe46cf46d9d58ab8cc61b42 Mon Sep 17 00:00:00 2001 From: kiramt Date: Wed, 16 Nov 2016 11:58:21 +0000 Subject: [PATCH] JAL-2316 Added unit tests for CustomUrlProvider --- src/jalview/urls/CustomUrlProvider.java | 4 +- test/jalview/urls/CustomUrlProviderTest.java | 136 ++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 test/jalview/urls/CustomUrlProviderTest.java diff --git a/src/jalview/urls/CustomUrlProvider.java b/src/jalview/urls/CustomUrlProvider.java index 50037b6..2d26883 100644 --- a/src/jalview/urls/CustomUrlProvider.java +++ b/src/jalview/urls/CustomUrlProvider.java @@ -85,7 +85,8 @@ public class CustomUrlProvider extends UrlProviderImpl { rxstart = url.indexOf(DELIM + SEQUENCE_ID + DELIM); } - while (rxstart == -1 && url.indexOf("/=" + DELIM) == -1) + while (rxstart == -1 && url.indexOf("/=" + DELIM) == -1 + && st.hasMoreTokens()) { url = url + SEP + st.nextToken(); } @@ -260,6 +261,7 @@ public class CustomUrlProvider extends UrlProviderImpl { urls.put(DEFAULT_LABEL, new UrlLink(DEFAULT_STRING)); } + defaultUrl = DEFAULT_LABEL; return DEFAULT_LABEL; } diff --git a/test/jalview/urls/CustomUrlProviderTest.java b/test/jalview/urls/CustomUrlProviderTest.java new file mode 100644 index 0000000..b536566 --- /dev/null +++ b/test/jalview/urls/CustomUrlProviderTest.java @@ -0,0 +1,136 @@ +/* + * 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 CustomUrlProviderTest +{ + + private static final String cachedList = "TEST|http://someurl.blah/$DB_ACCESSION$|" + + "ANOTHER|http://test/t$SEQUENCE_ID$|" + + "TEST2|http://address/$SEQUENCE_ID$|SRS|" + + "http://theSRSlink/$SEQUENCE_ID$"; + + private static final HashMap urlMap = new HashMap() + { + { + put("TEST","http://someurl.blah/$DB_ACCESSION$"); + put("ANOTHER","http://test/t$SEQUENCE_ID$"); + put("TEST2", "http://address/$SEQUENCE_ID$"); + put("SRS", "http://theSRSlink/$SEQUENCE_ID$"); + } + }; + + private static final String[] dlinks = { + "TEST|http://someurl.blah/$DB_ACCESSION$", + "ANOTHER|http://test/t$SEQUENCE_ID$", + "TEST2|http://address/$SEQUENCE_ID$", + UrlProviderI.DEFAULT_STRING }; + + private static final Vector displayLinks = new Vector( + Arrays.asList(dlinks)); + + private static final String[] dlinks2 = { "a|http://x.y.z/$SEQUENCE_ID$" }; + + private static final Vector displayLinks2 = new Vector( + Arrays.asList(dlinks2)); + + private static final String[] list1 = { "a" }; + + private static final String[] list2 = { "http://x.y.z/$SEQUENCE_ID$" }; + + private static final Vector names = new Vector( + Arrays.asList(list1)); + + private static final Vector urls = new Vector( + Arrays.asList(list2)); + + /* + * Test default url is set and returned correctly + */ + @Test(groups = { "Functional" }) + public void testDefaultUrl() + { + UrlProviderI customProv = new CustomUrlProvider(cachedList); + + // default url can be set + assertTrue(customProv.setDefaultUrl("ANOTHER")); + String result = customProv.getDefaultUrl(); + assertEquals("ANOTHER", result); + + // default url can be retrieved given a sequence id + result = customProv.getDefaultUrl("seqid"); + assertEquals("http://test/tseqid", result); + + // if there is no default url it sets the default to null + assertFalse(customProv.setDefaultUrl("No default")); + result = customProv.getDefaultUrl(); + assertEquals(null, result); + + // choosing the default picks the DEFAULT_STRING option + customProv.chooseDefaultUrl(); + result = customProv.getDefaultUrl("seqid"); + assertEquals( + UrlProviderI.DEFAULT_STRING.split("\\|")[1].split("\\$")[0] + + "seqid", + result); + } + + /* + * Test urls are set and returned correctly + */ + @Test(groups = { "Functional" }) + public void testUrlLinks() + { + // creation from cached url list works + old links upgraded + UrlProviderI customProv = new CustomUrlProvider(cachedList); + assertTrue(displayLinks.containsAll(customProv.getLinksForDisplay())); + + // creation from map works + old links upgraded + UrlProviderI customProv2 = new CustomUrlProvider(urlMap); + assertTrue(displayLinks.containsAll(customProv2.getLinksForDisplay())); + + // writing url links as a string works + String result = customProv.writeUrlsAsString(); + UrlProviderI up = new CustomUrlProvider(result); + assertTrue(displayLinks.containsAll(up.getLinksForDisplay())); + + result = customProv2.writeUrlsAsString(); + UrlProviderI up2 = new CustomUrlProvider(result); + assertTrue(displayLinks.containsAll(up2.getLinksForDisplay())); + + // resetting urls from a pair of lists works + customProv.setUrlLinks(names, urls); + assertTrue(displayLinks2.containsAll(customProv.getLinksForDisplay())); + + customProv2.setUrlLinks(names, urls); + assertTrue(displayLinks2.containsAll(customProv2.getLinksForDisplay())); + } +} -- 1.7.10.2