moving unit tests from .testsuite -> .core

This commit is contained in:
Chris Beams
2008-12-15 01:14:57 +00:00
parent 2359942dd7
commit 8977ad4032
8 changed files with 99 additions and 157 deletions

View File

@@ -0,0 +1,295 @@
/*
* Copyright 2002-2006 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.core.annotation;
import static org.junit.Assert.*;
import static org.springframework.core.annotation.AnnotationUtils.*;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import org.junit.Test;
import org.springframework.core.Ordered;
/**
* @author Rod Johnson
* @author Juergen Hoeller
* @author Sam Brannen
* @author Chris Beams
*/
public class AnnotationUtilsTests {
@Test
public void testFindMethodAnnotationOnLeaf() throws SecurityException, NoSuchMethodException {
final Method m = Leaf.class.getMethod("annotatedOnLeaf", (Class[]) null);
assertNotNull(m.getAnnotation(Order.class));
assertNotNull(getAnnotation(m, Order.class));
assertNotNull(findAnnotation(m, Order.class));
}
@Test
public void testFindMethodAnnotationOnRoot() throws SecurityException, NoSuchMethodException {
final Method m = Leaf.class.getMethod("annotatedOnRoot", (Class[]) null);
assertNotNull(m.getAnnotation(Order.class));
assertNotNull(getAnnotation(m, Order.class));
assertNotNull(findAnnotation(m, Order.class));
}
@Test
public void testFindMethodAnnotationOnRootButOverridden() throws SecurityException, NoSuchMethodException {
final Method m = Leaf.class.getMethod("overrideWithoutNewAnnotation", (Class[]) null);
assertNull(m.getAnnotation(Order.class));
assertNull(getAnnotation(m, Order.class));
assertNotNull(findAnnotation(m, Order.class));
}
@Test
public void testFindMethodAnnotationNotAnnotated() throws SecurityException, NoSuchMethodException {
final Method m = Leaf.class.getMethod("notAnnotated", (Class[]) null);
assertNull(findAnnotation(m, Order.class));
}
@Test
public void testFindMethodAnnotationOnBridgeMethod() throws Exception {
final Method m = SimpleFoo.class.getMethod("something", Object.class);
assertTrue(m.isBridge());
assertNull(m.getAnnotation(Order.class));
assertNull(getAnnotation(m, Order.class));
assertNotNull(findAnnotation(m, Order.class));
assertNull(m.getAnnotation(Transactional.class));
assertNotNull(getAnnotation(m, Transactional.class));
assertNotNull(findAnnotation(m, Transactional.class));
}
// TODO consider whether we want this to handle annotations on interfaces
// public void testFindMethodAnnotationFromInterfaceImplementedByRoot()
// throws Exception {
// Method m = Leaf.class.getMethod("fromInterfaceImplementedByRoot",
// (Class[]) null);
// Order o = findAnnotation(Order.class, m, Leaf.class);
// assertNotNull(o);
// }
@Test
public void testFindAnnotationDeclaringClass() throws Exception {
// no class-level annotation
assertNull(findAnnotationDeclaringClass(Transactional.class, NonAnnotatedInterface.class));
assertNull(findAnnotationDeclaringClass(Transactional.class, NonAnnotatedClass.class));
// inherited class-level annotation; note: @Transactional is inherited
assertEquals(InheritedAnnotationInterface.class, findAnnotationDeclaringClass(Transactional.class,
InheritedAnnotationInterface.class));
assertNull(findAnnotationDeclaringClass(Transactional.class, SubInheritedAnnotationInterface.class));
assertEquals(InheritedAnnotationClass.class, findAnnotationDeclaringClass(Transactional.class,
InheritedAnnotationClass.class));
assertEquals(InheritedAnnotationClass.class, findAnnotationDeclaringClass(Transactional.class,
SubInheritedAnnotationClass.class));
// non-inherited class-level annotation; note: @Order is not inherited,
// but findAnnotationDeclaringClass() should still find it.
assertEquals(NonInheritedAnnotationInterface.class, findAnnotationDeclaringClass(Order.class,
NonInheritedAnnotationInterface.class));
assertNull(findAnnotationDeclaringClass(Order.class, SubNonInheritedAnnotationInterface.class));
assertEquals(NonInheritedAnnotationClass.class, findAnnotationDeclaringClass(Order.class,
NonInheritedAnnotationClass.class));
assertEquals(NonInheritedAnnotationClass.class, findAnnotationDeclaringClass(Order.class,
SubNonInheritedAnnotationClass.class));
}
@Test
public void testIsAnnotationDeclaredLocally() throws Exception {
// no class-level annotation
assertFalse(isAnnotationDeclaredLocally(Transactional.class, NonAnnotatedInterface.class));
assertFalse(isAnnotationDeclaredLocally(Transactional.class, NonAnnotatedClass.class));
// inherited class-level annotation; note: @Transactional is inherited
assertTrue(isAnnotationDeclaredLocally(Transactional.class, InheritedAnnotationInterface.class));
assertFalse(isAnnotationDeclaredLocally(Transactional.class, SubInheritedAnnotationInterface.class));
assertTrue(isAnnotationDeclaredLocally(Transactional.class, InheritedAnnotationClass.class));
assertFalse(isAnnotationDeclaredLocally(Transactional.class, SubInheritedAnnotationClass.class));
// non-inherited class-level annotation; note: @Order is not inherited
assertTrue(isAnnotationDeclaredLocally(Order.class, NonInheritedAnnotationInterface.class));
assertFalse(isAnnotationDeclaredLocally(Order.class, SubNonInheritedAnnotationInterface.class));
assertTrue(isAnnotationDeclaredLocally(Order.class, NonInheritedAnnotationClass.class));
assertFalse(isAnnotationDeclaredLocally(Order.class, SubNonInheritedAnnotationClass.class));
}
@Test
public void testIsAnnotationInherited() throws Exception {
// no class-level annotation
assertFalse(isAnnotationInherited(Transactional.class, NonAnnotatedInterface.class));
assertFalse(isAnnotationInherited(Transactional.class, NonAnnotatedClass.class));
// inherited class-level annotation; note: @Transactional is inherited
assertFalse(isAnnotationInherited(Transactional.class, InheritedAnnotationInterface.class));
// isAnnotationInherited() does not currently traverse interface
// hierarchies. Thus the following, though perhaps counter intuitive,
// must be false:
assertFalse(isAnnotationInherited(Transactional.class, SubInheritedAnnotationInterface.class));
assertFalse(isAnnotationInherited(Transactional.class, InheritedAnnotationClass.class));
assertTrue(isAnnotationInherited(Transactional.class, SubInheritedAnnotationClass.class));
// non-inherited class-level annotation; note: @Order is not inherited
assertFalse(isAnnotationInherited(Order.class, NonInheritedAnnotationInterface.class));
assertFalse(isAnnotationInherited(Order.class, SubNonInheritedAnnotationInterface.class));
assertFalse(isAnnotationInherited(Order.class, NonInheritedAnnotationClass.class));
assertFalse(isAnnotationInherited(Order.class, SubNonInheritedAnnotationClass.class));
}
@Test
public void testGetValueFromAnnotation() throws Exception {
final Method method = SimpleFoo.class.getMethod("something", Object.class);
final Order order = findAnnotation(method, Order.class);
assertEquals(1, AnnotationUtils.getValue(order, AnnotationUtils.VALUE));
assertEquals(1, AnnotationUtils.getValue(order));
}
@Test
public void testGetDefaultValueFromAnnotation() throws Exception {
final Method method = SimpleFoo.class.getMethod("something", Object.class);
final Order order = findAnnotation(method, Order.class);
assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(order, AnnotationUtils.VALUE));
assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(order));
}
@Test
public void testGetDefaultValueFromAnnotationType() throws Exception {
assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(Order.class, AnnotationUtils.VALUE));
assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(Order.class));
}
public static interface AnnotatedInterface {
@Order(0)
void fromInterfaceImplementedByRoot();
}
public static class Root implements AnnotatedInterface {
@Order(27)
public void annotatedOnRoot() {
}
public void overrideToAnnotate() {
}
@Order(27)
public void overrideWithoutNewAnnotation() {
}
public void notAnnotated() {
}
public void fromInterfaceImplementedByRoot() {
}
}
public static class Leaf extends Root {
@Order(25)
public void annotatedOnLeaf() {
}
@Override
@Order(1)
public void overrideToAnnotate() {
}
@Override
public void overrideWithoutNewAnnotation() {
}
}
public static abstract class Foo<T> {
@Order(1)
public abstract void something(T arg);
}
public static class SimpleFoo extends Foo<String> {
@Transactional
public void something(final String arg) {
}
}
@Transactional
public static interface InheritedAnnotationInterface {
}
public static interface SubInheritedAnnotationInterface extends InheritedAnnotationInterface {
}
@Order
public static interface NonInheritedAnnotationInterface {
}
public static interface SubNonInheritedAnnotationInterface extends NonInheritedAnnotationInterface {
}
public static class NonAnnotatedClass {
}
public static interface NonAnnotatedInterface {
}
@Transactional
public static class InheritedAnnotationClass {
}
public static class SubInheritedAnnotationClass extends InheritedAnnotationClass {
}
@Order
public static class NonInheritedAnnotationClass {
}
public static class SubNonInheritedAnnotationClass extends NonInheritedAnnotationClass {
}
}
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface Transactional {
}

