/sisu-2.2.3/sisu-inject/containers/guice-bean/guice-bean-containers/src/main/java/org/sonatype/guice/bean/containers/InjectedTest.java
Java | 172 lines | 100 code | 28 blank | 44 comment | 2 complexity | a6ce08eb1c4b638d4bc0090a4fb4f09c MD5 | raw file
Possible License(s): Apache-2.0, EPL-1.0
- /*******************************************************************************
- * Copyright (c) 2010-2011 Sonatype, Inc.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * and Apache License v2.0 which accompanies this distribution.
- * The Eclipse Public License is available at
- * http://www.eclipse.org/legal/epl-v10.html
- * The Apache License v2.0 is available at
- * http://www.apache.org/licenses/LICENSE-2.0.html
- * You may elect to redistribute this code under either of these licenses.
- *******************************************************************************/
- package org.sonatype.guice.bean.containers;
- import java.io.File;
- import java.lang.annotation.Annotation;
- import java.util.Iterator;
- import java.util.Map.Entry;
- import java.util.Properties;
- import javax.inject.Inject;
- import org.junit.After;
- import org.junit.Before;
- import org.sonatype.guice.bean.binders.ParameterKeys;
- import org.sonatype.guice.bean.binders.SpaceModule;
- import org.sonatype.guice.bean.binders.WireModule;
- import org.sonatype.guice.bean.locators.MutableBeanLocator;
- import org.sonatype.guice.bean.reflect.ClassSpace;
- import org.sonatype.guice.bean.reflect.URLClassSpace;
- import org.sonatype.inject.BeanScanning;
- import org.testng.annotations.AfterMethod;
- import org.testng.annotations.BeforeMethod;
- import com.google.inject.AbstractModule;
- import com.google.inject.Binder;
- import com.google.inject.Guice;
- import com.google.inject.Key;
- import com.google.inject.Module;
- import com.google.inject.name.Names;
- /**
- * Abstract TestNG/JUnit4 test that automatically binds and injects itself.
- */
- public abstract class InjectedTest
- implements Module
- {
- // ----------------------------------------------------------------------
- // Implementation fields
- // ----------------------------------------------------------------------
- private String basedir;
- @Inject
- private MutableBeanLocator locator;
- // ----------------------------------------------------------------------
- // Setup
- // ----------------------------------------------------------------------
- @Before
- @BeforeMethod
- public void setUp()
- {
- Guice.createInjector( new WireModule( new SetUpModule(), new SpaceModule( space(), scanning() ) ) );
- }
- @After
- @AfterMethod
- public void tearDown()
- {
- locator.clear();
- }
- final class SetUpModule
- extends AbstractModule
- {
- @Override
- protected void configure()
- {
- install( InjectedTest.this );
- final Properties properties = new Properties();
- properties.put( "basedir", getBasedir() );
- InjectedTest.this.configure( properties );
- bind( ParameterKeys.PROPERTIES ).toInstance( properties );
- requestInjection( InjectedTest.this );
- }
- }
- public ClassSpace space()
- {
- return new URLClassSpace( getClass().getClassLoader() );
- }
- public BeanScanning scanning()
- {
- return BeanScanning.CACHE;
- }
- // ----------------------------------------------------------------------
- // Container configuration methods
- // ----------------------------------------------------------------------
- /**
- * Custom injection bindings.
- *
- * @param binder The Guice binder
- */
- public void configure( final Binder binder )
- {
- // place any per-test bindings here...
- }
- /**
- * Custom property values.
- *
- * @param properties The test properties
- */
- public void configure( final Properties properties )
- {
- // put any per-test properties here...
- }
- // ----------------------------------------------------------------------
- // Container lookup methods
- // ----------------------------------------------------------------------
- public final <T> T lookup( final Class<T> type )
- {
- return lookup( Key.get( type ) );
- }
- public final <T> T lookup( final Class<T> type, final String name )
- {
- return lookup( type, Names.named( name ) );
- }
- public final <T> T lookup( final Class<T> type, final Class<? extends Annotation> qualifier )
- {
- return lookup( Key.get( type, qualifier ) );
- }
- public final <T> T lookup( final Class<T> type, final Annotation qualifier )
- {
- return lookup( Key.get( type, qualifier ) );
- }
- // ----------------------------------------------------------------------
- // Container resource methods
- // ----------------------------------------------------------------------
- public final String getBasedir()
- {
- if ( null == basedir )
- {
- basedir = System.getProperty( "basedir", new File( "" ).getAbsolutePath() );
- }
- return basedir;
- }
- // ----------------------------------------------------------------------
- // Implementation methods
- // ----------------------------------------------------------------------
- private final <T> T lookup( final Key<T> key )
- {
- final Iterator<? extends Entry<Annotation, T>> i = locator.locate( key ).iterator();
- return i.hasNext() ? i.next().getValue() : null;
- }
- }