Fixed URI encoding in URIEditor to be RFC 2396 compliant
This commit is contained in:
parent
eb47a4b5be
commit
2fde8ef4d9
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2007 the original author or authors.
|
* Copyright 2002-2009 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -105,10 +105,19 @@ public class URIEditor extends PropertyEditorSupport {
|
||||||
* constructor, replacing spaces with "%20" quotes first.
|
* constructor, replacing spaces with "%20" quotes first.
|
||||||
* @param value the value to convert into a URI instance
|
* @param value the value to convert into a URI instance
|
||||||
* @return the URI instance
|
* @return the URI instance
|
||||||
* @throws URISyntaxException if URI conversion failed
|
* @throws java.net.URISyntaxException if URI conversion failed
|
||||||
*/
|
*/
|
||||||
protected URI createURI(String value) throws URISyntaxException {
|
protected URI createURI(String value) throws URISyntaxException {
|
||||||
return new URI(StringUtils.replace(value, " ", "%20"));
|
int idx = value.indexOf(':');
|
||||||
|
if (idx != -1) {
|
||||||
|
String scheme = value.substring(0, idx);
|
||||||
|
String ssp = value.substring(idx + 1);
|
||||||
|
return new URI(scheme, ssp, null);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// value contains no scheme, fallback to default
|
||||||
|
return new URI(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2007 the original author or authors.
|
* Copyright 2002-2009 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -19,16 +19,19 @@ package org.springframework.beans.propertyeditors;
|
||||||
import java.beans.PropertyEditor;
|
import java.beans.PropertyEditor;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Arjen Poutsma
|
||||||
*/
|
*/
|
||||||
public class URIEditorTests extends TestCase {
|
public class URIEditorTests {
|
||||||
|
|
||||||
public void testStandardURI() throws Exception {
|
@Test
|
||||||
|
public void standardURI() throws Exception {
|
||||||
PropertyEditor uriEditor = new URIEditor();
|
PropertyEditor uriEditor = new URIEditor();
|
||||||
uriEditor.setAsText("mailto:juergen.hoeller@interface21.com");
|
uriEditor.setAsText("mailto:juergen.hoeller@interface21.com");
|
||||||
Object value = uriEditor.getValue();
|
Object value = uriEditor.getValue();
|
||||||
|
|
@ -37,7 +40,8 @@ public class URIEditorTests extends TestCase {
|
||||||
assertEquals(uri.toString(), uriEditor.getAsText());
|
assertEquals(uri.toString(), uriEditor.getAsText());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testStandardURL() throws Exception {
|
@Test
|
||||||
|
public void standardURL() throws Exception {
|
||||||
PropertyEditor uriEditor = new URIEditor();
|
PropertyEditor uriEditor = new URIEditor();
|
||||||
uriEditor.setAsText("http://www.springframework.org");
|
uriEditor.setAsText("http://www.springframework.org");
|
||||||
Object value = uriEditor.getValue();
|
Object value = uriEditor.getValue();
|
||||||
|
|
@ -46,7 +50,8 @@ public class URIEditorTests extends TestCase {
|
||||||
assertEquals(uri.toString(), uriEditor.getAsText());
|
assertEquals(uri.toString(), uriEditor.getAsText());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testStandardURLWithWhitespace() throws Exception {
|
@Test
|
||||||
|
public void standardURLWithWhitespace() throws Exception {
|
||||||
PropertyEditor uriEditor = new URIEditor();
|
PropertyEditor uriEditor = new URIEditor();
|
||||||
uriEditor.setAsText(" http://www.springframework.org ");
|
uriEditor.setAsText(" http://www.springframework.org ");
|
||||||
Object value = uriEditor.getValue();
|
Object value = uriEditor.getValue();
|
||||||
|
|
@ -55,7 +60,8 @@ public class URIEditorTests extends TestCase {
|
||||||
assertEquals(uri.toString(), uriEditor.getAsText());
|
assertEquals(uri.toString(), uriEditor.getAsText());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testClasspathURL() throws Exception {
|
@Test
|
||||||
|
public void classpathURL() throws Exception {
|
||||||
PropertyEditor uriEditor = new URIEditor(getClass().getClassLoader());
|
PropertyEditor uriEditor = new URIEditor(getClass().getClassLoader());
|
||||||
uriEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
|
uriEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
|
||||||
"/" + ClassUtils.getShortName(getClass()) + ".class");
|
"/" + ClassUtils.getShortName(getClass()) + ".class");
|
||||||
|
|
@ -66,7 +72,8 @@ public class URIEditorTests extends TestCase {
|
||||||
assertTrue(!uri.getScheme().startsWith("classpath"));
|
assertTrue(!uri.getScheme().startsWith("classpath"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testClasspathURLWithWhitespace() throws Exception {
|
@Test
|
||||||
|
public void classpathURLWithWhitespace() throws Exception {
|
||||||
PropertyEditor uriEditor = new URIEditor(getClass().getClassLoader());
|
PropertyEditor uriEditor = new URIEditor(getClass().getClassLoader());
|
||||||
uriEditor.setAsText(" classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
|
uriEditor.setAsText(" classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
|
||||||
"/" + ClassUtils.getShortName(getClass()) + ".class ");
|
"/" + ClassUtils.getShortName(getClass()) + ".class ");
|
||||||
|
|
@ -77,7 +84,8 @@ public class URIEditorTests extends TestCase {
|
||||||
assertTrue(!uri.getScheme().startsWith("classpath"));
|
assertTrue(!uri.getScheme().startsWith("classpath"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testClasspathURLAsIs() throws Exception {
|
@Test
|
||||||
|
public void classpathURLAsIs() throws Exception {
|
||||||
PropertyEditor uriEditor = new URIEditor();
|
PropertyEditor uriEditor = new URIEditor();
|
||||||
uriEditor.setAsText("classpath:test.txt");
|
uriEditor.setAsText("classpath:test.txt");
|
||||||
Object value = uriEditor.getValue();
|
Object value = uriEditor.getValue();
|
||||||
|
|
@ -87,7 +95,8 @@ public class URIEditorTests extends TestCase {
|
||||||
assertTrue(uri.getScheme().startsWith("classpath"));
|
assertTrue(uri.getScheme().startsWith("classpath"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testWithNonExistentResource() throws Exception {
|
@Test
|
||||||
|
public void withNonExistentResource() throws Exception {
|
||||||
PropertyEditor uriEditor = new URIEditor();
|
PropertyEditor uriEditor = new URIEditor();
|
||||||
uriEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening");
|
uriEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening");
|
||||||
Object value = uriEditor.getValue();
|
Object value = uriEditor.getValue();
|
||||||
|
|
@ -96,16 +105,29 @@ public class URIEditorTests extends TestCase {
|
||||||
assertEquals(uri.toString(), uriEditor.getAsText());
|
assertEquals(uri.toString(), uriEditor.getAsText());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetAsTextWithNull() throws Exception {
|
@Test
|
||||||
|
public void setAsTextWithNull() throws Exception {
|
||||||
PropertyEditor uriEditor = new URIEditor();
|
PropertyEditor uriEditor = new URIEditor();
|
||||||
uriEditor.setAsText(null);
|
uriEditor.setAsText(null);
|
||||||
assertNull(uriEditor.getValue());
|
assertNull(uriEditor.getValue());
|
||||||
assertEquals("", uriEditor.getAsText());
|
assertEquals("", uriEditor.getAsText());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetAsTextReturnsEmptyStringIfValueNotSet() throws Exception {
|
@Test
|
||||||
|
public void getAsTextReturnsEmptyStringIfValueNotSet() throws Exception {
|
||||||
PropertyEditor uriEditor = new URIEditor();
|
PropertyEditor uriEditor = new URIEditor();
|
||||||
assertEquals("", uriEditor.getAsText());
|
assertEquals("", uriEditor.getAsText());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void encodeURI() throws Exception {
|
||||||
|
PropertyEditor uriEditor = new URIEditor();
|
||||||
|
uriEditor.setAsText("http://example.com/spaces and \u20AC");
|
||||||
|
Object value = uriEditor.getValue();
|
||||||
|
assertTrue(value instanceof URI);
|
||||||
|
URI uri = (URI) value;
|
||||||
|
assertEquals(uri.toString(), uriEditor.getAsText());
|
||||||
|
assertEquals("http://example.com/spaces%20and%20%E2%82%AC", uri.toASCIIString());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue