|
Java example source code file (FactoryModuleBuilderTest.java)
The FactoryModuleBuilderTest.java Java example source code/** * Copyright (C) 2009 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.inject.assistedinject; import static com.google.inject.Asserts.assertContains; import static com.google.inject.name.Names.named; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.inject.AbstractModule; import com.google.inject.Binding; import com.google.inject.CreationException; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Key; import com.google.inject.Module; import com.google.inject.Provides; import com.google.inject.Singleton; import com.google.inject.Stage; import com.google.inject.TypeLiteral; import com.google.inject.name.Named; import com.google.inject.name.Names; import com.google.inject.spi.Dependency; import com.google.inject.spi.Element; import com.google.inject.spi.Elements; import com.google.inject.spi.HasDependencies; import com.google.inject.spi.Message; import junit.framework.TestCase; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; public class FactoryModuleBuilderTest extends TestCase { private enum Color { BLUE, GREEN, RED, GRAY, BLACK } public void testImplicitForwardingAssistedBindingFailsWithInterface() { try { Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(Car.class).to(Golf.class); install(new FactoryModuleBuilder().build(ColoredCarFactory.class)); } }); fail(); } catch (CreationException ce) { assertContains( ce.getMessage(), "1) " + Car.class.getName() + " is an interface, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + Car.class.getName(), "at " + ColoredCarFactory.class.getName() + ".create("); assertEquals(1, ce.getErrorMessages().size()); } } public void testImplicitForwardingAssistedBindingFailsWithAbstractClass() { try { Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AbstractCar.class).to(ArtCar.class); install(new FactoryModuleBuilder().build(ColoredAbstractCarFactory.class)); } }); fail(); } catch (CreationException ce) { assertContains( ce.getMessage(), "1) " + AbstractCar.class.getName() + " is abstract, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + AbstractCar.class.getName(), "at " + ColoredAbstractCarFactory.class.getName() + ".create("); assertEquals(1, ce.getErrorMessages().size()); } } public void testImplicitForwardingAssistedBindingCreatesNewObjects() { final Mustang providedMustang = new Mustang(Color.BLUE); Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { install(new FactoryModuleBuilder().build(MustangFactory.class)); } @Provides Mustang provide() { return providedMustang; } }); assertSame(providedMustang, injector.getInstance(Mustang.class)); MustangFactory factory = injector.getInstance(MustangFactory.class); Mustang created = factory.create(Color.GREEN); assertNotSame(providedMustang, created); assertEquals(Color.BLUE, providedMustang.color); assertEquals(Color.GREEN, created.color); } public void testExplicitForwardingAssistedBindingFailsWithInterface() { try { Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(Volkswagen.class).to(Golf.class); install(new FactoryModuleBuilder() .implement(Car.class, Volkswagen.class) .build(ColoredCarFactory.class)); } }); fail(); } catch (CreationException ce) { assertContains( ce.getMessage(), "1) " + Volkswagen.class.getName() + " is an interface, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + Volkswagen.class.getName(), "while locating " + Car.class.getName(), "at " + ColoredCarFactory.class.getName() + ".create("); assertEquals(1, ce.getErrorMessages().size()); } } public void testExplicitForwardingAssistedBindingFailsWithAbstractClass() { try { Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AbstractCar.class).to(ArtCar.class); install(new FactoryModuleBuilder() .implement(Car.class, AbstractCar.class) .build(ColoredCarFactory.class)); } }); fail(); } catch (CreationException ce) { assertContains( ce.getMessage(), "1) " + AbstractCar.class.getName() + " is abstract, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + AbstractCar.class.getName(), "while locating " + Car.class.getName(), "at " + ColoredCarFactory.class.getName() + ".create("); assertEquals(1, ce.getErrorMessages().size()); } } public void testExplicitForwardingAssistedBindingCreatesNewObjects() { final Mustang providedMustang = new Mustang(Color.BLUE); Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { install(new FactoryModuleBuilder().implement(Car.class, Mustang.class).build( ColoredCarFactory.class)); } @Provides Mustang provide() { return providedMustang; } }); assertSame(providedMustang, injector.getInstance(Mustang.class)); ColoredCarFactory factory = injector.getInstance(ColoredCarFactory.class); Mustang created = (Mustang)factory.create(Color.GREEN); assertNotSame(providedMustang, created); assertEquals(Color.BLUE, providedMustang.color); assertEquals(Color.GREEN, created.color); } public void testAnnotatedAndParentBoundReturnValue() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(Car.class).to(Golf.class); bind(Integer.class).toInstance(911); bind(Double.class).toInstance(5.0d); install(new FactoryModuleBuilder() .implement(Car.class, Names.named("german"), Beetle.class) .implement(Car.class, Names.named("american"), Mustang.class) .build(AnnotatedVersatileCarFactory.class)); } }); AnnotatedVersatileCarFactory factory = injector.getInstance(AnnotatedVersatileCarFactory.class); assertTrue(factory.getGermanCar(Color.BLACK) instanceof Beetle); assertTrue(injector.getInstance(Car.class) instanceof Golf); } public void testParentBoundReturnValue() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(Car.class).to(Golf.class); bind(Double.class).toInstance(5.0d); install(new FactoryModuleBuilder() .implement(Car.class, Mustang.class) .build(ColoredCarFactory.class)); } }); ColoredCarFactory factory = injector.getInstance(ColoredCarFactory.class); assertTrue(factory.create(Color.RED) instanceof Mustang); assertTrue(injector.getInstance(Car.class) instanceof Golf); } public void testConfigureAnnotatedReturnValue() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { install(new FactoryModuleBuilder() .implement(Car.class, Names.named("german"), Beetle.class) .implement(Car.class, Names.named("american"), Mustang.class) .build(AnnotatedVersatileCarFactory.class)); } }); AnnotatedVersatileCarFactory factory = injector.getInstance(AnnotatedVersatileCarFactory.class); assertTrue(factory.getGermanCar(Color.GRAY) instanceof Beetle); assertTrue(factory.getAmericanCar(Color.BLACK) instanceof Mustang); } public void testNoBindingAssistedInject() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { install(new FactoryModuleBuilder().build(MustangFactory.class)); } }); MustangFactory factory = injector.getInstance(MustangFactory.class); Mustang mustang = factory.create(Color.BLUE); assertEquals(Color.BLUE, mustang.color); } public void testBindingAssistedInject() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { install(new FactoryModuleBuilder() .implement(Car.class, Mustang.class) .build(ColoredCarFactory.class)); } }); ColoredCarFactory factory = injector.getInstance(ColoredCarFactory.class); Mustang mustang = (Mustang) factory.create(Color.BLUE); assertEquals(Color.BLUE, mustang.color); } public void testDuplicateBindings() { 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, Mustang.class) .build(ColoredCarFactory.class)); } }); ColoredCarFactory factory = injector.getInstance(ColoredCarFactory.class); Mustang mustang = (Mustang) factory.create(Color.BLUE); assertEquals(Color.BLUE, mustang.color); } 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()); } } public void testMultipleReturnTypes() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(Double.class).toInstance(5.0d); install(new FactoryModuleBuilder().build(VersatileCarFactory.class)); } }); VersatileCarFactory factory = injector.getInstance(VersatileCarFactory.class); Mustang mustang = factory.getMustang(Color.RED); assertEquals(Color.RED, mustang.color); Beetle beetle = factory.getBeetle(Color.GREEN); assertEquals(Color.GREEN, beetle.color); } public void testParameterizedClassesWithNoImplements() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { install(new FactoryModuleBuilder().build(new TypeLiteral<Foo.Factory Other Java examples (source code examples)Here is a short list of links related to this Java FactoryModuleBuilderTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.