fixed URI construction to consider fragment as well (SPR-7083)

This commit is contained in:
Juergen Hoeller 2010-04-09 15:26:43 +00:00
parent 4d2a398cbc
commit 03120b70d0
2 changed files with 30 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -67,7 +67,6 @@ public class URIEditor extends PropertyEditorSupport {
/**
* Create a new URIEditor, converting "classpath:" locations into
* standard URIs (not trying to resolve them into physical resources).
*
* @param encode indicates whether Strings will be encoded or not
*/
public URIEditor(boolean encode) {
@ -141,14 +140,16 @@ public class URIEditor extends PropertyEditorSupport {
* @throws java.net.URISyntaxException if URI conversion failed
*/
protected URI createURI(String value) throws URISyntaxException {
int idx = value.indexOf(':');
if (encode && idx != -1) {
String scheme = value.substring(0, idx);
String ssp = value.substring(idx + 1);
return new URI(scheme, ssp, null);
int colonIndex = value.indexOf(':');
if (this.encode && colonIndex != -1) {
int fragmentIndex = value.indexOf('#', colonIndex + 1);
String scheme = value.substring(0, colonIndex);
String ssp = value.substring(colonIndex + 1, (fragmentIndex > 0 ? fragmentIndex : value.length()));
String fragment = (fragmentIndex > 0 ? value.substring(fragmentIndex + 1) : null);
return new URI(scheme, ssp, fragment);
}
else {
// not encoding or the value contains no scheme , fallback to default
// not encoding or the value contains no scheme - fallback to default
return new URI(value);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,24 +30,33 @@ import org.springframework.util.ClassUtils;
*/
public class URIEditorTests {
@Test
public void standardURI() throws Exception {
private void doTestURI(String uriSpec) {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText("mailto:juergen.hoeller@interface21.com");
uriEditor.setAsText(uriSpec);
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uri.toString(), uriEditor.getAsText());
assertEquals(uriSpec, uri.toString());
}
@Test
public void standardURI() throws Exception {
doTestURI("mailto:juergen.hoeller@interface21.com");
}
@Test
public void withNonExistentResource() throws Exception {
doTestURI("gonna:/freak/in/the/morning/freak/in/the.evening");
}
@Test
public void standardURL() throws Exception {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText("http://www.springframework.org");
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uri.toString(), uriEditor.getAsText());
doTestURI("http://www.springframework.org");
}
@Test
public void standardURLWithFragment() throws Exception {
doTestURI("http://www.springframework.org#1");
}
@Test
@ -57,7 +66,7 @@ public class URIEditorTests {
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uri.toString(), uriEditor.getAsText());
assertEquals("http://www.springframework.org", uri.toString());
}
@Test
@ -95,16 +104,6 @@ public class URIEditorTests {
assertTrue(uri.getScheme().startsWith("classpath"));
}
@Test
public void withNonExistentResource() throws Exception {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening");
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uri.toString(), uriEditor.getAsText());
}
@Test
public void setAsTextWithNull() throws Exception {
PropertyEditor uriEditor = new URIEditor();