@Test(expected = InvalidTypeException.class)
  public void ifNoAbleInstantiatorIsFoundWillThrowAnException() {
    final MultiInstantiator mutliInstantiator = new MultiInstantiator(theMockInstantiator);

    context.checking(
        new Expectations() {
          {
            allowing(mockSubInstantiator).isAbleToInstantiate(with(any(Target.class)));
            will(returnValue(false));
          }
        });

    mutliInstantiator.instantiate(Target.create(Object.class, ""), null);
  }
  @Test
  public void willCreateATargerForEachFormalParameterDeclaredByTheMethod() throws Exception {
    final ControllerMethod buyAHouse = buyA;
    requestParameterIs(buyAHouse, "house", "");

    final InstantiatorWithErrors mockInstantiator = mock(InstantiatorWithErrors.class);
    IogiParametersProvider iogiProvider =
        new IogiParametersProvider(nameProvider, request, mockInstantiator);
    final Target<House> expectedTarget = Target.create(House.class, "house");

    iogiProvider.getParametersFor(buyAHouse, errors);

    verify(mockInstantiator).instantiate(eq(expectedTarget), any(Parameters.class), eq(errors));
  }
  @Test
  public void multiInstantiatorWillUseAnAbleInstantiator() throws Exception {
    final MultiInstantiator multiInstantiator = new MultiInstantiator(theMockInstantiator);

    context.checking(
        new Expectations() {
          {
            allowing(mockSubInstantiator).isAbleToInstantiate(with(any(Target.class)));
            will(returnValue(true));

            one(mockSubInstantiator)
                .instantiate(with(any(Target.class)), (with(any(Parameters.class))));
          }
        });

    multiInstantiator.instantiate(Target.create(Object.class, ""), null);
  }
 public boolean isAbleToInstantiate(Target<?> target) {
   Object value = request.getAttribute(target.getName());
   return value != null && target.getClassType().isInstance(value);
 }
 public Object instantiate(Target<?> target, Parameters params) {
   return request.getAttribute(target.getName());
 }
 private <T> T convertWith(
     final TypeConverter<T> instantiator, final Class<T> type, final String stringValue) {
   final Target<T> target = Target.create(type, "foo");
   assertTrue(instantiator.isAbleToInstantiate(target));
   return instantiator.instantiate(target, new Parameters(new Parameter("foo", stringValue)));
 }
 public boolean isAbleToInstantiate(final Target<?> target) {
   return target.getClassType() == float.class;
 }
Example #8
0
 private Object instantiateArrayElement(final Parameters parameters) {
   final Target<?> elementTarget = arrayTarget.arrayElementTarget();
   return elementInstantiator.instantiate(elementTarget, parameters);
 }
Example #9
0
 private Object makeArray() {
   final int arrayLength = parametersByIndex.count();
   return Array.newInstance(arrayTarget.arrayElementType(), arrayLength);
 }
Example #10
0
 public boolean isAbleToInstantiate(final Target<?> target) {
   return target.getClassType().isArray();
 }