MvcAnnotationDrivenFeatureTests.java 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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;

22 23
import java.util.List;

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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;
39
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
40
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodAdapter;
41
import org.springframework.web.servlet.mvc.method.annotation.support.ServletWebArgumentResolverAdapter;
42 43 44 45 46 47 48 49 50

/**
 * Integration tests for the {@link MvcAnnotationDriven} feature specification.
 * @author Rossen Stoyanchev
 * @author Chris Beams
 * @since 3.1
 */
public class MvcAnnotationDrivenFeatureTests {

51
	@SuppressWarnings("unchecked")
52 53 54 55 56
	@Test
	public void testMessageCodesResolver() {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.register(MvcFeature.class, MvcBeans.class);
		ctx.refresh();
57
		RequestMappingHandlerMethodAdapter adapter = ctx.getBean(RequestMappingHandlerMethodAdapter.class);
58 59 60 61 62 63
		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]);
64 65 66 67 68 69
		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);
70 71
		Object converters = new DirectFieldAccessor(adapter).getPropertyValue("messageConverters");
		assertNotNull(converters);
72 73 74
		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);
75 76 77 78 79 80 81 82 83 84 85 86 87
	}

}

@FeatureConfiguration
class MvcFeature {
	@Feature
	public MvcAnnotationDriven annotationDriven(MvcBeans mvcBeans) {
		return new MvcAnnotationDriven()
			.conversionService(mvcBeans.conversionService())
			.messageCodesResolver(mvcBeans.messageCodesResolver())
			.validator(mvcBeans.validator())
			.messageConverters(new StringHttpMessageConverter())
88 89
			.argumentResolvers(new TestWebArgumentResolver())
			.argumentResolvers(new TestHandlerMethodArgumentResolver());
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	}
}

@Configuration
class MvcBeans {
	@Bean
	public FormattingConversionService conversionService() {
		return new DefaultFormattingConversionService();
	}
	@Bean
	public Validator validator() {
		return new LocalValidatorFactoryBean();
	}
	@Bean MessageCodesResolver messageCodesResolver() {
		return new TestMessageCodesResolver();
	}
}