FieldValidationSupportImpl implements FieldValidationSupport { public Object parseClient(String clientValue, ComponentResources componentResources, FieldTranslator<Object> translator, NullFieldStrategy nullFieldStrategy) throws ValidationException { assert componentResources != null; assert translator != null; assert nullFieldStrategy != null; String effectiveValue = clientValue; if (InternalUtils.isBlank(effectiveValue)) { effectiveValue = nullFieldStrategy.replaceFromClient(); if (effectiveValue == null) return null; } final Holder<Object> resultHolder = Holder.create(); ComponentEventCallback callback = new ComponentEventCallback() { public boolean handleResult(Object result) { resultHolder.put(result); return true; } }; try { componentResources.triggerEvent(EventConstants.PARSE_CLIENT, new Object[] {effectiveValue}, callback); } catch (RuntimeException ex) { rethrowValidationException(ex); } if (resultHolder.hasValue()) return resultHolder.get(); return translator.parse(effectiveValue); } FieldValidationSupportImpl(TypeCoercer typeCoercer, PropertyAccess propertyAccess); String toClient(Object value, ComponentResources componentResources, FieldTranslator<Object> translator,
                           NullFieldStrategy nullFieldStrategy); Object parseClient(String clientValue, ComponentResources componentResources,
                              FieldTranslator<Object> translator, NullFieldStrategy nullFieldStrategy); @SuppressWarnings( {"unchecked"}) void validate(Object value, ComponentResources componentResources, FieldValidator validator);  }
@SuppressWarnings({"unchecked"}) @Test public void parse_client_via_event() throws ValidationException { ComponentResources resources = mockComponentResources(); FieldTranslator translator = mockFieldTranslator(); NullFieldStrategy nullFieldStrategy = mockNullFieldStrategy(); String clientValue = "abracadabra"; IAnswer answer = new IAnswer() { @SuppressWarnings({"unchecked"}) public Object answer() throws Throwable { Object[] args = EasyMock.getCurrentArguments(); Object[] context = (Object[]) args[1]; ComponentEventCallback handler = (ComponentEventCallback) args[2]; return handler.handleResult(context[0].toString().toUpperCase()); } }; EasyMock.expect(resources.triggerEvent(EasyMock.eq(EventConstants.PARSE_CLIENT), EasyMock.isA(Object[].class), EasyMock.isA(ComponentEventCallback.class))).andAnswer(answer); replay(); FieldValidationSupport support = new FieldValidationSupportImpl(typeCoercer, propertyAccess); Object actual = support.parseClient(clientValue, resources, translator, nullFieldStrategy); assertEquals(actual, clientValue.toUpperCase()); verify(); }
@Test public void parse_client_for_null_value_returns_null_and_bypasses_events_and_translator() throws Exception { ComponentResources resources = mockComponentResources(); FieldTranslator translator = mockFieldTranslator(); NullFieldStrategy nullFieldStrategy = mockNullFieldStrategy(); String clientValue = ""; train_replaceFromClient(nullFieldStrategy, ""); ignoreEvent(resources, EventConstants.PARSE_CLIENT, clientValue); expect(translator.parse(clientValue)).andReturn(""); replay(); FieldValidationSupport support = new FieldValidationSupportImpl(typeCoercer, propertyAccess); Object actual = support.parseClient(clientValue, resources, translator, nullFieldStrategy); assertEquals(actual, ""); verify(); }
@SuppressWarnings({"ThrowableInstanceNeverThrown"}) @Test public void parse_client_event_handler_throws_validation_exception() throws Exception { ComponentResources resources = mockComponentResources(); FieldTranslator translator = mockFieldTranslator(); ValidationException ve = new ValidationException("Just didn't feel right."); NullFieldStrategy nullFieldStrategy = mockNullFieldStrategy(); String clientValue = "abracadabra"; EasyMock.expect(resources.triggerEvent(EasyMock.eq(EventConstants.PARSE_CLIENT), EasyMock.isA(Object[].class), EasyMock.isA(ComponentEventCallback.class))).andThrow( new RuntimeException(ve)); replay(); FieldValidationSupport support = new FieldValidationSupportImpl(typeCoercer, propertyAccess); try { support.parseClient(clientValue, resources, translator, nullFieldStrategy); unreachable(); } catch (ValidationException ex) { assertSame(ex, ve); } verify(); }
@SuppressWarnings({"ThrowableInstanceNeverThrown"}) @Test public void parse_client_event_handler_fails_with_other_exception() throws Exception { ComponentResources resources = mockComponentResources(); FieldTranslator translator = mockFieldTranslator(); RuntimeException re = new RuntimeException("Just didn't feel right."); NullFieldStrategy nullFieldStrategy = mockNullFieldStrategy(); String clientValue = "abracadabra"; EasyMock.expect(resources.triggerEvent(EasyMock.eq(EventConstants.PARSE_CLIENT), EasyMock.isA(Object[].class), EasyMock.isA(ComponentEventCallback.class))).andThrow(re); replay(); FieldValidationSupport support = new FieldValidationSupportImpl(typeCoercer, propertyAccess); try { support.parseClient(clientValue, resources, translator, nullFieldStrategy); unreachable(); } catch (RuntimeException ex) { assertSame(ex, re); } verify(); }
@Test public void parse_client_via_translator() throws ValidationException { ComponentResources resources = mockComponentResources(); FieldTranslator translator = mockFieldTranslator(); NullFieldStrategy nullFieldStrategy = mockNullFieldStrategy(); String clientValue = "abracadabra"; ignoreEvent(resources, EventConstants.PARSE_CLIENT, clientValue); expect(translator.parse(clientValue)).andReturn("foobar"); replay(); FieldValidationSupport support = new FieldValidationSupportImpl(typeCoercer, propertyAccess); Object actual = support.parseClient(clientValue, resources, translator, nullFieldStrategy); assertEquals(actual, "foobar"); verify(); }