Search in sources :

Example 1 with DefaultParameterNameDiscoverer

use of cn.taketoday.core.DefaultParameterNameDiscoverer in project today-framework by TAKETODAY.

the class HandlerMethodTests method testSimple.

@Test
public void testSimple() throws Throwable {
    final Method method = HandlerMethodTests.class.getDeclaredMethod("method", String.class);
    final HandlerMethod handlerMethod = HandlerMethod.from(method);
    DefaultParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
    handlerMethod.initParameterNameDiscovery(parameterNameDiscoverer);
    assertThat(handlerMethod.getMethod()).isEqualTo(method);
    assertThat(handlerMethod.getParameters()).hasSize(1);
    assertThat(handlerMethod.getReturnType()).isEqualTo(method.getReturnType()).isEqualTo(void.class);
    assertThat(handlerMethod.getContentType()).isNull();
    // produce
    final Method produce = HandlerMethodTests.class.getDeclaredMethod("produce", String.class);
    final HandlerMethod produceMethod = HandlerMethod.from(produce);
    assertThat(produceMethod.getContentType()).isEqualTo(MediaType.APPLICATION_JSON_VALUE);
    produceMethod.initParameterNameDiscovery(parameterNameDiscoverer);
    ApplicationContext applicationContext = getApplicationContext();
    final ParameterResolvingRegistryResolvableParameterFactory methodParameterBuilder = new ParameterResolvingRegistryResolvableParameterFactory();
    final ParameterResolvingRegistry resolversRegistry = methodParameterBuilder.getResolvingRegistry();
    resolversRegistry.setApplicationContext(applicationContext);
    resolversRegistry.registerDefaultStrategies();
    setResultHandlers(produceMethod, applicationContext);
    assertThat(produceMethod).isNotEqualTo(handlerMethod);
    assertThat(produceMethod).hasToString("HandlerMethodTests#produce(String name)");
}
Also used : AnnotationConfigServletWebApplicationContext(cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) DefaultParameterNameDiscoverer(cn.taketoday.core.DefaultParameterNameDiscoverer) Method(java.lang.reflect.Method) ParameterResolvingRegistry(cn.taketoday.web.bind.resolver.ParameterResolvingRegistry) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultParameterNameDiscoverer

use of cn.taketoday.core.DefaultParameterNameDiscoverer in project today-framework by TAKETODAY.

the class ExpressionValueMethodArgumentResolverTests method setUp.

@BeforeEach
@SuppressWarnings("resource")
public void setUp() throws Exception {
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.refresh();
    resolver = new ExpressionValueMethodArgumentResolver(context.getBeanFactory());
    DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
    Method method = getClass().getMethod("params", int.class, String.class, String.class);
    paramSystemProperty = new ResolvableMethodParameter(new MethodParameter(method, 0));
    paramContextPath = new ResolvableMethodParameter(new MethodParameter(method, 1));
    paramNotSupported = new ResolvableMethodParameter(new MethodParameter(method, 2));
    paramSystemProperty.getParameter().initParameterNameDiscovery(discoverer);
    paramContextPath.getParameter().initParameterNameDiscovery(discoverer);
    paramNotSupported.getParameter().initParameterNameDiscovery(discoverer);
    webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
    // Expose request to the current thread (for SpEL expressions)
    RequestContextHolder.set(webRequest);
}
Also used : ExpressionValueMethodArgumentResolver(cn.taketoday.web.bind.resolver.ExpressionValueMethodArgumentResolver) DefaultParameterNameDiscoverer(cn.taketoday.core.DefaultParameterNameDiscoverer) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) Method(java.lang.reflect.Method) MethodParameter(cn.taketoday.core.MethodParameter) GenericWebApplicationContext(cn.taketoday.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

DefaultParameterNameDiscoverer (cn.taketoday.core.DefaultParameterNameDiscoverer)2 Method (java.lang.reflect.Method)2 ApplicationContext (cn.taketoday.context.ApplicationContext)1 MethodParameter (cn.taketoday.core.MethodParameter)1 AnnotationConfigServletWebApplicationContext (cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebApplicationContext)1 ExpressionValueMethodArgumentResolver (cn.taketoday.web.bind.resolver.ExpressionValueMethodArgumentResolver)1 ParameterResolvingRegistry (cn.taketoday.web.bind.resolver.ParameterResolvingRegistry)1 GenericWebApplicationContext (cn.taketoday.web.context.support.GenericWebApplicationContext)1 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)1 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1