Search in sources :

Example 26 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 27 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 28 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 29 with CreationException

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

the class CheckedProviderTest method testManyMethods.

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

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

            @SuppressWarnings("unused")
            @CheckedProvides(ManyMethods.class)
            String foo() {
                return null;
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(ManyMethods.class.getName() + " may not declare any new methods, but declared " + Arrays.asList(ManyMethods.class.getDeclaredMethods()), Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 30 with CreationException

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

the class CheckedProviderTest method testCxtorWithSuperclassExceptionFails.

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

            @Override
            protected void configure() {
                ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(SuperclassExceptionFoo.class);
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(IOException.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) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) 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