Search in sources :

Example 91 with CreationException

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

the class CheckedProviderTest method testProviderMethodWithWrongException.

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

            @Override
            protected void configure() {
                install(ThrowingProviderBinder.forModule(this));
            }

            @SuppressWarnings("unused")
            @CheckedProvides(RemoteProvider.class)
            String foo() throws InterruptedException {
                return null;
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(InterruptedException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + ", " + BindException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : BindException(java.net.BindException) CreationException(com.google.inject.CreationException) RemoteException(java.rmi.RemoteException) AbstractModule(com.google.inject.AbstractModule)

Example 92 with CreationException

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

the class CheckedProviderTest method testBindingToNonInterfaceType_Provides.

public void testBindingToNonInterfaceType_Provides() throws Exception {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                install(ThrowingProviderBinder.forModule(this));
            }

            @SuppressWarnings("unused")
            @CheckedProvides(MockRemoteProvider.class)
            Foo foo() {
                return null;
            }
        });
        fail();
    } catch (CreationException expected) {
        assertEquals(MockRemoteProvider.class.getName() + " must be an interface", Iterables.getOnlyElement(expected.getErrorMessages()).getMessage());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 93 with CreationException

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

the class OptionalBinderTest method testDifferentBindingsFail_both.

public void testDifferentBindingsFail_both() {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("a");
            OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("b");
            OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toInstance("b");
            OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toInstance("c");
        }
    };
    try {
        Guice.createInjector(module);
        fail();
    } catch (CreationException ce) {
        assertEquals(ce.getMessage(), 2, ce.getErrorMessages().size());
        assertContains(ce.getMessage(), "1) A binding to java.lang.String annotated with @" + RealOptionalBinder.Default.class.getName() + " was already configured at " + module.getClass().getName() + ".configure(", "at " + module.getClass().getName() + ".configure(", "2) A binding to java.lang.String annotated with @" + RealOptionalBinder.Actual.class.getName() + " was already configured at " + module.getClass().getName() + ".configure(", "at " + module.getClass().getName() + ".configure(");
    }
}
Also used : CreationException(com.google.inject.CreationException) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 94 with CreationException

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

the class OptionalBinderTest method testTypeNotBoundByDefault.

public void testTypeNotBoundByDefault() {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), String.class);
            // the above specifies this.
            requireBinding(new Key<Optional<String>>() {
            });
            // but it doesn't specify this.
            requireBinding(String.class);
            // need to do this, otherwise String will JIT
            binder().requireExplicitBindings();
            if (HAS_JAVA_OPTIONAL) {
                requireBinding(Key.get(javaOptionalOfString));
            }
        }
    };
    try {
        Guice.createInjector(module);
        fail();
    } catch (CreationException ce) {
        assertContains(ce.getMessage(), "1) Explicit bindings are required and java.lang.String is not explicitly bound.");
        assertEquals(1, ce.getErrorMessages().size());
    }
}
Also used : Optional(com.google.common.base.Optional) CreationException(com.google.inject.CreationException) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 95 with CreationException

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

the class FactoryModuleBuilderTest method testSimilarBindingsWithConflictingImplementations.

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