Introduce FeatureSpecification support

Introduce FeatureSpecification interface and implementations

    FeatureSpecification objects decouple the configuration of
    spring container features from the concern of parsing XML
    namespaces, allowing for reuse in code-based configuration
    (see @Feature* annotations below).

    * ComponentScanSpec
    * TxAnnotationDriven
    * MvcAnnotationDriven
    * MvcDefaultServletHandler
    * MvcResources
    * MvcViewControllers

Refactor associated BeanDefinitionParsers to delegate to new impls above

    The following BeanDefinitionParser implementations now deal only
    with the concern of XML parsing.  Validation is handled by their
    corresponding FeatureSpecification object.  Bean definition creation
    and registration is handled by their corresponding
    FeatureSpecificationExecutor type.

    * ComponentScanBeanDefinitionParser
    * AnnotationDrivenBeanDefinitionParser (tx)
    * AnnotationDrivenBeanDefinitionParser (mvc)
    * DefaultServletHandlerBeanDefinitionParser
    * ResourcesBeanDefinitionParser
    * ViewControllerBeanDefinitionParser

Update AopNamespaceUtils to decouple from XML (DOM API)

    Methods necessary for executing TxAnnotationDriven specification
    (and eventually, the AspectJAutoProxy specification) have been
    added that accept boolean arguments for whether to proxy
    target classes and whether to expose the proxy via threadlocal.

    Methods that accepted and introspected DOM Element objects still
    exist but have been deprecated.

Introduce @FeatureConfiguration classes and @Feature methods

    Allow for creation and configuration of FeatureSpecification objects
    at the user level.  A companion for @Configuration classes allowing
    for completely code-driven configuration of the Spring container.

    See changes in ConfigurationClassPostProcessor for implementation
    details.

    See Feature*Tests for usage examples.

    FeatureTestSuite in .integration-tests is a JUnit test suite designed
    to aggregate all BDP and Feature* related tests for a convenient way
    to confirm that Feature-related changes don't break anything.
    Uncomment this test and execute from Eclipse / IDEA. Due to classpath
    issues, this cannot be compiled by Ant/Ivy at the command line.

Introduce @FeatureAnnotation meta-annotation and @ComponentScan impl

    @FeatureAnnotation provides an alternate mechanism for creating
    and executing FeatureSpecification objects.  See @ComponentScan
    and its corresponding ComponentScanAnnotationParser implementation
    for details.  See ComponentScanAnnotationIntegrationTests for usage
    examples

Introduce Default[Formatting]ConversionService implementations

    Allows for convenient instantiation of ConversionService objects
    containing defaults appropriate for most environments.  Replaces
    similar support originally in ConversionServiceFactory (which is now
    deprecated). This change was justified by the need to avoid use
    of FactoryBeans in @Configuration classes (such as
    FormattingConversionServiceFactoryBean). It is strongly preferred
    that users simply instantiate and configure the objects that underlie
    our FactoryBeans. In the case of the ConversionService types, the
    easiest way to do this is to create Default* subtypes. This also
    follows convention with the rest of the framework.

Minor updates to util classes

    All in service of changes above. See diffs for self-explanatory
    details.

    * BeanUtils
    * ObjectUtils
    * ReflectionUtils
This commit is contained in:
Chris Beams
2011-02-08 14:42:33 +00:00
parent b04987ccc3
commit b4fea47d5c
127 changed files with 7397 additions and 1132 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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.
@@ -55,7 +55,7 @@ import org.springframework.core.convert.converter.ConverterRegistry;
*/
public class DefaultConversionTests {
private ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
private ConversionService conversionService = new DefaultConversionService();
@Test
public void testStringToCharacter() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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.
@@ -78,6 +78,7 @@ public class GenericConversionServiceTests {
}
@Test
@SuppressWarnings("rawtypes")
public void addConverterNoSourceTargetClassInfoAvailable() {
try {
conversionService.addConverter(new Converter() {
@@ -109,7 +110,7 @@ public class GenericConversionServiceTests {
}
public void convertNullTargetClass() {
assertNull(conversionService.convert("3", (Class) null));
assertNull(conversionService.convert("3", (Class<?>) null));
assertNull(conversionService.convert("3", TypeDescriptor.NULL));
}
@@ -226,7 +227,7 @@ public class GenericConversionServiceTests {
@Test
public void testStringArrayToResourceArray() {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
GenericConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new MyStringArrayToResourceArrayConverter());
Resource[] converted = conversionService.convert(new String[] {"x1", "z3"}, Resource[].class);
assertEquals(2, converted.length);
@@ -236,7 +237,7 @@ public class GenericConversionServiceTests {
@Test
public void testStringArrayToIntegerArray() {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
GenericConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new MyStringArrayToIntegerArrayConverter());
Integer[] converted = conversionService.convert(new String[] {"x1", "z3"}, Integer[].class);
assertEquals(2, converted.length);
@@ -246,7 +247,7 @@ public class GenericConversionServiceTests {
@Test
public void testStringToIntegerArray() {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
GenericConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new MyStringToIntegerArrayConverter());
Integer[] converted = conversionService.convert("x1,z3", Integer[].class);
assertEquals(2, converted.length);
@@ -256,7 +257,7 @@ public class GenericConversionServiceTests {
@Test
public void testWildcardMap() throws Exception {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
GenericConversionService conversionService = new DefaultConversionService();
Map<String, String> input = new LinkedHashMap<String, String>();
input.put("key", "value");
Object converted = conversionService.convert(input, new TypeDescriptor(getClass().getField("wildcardMap")));
@@ -265,14 +266,14 @@ public class GenericConversionServiceTests {
@Test
public void testListOfList() {
GenericConversionService service = ConversionServiceFactory.createDefaultConversionService();
GenericConversionService service = new DefaultConversionService();
List<List<String>> list = Collections.singletonList(Collections.singletonList("Foo"));
assertNotNull(service.convert(list, String.class));
}
@Test
public void testStringToString() {
GenericConversionService service = ConversionServiceFactory.createDefaultConversionService();
GenericConversionService service = new DefaultConversionService();
String value = "myValue";
String result = service.convert(value, String.class);
assertSame(value, result);
@@ -280,7 +281,7 @@ public class GenericConversionServiceTests {
@Test
public void testStringToObject() {
GenericConversionService service = ConversionServiceFactory.createDefaultConversionService();
GenericConversionService service = new DefaultConversionService();
String value = "myValue";
Object result = service.convert(value, Object.class);
assertSame(value, result);
@@ -288,7 +289,7 @@ public class GenericConversionServiceTests {
@Test
public void testIgnoreCopyConstructor() {
GenericConversionService service = ConversionServiceFactory.createDefaultConversionService();
GenericConversionService service = new DefaultConversionService();
WithCopyConstructor value = new WithCopyConstructor();
Object result = service.convert(value, WithCopyConstructor.class);
assertSame(value, result);
@@ -296,7 +297,7 @@ public class GenericConversionServiceTests {
@Test
public void testPerformance1() {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
GenericConversionService conversionService = new DefaultConversionService();
StopWatch watch = new StopWatch("integer->string conversionPerformance");
watch.start("convert 4,000,000 with conversion service");
for (int i = 0; i < 4000000; i++) {
@@ -313,7 +314,7 @@ public class GenericConversionServiceTests {
@Test
public void testPerformance2() throws Exception {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
GenericConversionService conversionService = new DefaultConversionService();
StopWatch watch = new StopWatch("list<string> -> list<integer> conversionPerformance");
watch.start("convert 4,000,000 with conversion service");
List<String> source = new LinkedList<String>();
@@ -340,7 +341,7 @@ public class GenericConversionServiceTests {
@Test
public void testPerformance3() throws Exception {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
GenericConversionService conversionService = new DefaultConversionService();
StopWatch watch = new StopWatch("map<string, string> -> map<string, integer> conversionPerformance");
watch.start("convert 4,000,000 with conversion service");
Map<String, String> source = new HashMap<String, String>();
@@ -387,6 +388,7 @@ public class GenericConversionServiceTests {
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyListDifferentTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked")
LinkedList<Integer> result = (LinkedList<Integer>) conversionService.convert(list, sourceType, targetType);
assertEquals(LinkedList.class, result.getClass());
assertTrue(result.isEmpty());
@@ -437,6 +439,7 @@ public class GenericConversionServiceTests {
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked")
LinkedHashMap<String, String> result = (LinkedHashMap<String, String>) conversionService.convert(map, sourceType, targetType);
assertEquals(map, result);
assertEquals(LinkedHashMap.class, result.getClass());

View File

@@ -16,6 +16,9 @@
package org.springframework.util;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.sql.SQLException;
@@ -43,10 +46,10 @@ public final class ObjectUtilsTests extends TestCase {
}
public void testIsCompatibleWithThrowsClause() {
Class[] empty = new Class[0];
Class[] exception = new Class[] {Exception.class};
Class[] sqlAndIO = new Class[] {SQLException.class, IOException.class};
Class[] throwable = new Class[] {Throwable.class};
Class<?>[] empty = new Class[0];
Class<?>[] exception = new Class[] {Exception.class};
Class<?>[] sqlAndIO = new Class[] {SQLException.class, IOException.class};
Class<?>[] throwable = new Class[] {Throwable.class};
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), null));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), empty));
@@ -617,6 +620,35 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals("null", ObjectUtils.nullSafeToString((String[]) null));
}
enum Tropes { FOO, BAR, baz };
public void testContainsConstant() {
assertThat(ObjectUtils.containsConstant(Tropes.values(), "FOO"), is(true));
assertThat(ObjectUtils.containsConstant(Tropes.values(), "foo"), is(true));
assertThat(ObjectUtils.containsConstant(Tropes.values(), "BaR"), is(true));
assertThat(ObjectUtils.containsConstant(Tropes.values(), "bar"), is(true));
assertThat(ObjectUtils.containsConstant(Tropes.values(), "BAZ"), is(true));
assertThat(ObjectUtils.containsConstant(Tropes.values(), "baz"), is(true));
assertThat(ObjectUtils.containsConstant(Tropes.values(), "BOGUS"), is(false));
assertThat(ObjectUtils.containsConstant(Tropes.values(), "FOO", true), is(true));
assertThat(ObjectUtils.containsConstant(Tropes.values(), "foo", true), is(false));
}
public void testCaseInsensitiveValueOf() {
assertThat(ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "foo"), is(Tropes.FOO));
assertThat(ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "BAR"), is(Tropes.BAR));
try {
ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "bogus");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(),
is("constant [bogus] does not exist in enum type " +
"org.springframework.util.ObjectUtilsTests$Tropes"));
}
}
private void assertEqualHashCodes(int expected, Object array) {
int actual = ObjectUtils.nullSafeHashCode(array);
assertEquals(expected, actual);