Search in sources :

Example 41 with CreationException

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

the class BinderTest method testRecursiveBinding.

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

            @Override
            public void configure() {
                bind(Runnable.class).to(Runnable.class);
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) Binding points to itself.", "at " + getClass().getName(), getDeclaringSourcePart(getClass()));
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 42 with CreationException

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

the class BinderTest method testArrayTypeCanonicalization.

/**
   * Although {@code String[].class} isn't equal to {@code new
   * GenericArrayTypeImpl(String.class)}, Guice should treat these two types
   * interchangeably.
   */
public void testArrayTypeCanonicalization() {
    final String[] strings = new String[] { "A" };
    final Integer[] integers = new Integer[] { 1 };
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String[].class).toInstance(strings);
            bind(new TypeLiteral<Integer[]>() {
            }).toInstance(integers);
        }
    });
    assertSame(integers, injector.getInstance(Key.get(new TypeLiteral<Integer[]>() {
    })));
    assertSame(integers, injector.getInstance(new Key<Integer[]>() {
    }));
    assertSame(integers, injector.getInstance(Integer[].class));
    assertSame(strings, injector.getInstance(Key.get(new TypeLiteral<String[]>() {
    })));
    assertSame(strings, injector.getInstance(new Key<String[]>() {
    }));
    assertSame(strings, injector.getInstance(String[].class));
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(String[].class).toInstance(new String[] { "A" });
                bind(new TypeLiteral<String[]>() {
                }).toInstance(new String[] { "B" });
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) A binding to java.lang.String[] was already configured at " + getClass().getName(), "at " + getClass().getName(), getDeclaringSourcePart(getClass()));
        assertContains(expected.getMessage(), "1 error");
    }
    // passes because duplicates are ignored
    injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String[].class).toInstance(strings);
            bind(new TypeLiteral<String[]>() {
            }).toInstance(strings);
        }
    });
    assertSame(strings, injector.getInstance(Key.get(new TypeLiteral<String[]>() {
    })));
    assertSame(strings, injector.getInstance(new Key<String[]>() {
    }));
    assertSame(strings, injector.getInstance(String[].class));
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) CreationException(com.google.inject.CreationException) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 43 with CreationException

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

the class BinderTest method testUserReportedErrorsAreAlsoLogged.

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

            @Override
            protected void configure() {
                addError(new Message("Whoops!", new IllegalArgumentException()));
            }
        });
        fail();
    } catch (CreationException expected) {
    }
    LogRecord logRecord = Iterables.getOnlyElement(this.logRecords);
    assertContains(logRecord.getMessage(), "An exception was caught and reported. Message: java.lang.IllegalArgumentException");
}
Also used : Message(com.google.inject.spi.Message) LogRecord(java.util.logging.LogRecord) CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 44 with CreationException

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

the class BinderTest method testBindingToProvider.

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

            @Override
            protected void configure() {
                bind(new TypeLiteral<Provider<String>>() {
                }).toInstance(Providers.of("A"));
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) Binding to Provider is not allowed.", "at " + BinderTest.class.getName(), getDeclaringSourcePart(getClass()));
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 45 with CreationException

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

the class BinderTest method testToStringOnBinderApi.

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

            @Override
            public void configure() {
                assertEquals("Binder", binder().toString());
                assertEquals("Provider<java.lang.Integer>", getProvider(Integer.class).toString());
                assertEquals("Provider<java.util.List<java.lang.String>>", getProvider(Key.get(new TypeLiteral<List<String>>() {
                })).toString());
                assertEquals("BindingBuilder<java.lang.Integer>", bind(Integer.class).toString());
                assertEquals("BindingBuilder<java.lang.Integer>", bind(Integer.class).annotatedWith(Names.named("a")).toString());
                assertEquals("ConstantBindingBuilder", bindConstant().toString());
                assertEquals("ConstantBindingBuilder", bindConstant().annotatedWith(Names.named("b")).toString());
                assertEquals("AnnotatedElementBuilder", binder().newPrivateBinder().expose(Integer.class).toString());
            }
        });
        fail();
    } catch (CreationException ignored) {
    }
}
Also used : List(java.util.List) 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