From: Chris L. <chr...@jb...> - 2006-05-29 16:35:31
|
User: claprun Date: 06/05/29 12:34:47 Added: common/src/main/org/jboss/portal/test/common URLToolsTestCase.java Log: Extracted URL methods from Tools and WSRPPortletURL to common.util.URLTools (was URL). Revision Changes Path 1.1 date: 2006/05/29 16:34:47; author: claprun; state: Exp;jboss-portal/common/src/main/org/jboss/portal/test/common/URLToolsTestCase.java Index: URLToolsTestCase.java =================================================================== /* * JBoss, Home of Professional Open Source * Copyright 2005, JBoss Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.portal.test.common; import junit.framework.TestCase; /** * @author <a href="mailto:chr...@jb...">Chris Laprun</a> * @version $Revision: 1.1 $ * @since 2.4 (May 29, 2006) */ public class URLToolsTestCase extends TestCase { private static final String MARKUP = "Hello, Anonymous!\n" + "Counter: 0<a href='wsrp_rewrite?wsrp-urlType=render&wsrp-navigationalState=rO0ABXNyACdvcmc" + "uamJvc3MucG9ydGFsLnNlcnZlci51dGlsLlBhcmFtZXRlcnOJoAlMQZGhngIAAUwAA21hcHQAD0xqYXZhL3V0aWwvTWFwO3hwc3IAEWphd" + "mEudXRpbC5IYXNoTWFwBQfawcMWYNEDAAJGAApsb2FkRmFjdG9ySQAJdGhyZXNob2xkeHA_QAAAAAAADHcIAAAAEAAAAAF0AARuYW1ldXI" + "AE1tMamF2YS5sYW5nLlN0cmluZzut0lbn6R17RwIAAHhwAAAAAXQABkp1bGllbng*/wsrp_rewrite'>My name is Julien</a><a hr" + "ef='wsrp_rewrite?wsrp-urlType=render&wsrp-navigationalState=rO0ABXNyACdvcmcuamJvc3MucG9ydGFsLnNlcnZlci" + "51dGlsLlBhcmFtZXRlcnOJoAlMQZGhngIAAUwAA21hcHQAD0xqYXZhL3V0aWwvTWFwO3hwc3IAEWphdmEudXRpbC5IYXNoTWFwBQfawcMW" + "YNEDAAJGAApsb2FkRmFjdG9ySQAJdGhyZXNob2xkeHA_QAAAAAAADHcIAAAAEAAAAAF0AARuYW1ldXIAE1tMamF2YS5sYW5nLlN0cmluZz" + "ut0lbn6R17RwIAAHhwAAAAAXQAA1JveXg*/wsrp_rewrite'>My name is Roy</a><action='wsrp_rewrite?wsrp-urlType=bloc" + "kingAction&wsrp-interactionState=rO0ABXNyACdvcmcuamJvc3MucG9ydGFsLnNlcnZlci51dGlsLlBhcmFtZXRlcnOJoAlMQ" + "ZGhngIAAUwAA21hcHQAD0xqYXZhL3V0aWwvTWFwO3hwc3IAEWphdmEudXRpbC5IYXNoTWFwBQfawcMWYNEDAAJGAApsb2FkRmFjdG9ySQA" + "JdGhyZXNob2xkeHA_QAAAAAAADHcIAAAAEAAAAAF0AAJvcHVyABNbTGphdmEubGFuZy5TdHJpbmc7rdJW5-kde0cCAAB4cAAAAAF0AAIrK" + "3g*/wsrp_rewrite'>counter++</a><a href='wsrp_rewrite?wsrp-urlType=blockingAction&wsrp-interactionState" + "=rO0ABXNyACdvcmcuamJvc3MucG9ydGFsLnNlcnZlci51dGlsLlBhcmFtZXRlcnOJoAlMQZGhngIAAUwAA21hcHQAD0xqYXZhL3V0aWwvT" + "WFwO3hwc3IAEWphdmEudXRpbC5IYXNoTWFwBQfawcMWYNEDAAJGAApsb2FkRmFjdG9ySQAJdGhyZXNob2xkeHA_QAAAAAAADHcIAAAAEAA" + "AAAF0AAJvcHVyABNbTGphdmEubGFuZy5TdHJpbmc7rdJW5-kde0cCAAB4cAAAAAF0AAItLXg*/wsrp_rewrite'>counter--</a>"; public void testExtractURLs() { String markup = MARKUP; org.jboss.portal.common.util.URLTools.URLMatch[] links = org.jboss.portal.common.util.URLTools.extractURLsFrom(markup); assertEquals(4, links.length); org.jboss.portal.common.util.URLTools.URLMatch link = links[0]; assertEquals("wsrp_rewrite?wsrp-urlType=render&wsrp-navigationalState=rO0ABXNyACdvcmcuamJvc3MucG9ydGFsLnNl" + "cnZlci51dGlsLlBhcmFtZXRlcnOJoAlMQZGhngIAAUwAA21hcHQAD0xqYXZhL3V0aWwvTWFwO3hwc3IAEWphdmEudXRpbC5IYXNoTWFwBQ" + "fawcMWYNEDAAJGAApsb2FkRmFjdG9ySQAJdGhyZXNob2xkeHA_QAAAAAAADHcIAAAAEAAAAAF0AARuYW1ldXIAE1tMamF2YS5sYW5nLlN0" + "cmluZzut0lbn6R17RwIAAHhwAAAAAXQABkp1bGllbng*/wsrp_rewrite", link.getURLAsString()); assertEquals("wsrp_rewrite?wsrp-urlType=blockingAction&wsrp-interactionState=rO0ABXNyACdvcmcuamJvc3MucG9yd" + "GFsLnNlcnZlci51dGlsLlBhcmFtZXRlcnOJoAlMQZGhngIAAUwAA21hcHQAD0xqYXZhL3V0aWwvTWFwO3hwc3IAEWphdmEudXRpbC5IYXN" + "oTWFwBQfawcMWYNEDAAJGAApsb2FkRmFjdG9ySQAJdGhyZXNob2xkeHA_QAAAAAAADHcIAAAAEAAAAAF0AAJvcHVyABNbTGphdmEubGFuZ" + "y5TdHJpbmc7rdJW5-kde0cCAAB4cAAAAAF0AAIrK3g*/wsrp_rewrite", links[2].getURLAsString()); String url = "wsrp_rewrite?wsrp-urlType=render&wsrp-mode=help/wsrp_rewrite"; markup = "12345href='" + url + "'76"; links = org.jboss.portal.common.util.URLTools.extractURLsFrom(markup); link = links[0]; int startIndex = 11; assertEquals(startIndex, link.getStart()); assertEquals(url.length() + startIndex, link.getEnd()); assertEquals(url, markup.substring(link.getStart(), link.getEnd())); assertEquals(url, link.getURLAsString()); } public void testReplaceURLs() { String markup = org.jboss.portal.common.util.URLTools.replaceURLsBy(MARKUP, new String[]{"foo", "bar", "baz", "buz"}); String replaced = "Hello, Anonymous!\nCounter: 0<a href='foo'>My name is Julien</a><a href='bar'>My name is Roy</a>" + "<action='baz'>counter++</a><a href='buz'>counter--</a>"; assertEquals(replaced, markup); assertEquals(replaced, org.jboss.portal.common.util.URLTools.replaceURLsBy(replaced, (String[])null)); String mixed = "<a href='wsrp_rewrite?wsrp-urlType=render&wsrp-mode=help/wsrp_rewrite'>My name is Julien</a>" + "<a href='bar'>My name is Roy</a>"; assertEquals("<a href='foo'>My name is Julien</a><a href='bar'>My name is Roy</a>", org.jboss.portal.common.util.URLTools.replaceURLsBy(mixed, new org.jboss.portal.common.util.URLTools.URLReplacementGenerator() { public String getReplacementFor(int currentIndex, org.jboss.portal.common.util.URLTools.URLMatch currentMatch) { String urlAsString = currentMatch.getURLAsString(); if (urlAsString.startsWith("wsrp_rewrite")) { return "foo"; } return urlAsString; } })); } } |