View File

@@ -0,0 +1,227 @@
/*
* Copyright 2002-2008 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.core.io;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.util.FileCopyUtils;
/**
* @author Juergen Hoeller
* @since 09.09.2004
*/
public class ResourceTests {
@Test
public void testByteArrayResource() throws IOException {
Resource resource = new ByteArrayResource("testString".getBytes());
assertTrue(resource.exists());
assertFalse(resource.isOpen());
String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
assertEquals("testString", content);
assertEquals(resource, new ByteArrayResource("testString".getBytes()));
}
@Test
public void testByteArrayResourceWithDescription() throws IOException {
Resource resource = new ByteArrayResource("testString".getBytes(), "my description");
assertTrue(resource.exists());
assertFalse(resource.isOpen());
String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
assertEquals("testString", content);
assertEquals("my description", resource.getDescription());
assertEquals(resource, new ByteArrayResource("testString".getBytes()));
}
@Test
public void testInputStreamResource() throws IOException {
InputStream is = new ByteArrayInputStream("testString".getBytes());
Resource resource = new InputStreamResource(is);
assertTrue(resource.exists());
assertTrue(resource.isOpen());
String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
assertEquals("testString", content);
assertEquals(resource, new InputStreamResource(is));
}
@Test
public void testInputStreamResourceWithDescription() throws IOException {
InputStream is = new ByteArrayInputStream("testString".getBytes());
Resource resource = new InputStreamResource(is, "my description");
assertTrue(resource.exists());
assertTrue(resource.isOpen());
String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
assertEquals("testString", content);
assertEquals("my description", resource.getDescription());
assertEquals(resource, new InputStreamResource(is));
}
@Test
public void testClassPathResource() throws IOException {
Resource resource = new ClassPathResource("org/springframework/core/io/Resource.class");
doTestResource(resource);
Resource resource2 = new ClassPathResource("org/springframework/core/../core/io/./Resource.class");
assertEquals(resource, resource2);
Resource resource3 = new ClassPathResource("org/springframework/core/").createRelative("../core/io/./Resource.class");
assertEquals(resource, resource3);
// Check whether equal/hashCode works in a HashSet.
HashSet<Resource> resources = new HashSet<Resource>();
resources.add(resource);
resources.add(resource2);
assertEquals(1, resources.size());
}
@Test
public void testClassPathResourceWithClassLoader() throws IOException {
Resource resource =
new ClassPathResource("org/springframework/core/io/Resource.class", getClass().getClassLoader());
doTestResource(resource);
assertEquals(resource,
new ClassPathResource("org/springframework/core/../core/io/./Resource.class", getClass().getClassLoader()));
}
@Test
public void testClassPathResourceWithClass() throws IOException {
Resource resource = new ClassPathResource("Resource.class", getClass());
doTestResource(resource);
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());
doTestResource(resource);
assertEquals(new FileSystemResource(getClass().getResource("Resource.class").getFile()), resource);
Resource resource2 = new FileSystemResource("core/io/Resource.class");
assertEquals(resource2, new FileSystemResource("core/../core/io/./Resource.class"));
}
@Test
public void testUrlResource() throws IOException {
Resource resource = new UrlResource(getClass().getResource("Resource.class"));
doTestResource(resource);
assertEquals(new UrlResource(getClass().getResource("Resource.class")), resource);
Resource resource2 = new UrlResource("file:core/io/Resource.class");
assertEquals(resource2, new UrlResource("file:core/../core/io/./Resource.class"));
}
private void doTestResource(Resource resource) throws IOException {
assertEquals("Resource.class", resource.getFilename());
assertTrue(resource.getURL().getFile().endsWith("Resource.class"));
Resource relative1 = resource.createRelative("ClassPathResource.class");
assertEquals("ClassPathResource.class", relative1.getFilename());
assertTrue(relative1.getURL().getFile().endsWith("ClassPathResource.class"));
assertTrue(relative1.exists());
Resource relative2 = resource.createRelative("support/ResourcePatternResolver.class");
assertEquals("ResourcePatternResolver.class", relative2.getFilename());
assertTrue(relative2.getURL().getFile().endsWith("ResourcePatternResolver.class"));
assertTrue(relative2.exists());
/*
Resource relative3 = resource.createRelative("../SpringVersion.class");
assertEquals("SpringVersion.class", relative3.getFilename());
assertTrue(relative3.getURL().getFile().endsWith("SpringVersion.class"));
assertTrue(relative3.exists());
*/
}
@Test
public void testClassPathResourceWithRelativePath() throws IOException {
Resource resource = new ClassPathResource("dir/");
Resource relative = resource.createRelative("subdir");
assertEquals(new ClassPathResource("dir/subdir"), relative);
}
@Test
public void testFileSystemResourceWithRelativePath() throws IOException {
Resource resource = new FileSystemResource("dir/");
Resource relative = resource.createRelative("subdir");
assertEquals(new FileSystemResource("dir/subdir"), relative);
}
@Test
public void testUrlResourceWithRelativePath() throws IOException {
Resource resource = new UrlResource("file:dir/");
Resource relative = resource.createRelative("subdir");
assertEquals(new UrlResource("file:dir/subdir"), relative);
}
/*
* @Test
public void testNonFileResourceExists() throws Exception {
Resource resource = new UrlResource("http://www.springframework.org");
assertTrue(resource.exists());
}
*/
@Test
public void testAbstractResourceExceptions() throws Exception {
final String name = "test-resource";
Resource resource = new AbstractResource() {
public String getDescription() {
return name;
}
public InputStream getInputStream() {
return null;
}
};
try {
resource.getURL();
fail("FileNotFoundException should have been thrown");
}
catch (FileNotFoundException ex) {
assertTrue(ex.getMessage().indexOf(name) != -1);
}
try {
resource.getFile();
fail("FileNotFoundException should have been thrown");
}
catch (FileNotFoundException ex) {
assertTrue(ex.getMessage().indexOf(name) != -1);
}
try {
resource.createRelative("/testing");
fail("FileNotFoundException should have been thrown");
}
catch (FileNotFoundException ex) {
assertTrue(ex.getMessage().indexOf(name) != -1);
}
try {
resource.getFilename();
fail("IllegalStateException should have been thrown");
}
catch (IllegalStateException ex) {
assertTrue(ex.getMessage().indexOf(name) != -1);
}
}
}

