@Override public <T> T forwardTo(Class<T> controller) { return use(logic()).forwardTo(controller); }
@Override public <T> T onErrorForwardTo(Class<T> controller) { return onErrorUse(logic()).forwardTo(controller); }
@Test public void shouldNotForwardToMethodsDefaultViewWhenResponseIsCommited() throws Exception { givenDispatcherWillBeReturnedWhenRequested(); when(response.isCommitted()).thenReturn(true); logicResult.forwardTo(MyComponent.class).base(); verify(dispatcher, never()).forward(request, response); }
@Test public void shouldForwardToMethodsDefaultViewWhenResponseIsNotCommited() throws Exception { givenDispatcherWillBeReturnedWhenRequested(); when(response.isCommitted()).thenReturn(false); logicResult.forwardTo(MyComponent.class).base(); verify(dispatcher).forward(request, response); }
@Test public void shouldIncludeReturnValueOnForward() throws Exception { givenDispatcherWillBeReturnedWhenRequested(); when(extractor.nameFor(String.class)).thenReturn("string"); logicResult.forwardTo(MyComponent.class).returnsValue(); verify(dispatcher).forward(request, response); verify(request).setAttribute("string", "A value"); }
@Test public void shouldExecuteTheLogicAndRedirectToItsViewOnForward() throws Exception { final MyComponent component = givenDispatcherWillBeReturnedWhenRequested(); assertThat(component.calls, is(0)); logicResult.forwardTo(MyComponent.class).base(); assertThat(component.calls, is(1)); verify(dispatcher).forward(request, response); }
@Test public void shouldNotWrapValidationExceptionWhenForwarding() throws Exception { exception.expect(ValidationException.class); givenDispatcherWillBeReturnedWhenRequested(); when(response.isCommitted()).thenReturn(true); logicResult.forwardTo(MyComponent.class).throwsValidationException(); }
@Test public void forwardToCustomOnErrorPage() { try { when(logicResult.forwardTo(MyComponent.class)).thenReturn(instance); validator.add(A_MESSAGE); validator.onErrorForwardTo(MyComponent.class).logic(); fail("should stop flow"); } catch (ValidationException e) { verify(instance).logic(); } }
@Override public void forward() throws IllegalStateException { String referer = getReferer(); try { ControllerMethod method = router.parse(referer, HttpMethod.GET, request); executeMethod(method, result.use(logic()).forwardTo(method.getController().getType())); } catch (ControllerNotFoundException | MethodNotAllowedException e) { logger.warn("Could not find or doesn't allowed to get controller method", e); result.use(page()).forwardTo(referer); } }
@Test public void shouldUseValidationVersionOfLogicResult() throws Exception { exception.expect(ValidationException.class); when(result.use(LogicResult.class)).thenReturn(new MockedLogic()); factory.instanceFor(LogicResult.class, errors).forwardTo(RandomComponent.class).random(); }
@Before public void setup() { ResourceBundle bundle = new SafeResourceBundle(ResourceBundle.getBundle("messages")); ValidatorFactory validatorFactory = javax.validation.Validation.buildDefaultValidatorFactory(); javax.validation.Validator bvalidator = validatorFactory.getValidator(); MessageInterpolator interpolator = validatorFactory.getMessageInterpolator(); Proxifier proxifier = new JavassistProxifier(); Messages messages = new Messages(); validator = new DefaultValidator(result, new DefaultValidationViewsFactory(result, proxifier, new DefaultReflectionProvider()), outjector, proxifier, bundle, bvalidator, interpolator, Locale.ENGLISH, messages); when(result.use(LogicResult.class)).thenReturn(logicResult); when(result.use(PageResult.class)).thenReturn(pageResult); when(logicResult.forwardTo(MyComponent.class)).thenReturn(instance); when(pageResult.of(MyComponent.class)).thenReturn(instance); }