Search in sources :

Example 6 with CreationException

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

the class ManyConstructorsTest method testTooManyMatchingConstructors.

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

            @Override
            protected void configure() {
                install(new FactoryModuleBuilder().implement(Foo.class, TooManyMatches.class).build(SimpleFactory2.class));
            }
        });
        fail("should have failed");
    } catch (CreationException expected) {
        Asserts.assertContains(expected.getMessage(), "1) " + TooManyMatches.class.getName() + " has more than one constructor annotated with @AssistedInject that " + "matches the parameters in method " + SimpleFactory2.class.getName());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 7 with CreationException

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

the class BinderTest method testDanglingConstantBinding.

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

            @Override
            public void configure() {
                bindConstant();
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) Missing constant value. Please call to(...).", "at " + getClass().getName());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 8 with CreationException

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

the class BinderTest method testBindingNullConstant.

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

            @Override
            public void configure() {
                String none = null;
                bindConstant().annotatedWith(Names.named("nullOne")).to(none);
                bind(String.class).annotatedWith(Names.named("nullTwo")).toInstance(none);
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) Binding to null instances is not allowed. Use toProvider(Providers.of(null))", "2) Binding to null instances is not allowed. Use toProvider(Providers.of(null))");
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 9 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 10 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)

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