Remove "Feature" support introduced in 3.1 M1

Feature-related support such as @Feature, @FeatureConfiguration,
and FeatureSpecification types will be replaced by framework-provided
@Configuration classes and convenience annotations such as
@ComponentScan (already exists), @EnableAsync, @EnableScheduling,
@EnableTransactionManagement and others.

Issue: SPR-8012,SPR-8034,SPR-8039,SPR-8188,SPR-8206,SPR-8223,
SPR-8225,SPR-8226,SPR-8227
This commit is contained in:
Chris Beams
2011-05-06 19:03:52 +00:00
parent 0a790c143f
commit 111fb71fe1
80 changed files with 857 additions and 6737 deletions

View File

@@ -1,107 +0,0 @@
/*
* 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.
* 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.web.servlet.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Feature;
import org.springframework.context.annotation.FeatureConfiguration;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodAdapter;
import org.springframework.web.servlet.mvc.method.annotation.support.ServletWebArgumentResolverAdapter;
/**
* Integration tests for the {@link MvcAnnotationDriven} feature specification.
* @author Rossen Stoyanchev
* @author Chris Beams
* @since 3.1
*/
public class MvcAnnotationDrivenFeatureTests {
@SuppressWarnings("unchecked")
@Test
public void testMessageCodesResolver() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MvcFeature.class, MvcBeans.class);
ctx.refresh();
RequestMappingHandlerMethodAdapter adapter = ctx.getBean(RequestMappingHandlerMethodAdapter.class);
assertNotNull(adapter);
Object initializer = new DirectFieldAccessor(adapter).getPropertyValue("webBindingInitializer");
assertNotNull(initializer);
MessageCodesResolver resolver = ((ConfigurableWebBindingInitializer) initializer).getMessageCodesResolver();
assertNotNull(resolver);
assertEquals("test.foo.bar", resolver.resolveMessageCodes("foo", "bar")[0]);
Object value = new DirectFieldAccessor(adapter).getPropertyValue("customArgumentResolvers");
assertNotNull(value);
List<HandlerMethodArgumentResolver> resolvers = (List<HandlerMethodArgumentResolver>) value;
assertEquals(2, resolvers.size());
assertTrue(resolvers.get(0) instanceof ServletWebArgumentResolverAdapter);
assertTrue(resolvers.get(1) instanceof TestHandlerMethodArgumentResolver);
Object converters = new DirectFieldAccessor(adapter).getPropertyValue("messageConverters");
assertNotNull(converters);
List<HttpMessageConverter<?>> convertersArray = (List<HttpMessageConverter<?>>) converters;
assertTrue("Default converters are registered in addition to the custom one", convertersArray.size() > 1);
assertTrue(convertersArray.get(0) instanceof StringHttpMessageConverter);
}
}
@FeatureConfiguration
class MvcFeature {
@Feature
public MvcAnnotationDriven annotationDriven(MvcBeans mvcBeans) {
return new MvcAnnotationDriven()
.conversionService(mvcBeans.conversionService())
.messageCodesResolver(mvcBeans.messageCodesResolver())
.validator(mvcBeans.validator())
.messageConverters(new StringHttpMessageConverter())
.argumentResolvers(new TestWebArgumentResolver())
.argumentResolvers(new TestHandlerMethodArgumentResolver());
}
}
@Configuration
class MvcBeans {
@Bean
public FormattingConversionService conversionService() {
return new DefaultFormattingConversionService();
}
@Bean
public Validator validator() {
return new LocalValidatorFactoryBean();
}
@Bean MessageCodesResolver messageCodesResolver() {
return new TestMessageCodesResolver();
}
}

View File

@@ -1,61 +0,0 @@
/*
* 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.
* 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.web.servlet.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Feature;
import org.springframework.context.annotation.FeatureConfiguration;
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;
/**
* Test fixture for {@link MvcDefaultServletHandler} feature specification.
* @author Rossen Stoyanchev
* @since 3.1
*/
public class MvcDefaultServletHandlerTests {
@Test
public void testDefaultServletHandler() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MvcDefaultServletHandlerFeature.class);
ctx.refresh();
HttpRequestHandlerAdapter adapter = ctx.getBean(HttpRequestHandlerAdapter.class);
assertNotNull(adapter);
DefaultServletHttpRequestHandler handler = ctx.getBean(DefaultServletHttpRequestHandler.class);
assertNotNull(handler);
String defaultServletHandlerName = (String) new DirectFieldAccessor(handler)
.getPropertyValue("defaultServletName");
assertEquals("foo", defaultServletHandlerName);
}
@FeatureConfiguration
private static class MvcDefaultServletHandlerFeature {
@SuppressWarnings("unused")
@Feature
public MvcDefaultServletHandler defaultServletHandler() {
return new MvcDefaultServletHandler("foo");
}
}
}

View File

@@ -1,126 +0,0 @@
/*
* 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.
* 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.web.servlet.config;
import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Iterator;
import org.easymock.Capture;
import org.junit.Test;
import org.springframework.beans.factory.parsing.Problem;
import org.springframework.beans.factory.parsing.ProblemReporter;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Feature;
import org.springframework.context.annotation.FeatureConfiguration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.MappedInterceptor;
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
/**
* Test fixture for {@link MvcInterceptors}.
* @author Rossen Stoyanchev
*/
public class MvcInterceptorsTests {
@Test
public void testInterceptors() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MvcInterceptorsFeature.class);
ctx.refresh();
Iterator<MappedInterceptor> itr = ctx.getBeansOfType(MappedInterceptor.class).values().iterator();
MappedInterceptor interceptor = itr.next();
assertTrue(interceptor.getInterceptor() instanceof UserRoleAuthorizationInterceptor);
assertNull(interceptor.getPathPatterns());
interceptor = itr.next();
assertTrue(interceptor.getInterceptor() instanceof LocaleChangeInterceptor);
assertArrayEquals(new String[] { "/locale", "/locale/**" }, interceptor.getPathPatterns());
interceptor = itr.next();
assertTrue(interceptor.getInterceptor() instanceof ThemeChangeInterceptor);
assertArrayEquals(new String[] { "/theme", "/theme/**" }, interceptor.getPathPatterns());
}
@Test
public void validateNoInterceptors() {
ProblemReporter reporter = createMock(ProblemReporter.class);
Capture<Problem> captured = new Capture<Problem>();
reporter.error(capture(captured));
replay(reporter);
boolean result = new MvcInterceptors().validate(reporter);
assertFalse(result);
assertEquals("No interceptors defined.", captured.getValue().getMessage());
}
@Test
public void validateNullHandler() {
ProblemReporter reporter = createMock(ProblemReporter.class);
Capture<Problem> captured = new Capture<Problem>();
reporter.error(capture(captured));
replay(reporter);
HandlerInterceptor[] interceptors = new HandlerInterceptor[] { null };
boolean result = new MvcInterceptors().globalInterceptors(interceptors).validate(reporter);
assertFalse(result);
assertTrue(captured.getValue().getMessage().contains("Null interceptor"));
}
@Test
public void validateEmptyPath() {
ProblemReporter reporter = createMock(ProblemReporter.class);
Capture<Problem> captured = new Capture<Problem>();
reporter.error(capture(captured));
replay(reporter);
HandlerInterceptor[] interceptors = new HandlerInterceptor[] { new LocaleChangeInterceptor() };
String[] patterns = new String[] { "" };
boolean result = new MvcInterceptors().mappedInterceptors(patterns, interceptors).validate(reporter);
assertFalse(result);
assertTrue(captured.getValue().getMessage().startsWith("Empty path pattern specified for "));
}
@FeatureConfiguration
private static class MvcInterceptorsFeature {
@SuppressWarnings("unused")
@Feature
public MvcInterceptors interceptors() {
return new MvcInterceptors()
.globalInterceptors(new UserRoleAuthorizationInterceptor())
.mappedInterceptors(new String[] { "/locale", "/locale/**" }, new LocaleChangeInterceptor())
.mappedInterceptors(new String[] { "/theme", "/theme/**"}, new ThemeChangeInterceptor());
}
}
}

View File

@@ -1,98 +0,0 @@
/*
* 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.
* 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.web.servlet.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Feature;
import org.springframework.context.annotation.FeatureConfiguration;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
/**
* Test fixture for {@link MvcResources} feature specification.
* @author Rossen Stoyanchev
* @since 3.1
*/
public class MvcResourcesTests {
@Test
public void testResources() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MvcResourcesFeature.class);
ctx.refresh();
HttpRequestHandlerAdapter adapter = ctx.getBean(HttpRequestHandlerAdapter.class);
assertNotNull(adapter);
ResourceHttpRequestHandler handler = ctx.getBean(ResourceHttpRequestHandler.class);
assertNotNull(handler);
@SuppressWarnings("unchecked")
List<Resource> locations = (List<Resource>) new DirectFieldAccessor(handler).getPropertyValue("locations");
assertNotNull(locations);
assertEquals(2, locations.size());
assertEquals("foo", locations.get(0).getFilename());
assertEquals("bar", locations.get(1).getFilename());
SimpleUrlHandlerMapping mapping = ctx.getBean(SimpleUrlHandlerMapping.class);
assertEquals(1, mapping.getOrder());
assertSame(handler, mapping.getHandlerMap().get("/resources/**"));
}
@Test
public void testInvalidResources() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(InvalidMvcResourcesFeature.class);
try {
ctx.refresh();
fail("Invalid feature spec should not validate");
} catch (RuntimeException e) {
assertTrue(e.getCause().getMessage().contains("Mapping is required"));
// TODO : should all problems be in the message ?
}
}
@FeatureConfiguration
private static class MvcResourcesFeature {
@SuppressWarnings("unused")
@Feature
public MvcResources resources() {
return new MvcResources("/resources/**", new String[] { "/foo", "/bar" }).cachePeriod(86400).order(1);
}
}
@FeatureConfiguration
private static class InvalidMvcResourcesFeature {
@SuppressWarnings("unused")
@Feature
public MvcResources resources() {
return new MvcResources(" ", new String[] {});
}
}
}

View File

@@ -1,131 +0,0 @@
/*
* 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.
* 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.web.servlet.config;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Map;
import org.junit.Test;
import org.springframework.beans.factory.parsing.FailFastProblemReporter;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Feature;
import org.springframework.context.annotation.FeatureConfiguration;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
/**
* Test fixture for {@link MvcViewControllers} feature specification.
* @author Rossen Stoyanchev
* @since 3.1
*/
public class MvcViewControllersTests {
@Test
public void testMvcViewControllers() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MvcViewControllersFeature.class);
ctx.refresh();
SimpleControllerHandlerAdapter adapter = ctx.getBean(SimpleControllerHandlerAdapter.class);
assertNotNull(adapter);
SimpleUrlHandlerMapping handler = ctx.getBean(SimpleUrlHandlerMapping.class);
assertNotNull(handler);
Map<String, ?> urlMap = handler.getUrlMap();
assertNotNull(urlMap);
assertEquals(2, urlMap.size());
ParameterizableViewController controller = (ParameterizableViewController) urlMap.get("/");
assertNotNull(controller);
assertEquals("home", controller.getViewName());
controller = (ParameterizableViewController) urlMap.get("/account");
assertNotNull(controller);
assertNull(controller.getViewName());
}
@Test
public void testEmptyPath() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(EmptyPathViewControllersFeature.class);
try {
ctx.refresh();
fail("expected exception");
} catch (Exception ex) {
assertTrue(ex.getCause().getMessage().contains("path attribute"));
}
}
@Test
public void testEmptyViewName() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(EmptyViewNameViewControllersFeature.class);
try {
ctx.refresh();
fail("expected exception");
} catch (Exception ex) {
assertTrue(ex.getCause().getMessage().contains("not empty"));
}
}
@Test
public void testNullViewName() {
FailFastProblemReporter problemReporter = new FailFastProblemReporter();
assertThat(new MvcViewControllers("/some/path").validate(problemReporter), is(true));
}
@FeatureConfiguration
private static class MvcViewControllersFeature {
@SuppressWarnings("unused")
@Feature
public MvcViewControllers mvcViewControllers() {
return new MvcViewControllers("/", "home").viewController("/account");
}
}
@FeatureConfiguration
private static class EmptyViewNameViewControllersFeature {
@SuppressWarnings("unused")
@Feature
public MvcViewControllers mvcViewControllers() {
return new MvcViewControllers("/some/path", "");
}
}
@FeatureConfiguration
private static class EmptyPathViewControllersFeature {
@SuppressWarnings("unused")
@Feature
public MvcViewControllers mvcViewControllers() {
return new MvcViewControllers("", "someViewName");
}
}
}