You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by pe...@apache.org on 2011/06/25 03:22:07 UTC

svn commit: r1139477 - in /wicket/trunk/wicket-request/src: main/java/org/apache/wicket/request/Url.java test/java/org/apache/wicket/request/UrlTest.java

Author: pete
Date: Sat Jun 25 01:22:07 2011
New Revision: 1139477

URL: http://svn.apache.org/viewvc?rev=1139477&view=rev
Log:
WICKET-3834 WicketTester does not follow absolute redirects: make Url.parse(..) capable of parsing absolute urls

Added:
    wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java
      - copied, changed from r1139453, wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java
Modified:
    wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java

Modified: wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java?rev=1139477&r1=1139476&r2=1139477&view=diff
==============================================================================
--- wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java (original)
+++ wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java Sat Jun 25 01:22:07 2011
@@ -116,10 +116,11 @@ public final class Url implements Serial
 	 * Parses the given URL string.
 	 * 
 	 * @param url
+	 *           full absolute or relative url with query string
 	 * @param charset
 	 * @return Url object
 	 */
-	public static Url parse(final String url, Charset charset)
+	public static Url parse(String url, Charset charset)
 	{
 		Args.notNull(url, "url");
 
@@ -128,36 +129,79 @@ public final class Url implements Serial
 		// the url object resolved the charset, use that
 		charset = result.getCharset();
 
-		String segments;
-		String query;
+		// extract query string part
+		final String queryString;
+		final String absoluteUrl;
 
-		int qIndex = url.indexOf('?');
+		int queryAt = url.indexOf('?');
 
-		if (qIndex == -1)
+		if (queryAt == -1)
 		{
-			segments = url;
-			query = "";
+			queryString = "";
+			absoluteUrl = url;
 		}
 		else
 		{
-			segments = url.substring(0, qIndex);
-			query = url.substring(qIndex + 1);
+			absoluteUrl = url.substring(0, queryAt);
+			queryString = url.substring(queryAt + 1);
 		}
+		
+		// get absolute / relative part of url
+		String relativeUrl;
 
-		if (segments.length() > 0)
+		// absolute urls contain a scheme://
+		final int protocolAt = absoluteUrl.indexOf("://");
+
+		if (protocolAt != -1)
+		{
+			result.protocol = absoluteUrl.substring(0, protocolAt);
+			final String afterProto = absoluteUrl.substring(protocolAt + 3);
+			final String hostAndPort;
+
+			int relativeAt = afterProto.indexOf('/');
+
+			if (relativeAt == -1)
+			{
+				relativeUrl = "";
+				hostAndPort = afterProto;
+			}
+			else
+			{
+				relativeUrl = afterProto.substring(relativeAt);
+				hostAndPort = afterProto.substring(0, relativeAt);
+			}
+
+			int portAt = hostAndPort.indexOf(':');
+
+			if (portAt == -1)
+			{
+				result.host = hostAndPort;
+				result.port = null;
+			}
+			else
+			{
+				result.host = hostAndPort.substring(0, portAt);
+				result.port = Integer.parseInt(hostAndPort.substring(portAt + 1));
+			}
+		}
+		else
 		{
+			relativeUrl = absoluteUrl;
+		}
 
+		if (relativeUrl.length() > 0)
+		{
 			boolean removeLast = false;
-			if (segments.endsWith("/"))
+			if (relativeUrl.endsWith("/"))
 			{
 				// we need to append something and remove it after splitting
 				// because otherwise the
 				// trailing slashes will be lost
-				segments += "/x";
+				relativeUrl += "/x";
 				removeLast = true;
 			}
 
-			String segmentArray[] = Strings.split(segments, '/');
+			String segmentArray[] = Strings.split(relativeUrl, '/');
 
 			if (removeLast)
 			{
@@ -173,9 +217,9 @@ public final class Url implements Serial
 			}
 		}
 
-		if (query.length() > 0)
+		if (queryString.length() > 0)
 		{
-			String queryArray[] = Strings.split(query, '&');
+			String queryArray[] = Strings.split(queryString, '&');
 			for (String s : queryArray)
 			{
 				result.parameters.add(parseQueryParameter(s, charset));

Copied: wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java (from r1139453, wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java)
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java?p2=wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java&p1=wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java&r1=1139453&r2=1139477&rev=1139477&view=diff
==============================================================================
--- wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java (original)
+++ wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java Sat Jun 25 01:22:07 2011
@@ -16,25 +16,67 @@
  */
 package org.apache.wicket.request;
 
-import org.apache.wicket.util.crypt.CharEncoding;
+import java.util.Arrays;
+
 import org.junit.Assert;
 import org.junit.Test;
 
 /**
- * Tests for {@link UrlDecoder}
+ * Tests for {@link org.apache.wicket.request.Url}
  */
-public class UrlEncoderTest
+public class UrlTest extends Assert
 {
+	@Test
+	public void parseRelativeUrl()
+	{
+		Url url = Url.parse("foo");
+		checkUrl(url, null, null, null, "foo");
+		assertFalse(url.isAbsolute());
+
+		url = Url.parse("foo/bar/baz");
+		checkUrl(url, null, null, null, "foo", "bar", "baz");
+		assertFalse(url.isAbsolute());
+
+		url = Url.parse("?foobar");
+		checkUrl(url, null, null, null);
+		assertEquals("", url.getQueryParameter("foobar").getValue());
+		assertFalse(url.isAbsolute());
+
+		url = Url.parse("foo?a=123");
+		checkUrl(url, null, null, null, "foo");
+		assertEquals("123", url.getQueryParameter("a").getValue());
+		assertFalse(url.isAbsolute());
+	
+		url = Url.parse("/foo");
+		checkUrl(url, null, null, null, "", "foo");
+		assertTrue(url.isAbsolute());
+	}
 
-	/**
-	 * <a href="https://issues.apache.org/jira/browse/WICKET-3721">WICKET-3721</a> Encode
-	 * apostrophes because otherwise they get XML encoded by ComponentTag#writeOutput() to
-	 * &amp;#039; and eventually break links with javascript:
-	 */
 	@Test
-	public void encodeApostrophe()
+	public void parseAbsoluteUrl()
+	{
+		Url url = Url.parse("ftp://myhost:8081");
+		checkUrl(url, "ftp", "myhost", 8081);
+		assertFalse(url.isAbsolute());
+	
+		url = Url.parse("gopher://myhost:8081/foo");
+		checkUrl(url, "gopher", "myhost", 8081, "", "foo");
+		assertTrue(url.isAbsolute());
+
+		url = Url.parse("https://myhost/foo");
+		checkUrl(url, "https", "myhost", null, "", "foo");
+		assertTrue(url.isAbsolute());
+
+		url = Url.parse("https://myhost/foo:123");
+		checkUrl(url, "https", "myhost", null, "", "foo:123");
+		assertTrue(url.isAbsolute());
+	}
+
+	private void checkUrl(Url url, String protocol, String host, Integer port, String... segments)
 	{
-		Assert.assertEquals("someone%27s%20bad%20url",
-			UrlEncoder.FULL_PATH_INSTANCE.encode("someone's bad url", CharEncoding.UTF_8));
+		assertNotNull(url);
+		assertEquals(host, url.getHost());
+		assertEquals(port, url.getPort());
+		assertEquals(Arrays.asList(segments), url.getSegments());
 	}
 }



Re: svn commit: r1139477 - in /wicket/trunk/wicket-request/src: main/java/org/apache/wicket/request/Url.java test/java/org/apache/wicket/request/UrlTest.java

Posted by Martin Grigorov <mg...@apache.org>.
The commit diff looked like you renamed and removed the test for UrlDecoder.
Sorry. It is here.

I'll move the test for Url in -request.

On Sat, Jun 25, 2011 at 1:18 PM, Peter Ertl <pe...@gmx.org> wrote:
> Hi Martin,
>
> what test for URLDecoder are you talking about? To the best of my knowledge I did not delete anything.
>
> I _copied_ from UrlEncoderTest but the file is still there.
>
> Merging UrlTest from wicket-core with UrlTest in wicket-request makes sense. Did not realize there's another test case with the same name in another module. Having them split is odd indeed.
>
> Am 25.06.2011 um 04:24 schrieb Martin Grigorov:
>
>> Why the hell the test for URLDecoder has been killed and replaced with
>> something else ?!?
>> We have tests for Url at org.apache.wicket.request.UrlTest in wicket-core.
>> I agree that it belongs to wicket-request and to be able to move it
>> there it should get rid of WicketObjects usage but this kind of
>> killing other tests is totally wrong!
>>
>> On Sat, Jun 25, 2011 at 4:22 AM,  <pe...@apache.org> wrote:
>>> Author: pete
>>> Date: Sat Jun 25 01:22:07 2011
>>> New Revision: 1139477
>>>
>>> URL: http://svn.apache.org/viewvc?rev=1139477&view=rev
>>> Log:
>>> WICKET-3834 WicketTester does not follow absolute redirects: make Url.parse(..) capable of parsing absolute urls
>>>
>>> Added:
>>>    wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java
>>>      - copied, changed from r1139453, wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java
>>> Modified:
>>>    wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java
>>>
>>> Modified: wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java
>>> URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java?rev=1139477&r1=1139476&r2=1139477&view=diff
>>> ==============================================================================
>>> --- wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java (original)
>>> +++ wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java Sat Jun 25 01:22:07 2011
>>> @@ -116,10 +116,11 @@ public final class Url implements Serial
>>>         * Parses the given URL string.
>>>         *
>>>         * @param url
>>> +        *           full absolute or relative url with query string
>>>         * @param charset
>>>         * @return Url object
>>>         */
>>> -       public static Url parse(final String url, Charset charset)
>>> +       public static Url parse(String url, Charset charset)
>>>        {
>>>                Args.notNull(url, "url");
>>>
>>> @@ -128,36 +129,79 @@ public final class Url implements Serial
>>>                // the url object resolved the charset, use that
>>>                charset = result.getCharset();
>>>
>>> -               String segments;
>>> -               String query;
>>> +               // extract query string part
>>> +               final String queryString;
>>> +               final String absoluteUrl;
>>>
>>> -               int qIndex = url.indexOf('?');
>>> +               int queryAt = url.indexOf('?');
>>>
>>> -               if (qIndex == -1)
>>> +               if (queryAt == -1)
>>>                {
>>> -                       segments = url;
>>> -                       query = "";
>>> +                       queryString = "";
>>> +                       absoluteUrl = url;
>>>                }
>>>                else
>>>                {
>>> -                       segments = url.substring(0, qIndex);
>>> -                       query = url.substring(qIndex + 1);
>>> +                       absoluteUrl = url.substring(0, queryAt);
>>> +                       queryString = url.substring(queryAt + 1);
>>>                }
>>> +
>>> +               // get absolute / relative part of url
>>> +               String relativeUrl;
>>>
>>> -               if (segments.length() > 0)
>>> +               // absolute urls contain a scheme://
>>> +               final int protocolAt = absoluteUrl.indexOf("://");
>>> +
>>> +               if (protocolAt != -1)
>>> +               {
>>> +                       result.protocol = absoluteUrl.substring(0, protocolAt);
>>> +                       final String afterProto = absoluteUrl.substring(protocolAt + 3);
>>> +                       final String hostAndPort;
>>> +
>>> +                       int relativeAt = afterProto.indexOf('/');
>>> +
>>> +                       if (relativeAt == -1)
>>> +                       {
>>> +                               relativeUrl = "";
>>> +                               hostAndPort = afterProto;
>>> +                       }
>>> +                       else
>>> +                       {
>>> +                               relativeUrl = afterProto.substring(relativeAt);
>>> +                               hostAndPort = afterProto.substring(0, relativeAt);
>>> +                       }
>>> +
>>> +                       int portAt = hostAndPort.indexOf(':');
>>> +
>>> +                       if (portAt == -1)
>>> +                       {
>>> +                               result.host = hostAndPort;
>>> +                               result.port = null;
>>> +                       }
>>> +                       else
>>> +                       {
>>> +                               result.host = hostAndPort.substring(0, portAt);
>>> +                               result.port = Integer.parseInt(hostAndPort.substring(portAt + 1));
>>> +                       }
>>> +               }
>>> +               else
>>>                {
>>> +                       relativeUrl = absoluteUrl;
>>> +               }
>>>
>>> +               if (relativeUrl.length() > 0)
>>> +               {
>>>                        boolean removeLast = false;
>>> -                       if (segments.endsWith("/"))
>>> +                       if (relativeUrl.endsWith("/"))
>>>                        {
>>>                                // we need to append something and remove it after splitting
>>>                                // because otherwise the
>>>                                // trailing slashes will be lost
>>> -                               segments += "/x";
>>> +                               relativeUrl += "/x";
>>>                                removeLast = true;
>>>                        }
>>>
>>> -                       String segmentArray[] = Strings.split(segments, '/');
>>> +                       String segmentArray[] = Strings.split(relativeUrl, '/');
>>>
>>>                        if (removeLast)
>>>                        {
>>> @@ -173,9 +217,9 @@ public final class Url implements Serial
>>>                        }
>>>                }
>>>
>>> -               if (query.length() > 0)
>>> +               if (queryString.length() > 0)
>>>                {
>>> -                       String queryArray[] = Strings.split(query, '&');
>>> +                       String queryArray[] = Strings.split(queryString, '&');
>>>                        for (String s : queryArray)
>>>                        {
>>>                                result.parameters.add(parseQueryParameter(s, charset));
>>>
>>> Copied: wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java (from r1139453, wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java)
>>> URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java?p2=wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java&p1=wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java&r1=1139453&r2=1139477&rev=1139477&view=diff
>>> ==============================================================================
>>> --- wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java (original)
>>> +++ wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java Sat Jun 25 01:22:07 2011
>>> @@ -16,25 +16,67 @@
>>>  */
>>>  package org.apache.wicket.request;
>>>
>>> -import org.apache.wicket.util.crypt.CharEncoding;
>>> +import java.util.Arrays;
>>> +
>>>  import org.junit.Assert;
>>>  import org.junit.Test;
>>>
>>>  /**
>>> - * Tests for {@link UrlDecoder}
>>> + * Tests for {@link org.apache.wicket.request.Url}
>>>  */
>>> -public class UrlEncoderTest
>>> +public class UrlTest extends Assert
>>>  {
>>> +       @Test
>>> +       public void parseRelativeUrl()
>>> +       {
>>> +               Url url = Url.parse("foo");
>>> +               checkUrl(url, null, null, null, "foo");
>>> +               assertFalse(url.isAbsolute());
>>> +
>>> +               url = Url.parse("foo/bar/baz");
>>> +               checkUrl(url, null, null, null, "foo", "bar", "baz");
>>> +               assertFalse(url.isAbsolute());
>>> +
>>> +               url = Url.parse("?foobar");
>>> +               checkUrl(url, null, null, null);
>>> +               assertEquals("", url.getQueryParameter("foobar").getValue());
>>> +               assertFalse(url.isAbsolute());
>>> +
>>> +               url = Url.parse("foo?a=123");
>>> +               checkUrl(url, null, null, null, "foo");
>>> +               assertEquals("123", url.getQueryParameter("a").getValue());
>>> +               assertFalse(url.isAbsolute());
>>> +
>>> +               url = Url.parse("/foo");
>>> +               checkUrl(url, null, null, null, "", "foo");
>>> +               assertTrue(url.isAbsolute());
>>> +       }
>>>
>>> -       /**
>>> -        * <a href="https://issues.apache.org/jira/browse/WICKET-3721">WICKET-3721</a> Encode
>>> -        * apostrophes because otherwise they get XML encoded by ComponentTag#writeOutput() to
>>> -        * &amp;#039; and eventually break links with javascript:
>>> -        */
>>>        @Test
>>> -       public void encodeApostrophe()
>>> +       public void parseAbsoluteUrl()
>>> +       {
>>> +               Url url = Url.parse("ftp://myhost:8081");
>>> +               checkUrl(url, "ftp", "myhost", 8081);
>>> +               assertFalse(url.isAbsolute());
>>> +
>>> +               url = Url.parse("gopher://myhost:8081/foo");
>>> +               checkUrl(url, "gopher", "myhost", 8081, "", "foo");
>>> +               assertTrue(url.isAbsolute());
>>> +
>>> +               url = Url.parse("https://myhost/foo");
>>> +               checkUrl(url, "https", "myhost", null, "", "foo");
>>> +               assertTrue(url.isAbsolute());
>>> +
>>> +               url = Url.parse("https://myhost/foo:123");
>>> +               checkUrl(url, "https", "myhost", null, "", "foo:123");
>>> +               assertTrue(url.isAbsolute());
>>> +       }
>>> +
>>> +       private void checkUrl(Url url, String protocol, String host, Integer port, String... segments)
>>>        {
>>> -               Assert.assertEquals("someone%27s%20bad%20url",
>>> -                       UrlEncoder.FULL_PATH_INSTANCE.encode("someone's bad url", CharEncoding.UTF_8));
>>> +               assertNotNull(url);
>>> +               assertEquals(host, url.getHost());
>>> +               assertEquals(port, url.getPort());
>>> +               assertEquals(Arrays.asList(segments), url.getSegments());
>>>        }
>>>  }
>>>
>>>
>>>
>>
>>
>>
>> --
>> Martin Grigorov
>> jWeekend
>> Training, Consulting, Development
>> http://jWeekend.com
>
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

Re: svn commit: r1139477 - in /wicket/trunk/wicket-request/src: main/java/org/apache/wicket/request/Url.java test/java/org/apache/wicket/request/UrlTest.java

Posted by Peter Ertl <pe...@gmx.org>.
Hi Martin,

what test for URLDecoder are you talking about? To the best of my knowledge I did not delete anything.

I _copied_ from UrlEncoderTest but the file is still there.

Merging UrlTest from wicket-core with UrlTest in wicket-request makes sense. Did not realize there's another test case with the same name in another module. Having them split is odd indeed.

Am 25.06.2011 um 04:24 schrieb Martin Grigorov:

> Why the hell the test for URLDecoder has been killed and replaced with
> something else ?!?
> We have tests for Url at org.apache.wicket.request.UrlTest in wicket-core.
> I agree that it belongs to wicket-request and to be able to move it
> there it should get rid of WicketObjects usage but this kind of
> killing other tests is totally wrong!
> 
> On Sat, Jun 25, 2011 at 4:22 AM,  <pe...@apache.org> wrote:
>> Author: pete
>> Date: Sat Jun 25 01:22:07 2011
>> New Revision: 1139477
>> 
>> URL: http://svn.apache.org/viewvc?rev=1139477&view=rev
>> Log:
>> WICKET-3834 WicketTester does not follow absolute redirects: make Url.parse(..) capable of parsing absolute urls
>> 
>> Added:
>>    wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java
>>      - copied, changed from r1139453, wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java
>> Modified:
>>    wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java
>> 
>> Modified: wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java
>> URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java?rev=1139477&r1=1139476&r2=1139477&view=diff
>> ==============================================================================
>> --- wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java (original)
>> +++ wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java Sat Jun 25 01:22:07 2011
>> @@ -116,10 +116,11 @@ public final class Url implements Serial
>>         * Parses the given URL string.
>>         *
>>         * @param url
>> +        *           full absolute or relative url with query string
>>         * @param charset
>>         * @return Url object
>>         */
>> -       public static Url parse(final String url, Charset charset)
>> +       public static Url parse(String url, Charset charset)
>>        {
>>                Args.notNull(url, "url");
>> 
>> @@ -128,36 +129,79 @@ public final class Url implements Serial
>>                // the url object resolved the charset, use that
>>                charset = result.getCharset();
>> 
>> -               String segments;
>> -               String query;
>> +               // extract query string part
>> +               final String queryString;
>> +               final String absoluteUrl;
>> 
>> -               int qIndex = url.indexOf('?');
>> +               int queryAt = url.indexOf('?');
>> 
>> -               if (qIndex == -1)
>> +               if (queryAt == -1)
>>                {
>> -                       segments = url;
>> -                       query = "";
>> +                       queryString = "";
>> +                       absoluteUrl = url;
>>                }
>>                else
>>                {
>> -                       segments = url.substring(0, qIndex);
>> -                       query = url.substring(qIndex + 1);
>> +                       absoluteUrl = url.substring(0, queryAt);
>> +                       queryString = url.substring(queryAt + 1);
>>                }
>> +
>> +               // get absolute / relative part of url
>> +               String relativeUrl;
>> 
>> -               if (segments.length() > 0)
>> +               // absolute urls contain a scheme://
>> +               final int protocolAt = absoluteUrl.indexOf("://");
>> +
>> +               if (protocolAt != -1)
>> +               {
>> +                       result.protocol = absoluteUrl.substring(0, protocolAt);
>> +                       final String afterProto = absoluteUrl.substring(protocolAt + 3);
>> +                       final String hostAndPort;
>> +
>> +                       int relativeAt = afterProto.indexOf('/');
>> +
>> +                       if (relativeAt == -1)
>> +                       {
>> +                               relativeUrl = "";
>> +                               hostAndPort = afterProto;
>> +                       }
>> +                       else
>> +                       {
>> +                               relativeUrl = afterProto.substring(relativeAt);
>> +                               hostAndPort = afterProto.substring(0, relativeAt);
>> +                       }
>> +
>> +                       int portAt = hostAndPort.indexOf(':');
>> +
>> +                       if (portAt == -1)
>> +                       {
>> +                               result.host = hostAndPort;
>> +                               result.port = null;
>> +                       }
>> +                       else
>> +                       {
>> +                               result.host = hostAndPort.substring(0, portAt);
>> +                               result.port = Integer.parseInt(hostAndPort.substring(portAt + 1));
>> +                       }
>> +               }
>> +               else
>>                {
>> +                       relativeUrl = absoluteUrl;
>> +               }
>> 
>> +               if (relativeUrl.length() > 0)
>> +               {
>>                        boolean removeLast = false;
>> -                       if (segments.endsWith("/"))
>> +                       if (relativeUrl.endsWith("/"))
>>                        {
>>                                // we need to append something and remove it after splitting
>>                                // because otherwise the
>>                                // trailing slashes will be lost
>> -                               segments += "/x";
>> +                               relativeUrl += "/x";
>>                                removeLast = true;
>>                        }
>> 
>> -                       String segmentArray[] = Strings.split(segments, '/');
>> +                       String segmentArray[] = Strings.split(relativeUrl, '/');
>> 
>>                        if (removeLast)
>>                        {
>> @@ -173,9 +217,9 @@ public final class Url implements Serial
>>                        }
>>                }
>> 
>> -               if (query.length() > 0)
>> +               if (queryString.length() > 0)
>>                {
>> -                       String queryArray[] = Strings.split(query, '&');
>> +                       String queryArray[] = Strings.split(queryString, '&');
>>                        for (String s : queryArray)
>>                        {
>>                                result.parameters.add(parseQueryParameter(s, charset));
>> 
>> Copied: wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java (from r1139453, wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java)
>> URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java?p2=wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java&p1=wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java&r1=1139453&r2=1139477&rev=1139477&view=diff
>> ==============================================================================
>> --- wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java (original)
>> +++ wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java Sat Jun 25 01:22:07 2011
>> @@ -16,25 +16,67 @@
>>  */
>>  package org.apache.wicket.request;
>> 
>> -import org.apache.wicket.util.crypt.CharEncoding;
>> +import java.util.Arrays;
>> +
>>  import org.junit.Assert;
>>  import org.junit.Test;
>> 
>>  /**
>> - * Tests for {@link UrlDecoder}
>> + * Tests for {@link org.apache.wicket.request.Url}
>>  */
>> -public class UrlEncoderTest
>> +public class UrlTest extends Assert
>>  {
>> +       @Test
>> +       public void parseRelativeUrl()
>> +       {
>> +               Url url = Url.parse("foo");
>> +               checkUrl(url, null, null, null, "foo");
>> +               assertFalse(url.isAbsolute());
>> +
>> +               url = Url.parse("foo/bar/baz");
>> +               checkUrl(url, null, null, null, "foo", "bar", "baz");
>> +               assertFalse(url.isAbsolute());
>> +
>> +               url = Url.parse("?foobar");
>> +               checkUrl(url, null, null, null);
>> +               assertEquals("", url.getQueryParameter("foobar").getValue());
>> +               assertFalse(url.isAbsolute());
>> +
>> +               url = Url.parse("foo?a=123");
>> +               checkUrl(url, null, null, null, "foo");
>> +               assertEquals("123", url.getQueryParameter("a").getValue());
>> +               assertFalse(url.isAbsolute());
>> +
>> +               url = Url.parse("/foo");
>> +               checkUrl(url, null, null, null, "", "foo");
>> +               assertTrue(url.isAbsolute());
>> +       }
>> 
>> -       /**
>> -        * <a href="https://issues.apache.org/jira/browse/WICKET-3721">WICKET-3721</a> Encode
>> -        * apostrophes because otherwise they get XML encoded by ComponentTag#writeOutput() to
>> -        * &amp;#039; and eventually break links with javascript:
>> -        */
>>        @Test
>> -       public void encodeApostrophe()
>> +       public void parseAbsoluteUrl()
>> +       {
>> +               Url url = Url.parse("ftp://myhost:8081");
>> +               checkUrl(url, "ftp", "myhost", 8081);
>> +               assertFalse(url.isAbsolute());
>> +
>> +               url = Url.parse("gopher://myhost:8081/foo");
>> +               checkUrl(url, "gopher", "myhost", 8081, "", "foo");
>> +               assertTrue(url.isAbsolute());
>> +
>> +               url = Url.parse("https://myhost/foo");
>> +               checkUrl(url, "https", "myhost", null, "", "foo");
>> +               assertTrue(url.isAbsolute());
>> +
>> +               url = Url.parse("https://myhost/foo:123");
>> +               checkUrl(url, "https", "myhost", null, "", "foo:123");
>> +               assertTrue(url.isAbsolute());
>> +       }
>> +
>> +       private void checkUrl(Url url, String protocol, String host, Integer port, String... segments)
>>        {
>> -               Assert.assertEquals("someone%27s%20bad%20url",
>> -                       UrlEncoder.FULL_PATH_INSTANCE.encode("someone's bad url", CharEncoding.UTF_8));
>> +               assertNotNull(url);
>> +               assertEquals(host, url.getHost());
>> +               assertEquals(port, url.getPort());
>> +               assertEquals(Arrays.asList(segments), url.getSegments());
>>        }
>>  }
>> 
>> 
>> 
> 
> 
> 
> -- 
> Martin Grigorov
> jWeekend
> Training, Consulting, Development
> http://jWeekend.com


Re: svn commit: r1139477 - in /wicket/trunk/wicket-request/src: main/java/org/apache/wicket/request/Url.java test/java/org/apache/wicket/request/UrlTest.java

Posted by Martin Grigorov <mg...@apache.org>.
Why the hell the test for URLDecoder has been killed and replaced with
something else ?!?
We have tests for Url at org.apache.wicket.request.UrlTest in wicket-core.
I agree that it belongs to wicket-request and to be able to move it
there it should get rid of WicketObjects usage but this kind of
killing other tests is totally wrong!

On Sat, Jun 25, 2011 at 4:22 AM,  <pe...@apache.org> wrote:
> Author: pete
> Date: Sat Jun 25 01:22:07 2011
> New Revision: 1139477
>
> URL: http://svn.apache.org/viewvc?rev=1139477&view=rev
> Log:
> WICKET-3834 WicketTester does not follow absolute redirects: make Url.parse(..) capable of parsing absolute urls
>
> Added:
>    wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java
>      - copied, changed from r1139453, wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java
> Modified:
>    wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java
>
> Modified: wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java
> URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java?rev=1139477&r1=1139476&r2=1139477&view=diff
> ==============================================================================
> --- wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java (original)
> +++ wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java Sat Jun 25 01:22:07 2011
> @@ -116,10 +116,11 @@ public final class Url implements Serial
>         * Parses the given URL string.
>         *
>         * @param url
> +        *           full absolute or relative url with query string
>         * @param charset
>         * @return Url object
>         */
> -       public static Url parse(final String url, Charset charset)
> +       public static Url parse(String url, Charset charset)
>        {
>                Args.notNull(url, "url");
>
> @@ -128,36 +129,79 @@ public final class Url implements Serial
>                // the url object resolved the charset, use that
>                charset = result.getCharset();
>
> -               String segments;
> -               String query;
> +               // extract query string part
> +               final String queryString;
> +               final String absoluteUrl;
>
> -               int qIndex = url.indexOf('?');
> +               int queryAt = url.indexOf('?');
>
> -               if (qIndex == -1)
> +               if (queryAt == -1)
>                {
> -                       segments = url;
> -                       query = "";
> +                       queryString = "";
> +                       absoluteUrl = url;
>                }
>                else
>                {
> -                       segments = url.substring(0, qIndex);
> -                       query = url.substring(qIndex + 1);
> +                       absoluteUrl = url.substring(0, queryAt);
> +                       queryString = url.substring(queryAt + 1);
>                }
> +
> +               // get absolute / relative part of url
> +               String relativeUrl;
>
> -               if (segments.length() > 0)
> +               // absolute urls contain a scheme://
> +               final int protocolAt = absoluteUrl.indexOf("://");
> +
> +               if (protocolAt != -1)
> +               {
> +                       result.protocol = absoluteUrl.substring(0, protocolAt);
> +                       final String afterProto = absoluteUrl.substring(protocolAt + 3);
> +                       final String hostAndPort;
> +
> +                       int relativeAt = afterProto.indexOf('/');
> +
> +                       if (relativeAt == -1)
> +                       {
> +                               relativeUrl = "";
> +                               hostAndPort = afterProto;
> +                       }
> +                       else
> +                       {
> +                               relativeUrl = afterProto.substring(relativeAt);
> +                               hostAndPort = afterProto.substring(0, relativeAt);
> +                       }
> +
> +                       int portAt = hostAndPort.indexOf(':');
> +
> +                       if (portAt == -1)
> +                       {
> +                               result.host = hostAndPort;
> +                               result.port = null;
> +                       }
> +                       else
> +                       {
> +                               result.host = hostAndPort.substring(0, portAt);
> +                               result.port = Integer.parseInt(hostAndPort.substring(portAt + 1));
> +                       }
> +               }
> +               else
>                {
> +                       relativeUrl = absoluteUrl;
> +               }
>
> +               if (relativeUrl.length() > 0)
> +               {
>                        boolean removeLast = false;
> -                       if (segments.endsWith("/"))
> +                       if (relativeUrl.endsWith("/"))
>                        {
>                                // we need to append something and remove it after splitting
>                                // because otherwise the
>                                // trailing slashes will be lost
> -                               segments += "/x";
> +                               relativeUrl += "/x";
>                                removeLast = true;
>                        }
>
> -                       String segmentArray[] = Strings.split(segments, '/');
> +                       String segmentArray[] = Strings.split(relativeUrl, '/');
>
>                        if (removeLast)
>                        {
> @@ -173,9 +217,9 @@ public final class Url implements Serial
>                        }
>                }
>
> -               if (query.length() > 0)
> +               if (queryString.length() > 0)
>                {
> -                       String queryArray[] = Strings.split(query, '&');
> +                       String queryArray[] = Strings.split(queryString, '&');
>                        for (String s : queryArray)
>                        {
>                                result.parameters.add(parseQueryParameter(s, charset));
>
> Copied: wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java (from r1139453, wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java)
> URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java?p2=wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java&p1=wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java&r1=1139453&r2=1139477&rev=1139477&view=diff
> ==============================================================================
> --- wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java (original)
> +++ wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java Sat Jun 25 01:22:07 2011
> @@ -16,25 +16,67 @@
>  */
>  package org.apache.wicket.request;
>
> -import org.apache.wicket.util.crypt.CharEncoding;
> +import java.util.Arrays;
> +
>  import org.junit.Assert;
>  import org.junit.Test;
>
>  /**
> - * Tests for {@link UrlDecoder}
> + * Tests for {@link org.apache.wicket.request.Url}
>  */
> -public class UrlEncoderTest
> +public class UrlTest extends Assert
>  {
> +       @Test
> +       public void parseRelativeUrl()
> +       {
> +               Url url = Url.parse("foo");
> +               checkUrl(url, null, null, null, "foo");
> +               assertFalse(url.isAbsolute());
> +
> +               url = Url.parse("foo/bar/baz");
> +               checkUrl(url, null, null, null, "foo", "bar", "baz");
> +               assertFalse(url.isAbsolute());
> +
> +               url = Url.parse("?foobar");
> +               checkUrl(url, null, null, null);
> +               assertEquals("", url.getQueryParameter("foobar").getValue());
> +               assertFalse(url.isAbsolute());
> +
> +               url = Url.parse("foo?a=123");
> +               checkUrl(url, null, null, null, "foo");
> +               assertEquals("123", url.getQueryParameter("a").getValue());
> +               assertFalse(url.isAbsolute());
> +
> +               url = Url.parse("/foo");
> +               checkUrl(url, null, null, null, "", "foo");
> +               assertTrue(url.isAbsolute());
> +       }
>
> -       /**
> -        * <a href="https://issues.apache.org/jira/browse/WICKET-3721">WICKET-3721</a> Encode
> -        * apostrophes because otherwise they get XML encoded by ComponentTag#writeOutput() to
> -        * &amp;#039; and eventually break links with javascript:
> -        */
>        @Test
> -       public void encodeApostrophe()
> +       public void parseAbsoluteUrl()
> +       {
> +               Url url = Url.parse("ftp://myhost:8081");
> +               checkUrl(url, "ftp", "myhost", 8081);
> +               assertFalse(url.isAbsolute());
> +
> +               url = Url.parse("gopher://myhost:8081/foo");
> +               checkUrl(url, "gopher", "myhost", 8081, "", "foo");
> +               assertTrue(url.isAbsolute());
> +
> +               url = Url.parse("https://myhost/foo");
> +               checkUrl(url, "https", "myhost", null, "", "foo");
> +               assertTrue(url.isAbsolute());
> +
> +               url = Url.parse("https://myhost/foo:123");
> +               checkUrl(url, "https", "myhost", null, "", "foo:123");
> +               assertTrue(url.isAbsolute());
> +       }
> +
> +       private void checkUrl(Url url, String protocol, String host, Integer port, String... segments)
>        {
> -               Assert.assertEquals("someone%27s%20bad%20url",
> -                       UrlEncoder.FULL_PATH_INSTANCE.encode("someone's bad url", CharEncoding.UTF_8));
> +               assertNotNull(url);
> +               assertEquals(host, url.getHost());
> +               assertEquals(port, url.getPort());
> +               assertEquals(Arrays.asList(segments), url.getSegments());
>        }
>  }
>
>
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com