View File

@@ -0,0 +1,165 @@
/*
* Copyright 2002-2006 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.core.io.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.io.Resource;
/**
* If this test case fails, uncomment diagnostics in
* <code>assertProtocolAndFilenames</code> method.
*
* @author Oliver Hutchison
* @author Juergen Hoeller
* @author Chris Beams
* @since 17.11.2004
*/
public class PathMatchingResourcePatternResolverTests {
private static final String[] CLASSES_IN_CORE_IO_SUPPORT =
new String[] {"EncodedResource.class", "LocalizedResourceHelper.class",
"PathMatchingResourcePatternResolver.class",
"PropertiesLoaderSupport.class", "PropertiesLoaderUtils.class",
"ResourceArrayPropertyEditor.class",
"ResourcePatternResolver.class", "ResourcePatternUtils.class"};
private static final String[] TEST_CLASSES_IN_CORE_IO_SUPPORT =
new String[] {"PathMatchingResourcePatternResolverTests.class"};
private static final String[] CLASSES_IN_COMMONSLOGGING =
new String[] {"Log.class", "LogConfigurationException.class", "LogFactory.class",
"LogFactory$1.class", "LogFactory$2.class", "LogFactory$3.class",
"LogFactory$4.class", "LogFactory$5.class", "LogFactory$6.class",
"LogSource.class"};
private PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
@Test
public void testInvalidPrefixWithPatternElementInIt() throws IOException {
try {
resolver.getResources("xx**:**/*.xy");
fail("Should have thrown FileNotFoundException");
}
catch (FileNotFoundException ex) {
// expected
}
}
@Test
public void testSingleResourceOnFileSystem() throws IOException {
Resource[] resources =
resolver.getResources("org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.class");
assertEquals(1, resources.length);
assertProtocolAndFilename(resources[0], "file", "PathMatchingResourcePatternResolverTests.class");
}
@Test
public void testSingleResourceInJar() throws IOException {
Resource[] resources = resolver.getResources("java/net/URL.class");
assertEquals(1, resources.length);
assertProtocolAndFilename(resources[0], "jar", "URL.class");
}
@Ignore // passes under eclipse, fails under ant
@Test
public void testClasspathStarWithPatternOnFileSystem() throws IOException {
Resource[] resources = resolver.getResources("classpath*:org/springframework/core/io/sup*/*.class");
// Have to exclude Clover-generated class files here,
// as we might be running as part of a Clover test run.
List noCloverResources = new ArrayList();
for (int i = 0; i < resources.length; i++) {
if (resources[i].getFilename().indexOf("$__CLOVER_") == -1) {
noCloverResources.add(resources[i]);
}
}
resources = (Resource[]) noCloverResources.toArray(new Resource[noCloverResources.size()]);
assertProtocolAndFilenames(resources, "file", CLASSES_IN_CORE_IO_SUPPORT, TEST_CLASSES_IN_CORE_IO_SUPPORT);
}
@Test
public void testClasspathWithPatternInJar() throws IOException {
Resource[] resources = resolver.getResources("classpath:org/apache/commons/logging/*.class");
assertProtocolAndFilenames(resources, "jar", CLASSES_IN_COMMONSLOGGING);
}
@Test
public void testClasspathStartWithPatternInJar() throws IOException {
Resource[] resources = resolver.getResources("classpath*:org/apache/commons/logging/*.class");
assertProtocolAndFilenames(resources, "jar", CLASSES_IN_COMMONSLOGGING);
}
private void assertProtocolAndFilename(Resource resource, String urlProtocol, String fileName) throws IOException {
assertProtocolAndFilenames(new Resource[] {resource}, urlProtocol, new String[] {fileName});
}
private void assertProtocolAndFilenames(
Resource[] resources, String urlProtocol, String[] fileNames1, String[] fileNames2) throws IOException {
List fileNames = new ArrayList(Arrays.asList(fileNames1));
fileNames.addAll(Arrays.asList(fileNames2));
assertProtocolAndFilenames(resources, urlProtocol, (String[]) fileNames.toArray(new String[fileNames.size()]));
}
private void assertProtocolAndFilenames(Resource[] resources, String urlProtocol, String[] fileNames)
throws IOException {
// Uncomment the following if you encounter problems with matching against the file system
// It shows file locations.
// String[] actualNames = new String[resources.length];
// for (int i = 0; i < resources.length; i++) {
// actualNames[i] = resources[i].getFilename();
// }
// List sortedActualNames = new LinkedList(Arrays.asList(actualNames));
// List expectedNames = new LinkedList(Arrays.asList(fileNames));
// Collections.sort(sortedActualNames);
// Collections.sort(expectedNames);
//
// System.out.println("-----------");
// System.out.println("Expected: " + StringUtils.collectionToCommaDelimitedString(expectedNames));
// System.out.println("Actual: " + StringUtils.collectionToCommaDelimitedString(sortedActualNames));
// for (int i = 0; i < resources.length; i++) {
// System.out.println(resources[i]);
// }
assertEquals("Correct number of files found", fileNames.length, resources.length);
for (int i = 0; i < resources.length; i++) {
Resource resource = resources[i];
assertEquals(urlProtocol, resource.getURL().getProtocol());
assertFilenameIn(resource, fileNames);
}
}
private void assertFilenameIn(Resource resource, String[] fileNames) {
for (int i = 0; i < fileNames.length; i++) {
if (resource.getFilename().endsWith(fileNames[i])) {
return;
}
}
fail("resource [" + resource + "] does not have a filename that matches and of the names in 'fileNames'");
}
}