Refine search
public void testFactoryFailsWithMissingBinding() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(ColoredCarFactory.class) .toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class)); } }); fail(); } catch (CreationException expected) { assertContains( expected.getMessage(), "Could not find a suitable constructor in java.lang.Double.", "at " + ColoredCarFactory.class.getName() + ".create(FactoryProvider2Test.java"); } }
public void testFactoryFailsWithMissingBinding() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(ColoredCarFactory.class) .toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class)); } }); fail(); } catch (CreationException expected) { assertContains( expected.getMessage(), "1) Parameter of type 'double' is not injectable or annotated with @Assisted"); } }
public void testFactoryFailsWithMissingBindingInToolStage() { try { Guice.createInjector( Stage.TOOL, new AbstractModule() { @Override protected void configure() { bind(ColoredCarFactory.class) .toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class)); } }); fail(); } catch (CreationException expected) { assertContains( expected.getMessage(), "Could not find a suitable constructor in java.lang.Double.", "at " + ColoredCarFactory.class.getName() + ".create(FactoryProvider2Test.java"); } }
public void testNoMatchingConstructorsBecauseTooManyParams() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { install(new FactoryModuleBuilder().build(ComplexFactory.class)); } }); fail("should have failed"); } catch (CreationException expected) { Asserts.assertContains( expected.getMessage(), "1) " + Foo.class.getName() + " has @AssistedInject constructors, but none of them match the parameters in method " + ComplexFactory.class.getName()); } }
public void testNoMatchingConstrucotsBecauseTooLittleParams() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { install(new FactoryModuleBuilder().build(NullFactory.class)); } }); fail("should have failed"); } catch (CreationException expected) { Asserts.assertContains( expected.getMessage(), "1) " + Foo.class.getName() + " has @AssistedInject constructors, but none of them match the parameters in method " + NullFactory.class.getName()); } }
public void testAssistedInjectConstructorAndAssistedFactoryParameterMustNotMix() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(Double.class).toInstance(5.0d); bind(AssistedParamsFactory.class) .toProvider( FactoryProvider.newFactory(AssistedParamsFactory.class, Mustang.class)); } }); fail(); } catch (CreationException expected) { assertContains( expected.getMessage(), "Factory method " + AssistedParamsFactory.class.getName() + ".create() has an @Assisted parameter, which " + "is incompatible with the deprecated @AssistedInject annotation."); } }
public void testTooManyMatchingConstructors() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { install( new FactoryModuleBuilder() .implement(Foo.class, TooManyMatches.class) .build(SimpleFactory2.class)); } }); fail("should have failed"); } catch (CreationException expected) { Asserts.assertContains( expected.getMessage(), "1) " + TooManyMatches.class.getName() + " has more than one constructor annotated with @AssistedInject that " + "matches the parameters in method " + SimpleFactory2.class.getName()); } }
public void testImplicitForwardingAssistedBindingFailsWithAbstractClass() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(AbstractCar.class).to(ArtCar.class); install(new FactoryModuleBuilder().build(ColoredAbstractCarFactory.class)); } }); fail(); } catch (CreationException ce) { assertContains( ce.getMessage(), "1) " + AbstractCar.class.getName() + " is abstract, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + AbstractCar.class.getName(), "at " + ColoredAbstractCarFactory.class.getName() + ".create("); assertEquals(1, ce.getErrorMessages().size()); } }
public void testDuplicateKeys() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(DoubleToneCarFactory.class) .toProvider(FactoryProvider.newFactory(DoubleToneCarFactory.class, Maxima.class)); } }); fail(); } catch (CreationException expected) { assertContains( expected.getMessage(), "A binding to " + Color.class.getName() + " annotated with @" + Assisted.class.getName() + "(value=" + Annotations.memberValueString("paint") + ") was already configured at"); } }
fail(); } catch (CreationException expected) { assertEquals(expected.getMessage(), 4, expected.getErrorMessages().size()); assertContains( expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [1] with key" + ProviderBasedColoredCarFactory.class.getName() + ".createCar()]"); assertContains( expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [2] with key" + ProviderBasedColoredCarFactory.class.getName() + ".createCar()]"); assertContains( expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [1] with key" + ".createMustang()]"); assertContains( expected.getMessage(), ") No implementation for com.google.inject.assistedinject." + "FactoryProvider2Test$ProviderBasedColoredCarFactory was bound.");
public void testImplicitForwardingAssistedBindingFailsWithInterface() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(Car.class).to(Golf.class); install(new FactoryModuleBuilder().build(ColoredCarFactory.class)); } }); fail(); } catch (CreationException ce) { assertContains( ce.getMessage(), "1) " + Car.class.getName() + " is an interface, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + Car.class.getName(), "at " + ColoredCarFactory.class.getName() + ".create("); assertEquals(1, ce.getErrorMessages().size()); } }
public void testExplicitForwardingAssistedBindingFailsWithInterface() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(Volkswagen.class).to(Golf.class); install( new FactoryModuleBuilder() .implement(Car.class, Volkswagen.class) .build(ColoredCarFactory.class)); } }); fail(); } catch (CreationException ce) { assertContains( ce.getMessage(), "1) " + Volkswagen.class.getName() + " is an interface, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + Volkswagen.class.getName(), "while locating " + Car.class.getName(), "at " + ColoredCarFactory.class.getName() + ".create("); assertEquals(1, ce.getErrorMessages().size()); } }
fail(); } catch (CreationException expected) { assertEquals(expected.getMessage(), 4, expected.getErrorMessages().size()); assertContains( expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [1] with key" + JavaxProviderBasedColoredCarFactory.class.getName() + ".createCar()]"); assertContains( expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [2] with key" + JavaxProviderBasedColoredCarFactory.class.getName() + ".createCar()]"); assertContains( expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [1] with key" + ".createMustang()]"); assertContains( expected.getMessage(), ") No implementation for com.google.inject.assistedinject." + "FactoryProvider2Test$JavaxProviderBasedColoredCarFactory was bound.");
public void testSimilarBindingsWithConflictingImplementations() { try { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { install( new FactoryModuleBuilder() .implement(Car.class, Mustang.class) .build(ColoredCarFactory.class)); install( new FactoryModuleBuilder() .implement(Car.class, Golf.class) .build(ColoredCarFactory.class)); } }); injector.getInstance(ColoredCarFactory.class); fail(); } catch (CreationException ce) { assertContains( ce.getMessage(), "A binding to " + ColoredCarFactory.class.getName() + " was already configured"); assertEquals(1, ce.getErrorMessages().size()); } }
public void testExplicitForwardingAssistedBindingFailsWithAbstractClass() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(AbstractCar.class).to(ArtCar.class); install( new FactoryModuleBuilder() .implement(Car.class, AbstractCar.class) .build(ColoredCarFactory.class)); } }); fail(); } catch (CreationException ce) { assertContains( ce.getMessage(), "1) " + AbstractCar.class.getName() + " is abstract, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + AbstractCar.class.getName(), "while locating " + Car.class.getName(), "at " + ColoredCarFactory.class.getName() + ".create("); assertEquals(1, ce.getErrorMessages().size()); } }
/** * Our factories aren't reusable across injectors. Although this behaviour isn't something we * like, I have a test case to make sure the error message is pretty. */ public void testFactoryReuseErrorMessageIsPretty() { final Provider<ColoredCarFactory> factoryProvider = FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class); Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(Double.class).toInstance(5.0d); bind(ColoredCarFactory.class).toProvider(factoryProvider); } }); try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(Double.class).toInstance(5.0d); bind(ColoredCarFactory.class).toProvider(factoryProvider); } }); fail(); } catch (CreationException expected) { assertContains( expected.getMessage(), "Factories.create() factories may only be used in one Injector!"); } }