Implement description consistently across Resources

This commit ensures Resources have consistent, non-empty, meaningful
implementations for getDescription(), thus ensuring that calls to
toString() (e.g., in log statements) no longer return an empty String.

This commit also polishes the Javadoc for various Resource
implementations.
This commit is contained in:
Sam Brannen
2015-02-27 19:57:16 +01:00
parent 088238443b
commit fe8289b5e3
7 changed files with 54 additions and 45 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
@@ -32,8 +32,11 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
* Unit tests for various {@link Resource} implementations.
*
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @since 09.09.2004
*/
public class ResourceTests {
@@ -55,7 +58,7 @@ public class ResourceTests {
assertFalse(resource.isOpen());
String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
assertEquals("testString", content);
assertEquals("my description", resource.getDescription());
assertTrue(resource.getDescription().contains("my description"));
assertEquals(resource, new ByteArrayResource("testString".getBytes()));
}
@@ -78,7 +81,7 @@ public class ResourceTests {
assertTrue(resource.isOpen());
String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
assertEquals("testString", content);
assertEquals("my description", resource.getDescription());
assertTrue(resource.getDescription().contains("my description"));
assertEquals(resource, new InputStreamResource(is));
}
@@ -114,7 +117,6 @@ public class ResourceTests {
assertEquals(resource, new ClassPathResource("Resource.class", getClass()));
}
@Ignore // passes under eclipse, fails under ant
@Test
public void testFileSystemResource() throws IOException {
Resource resource = new FileSystemResource(getClass().getResource("Resource.class").getFile());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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,6 +31,7 @@ import static org.junit.Assert.*;
* Unit tests for {@link ResourcePropertySource}.
*
* @author Chris Beams
* @author Sam Brannen
* @since 3.1
*/
public class ResourcePropertySourceTests {
@@ -46,57 +47,57 @@ public class ResourcePropertySourceTests {
@Test
public void withLocationAndGeneratedName() throws IOException {
PropertySource<?> ps = new ResourcePropertySource(PROPERTIES_LOCATION);
assertEquals(ps.getProperty("foo"), "bar");
assertEquals("bar", ps.getProperty("foo"));
assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION));
}
@Test
public void xmlWithLocationAndGeneratedName() throws IOException {
PropertySource<?> ps = new ResourcePropertySource(XML_PROPERTIES_LOCATION);
assertEquals(ps.getProperty("foo"), "bar");
assertEquals("bar", ps.getProperty("foo"));
assertThat(ps.getName(), is(XML_PROPERTIES_RESOURCE_DESCRIPTION));
}
@Test
public void withLocationAndExplicitName() throws IOException {
PropertySource<?> ps = new ResourcePropertySource("ps1", PROPERTIES_LOCATION);
assertEquals(ps.getProperty("foo"), "bar");
assertEquals("bar", ps.getProperty("foo"));
assertThat(ps.getName(), is("ps1"));
}
@Test
public void withLocationAndExplicitNameAndExplicitClassLoader() throws IOException {
PropertySource<?> ps = new ResourcePropertySource("ps1", PROPERTIES_LOCATION, getClass().getClassLoader());
assertEquals(ps.getProperty("foo"), "bar");
assertEquals("bar", ps.getProperty("foo"));
assertThat(ps.getName(), is("ps1"));
}
@Test
public void withLocationAndGeneratedNameAndExplicitClassLoader() throws IOException {
PropertySource<?> ps = new ResourcePropertySource(PROPERTIES_LOCATION, getClass().getClassLoader());
assertEquals(ps.getProperty("foo"), "bar");
assertEquals("bar", ps.getProperty("foo"));
assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION));
}
@Test
public void withResourceAndGeneratedName() throws IOException {
PropertySource<?> ps = new ResourcePropertySource(new ClassPathResource(PROPERTIES_PATH));
assertEquals(ps.getProperty("foo"), "bar");
assertEquals("bar", ps.getProperty("foo"));
assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION));
}
@Test
public void withResourceAndExplicitName() throws IOException {
PropertySource<?> ps = new ResourcePropertySource("ps1", new ClassPathResource(PROPERTIES_PATH));
assertEquals(ps.getProperty("foo"), "bar");
assertEquals("bar", ps.getProperty("foo"));
assertThat(ps.getName(), is("ps1"));
}
@Test
public void withResourceHavingNoDescriptionAndGeneratedName() throws IOException {
public void withResourceHavingNoDescription() throws IOException {
PropertySource<?> ps = new ResourcePropertySource(new ByteArrayResource("foo=bar".getBytes(), ""));
assertEquals(ps.getProperty("foo"), "bar");
assertTrue(ps.getName().startsWith("ByteArrayResource@"));
assertEquals("bar", ps.getProperty("foo"));
assertEquals("Byte array resource []", ps.getName());
}
}