PageRenderTime 1243ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1. /*******************************************************************************
  2. * Copyright (c) 2010-2011 Sonatype, Inc.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * and Apache License v2.0 which accompanies this distribution.
  6. * The Eclipse Public License is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. * The Apache License v2.0 is available at
  9. * http://www.apache.org/licenses/LICENSE-2.0.html
  10. * You may elect to redistribute this code under either of these licenses.
  11. *******************************************************************************/
  12. package org.sonatype.guice.bean.containers;
  13. import java.io.File;
  14. import java.lang.annotation.Annotation;
  15. import java.util.Iterator;
  16. import java.util.Map.Entry;
  17. import java.util.Properties;
  18. import javax.inject.Inject;
  19. import org.junit.After;
  20. import org.junit.Before;
  21. import org.sonatype.guice.bean.binders.ParameterKeys;
  22. import org.sonatype.guice.bean.binders.SpaceModule;
  23. import org.sonatype.guice.bean.binders.WireModule;
  24. import org.sonatype.guice.bean.locators.MutableBeanLocator;
  25. import org.sonatype.guice.bean.reflect.ClassSpace;
  26. import org.sonatype.guice.bean.reflect.URLClassSpace;
  27. import org.sonatype.inject.BeanScanning;
  28. import org.testng.annotations.AfterMethod;
  29. import org.testng.annotations.BeforeMethod;
  30. import com.google.inject.AbstractModule;
  31. import com.google.inject.Binder;
  32. import com.google.inject.Guice;
  33. import com.google.inject.Key;
  34. import com.google.inject.Module;
  35. import com.google.inject.name.Names;
  36. /**
  37. * Abstract TestNG/JUnit4 test that automatically binds and injects itself.
  38. */
  39. public abstract class InjectedTest
  40. implements Module
  41. {
  42. // ----------------------------------------------------------------------
  43. // Implementation fields
  44. // ----------------------------------------------------------------------
  45. private String basedir;
  46. @Inject
  47. private MutableBeanLocator locator;
  48. // ----------------------------------------------------------------------
  49. // Setup
  50. // ----------------------------------------------------------------------
  51. @Before
  52. @BeforeMethod
  53. public void setUp()
  54. {
  55. Guice.createInjector( new WireModule( new SetUpModule(), new SpaceModule( space(), scanning() ) ) );
  56. }
  57. @After
  58. @AfterMethod
  59. public void tearDown()
  60. {
  61. locator.clear();
  62. }
  63. final class SetUpModule
  64. extends AbstractModule
  65. {
  66. @Override
  67. protected void configure()
  68. {
  69. install( InjectedTest.this );
  70. final Properties properties = new Properties();
  71. properties.put( "basedir", getBasedir() );
  72. InjectedTest.this.configure( properties );
  73. bind( ParameterKeys.PROPERTIES ).toInstance( properties );
  74. requestInjection( InjectedTest.this );
  75. }
  76. }
  77. public ClassSpace space()
  78. {
  79. return new URLClassSpace( getClass().getClassLoader() );
  80. }
  81. public BeanScanning scanning()
  82. {
  83. return BeanScanning.CACHE;
  84. }
  85. // ----------------------------------------------------------------------
  86. // Container configuration methods
  87. // ----------------------------------------------------------------------
  88. /**
  89. * Custom injection bindings.
  90. *
  91. * @param binder The Guice binder
  92. */
  93. public void configure( final Binder binder )
  94. {
  95. // place any per-test bindings here...
  96. }
  97. /**
  98. * Custom property values.
  99. *
  100. * @param properties The test properties
  101. */
  102. public void configure( final Properties properties )
  103. {
  104. // put any per-test properties here...
  105. }
  106. // ----------------------------------------------------------------------
  107. // Container lookup methods
  108. // ----------------------------------------------------------------------
  109. public final <T> T lookup( final Class<T> type )
  110. {
  111. return lookup( Key.get( type ) );
  112. }
  113. public final <T> T lookup( final Class<T> type, final String name )
  114. {
  115. return lookup( type, Names.named( name ) );
  116. }
  117. public final <T> T lookup( final Class<T> type, final Class<? extends Annotation> qualifier )
  118. {
  119. return lookup( Key.get( type, qualifier ) );
  120. }
  121. public final <T> T lookup( final Class<T> type, final Annotation qualifier )
  122. {
  123. return lookup( Key.get( type, qualifier ) );
  124. }
  125. // ----------------------------------------------------------------------
  126. // Container resource methods
  127. // ----------------------------------------------------------------------
  128. public final String getBasedir()
  129. {
  130. if ( null == basedir )
  131. {
  132. basedir = System.getProperty( "basedir", new File( "" ).getAbsolutePath() );
  133. }
  134. return basedir;
  135. }
  136. // ----------------------------------------------------------------------
  137. // Implementation methods
  138. // ----------------------------------------------------------------------
  139. private final <T> T lookup( final Key<T> key )
  140. {
  141. final Iterator<? extends Entry<Annotation, T>> i = locator.locate( key ).iterator();
  142. return i.hasNext() ? i.next().getValue() : null;
  143. }
  144. }