forked from microsphere-projects/microsphere-spring
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9852e36
commit 57f8367
Showing
6 changed files
with
279 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
.../test/java/io/microsphere/spring/webmvc/annotation/AbstractEnableWebMvcExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 io.microsphere.spring.webmvc.annotation; | ||
|
||
import io.microsphere.spring.web.event.HandlerMethodArgumentsResolvedEvent; | ||
import io.microsphere.spring.web.event.WebEndpointMappingsReadyEvent; | ||
import io.microsphere.spring.web.metadata.WebEndpointMapping; | ||
import io.microsphere.spring.webmvc.advice.StoringRequestBodyArgumentAdvice; | ||
import io.microsphere.spring.webmvc.advice.StoringResponseBodyReturnValueAdvice; | ||
import io.microsphere.spring.webmvc.controller.TestController; | ||
import io.microsphere.spring.webmvc.interceptor.LazyCompositeHandlerInterceptor; | ||
import io.microsphere.spring.webmvc.metadata.WebEndpointMappingRegistrar; | ||
import io.microsphere.spring.webmvc.method.support.InterceptingHandlerMethodProcessor; | ||
import org.junit.Before; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.web.context.ConfigurableWebApplicationContext; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Collection; | ||
|
||
import static io.microsphere.spring.beans.BeanUtils.isBeanPresent; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; | ||
|
||
/** | ||
* Abstract {@link EnableWebMvcExtension} Test | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy<a/> | ||
* @see EnableWebMvcExtension | ||
* @since 1.0.0 | ||
*/ | ||
@RunWith(SpringRunner.class) | ||
@WebAppConfiguration | ||
@EnableWebMvc | ||
@Ignore | ||
@Import(TestController.class) | ||
abstract class AbstractEnableWebMvcExtensionTest { | ||
|
||
@Autowired | ||
protected ConfigurableWebApplicationContext wac; | ||
|
||
protected MockMvc mockMvc; | ||
|
||
protected boolean registerWebEndpointMappings; | ||
|
||
protected boolean interceptHandlerMethods; | ||
|
||
protected boolean publishEvents; | ||
|
||
protected boolean registerHandlerInterceptors; | ||
|
||
protected boolean storeRequestBodyArgument; | ||
|
||
protected boolean storeResponseBodyReturnValue; | ||
|
||
@Before | ||
public void setup() { | ||
this.mockMvc = webAppContextSetup(this.wac).build(); | ||
EnableWebMvcExtension enableWebMvcExtension = this.getClass().getAnnotation(EnableWebMvcExtension.class); | ||
this.registerWebEndpointMappings = enableWebMvcExtension.registerWebEndpointMappings(); | ||
this.interceptHandlerMethods = enableWebMvcExtension.interceptHandlerMethods(); | ||
this.publishEvents = enableWebMvcExtension.publishEvents(); | ||
this.registerHandlerInterceptors = enableWebMvcExtension.registerHandlerInterceptors(); | ||
this.storeRequestBodyArgument = enableWebMvcExtension.storeRequestBodyArgument(); | ||
this.storeResponseBodyReturnValue = enableWebMvcExtension.storeResponseBodyReturnValue(); | ||
} | ||
|
||
@Test | ||
public void testRegisteredBeans() { | ||
assertTrue(isBeanPresent(this.wac, WebMvcExtensionConfiguration.class)); | ||
assertEquals(this.registerWebEndpointMappings, isBeanPresent(this.wac, WebEndpointMappingRegistrar.class)); | ||
assertEquals(this.interceptHandlerMethods, this.wac.containsBean(InterceptingHandlerMethodProcessor.BEAN_NAME)); | ||
assertEquals(this.interceptHandlerMethods, isBeanPresent(this.wac, InterceptingHandlerMethodProcessor.class)); | ||
assertEquals(this.registerHandlerInterceptors, isBeanPresent(this.wac, LazyCompositeHandlerInterceptor.class)); | ||
assertEquals(this.storeRequestBodyArgument, isBeanPresent(this.wac, StoringRequestBodyArgumentAdvice.class)); | ||
assertEquals(this.storeResponseBodyReturnValue, isBeanPresent(this.wac, StoringResponseBodyReturnValueAdvice.class)); | ||
} | ||
|
||
@Test | ||
public void test() throws Exception { | ||
this.mockMvc.perform(get("/echo/hello")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json("[ECHO] : hello")); | ||
} | ||
|
||
/** | ||
* Test only one mapping : {@link TestController#echo(String)} | ||
* | ||
* @param event {@link WebEndpointMappingsReadyEvent} | ||
*/ | ||
@EventListener(WebEndpointMappingsReadyEvent.class) | ||
public void onWebEndpointMappingsReadyEvent(WebEndpointMappingsReadyEvent event) { | ||
// Only TestController | ||
Collection<WebEndpointMapping> mappings = event.getMappings(); | ||
assertEquals(1, mappings.size()); | ||
WebEndpointMapping webEndpointMapping = mappings.iterator().next(); | ||
String[] patterns = webEndpointMapping.getPatterns(); | ||
assertEquals(1, patterns.length); | ||
assertEquals("/echo/{message}", patterns[0]); | ||
} | ||
|
||
/** | ||
* Test only one method : {@link TestController#echo(String)} | ||
* | ||
* @param event {@link HandlerMethodArgumentsResolvedEvent} | ||
*/ | ||
@EventListener(HandlerMethodArgumentsResolvedEvent.class) | ||
public void onHandlerMethodArgumentsResolvedEvent(HandlerMethodArgumentsResolvedEvent event) { | ||
Method method = event.getMethod(); | ||
assertEquals("echo", method.getName()); | ||
assertEquals(String.class, method.getReturnType()); | ||
|
||
Class<?>[] parameterTypes = method.getParameterTypes(); | ||
assertEquals(1, parameterTypes.length); | ||
assertEquals(String.class, parameterTypes[0]); | ||
|
||
HandlerMethod handlerMethod = event.getHandlerMethod(); | ||
assertNotNull(handlerMethod); | ||
|
||
Object bean = handlerMethod.getBean(); | ||
assertNotNull(bean); | ||
assertEquals(TestController.class, bean.getClass()); | ||
assertEquals(method, handlerMethod.getMethod()); | ||
|
||
Object[] arguments = event.getArguments(); | ||
assertEquals(1, arguments.length); | ||
assertEquals("hello", arguments[0]); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
.../test/java/io/microsphere/spring/webmvc/annotation/EnableWebMvcExtensionDefaultsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 io.microsphere.spring.webmvc.annotation; | ||
|
||
import io.microsphere.spring.web.event.HandlerMethodArgumentsResolvedEvent; | ||
import io.microsphere.spring.web.event.WebEndpointMappingsReadyEvent; | ||
import io.microsphere.spring.web.metadata.WebEndpointMapping; | ||
import io.microsphere.spring.webmvc.advice.StoringRequestBodyArgumentAdvice; | ||
import io.microsphere.spring.webmvc.advice.StoringResponseBodyReturnValueAdvice; | ||
import io.microsphere.spring.webmvc.controller.TestController; | ||
import io.microsphere.spring.webmvc.interceptor.LazyCompositeHandlerInterceptor; | ||
import io.microsphere.spring.webmvc.metadata.WebEndpointMappingRegistrar; | ||
import io.microsphere.spring.webmvc.method.support.InterceptingHandlerMethodProcessor; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.web.context.ConfigurableWebApplicationContext; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Collection; | ||
|
||
import static io.microsphere.spring.beans.BeanUtils.isBeanPresent; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; | ||
|
||
/** | ||
* {@link EnableWebMvcExtension} Test with defaults | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy<a/> | ||
* @see EnableWebMvcExtension | ||
* @since 1.0.0 | ||
*/ | ||
@ContextConfiguration(classes = { | ||
EnableWebMvcExtensionDefaultsTest.class | ||
}) | ||
@EnableWebMvcExtension | ||
public class EnableWebMvcExtensionDefaultsTest extends AbstractEnableWebMvcExtensionTest { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,12 @@ | |
*/ | ||
package io.microsphere.spring.webmvc.annotation; | ||
|
||
import io.microsphere.spring.webmvc.advice.StoringRequestBodyArgumentAdvice; | ||
import io.microsphere.spring.webmvc.advice.StoringResponseBodyReturnValueAdvice; | ||
import io.microsphere.spring.webmvc.controller.TestController; | ||
import io.microsphere.spring.webmvc.interceptor.LazyCompositeHandlerInterceptor; | ||
import io.microsphere.spring.webmvc.metadata.WebEndpointMappingRegistrar; | ||
import io.microsphere.spring.webmvc.method.support.InterceptingHandlerMethodProcessor; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
@@ -24,38 +30,31 @@ | |
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import org.springframework.web.context.ConfigurableWebApplicationContext; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
|
||
import static io.microsphere.spring.beans.BeanUtils.isBeanPresent; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; | ||
|
||
/** | ||
* {@link EnableWebMvcExtension} Test with defaults | ||
* {@link EnableWebMvcExtension} Test with disable features | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy<a/> | ||
* @see EnableWebMvcExtension | ||
* @since 1.0.0 | ||
*/ | ||
@RunWith(SpringRunner.class) | ||
@WebAppConfiguration | ||
@ContextConfiguration(classes = { | ||
EnableWebMvcExtensionDefaultTest.class | ||
EnableWebMvcExtensionDisableTest.class | ||
}) | ||
@EnableWebMvc | ||
@EnableWebMvcExtension | ||
public class EnableWebMvcExtensionDefaultTest { | ||
|
||
@Autowired | ||
private WebApplicationContext wac; | ||
|
||
private MockMvc mockMvc; | ||
|
||
@Before | ||
public void setup() { | ||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
|
||
} | ||
@EnableWebMvcExtension( | ||
registerWebEndpointMappings = false, | ||
interceptHandlerMethods = false, | ||
publishEvents = false | ||
) | ||
public class EnableWebMvcExtensionDisableTest extends AbstractEnableWebMvcExtensionTest { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,12 @@ | |
*/ | ||
package io.microsphere.spring.webmvc.annotation; | ||
|
||
import io.microsphere.spring.webmvc.advice.StoringRequestBodyArgumentAdvice; | ||
import io.microsphere.spring.webmvc.advice.StoringResponseBodyReturnValueAdvice; | ||
import io.microsphere.spring.webmvc.controller.TestController; | ||
import io.microsphere.spring.webmvc.interceptor.LazyCompositeHandlerInterceptor; | ||
import io.microsphere.spring.webmvc.metadata.WebEndpointMappingRegistrar; | ||
import io.microsphere.spring.webmvc.method.support.InterceptingHandlerMethodProcessor; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
@@ -24,42 +30,30 @@ | |
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import org.springframework.web.context.ConfigurableWebApplicationContext; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
|
||
import static io.microsphere.spring.beans.BeanUtils.isBeanPresent; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; | ||
|
||
/** | ||
* {@link EnableWebMvcExtension} Test | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy<a/> | ||
* @see EnableWebMvcExtension | ||
* @since 1.0.0 | ||
*/ | ||
@RunWith(SpringRunner.class) | ||
@WebAppConfiguration | ||
@ContextConfiguration(classes = { | ||
EnableWebMvcExtensionTest.class | ||
}) | ||
@EnableWebMvc | ||
@EnableWebMvcExtension( | ||
registerHandlerInterceptors = true, | ||
storeRequestBodyArgument = true, | ||
storeResponseBodyReturnValue = true | ||
) | ||
public class EnableWebMvcExtensionTest { | ||
|
||
@Autowired | ||
private WebApplicationContext wac; | ||
|
||
private MockMvc mockMvc; | ||
|
||
@Before | ||
public void setup() { | ||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
|
||
} | ||
public class EnableWebMvcExtensionTest extends AbstractEnableWebMvcExtensionTest { | ||
} |