Search in sources :

Example 81 with CreationException

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

the class ThrowingProviderTest method testIncorrectPredefinedType_Bind.

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

            protected void configure() {
                ThrowingProviderBinder.create(binder()).bind(StringRemoteProvider.class, Integer.class).to(new StringRemoteProvider() {

                    public String get() throws RemoteException {
                        return "A";
                    }
                });
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(StringRemoteProvider.class.getName() + " expects the value type to be java.lang.String, but it was java.lang.Integer", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 82 with CreationException

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

the class ThrowingProviderTest method testOneMethodThatIsntGet.

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

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

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

Example 83 with CreationException

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

the class ThrowingProviderTest method testProviderMethodWithManyExceptions.

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

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

            @SuppressWarnings("unused")
            @CheckedProvides(RemoteProvider.class)
            String foo() throws InterruptedException, RuntimeException, RemoteException, AccessException, TooManyListenersException {
                return null;
            }
        });
        fail();
    } catch (CreationException ce) {
        // The only two that should fail are Interrupted & TooManyListeners.. the rest are OK.
        List<Message> errors = ImmutableList.copyOf(ce.getErrorMessages());
        assertEquals(InterruptedException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", errors.get(0).getMessage());
        assertEquals(TooManyListenersException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", errors.get(1).getMessage());
        assertEquals(2, errors.size());
    }
}
Also used : TooManyListenersException(java.util.TooManyListenersException) AccessException(java.rmi.AccessException) CreationException(com.google.inject.CreationException) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) RemoteException(java.rmi.RemoteException) AbstractModule(com.google.inject.AbstractModule)

Example 84 with CreationException

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

the class ThrowingProviderTest method testIncorrectPredefinedType_Provides.

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

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

            @SuppressWarnings("unused")
            @CheckedProvides(StringRemoteProvider.class)
            Integer foo() {
                return null;
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(StringRemoteProvider.class.getName() + " expects the value type to be java.lang.String, but it was java.lang.Integer", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 85 with CreationException

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

the class ThrowingProviderTest method testManyMethods.

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

            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)

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