Added PathEditor for NIO file system resolution
Issue: SPR-14436
This commit is contained in:
		
							parent
							
								
									40b2d26bd4
								
							
						
					
					
						commit
						23c2b6ad41
					
				| 
						 | 
				
			
			@ -25,6 +25,7 @@ import java.math.BigInteger;
 | 
			
		|||
import java.net.URI;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
import java.nio.charset.Charset;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.Currency;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
| 
						 | 
				
			
			@ -59,6 +60,7 @@ import org.springframework.beans.propertyeditors.FileEditor;
 | 
			
		|||
import org.springframework.beans.propertyeditors.InputSourceEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.InputStreamEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.LocaleEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.PathEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.PatternEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.PropertiesEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.ReaderEditor;
 | 
			
		||||
| 
						 | 
				
			
			@ -211,6 +213,7 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
 | 
			
		|||
		this.defaultEditors.put(InputStream.class, new InputStreamEditor());
 | 
			
		||||
		this.defaultEditors.put(InputSource.class, new InputSourceEditor());
 | 
			
		||||
		this.defaultEditors.put(Locale.class, new LocaleEditor());
 | 
			
		||||
		this.defaultEditors.put(Path.class, new PathEditor());
 | 
			
		||||
		this.defaultEditors.put(Pattern.class, new PatternEditor());
 | 
			
		||||
		this.defaultEditors.put(Properties.class, new PropertiesEditor());
 | 
			
		||||
		this.defaultEditors.put(Reader.class, new ReaderEditor());
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2012 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -59,16 +59,14 @@ public class FileEditor extends PropertyEditorSupport {
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new FileEditor,
 | 
			
		||||
	 * using the default ResourceEditor underneath.
 | 
			
		||||
	 * Create a new FileEditor, using a default ResourceEditor underneath.
 | 
			
		||||
	 */
 | 
			
		||||
	public FileEditor() {
 | 
			
		||||
		this.resourceEditor = new ResourceEditor();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new FileEditor,
 | 
			
		||||
	 * using the given ResourceEditor underneath.
 | 
			
		||||
	 * Create a new FileEditor, using the given ResourceEditor underneath.
 | 
			
		||||
	 * @param resourceEditor the ResourceEditor to use
 | 
			
		||||
	 */
 | 
			
		||||
	public FileEditor(ResourceEditor resourceEditor) {
 | 
			
		||||
| 
						 | 
				
			
			@ -105,7 +103,7 @@ public class FileEditor extends PropertyEditorSupport {
 | 
			
		|||
			}
 | 
			
		||||
			catch (IOException ex) {
 | 
			
		||||
				throw new IllegalArgumentException(
 | 
			
		||||
						"Could not retrieve File for " + resource + ": " + ex.getMessage());
 | 
			
		||||
						"Could not retrieve file for " + resource + ": " + ex.getMessage());
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2015 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -47,16 +47,14 @@ public class InputStreamEditor extends PropertyEditorSupport {
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new InputStreamEditor,
 | 
			
		||||
	 * using the default ResourceEditor underneath.
 | 
			
		||||
	 * Create a new InputStreamEditor, using the default ResourceEditor underneath.
 | 
			
		||||
	 */
 | 
			
		||||
	public InputStreamEditor() {
 | 
			
		||||
		this.resourceEditor = new ResourceEditor();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new InputStreamEditor,
 | 
			
		||||
	 * using the given ResourceEditor underneath.
 | 
			
		||||
	 * Create a new InputStreamEditor, using the given ResourceEditor underneath.
 | 
			
		||||
	 * @param resourceEditor the ResourceEditor to use
 | 
			
		||||
	 */
 | 
			
		||||
	public InputStreamEditor(ResourceEditor resourceEditor) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,114 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *      http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package org.springframework.beans.propertyeditors;
 | 
			
		||||
 | 
			
		||||
import java.beans.PropertyEditorSupport;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
import java.net.URISyntaxException;
 | 
			
		||||
import java.nio.file.FileSystemNotFoundException;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
import java.nio.file.Paths;
 | 
			
		||||
 | 
			
		||||
import org.springframework.core.io.Resource;
 | 
			
		||||
import org.springframework.core.io.ResourceEditor;
 | 
			
		||||
import org.springframework.core.io.ResourceLoader;
 | 
			
		||||
import org.springframework.util.Assert;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Editor for {@code java.nio.file.Path}, to directly populate a Path
 | 
			
		||||
 * property instead of using a String property as bridge.
 | 
			
		||||
 *
 | 
			
		||||
 * <p>Based on {@link Paths#get(URI)}'s resolution algorithm, checking
 | 
			
		||||
 * registered NIO file system providers, including the default file system
 | 
			
		||||
 * for "file:..." paths. Also supports Spring-style URL notation: any fully
 | 
			
		||||
 * qualified standard URL and Spring's special "classpath:" pseudo-URL,
 | 
			
		||||
 * as well as Spring's context-specific relative file paths.
 | 
			
		||||
 *
 | 
			
		||||
 * <p>Note that, in contrast to {@link FileEditor}, relative paths are only
 | 
			
		||||
 * supported by Spring's resource abstraction here. Direct {@code Paths.get}
 | 
			
		||||
 * resolution in a file system always has to go through the corresponding
 | 
			
		||||
 * file system provider's scheme, i.e. "file" for the default file system.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Juergen Hoeller
 | 
			
		||||
 * @since 4.3.2
 | 
			
		||||
 * @see java.nio.file.Path
 | 
			
		||||
 * @see Paths#get(URI)
 | 
			
		||||
 * @see ResourceEditor
 | 
			
		||||
 * @see org.springframework.core.io.ResourceLoader
 | 
			
		||||
 * @see FileEditor
 | 
			
		||||
 * @see URLEditor
 | 
			
		||||
 */
 | 
			
		||||
public class PathEditor extends PropertyEditorSupport {
 | 
			
		||||
 | 
			
		||||
	private final ResourceEditor resourceEditor;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new PathEditor, using the default ResourceEditor underneath.
 | 
			
		||||
	 */
 | 
			
		||||
	public PathEditor() {
 | 
			
		||||
		this.resourceEditor = new ResourceEditor();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new PathEditor, using the given ResourceEditor underneath.
 | 
			
		||||
	 * @param resourceEditor the ResourceEditor to use
 | 
			
		||||
	 */
 | 
			
		||||
	public PathEditor(ResourceEditor resourceEditor) {
 | 
			
		||||
		Assert.notNull(resourceEditor, "ResourceEditor must not be null");
 | 
			
		||||
		this.resourceEditor = resourceEditor;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void setAsText(String text) throws IllegalArgumentException {
 | 
			
		||||
		if (!text.startsWith("/") && !text.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)) {
 | 
			
		||||
			try {
 | 
			
		||||
				URI uri = new URI(text);
 | 
			
		||||
				if (uri.getScheme() != null) {
 | 
			
		||||
					// Let's try NIO file system providers via Paths.get(URI)
 | 
			
		||||
					setValue(Paths.get(uri).normalize());
 | 
			
		||||
					return;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			catch (URISyntaxException ex) {
 | 
			
		||||
				// Not a valid URI: Let's try as Spring resource location.
 | 
			
		||||
			}
 | 
			
		||||
			catch (FileSystemNotFoundException ex) {
 | 
			
		||||
				// URI scheme not registered for NIO:
 | 
			
		||||
				// Let's try URL protocol handlers via Spring's resource mechanism.
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		this.resourceEditor.setAsText(text);
 | 
			
		||||
		Resource resource = (Resource) this.resourceEditor.getValue();
 | 
			
		||||
		try {
 | 
			
		||||
			setValue(resource != null ? resource.getFile().toPath() : null);
 | 
			
		||||
		}
 | 
			
		||||
		catch (IOException ex) {
 | 
			
		||||
			throw new IllegalArgumentException("Failed to retrieve file for " + resource, ex);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public String getAsText() {
 | 
			
		||||
		Path value = (Path) getValue();
 | 
			
		||||
		return (value != null ? value.toString() : "");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2015 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -47,16 +47,14 @@ public class ReaderEditor extends PropertyEditorSupport {
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new ReaderEditor,
 | 
			
		||||
	 * using the default ResourceEditor underneath.
 | 
			
		||||
	 * Create a new ReaderEditor, using the default ResourceEditor underneath.
 | 
			
		||||
	 */
 | 
			
		||||
	public ReaderEditor() {
 | 
			
		||||
		this.resourceEditor = new ResourceEditor();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new ReaderEditor,
 | 
			
		||||
	 * using the given ResourceEditor underneath.
 | 
			
		||||
	 * Create a new ReaderEditor, using the given ResourceEditor underneath.
 | 
			
		||||
	 * @param resourceEditor the ResourceEditor to use
 | 
			
		||||
	 */
 | 
			
		||||
	public ReaderEditor(ResourceEditor resourceEditor) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2012 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -60,8 +60,7 @@ public class URIEditor extends PropertyEditorSupport {
 | 
			
		|||
	 * standard URIs (not trying to resolve them into physical resources).
 | 
			
		||||
	 */
 | 
			
		||||
	public URIEditor() {
 | 
			
		||||
		this.classLoader = null;
 | 
			
		||||
		this.encode = true;
 | 
			
		||||
		this(true);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
| 
						 | 
				
			
			@ -74,7 +73,6 @@ public class URIEditor extends PropertyEditorSupport {
 | 
			
		|||
		this.encode = encode;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new URIEditor, using the given ClassLoader to resolve
 | 
			
		||||
	 * "classpath:" locations into physical resource URLs.
 | 
			
		||||
| 
						 | 
				
			
			@ -82,8 +80,7 @@ public class URIEditor extends PropertyEditorSupport {
 | 
			
		|||
	 * (may be {@code null} to indicate the default ClassLoader)
 | 
			
		||||
	 */
 | 
			
		||||
	public URIEditor(ClassLoader classLoader) {
 | 
			
		||||
		this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
 | 
			
		||||
		this.encode = true;
 | 
			
		||||
		this(classLoader, true);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2012 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -50,7 +50,7 @@ public class URLEditor extends PropertyEditorSupport {
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new URLEditor, using the default ResourceEditor underneath.
 | 
			
		||||
	 * Create a new URLEditor, using a default ResourceEditor underneath.
 | 
			
		||||
	 */
 | 
			
		||||
	public URLEditor() {
 | 
			
		||||
		this.resourceEditor = new ResourceEditor();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2015 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -22,6 +22,7 @@ import java.io.InputStream;
 | 
			
		|||
import java.io.Reader;
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
 | 
			
		||||
import org.xml.sax.InputSource;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -33,6 +34,7 @@ import org.springframework.beans.propertyeditors.ClassEditor;
 | 
			
		|||
import org.springframework.beans.propertyeditors.FileEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.InputSourceEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.InputStreamEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.PathEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.ReaderEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.URIEditor;
 | 
			
		||||
import org.springframework.beans.propertyeditors.URLEditor;
 | 
			
		||||
| 
						 | 
				
			
			@ -103,6 +105,7 @@ public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
 | 
			
		|||
		doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
 | 
			
		||||
		doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
 | 
			
		||||
		doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
 | 
			
		||||
		doRegisterEditor(registry, Path.class, new PathEditor(baseEditor));
 | 
			
		||||
		doRegisterEditor(registry, Reader.class, new ReaderEditor(baseEditor));
 | 
			
		||||
		doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2008 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -29,20 +29,20 @@ import static org.junit.Assert.*;
 | 
			
		|||
 * @author Thomas Risberg
 | 
			
		||||
 * @author Chris Beams
 | 
			
		||||
 */
 | 
			
		||||
public final class FileEditorTests {
 | 
			
		||||
public class FileEditorTests {
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void testClasspathFileName() throws Exception {
 | 
			
		||||
		PropertyEditor fileEditor = new FileEditor();
 | 
			
		||||
		fileEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/"
 | 
			
		||||
				+ ClassUtils.getShortName(getClass()) + ".class");
 | 
			
		||||
		fileEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
 | 
			
		||||
				ClassUtils.getShortName(getClass()) + ".class");
 | 
			
		||||
		Object value = fileEditor.getValue();
 | 
			
		||||
		assertTrue(value instanceof File);
 | 
			
		||||
		File file = (File) value;
 | 
			
		||||
		assertTrue(file.exists());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test(expected=IllegalArgumentException.class)
 | 
			
		||||
	@Test(expected = IllegalArgumentException.class)
 | 
			
		||||
	public void testWithNonExistentResource() throws Exception {
 | 
			
		||||
		PropertyEditor propertyEditor = new FileEditor();
 | 
			
		||||
		propertyEditor.setAsText("classpath:no_way_this_file_is_found.doc");
 | 
			
		||||
| 
						 | 
				
			
			@ -71,8 +71,8 @@ public final class FileEditorTests {
 | 
			
		|||
	@Test
 | 
			
		||||
	public void testUnqualifiedFileNameFound() throws Exception {
 | 
			
		||||
		PropertyEditor fileEditor = new FileEditor();
 | 
			
		||||
		String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass())
 | 
			
		||||
				+ ".class";
 | 
			
		||||
		String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
 | 
			
		||||
				ClassUtils.getShortName(getClass()) + ".class";
 | 
			
		||||
		fileEditor.setAsText(fileName);
 | 
			
		||||
		Object value = fileEditor.getValue();
 | 
			
		||||
		assertTrue(value instanceof File);
 | 
			
		||||
| 
						 | 
				
			
			@ -88,8 +88,8 @@ public final class FileEditorTests {
 | 
			
		|||
	@Test
 | 
			
		||||
	public void testUnqualifiedFileNameNotFound() throws Exception {
 | 
			
		||||
		PropertyEditor fileEditor = new FileEditor();
 | 
			
		||||
		String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass())
 | 
			
		||||
				+ ".clazz";
 | 
			
		||||
		String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
 | 
			
		||||
				ClassUtils.getShortName(getClass()) + ".clazz";
 | 
			
		||||
		fileEditor.setAsText(fileName);
 | 
			
		||||
		Object value = fileEditor.getValue();
 | 
			
		||||
		assertTrue(value instanceof File);
 | 
			
		||||
| 
						 | 
				
			
			@ -101,4 +101,5 @@ public final class FileEditorTests {
 | 
			
		|||
		}
 | 
			
		||||
		assertTrue(absolutePath.endsWith(fileName));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2015 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -60,9 +60,8 @@ public class InputStreamEditorTests {
 | 
			
		|||
 | 
			
		||||
	@Test(expected = IllegalArgumentException.class)
 | 
			
		||||
	public void testWhenResourceDoesNotExist() throws Exception {
 | 
			
		||||
		String resource = "classpath:bingo!";
 | 
			
		||||
		InputStreamEditor editor = new InputStreamEditor();
 | 
			
		||||
		editor.setAsText(resource);
 | 
			
		||||
		editor.setAsText("classpath:bingo!");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,80 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *      http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package org.springframework.beans.propertyeditors;
 | 
			
		||||
 | 
			
		||||
import java.beans.PropertyEditor;
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import org.springframework.util.ClassUtils;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.*;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author Juergen Hoeller
 | 
			
		||||
 * @since 4.3.2
 | 
			
		||||
 */
 | 
			
		||||
public class PathEditorTests {
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void testClasspathPathName() throws Exception {
 | 
			
		||||
		PropertyEditor pathEditor = new PathEditor();
 | 
			
		||||
		pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
 | 
			
		||||
				ClassUtils.getShortName(getClass()) + ".class");
 | 
			
		||||
		Object value = pathEditor.getValue();
 | 
			
		||||
		assertTrue(value instanceof Path);
 | 
			
		||||
		Path path = (Path) value;
 | 
			
		||||
		assertTrue(path.toFile().exists());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test(expected = IllegalArgumentException.class)
 | 
			
		||||
	public void testWithNonExistentResource() throws Exception {
 | 
			
		||||
		PropertyEditor propertyEditor = new PathEditor();
 | 
			
		||||
		propertyEditor.setAsText("classpath:/no_way_this_file_is_found.doc");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void testWithNonExistentPath() throws Exception {
 | 
			
		||||
		PropertyEditor pathEditor = new PathEditor();
 | 
			
		||||
		pathEditor.setAsText("file:/no_way_this_file_is_found.doc");
 | 
			
		||||
		Object value = pathEditor.getValue();
 | 
			
		||||
		assertTrue(value instanceof Path);
 | 
			
		||||
		Path path = (Path) value;
 | 
			
		||||
		assertTrue(!path.toFile().exists());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void testUnqualifiedPathNameFound() throws Exception {
 | 
			
		||||
		PropertyEditor pathEditor = new PathEditor();
 | 
			
		||||
		String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
 | 
			
		||||
				ClassUtils.getShortName(getClass()) + ".class";
 | 
			
		||||
		pathEditor.setAsText(fileName);
 | 
			
		||||
		Object value = pathEditor.getValue();
 | 
			
		||||
		assertTrue(value instanceof Path);
 | 
			
		||||
		Path path = (Path) value;
 | 
			
		||||
		File file = path.toFile();
 | 
			
		||||
		assertTrue(file.exists());
 | 
			
		||||
		String absolutePath = file.getAbsolutePath();
 | 
			
		||||
		if (File.separatorChar == '\\') {
 | 
			
		||||
			absolutePath = absolutePath.replace('\\', '/');
 | 
			
		||||
		}
 | 
			
		||||
		assertTrue(absolutePath.endsWith(fileName));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2015 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -34,7 +34,7 @@ public class ReaderEditorTests {
 | 
			
		|||
 | 
			
		||||
	@Test(expected = IllegalArgumentException.class)
 | 
			
		||||
	public void testCtorWithNullResourceEditor() throws Exception {
 | 
			
		||||
		new InputStreamEditor(null);
 | 
			
		||||
		new ReaderEditor(null);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2010 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -31,15 +31,6 @@ import static org.junit.Assert.*;
 | 
			
		|||
 */
 | 
			
		||||
public class URIEditorTests {
 | 
			
		||||
 | 
			
		||||
	private void doTestURI(String uriSpec) {
 | 
			
		||||
		PropertyEditor uriEditor = new URIEditor();
 | 
			
		||||
		uriEditor.setAsText(uriSpec);
 | 
			
		||||
		Object value = uriEditor.getValue();
 | 
			
		||||
		assertTrue(value instanceof URI);
 | 
			
		||||
		URI uri = (URI) value;
 | 
			
		||||
		assertEquals(uriSpec, uri.toString());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void standardURI() throws Exception {
 | 
			
		||||
		doTestURI("mailto:juergen.hoeller@interface21.com");
 | 
			
		||||
| 
						 | 
				
			
			@ -141,4 +132,14 @@ public class URIEditorTests {
 | 
			
		|||
		assertEquals("http://example.com/spaces%20and%20%E2%82%AC", uri.toASCIIString());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	private void doTestURI(String uriSpec) {
 | 
			
		||||
		PropertyEditor uriEditor = new URIEditor();
 | 
			
		||||
		uriEditor.setAsText(uriSpec);
 | 
			
		||||
		Object value = uriEditor.getValue();
 | 
			
		||||
		assertTrue(value instanceof URI);
 | 
			
		||||
		URI uri = (URI) value;
 | 
			
		||||
		assertEquals(uriSpec, uri.toString());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2006 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -29,7 +29,12 @@ import static org.junit.Assert.*;
 | 
			
		|||
 * @author Rick Evans
 | 
			
		||||
 * @author Chris Beams
 | 
			
		||||
 */
 | 
			
		||||
public final class URLEditorTests {
 | 
			
		||||
public class URLEditorTests {
 | 
			
		||||
 | 
			
		||||
	@Test(expected = IllegalArgumentException.class)
 | 
			
		||||
	public void testCtorWithNullResourceEditor() throws Exception {
 | 
			
		||||
		new URLEditor(null);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void testStandardURI() throws Exception {
 | 
			
		||||
| 
						 | 
				
			
			@ -63,7 +68,7 @@ public final class URLEditorTests {
 | 
			
		|||
		assertTrue(!url.getProtocol().startsWith("classpath"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test(expected=IllegalArgumentException.class)
 | 
			
		||||
	@Test(expected = IllegalArgumentException.class)
 | 
			
		||||
	public void testWithNonExistentResource() throws Exception {
 | 
			
		||||
		PropertyEditor urlEditor = new URLEditor();
 | 
			
		||||
		urlEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening");
 | 
			
		||||
| 
						 | 
				
			
			@ -83,9 +88,4 @@ public final class URLEditorTests {
 | 
			
		|||
		assertEquals("", urlEditor.getAsText());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test(expected=IllegalArgumentException.class)
 | 
			
		||||
	public void testCtorWithNullResourceEditor() throws Exception {
 | 
			
		||||
		new URLEditor(null);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue