Skip to content

Commit 03120b7

Browse files
committed
fixed URI construction to consider fragment as well (SPR-7083)
1 parent 4d2a398 commit 03120b7

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/URIEditor.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,7 +67,6 @@ public URIEditor() {
6767
/**
6868
* Create a new URIEditor, converting "classpath:" locations into
6969
* standard URIs (not trying to resolve them into physical resources).
70-
*
7170
* @param encode indicates whether Strings will be encoded or not
7271
*/
7372
public URIEditor(boolean encode) {
@@ -141,14 +140,16 @@ public void setAsText(String text) throws IllegalArgumentException {
141140
* @throws java.net.URISyntaxException if URI conversion failed
142141
*/
143142
protected URI createURI(String value) throws URISyntaxException {
144-
int idx = value.indexOf(':');
145-
if (encode && idx != -1) {
146-
String scheme = value.substring(0, idx);
147-
String ssp = value.substring(idx + 1);
148-
return new URI(scheme, ssp, null);
143+
int colonIndex = value.indexOf(':');
144+
if (this.encode && colonIndex != -1) {
145+
int fragmentIndex = value.indexOf('#', colonIndex + 1);
146+
String scheme = value.substring(0, colonIndex);
147+
String ssp = value.substring(colonIndex + 1, (fragmentIndex > 0 ? fragmentIndex : value.length()));
148+
String fragment = (fragmentIndex > 0 ? value.substring(fragmentIndex + 1) : null);
149+
return new URI(scheme, ssp, fragment);
149150
}
150151
else {
151-
// not encoding or the value contains no scheme , fallback to default
152+
// not encoding or the value contains no scheme - fallback to default
152153
return new URI(value);
153154
}
154155
}

org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/URIEditorTests.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,24 +30,33 @@
3030
*/
3131
public class URIEditorTests {
3232

33-
@Test
34-
public void standardURI() throws Exception {
33+
private void doTestURI(String uriSpec) {
3534
PropertyEditor uriEditor = new URIEditor();
36-
uriEditor.setAsText("mailto:[email protected]");
35+
uriEditor.setAsText(uriSpec);
3736
Object value = uriEditor.getValue();
3837
assertTrue(value instanceof URI);
3938
URI uri = (URI) value;
40-
assertEquals(uri.toString(), uriEditor.getAsText());
39+
assertEquals(uriSpec, uri.toString());
40+
}
41+
42+
@Test
43+
public void standardURI() throws Exception {
44+
doTestURI("mailto:[email protected]");
45+
}
46+
47+
@Test
48+
public void withNonExistentResource() throws Exception {
49+
doTestURI("gonna:/freak/in/the/morning/freak/in/the.evening");
4150
}
4251

4352
@Test
4453
public void standardURL() throws Exception {
45-
PropertyEditor uriEditor = new URIEditor();
46-
uriEditor.setAsText("http://www.springframework.org");
47-
Object value = uriEditor.getValue();
48-
assertTrue(value instanceof URI);
49-
URI uri = (URI) value;
50-
assertEquals(uri.toString(), uriEditor.getAsText());
54+
doTestURI("http://www.springframework.org");
55+
}
56+
57+
@Test
58+
public void standardURLWithFragment() throws Exception {
59+
doTestURI("http://www.springframework.org#1");
5160
}
5261

5362
@Test
@@ -57,7 +66,7 @@ public void standardURLWithWhitespace() throws Exception {
5766
Object value = uriEditor.getValue();
5867
assertTrue(value instanceof URI);
5968
URI uri = (URI) value;
60-
assertEquals(uri.toString(), uriEditor.getAsText());
69+
assertEquals("http://www.springframework.org", uri.toString());
6170
}
6271

6372
@Test
@@ -95,16 +104,6 @@ public void classpathURLAsIs() throws Exception {
95104
assertTrue(uri.getScheme().startsWith("classpath"));
96105
}
97106

98-
@Test
99-
public void withNonExistentResource() throws Exception {
100-
PropertyEditor uriEditor = new URIEditor();
101-
uriEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening");
102-
Object value = uriEditor.getValue();
103-
assertTrue(value instanceof URI);
104-
URI uri = (URI) value;
105-
assertEquals(uri.toString(), uriEditor.getAsText());
106-
}
107-
108107
@Test
109108
public void setAsTextWithNull() throws Exception {
110109
PropertyEditor uriEditor = new URIEditor();

0 commit comments

Comments
 (0)