Search in sources :

Example 66 with CreationException

use of com.google.inject.CreationException in project roboguice by roboguice.

the class FactoryProvider2Test method testAssistedJavaxProviderIsDisallowed.

public void testAssistedJavaxProviderIsDisallowed() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(JavaxProviderBasedColoredCarFactory.class).toProvider(FactoryProvider.newFactory(JavaxProviderBasedColoredCarFactory.class, Subaru.class));
            }
        });
        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" + " [com.google.inject.Provider<" + Color.class.getName() + ">] on method [" + 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" + " [com.google.inject.Provider<java.lang.String>] on method [" + 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" + " [com.google.inject.Provider<" + Color.class.getName() + ">" + " annotated with @com.google.inject.assistedinject.Assisted(value=color)]" + " on method [" + JavaxProviderBasedColoredCarFactory.class.getName() + ".createMustang()]");
        assertContains(expected.getMessage(), ") No implementation for com.google.inject.assistedinject." + "FactoryProvider2Test$JavaxProviderBasedColoredCarFactory was bound.");
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 67 with CreationException

use of com.google.inject.CreationException in project roboguice by roboguice.

the class FactoryProvider2Test method testFactoryFailsWithMissingBinding.

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");
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 68 with CreationException

use of com.google.inject.CreationException in project roboguice by roboguice.

the class FactoryProvider2Test method testAssistedProviderIsDisallowed.

public void testAssistedProviderIsDisallowed() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(ProviderBasedColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ProviderBasedColoredCarFactory.class, Subaru.class));
            }
        });
        fail();
    } catch (CreationException expected) {
        assertEquals(expected.getMessage(), 4, expected.getErrorMessages().size());
        // Assert each method individually, because JDK7 doesn't guarantee method ordering.
        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" + " [com.google.inject.Provider<" + Color.class.getName() + ">] on method [" + 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" + " [com.google.inject.Provider<java.lang.String>] on method [" + 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" + " [com.google.inject.Provider<" + Color.class.getName() + ">" + " annotated with @com.google.inject.assistedinject.Assisted(value=color)]" + " on method [" + ProviderBasedColoredCarFactory.class.getName() + ".createMustang()]");
        assertContains(expected.getMessage(), ") No implementation for com.google.inject.assistedinject." + "FactoryProvider2Test$ProviderBasedColoredCarFactory was bound.");
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 69 with CreationException

use of com.google.inject.CreationException in project roboguice by roboguice.

the class OverrideModuleTest method testFailsIfOverridenScopeInstanceHasBeenUsed.

public void testFailsIfOverridenScopeInstanceHasBeenUsed() {
    final Scope scope = new Scope() {

        public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
            return unscoped;
        }

        @Override
        public String toString() {
            return "ORIGINAL SCOPE";
        }
    };
    final Module original = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, scope);
            bind(Date.class).in(scope);
            bind(String.class).in(scope);
        }
    };
    Module originalWrapper = new AbstractModule() {

        @Override
        protected void configure() {
            install(original);
        }
    };
    Module replacements = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, new SingleUseScope());
        }
    };
    try {
        createInjector(Modules.override(originalWrapper).with(replacements));
        fail("Exception expected");
    } catch (CreationException e) {
        assertContains(e.getMessage(), "1) The scope for @TestScopeAnnotation is bound directly and cannot be overridden.", "original binding at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "at ", replacements.getClass().getName() + ".configure(", asModuleChain(Modules.OverrideModule.class, replacements.getClass()));
    }
}
Also used : Scope(com.google.inject.Scope) Modules(com.google.inject.util.Modules) CreationException(com.google.inject.CreationException) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) Key(com.google.inject.Key) Date(java.util.Date) Provider(com.google.inject.Provider) AbstractModule(com.google.inject.AbstractModule)

Example 70 with CreationException

use of com.google.inject.CreationException in project roboguice by roboguice.

the class MapBinderTest method testMapBinderMapForbidsNullKeys.

public void testMapBinderMapForbidsNullKeys() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                MapBinder.newMapBinder(binder(), String.class, String.class).addBinding(null);
            }
        });
        fail();
    } catch (CreationException expected) {
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Aggregations

CreationException (com.google.inject.CreationException)169 AbstractModule (com.google.inject.AbstractModule)163 Module (com.google.inject.Module)26 RemoteException (java.rmi.RemoteException)18 BindException (java.net.BindException)12 IOException (java.io.IOException)9 List (java.util.List)8 ImmutableList (com.google.common.collect.ImmutableList)7 PrivateModule (com.google.inject.PrivateModule)7 TooManyListenersException (java.util.TooManyListenersException)6 Provider (com.google.inject.Provider)5 Message (com.google.inject.spi.Message)5 ArrayList (java.util.ArrayList)5 Injector (com.google.inject.Injector)4 Provides (com.google.inject.Provides)4 AccessException (java.rmi.AccessException)4 Key (com.google.inject.Key)3 TypeLiteral (com.google.inject.TypeLiteral)3 JCommander (com.beust.jcommander.JCommander)2 Console (com.beust.jcommander.internal.Console)2