fixed URI construction to consider fragment as well (SPR-7083)
This commit is contained in:
parent
4d2a398cbc
commit
03120b70d0
|
|
@ -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");
|
* 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.
|
||||||
|
|
@ -67,7 +67,6 @@ public class URIEditor extends PropertyEditorSupport {
|
||||||
/**
|
/**
|
||||||
* Create a new URIEditor, converting "classpath:" locations into
|
* Create a new URIEditor, converting "classpath:" locations into
|
||||||
* standard URIs (not trying to resolve them into physical resources).
|
* standard URIs (not trying to resolve them into physical resources).
|
||||||
*
|
|
||||||
* @param encode indicates whether Strings will be encoded or not
|
* @param encode indicates whether Strings will be encoded or not
|
||||||
*/
|
*/
|
||||||
public URIEditor(boolean encode) {
|
public URIEditor(boolean encode) {
|
||||||
|
|
@ -141,14 +140,16 @@ public class URIEditor extends PropertyEditorSupport {
|
||||||
* @throws java.net.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 {
|
||||||
int idx = value.indexOf(':');
|
int colonIndex = value.indexOf(':');
|
||||||
if (encode && idx != -1) {
|
if (this.encode && colonIndex != -1) {
|
||||||
String scheme = value.substring(0, idx);
|
int fragmentIndex = value.indexOf('#', colonIndex + 1);
|
||||||
String ssp = value.substring(idx + 1);
|
String scheme = value.substring(0, colonIndex);
|
||||||
return new URI(scheme, ssp, null);
|
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 {
|
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);
|
return new URI(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* 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.
|
||||||
|
|
@ -30,24 +30,33 @@ import org.springframework.util.ClassUtils;
|
||||||
*/
|
*/
|
||||||
public class URIEditorTests {
|
public class URIEditorTests {
|
||||||
|
|
||||||
@Test
|
private void doTestURI(String uriSpec) {
|
||||||
public void standardURI() throws Exception {
|
|
||||||
PropertyEditor uriEditor = new URIEditor();
|
PropertyEditor uriEditor = new URIEditor();
|
||||||
uriEditor.setAsText("mailto:juergen.hoeller@interface21.com");
|
uriEditor.setAsText(uriSpec);
|
||||||
Object value = uriEditor.getValue();
|
Object value = uriEditor.getValue();
|
||||||
assertTrue(value instanceof URI);
|
assertTrue(value instanceof URI);
|
||||||
URI uri = (URI) value;
|
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
|
@Test
|
||||||
public void standardURL() throws Exception {
|
public void standardURL() throws Exception {
|
||||||
PropertyEditor uriEditor = new URIEditor();
|
doTestURI("http://www.springframework.org");
|
||||||
uriEditor.setAsText("http://www.springframework.org");
|
}
|
||||||
Object value = uriEditor.getValue();
|
|
||||||
assertTrue(value instanceof URI);
|
@Test
|
||||||
URI uri = (URI) value;
|
public void standardURLWithFragment() throws Exception {
|
||||||
assertEquals(uri.toString(), uriEditor.getAsText());
|
doTestURI("http://www.springframework.org#1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -57,7 +66,7 @@ public class URIEditorTests {
|
||||||
Object value = uriEditor.getValue();
|
Object value = uriEditor.getValue();
|
||||||
assertTrue(value instanceof URI);
|
assertTrue(value instanceof URI);
|
||||||
URI uri = (URI) value;
|
URI uri = (URI) value;
|
||||||
assertEquals(uri.toString(), uriEditor.getAsText());
|
assertEquals("http://www.springframework.org", uri.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -95,16 +104,6 @@ public class URIEditorTests {
|
||||||
assertTrue(uri.getScheme().startsWith("classpath"));
|
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
|
@Test
|
||||||
public void setAsTextWithNull() throws Exception {
|
public void setAsTextWithNull() throws Exception {
|
||||||
PropertyEditor uriEditor = new URIEditor();
|
PropertyEditor uriEditor = new URIEditor();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue