|
Java example source code file (ExtensionSpiTest.java)
The ExtensionSpiTest.java Java example source code/** * Copyright (C) 2010 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.name.Names.named; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.google.inject.AbstractModule; import com.google.inject.Binding; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Key; import com.google.inject.Stage; import com.google.inject.name.Named; import com.google.inject.spi.DefaultBindingTargetVisitor; import com.google.inject.spi.Dependency; import com.google.inject.spi.Element; import com.google.inject.spi.Elements; import junit.framework.AssertionFailedError; import junit.framework.TestCase; import java.util.List; import java.util.Set; import java.util.logging.Logger; /** * Tests for AssistedInject Spi. * * @author ramakrishna@google.com (Ramakrishna Rajanna) */ public class ExtensionSpiTest extends TestCase { public final void testSpiOnElements() throws Exception { AssistedInjectSpiVisitor visitor = new AssistedInjectSpiVisitor(); Integer count = 0; for(Element element : Elements.getElements(new Module())) { if(element instanceof Binding) { assertEquals(count++, ((Binding<?>)element).acceptTargetVisitor(visitor)); } } validateVisitor(visitor); } public void testSpiOnVisitor() throws Exception { AssistedInjectSpiVisitor visitor = new AssistedInjectSpiVisitor(); Integer count = 0; Injector injector = Guice.createInjector(new Module()); for(Binding<?> binding : injector.getBindings().values()) { assertEquals(count++, binding.acceptTargetVisitor(visitor)); } validateVisitor(visitor); } private void validateVisitor(AssistedInjectSpiVisitor visitor) throws Exception { assertEquals(1, visitor.assistedBindingCount); List<AssistedMethod> assistedMethods = Lists.newArrayList(Iterables.getOnlyElement( visitor.assistedInjectBindings).getAssistedMethods()); assertEquals(7, assistedMethods.size()); assertEquals(1, visitor.assistedBindingCount); assertEquals(1, visitor.assistedInjectBindings.size()); // Validate for each of the methods in AnimalFactory Set<String> names = Sets.newHashSet(); for (AssistedMethod method : assistedMethods) { String name = method.getFactoryMethod().getName(); names.add(name); if (name.equals("createAStrangeCatAsAnimal")) { validateAssistedMethod(method, name, StrangeCat.class, ImmutableList.<Key>>of()); } else if (name.equals("createStrangeCatWithConstructorForOwner")) { validateAssistedMethod(method, name, StrangeCat.class, ImmutableList.<Key>>of()); } else if (name.equals("createStrangeCatWithConstructorForAge")) { validateAssistedMethod(method, name, StrangeCat.class, ImmutableList.<Key>>of()); } else if (name.equals("createCatWithANonAssistedDependency")) { validateAssistedMethod(method, name, CatWithAName.class, ImmutableList.<Key>>of(Key.get(String.class, named("catName2")))); } else if (name.equals("createCat")) { validateAssistedMethod(method, name, Cat.class, ImmutableList.<Key>>of()); } else if (name.equals("createASimpleCatAsAnimal")) { validateAssistedMethod(method, name, SimpleCat.class, ImmutableList.<Key>>of()); } else if (name.equals("createCatWithNonAssistedDependencies")) { List<Key>> dependencyKeys = ImmutableList. Other Java examples (source code examples)Here is a short list of links related to this Java ExtensionSpiTest.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.