Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Jan 10, 2025
1 parent 9852e36 commit 57f8367
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 45 deletions.
9 changes: 9 additions & 0 deletions microsphere-spring-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<logback.version>1.2.12</logback.version>
<junit.version>4.13.2</junit.version>
<mockito.version>4.11.0</mockito.version>
<jsonassert.version>1.5.3</jsonassert.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -99,6 +100,14 @@
<scope>import</scope>
</dependency>

<!-- JSONasserta -->
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>${jsonassert.version}</version>
</dependency>

<!-- Logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions microsphere-spring-webmvc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
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]);
}
}
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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
}

0 comments on commit 57f8367

Please sign in to comment.