PageRenderTime 89ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/sisu-inject/containers/guice-bean/guice-bean-inject/src/test/java/org/sonatype/guice/bean/inject/PropertyListenerTest.java

https://github.com/jglick/sisu
Java | 205 lines | 167 code | 27 blank | 11 comment | 4 complexity | a83a154bf733f8275513473395a7dfcf MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2009-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.inject;
  13. import junit.framework.TestCase;
  14. import org.sonatype.guice.bean.reflect.BeanProperty;
  15. import com.google.inject.AbstractModule;
  16. import com.google.inject.ConfigurationException;
  17. import com.google.inject.Guice;
  18. import com.google.inject.Injector;
  19. import com.google.inject.Key;
  20. import com.google.inject.TypeLiteral;
  21. import com.google.inject.matcher.AbstractMatcher;
  22. import com.google.inject.spi.TypeEncounter;
  23. public class PropertyListenerTest
  24. extends TestCase
  25. {
  26. static class Base
  27. {
  28. String a;
  29. }
  30. static class Bean0
  31. extends Base
  32. {
  33. String last;
  34. }
  35. static class Bean1
  36. extends Base
  37. {
  38. String b;
  39. Bean2 bean;
  40. }
  41. static class Bean2
  42. extends Base
  43. {
  44. String b;
  45. String last;
  46. String c;
  47. String ignore;
  48. String d;
  49. }
  50. static class Bean3
  51. extends Base
  52. {
  53. String b;
  54. String error;
  55. String c;
  56. }
  57. static class Bean4
  58. extends Bean1
  59. {
  60. void setA( @SuppressWarnings( "unused" ) final String unused )
  61. {
  62. assertNull( a ); // hidden by our setter method
  63. assertNotNull( b ); // should be injected first
  64. a = "correct order";
  65. }
  66. }
  67. class NamedPropertyBinder
  68. implements PropertyBinder
  69. {
  70. public <T> PropertyBinding bindProperty( final BeanProperty<T> property )
  71. {
  72. if ( "last".equals( property.getName() ) )
  73. {
  74. return PropertyBinder.LAST_BINDING;
  75. }
  76. if ( "ignore".equals( property.getName() ) )
  77. {
  78. return null;
  79. }
  80. if ( "error".equals( property.getName() ) )
  81. {
  82. throw new RuntimeException( "Broken binding" );
  83. }
  84. if ( "bean".equals( property.getName() ) )
  85. {
  86. return new PropertyBinding()
  87. {
  88. public void injectProperty( final Object bean )
  89. {
  90. property.set( bean, injector.getInstance( Key.get( property.getType() ) ) );
  91. }
  92. };
  93. }
  94. return new PropertyBinding()
  95. {
  96. @SuppressWarnings( "unchecked" )
  97. public void injectProperty( final Object bean )
  98. {
  99. property.set( bean, (T) ( property.getName() + "Value" ) );
  100. }
  101. };
  102. }
  103. }
  104. final PropertyBinder namedPropertyBinder = new NamedPropertyBinder();
  105. Injector injector;
  106. @Override
  107. public void setUp()
  108. {
  109. injector = Guice.createInjector( new AbstractModule()
  110. {
  111. @Override
  112. protected void configure()
  113. {
  114. bindListener( new AbstractMatcher<TypeLiteral<?>>()
  115. {
  116. public boolean matches( final TypeLiteral<?> type )
  117. {
  118. return Base.class.isAssignableFrom( type.getRawType() );
  119. }
  120. }, new BeanListener( new BeanBinder()
  121. {
  122. public <T> PropertyBinder bindBean( final TypeLiteral<T> type, final TypeEncounter<T> encounter )
  123. {
  124. return type.getRawType().getName().contains( "Bean" ) ? namedPropertyBinder : null;
  125. }
  126. } ) );
  127. }
  128. } );
  129. }
  130. public void testNoBean()
  131. {
  132. final Base base = injector.getInstance( Base.class );
  133. assertNull( base.a );
  134. }
  135. public void testNoBindings()
  136. {
  137. final Bean0 bean0 = injector.getInstance( Bean0.class );
  138. assertNull( bean0.a );
  139. }
  140. public void testPropertyBindings()
  141. {
  142. final Bean1 bean1 = injector.getInstance( Bean1.class );
  143. assertEquals( "bValue", bean1.b );
  144. assertEquals( "aValue", bean1.a );
  145. }
  146. public void testSpecialProperties()
  147. {
  148. final Bean2 bean2 = injector.getInstance( Bean2.class );
  149. assertEquals( "dValue", bean2.d );
  150. assertEquals( "cValue", bean2.c );
  151. assertNull( bean2.b );
  152. assertNull( bean2.a );
  153. try
  154. {
  155. PropertyBinder.LAST_BINDING.injectProperty( bean2 );
  156. fail( "Expected UnsupportedOperationException" );
  157. }
  158. catch ( final UnsupportedOperationException e )
  159. {
  160. }
  161. }
  162. public void testBrokenBinding()
  163. {
  164. try
  165. {
  166. injector.getInstance( Bean3.class );
  167. fail( "Expected ConfigurationException" );
  168. }
  169. catch ( final ConfigurationException e )
  170. {
  171. e.printStackTrace();
  172. }
  173. }
  174. public void testInjectionOrder()
  175. {
  176. assertEquals( "correct order", injector.getInstance( Bean4.class ).a );
  177. }
  178. }