use of cn.taketoday.web.handler.method.ActionMappingAnnotationHandler in project today-infrastructure by TAKETODAY.
the class ReturnValueHandlerManagerTests method addHandler.
@Test
void addHandler() {
ReturnValueHandlerManager manager = new ReturnValueHandlerManager();
HttpStatusReturnValueHandler returnValueHandler = new HttpStatusReturnValueHandler();
manager.addHandlers(returnValueHandler);
assertThat(manager.getHandlers()).hasSize(1);
HttpStatusReturnValueHandler highestValueHandler = new HttpStatusReturnValueHandler();
manager.addHandlers(List.of(highestValueHandler));
assertThat(manager.getHandlers()).hasSize(2);
assertThat(manager.getByReturnValue(HttpStatus.OK)).isNotNull();
assertThat(manager.getByReturnValue("")).isNull();
assertThat(manager.getByReturnValue(null)).isNull();
assertThat(manager.getHandler("")).isNull();
// getHandler(handler)
HandlerMethod handler = Mockito.mock(HandlerMethod.class);
Mockito.when(handler.isReturn(HttpStatus.class)).thenReturn(true);
ActionMappingAnnotationHandler annotationHandler = new ActionMappingAnnotationHandler(handler, null, Object.class) {
@Override
public Object getHandlerObject() {
return null;
}
};
assertThat(manager.getHandler(annotationHandler)).isNotNull();
// obtainHandler
assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
// sort
// returnValueHandler.setOrder(2);
// highestValueHandler.setOrder(1);
assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(highestValueHandler).isNotNull();
// returnValueHandler.setOrder(1);
// highestValueHandler.setOrder(2);
assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
assertThatThrownBy(() -> manager.obtainHandler("")).isInstanceOf(ReturnValueHandlerNotFoundException.class).hasMessageStartingWith("No ReturnValueHandler for handler");
}
use of cn.taketoday.web.handler.method.ActionMappingAnnotationHandler in project today-framework by TAKETODAY.
the class HandlerMethodRegistry method buildHandlerMethod.
/**
* Set Action Mapping
*
* @param beanName bean name
* @param method Action or Handler
* @param beanClass Controller
* @param controllerMapping find mapping on class
*/
protected void buildHandlerMethod(String beanName, Method method, Class<?> beanClass, @Nullable MergedAnnotation<ActionMapping> controllerMapping) {
MergedAnnotation<ActionMapping> annotation = MergedAnnotations.from(method).get(ActionMapping.class);
if (annotation.isPresent()) {
// build HandlerMethod
ActionMappingAnnotationHandler handler = createHandler(beanName, beanClass, method);
// do mapping url
mappingHandlerMethod(handler, controllerMapping, annotation);
}
}
use of cn.taketoday.web.handler.method.ActionMappingAnnotationHandler in project today-framework by TAKETODAY.
the class ReturnValueHandlerManagerTests method addHandler.
@Test
void addHandler() {
ReturnValueHandlerManager manager = new ReturnValueHandlerManager();
HttpStatusReturnValueHandler returnValueHandler = new HttpStatusReturnValueHandler();
manager.addHandlers(returnValueHandler);
assertThat(manager.getHandlers()).hasSize(1);
HttpStatusReturnValueHandler highestValueHandler = new HttpStatusReturnValueHandler();
manager.addHandlers(List.of(highestValueHandler));
assertThat(manager.getHandlers()).hasSize(2);
assertThat(manager.getByReturnValue(HttpStatus.OK)).isNotNull();
assertThat(manager.getByReturnValue("")).isNull();
assertThat(manager.getByReturnValue(null)).isNull();
assertThat(manager.getHandler("")).isNull();
// getHandler(handler)
HandlerMethod handler = Mockito.mock(HandlerMethod.class);
Mockito.when(handler.isReturn(HttpStatus.class)).thenReturn(true);
ActionMappingAnnotationHandler annotationHandler = new ActionMappingAnnotationHandler(handler, null, Object.class) {
@Override
public Object getHandlerObject() {
return null;
}
};
assertThat(manager.getHandler(annotationHandler)).isNotNull();
// obtainHandler
assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
// sort
// returnValueHandler.setOrder(2);
// highestValueHandler.setOrder(1);
assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(highestValueHandler).isNotNull();
// returnValueHandler.setOrder(1);
// highestValueHandler.setOrder(2);
assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
assertThatThrownBy(() -> manager.obtainHandler("")).isInstanceOf(ReturnValueHandlerNotFoundException.class).hasMessageStartingWith("No ReturnValueHandler for handler");
}
use of cn.taketoday.web.handler.method.ActionMappingAnnotationHandler in project today-framework by TAKETODAY.
the class ViewControllerHandlerAdapter method invokeHandlerMethod.
protected Object invokeHandlerMethod(final RequestContext context, final ViewController view) throws Throwable {
final ActionMappingAnnotationHandler handler = view.getHandler();
final Object result = handler.invokeHandler(context);
// If return type is void or result is null use xml configuration's resource
if (result == null || handler.getMethod().isReturn(void.class)) {
return view.getResource();
}
handler.handleReturnValue(context, handler, result);
return NONE_RETURN_VALUE;
}
use of cn.taketoday.web.handler.method.ActionMappingAnnotationHandler in project today-infrastructure by TAKETODAY.
the class ViewControllerHandlerAdapter method invokeHandlerMethod.
protected Object invokeHandlerMethod(final RequestContext context, final ViewController view) throws Throwable {
final ActionMappingAnnotationHandler handler = view.getHandler();
final Object result = handler.invokeHandler(context);
// If return type is void or result is null use xml configuration's resource
if (result == null || handler.getMethod().isReturn(void.class)) {
return view.getResource();
}
handler.handleReturnValue(context, handler, result);
return NONE_RETURN_VALUE;
}
Aggregations