diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/URIEditor.java b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/URIEditor.java index 01f3849ff29..3dd98379981 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/URIEditor.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/URIEditor.java @@ -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); } } diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/URIEditorTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/URIEditorTests.java index b0437fa03b0..426ceac13cb 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/URIEditorTests.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/URIEditorTests.java @@ -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();