PageRenderTime 645ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/google/inject/identityinjection/test/ListenerSingleInjectionTestCase.java

https://bitbucket.org/noctarius/guiceidentityinjection
Java | 217 lines | 156 code | 46 blank | 15 comment | 6 complexity | 9119cb49cb334db65d1963b384067d01 MD5 | raw file
  1. /*
  2. * Copyright (C) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.inject.identityinjection.test;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertTrue;
  19. import org.junit.Test;
  20. import com.google.inject.AbstractModule;
  21. import com.google.inject.Guice;
  22. import com.google.inject.Inject;
  23. import com.google.inject.Injector;
  24. import com.google.inject.identityinjection.IdentityModule;
  25. import com.google.inject.identityinjection.IdentityProvision;
  26. import com.google.inject.identityinjection.IdentityProvisionListener;
  27. import com.google.inject.identityinjection.test.annotations.User;
  28. import com.google.inject.identityinjection.test.impl.UserEntity;
  29. import com.google.inject.identityinjection.test.impl.UserValueInjector;
  30. public class ListenerSingleInjectionTestCase {
  31. @Test
  32. public void testInjectionListenerValueProvider1() {
  33. final Listener listener = new Listener();
  34. final Injector injector = Guice.createInjector(new IdentityModule() {
  35. @Override
  36. protected void configure() {
  37. bindIdentity(UserEntity.class).annotatedWith(User.class)
  38. .applyListener(listener)
  39. .toIdentityProvider(new UserValueInjector());
  40. }
  41. });
  42. final UserInjectable1 injectable = new UserInjectable1();
  43. injector.injectMembers(injectable);
  44. assertEquals("User1", injectable.user1.getName());
  45. assertEquals(1, injectable.user1.getId());
  46. assertTrue(listener.executed[0]);
  47. assertTrue(listener.executed[1]);
  48. assertEquals("before", listener.orderOfExecution[0]);
  49. assertEquals("after", listener.orderOfExecution[1]);
  50. }
  51. @Test
  52. public void testInjectionListenerValueProvider2() {
  53. final Listener listener = new Listener();
  54. final Injector injector = Guice.createInjector(new IdentityModule() {
  55. @Override
  56. protected void configure() {
  57. bindIdentity(UserEntity.class).annotatedWith(User.class)
  58. .applyListener(listener)
  59. .toIdentityProvider(new UserValueInjector());
  60. }
  61. }, new AbstractModule() {
  62. @Override
  63. protected void configure() {
  64. bind(UserInjectable2.class);
  65. }
  66. });
  67. final UserInjectable2 injectable = injector
  68. .getInstance(UserInjectable2.class);
  69. assertEquals("User1", injectable.user1.getName());
  70. assertEquals(1, injectable.user1.getId());
  71. assertTrue(listener.executed[0]);
  72. assertTrue(listener.executed[1]);
  73. assertEquals("before", listener.orderOfExecution[0]);
  74. assertEquals("after", listener.orderOfExecution[1]);
  75. }
  76. @Test
  77. public void testInjectionListenerValueProvider3() {
  78. final Listener listener = new Listener();
  79. final Injector injector = Guice.createInjector(new IdentityModule() {
  80. @Override
  81. protected void configure() {
  82. bindIdentity(UserEntity.class).annotatedWith(User.class)
  83. .applyListener(listener)
  84. .toIdentityProvider(new UserValueInjector());
  85. }
  86. }, new AbstractModule() {
  87. @Override
  88. protected void configure() {
  89. bind(UserInjectable3.class);
  90. }
  91. });
  92. final UserInjectable3 injectable = injector
  93. .getInstance(UserInjectable3.class);
  94. assertEquals("User1", injectable.user1.getName());
  95. assertEquals(1, injectable.user1.getId());
  96. assertTrue(listener.executed[0]);
  97. assertTrue(listener.executed[1]);
  98. assertEquals("before", listener.orderOfExecution[0]);
  99. assertEquals("after", listener.orderOfExecution[1]);
  100. }
  101. @Test
  102. public void testInjectionListenerValueProvider4() {
  103. final Listener listener = new Listener();
  104. final Injector injector = Guice.createInjector(new IdentityModule() {
  105. @Override
  106. protected void configure() {
  107. bindIdentity(UserEntity.class).annotatedWith(User.class)
  108. .applyListener(listener)
  109. .toIdentityProvider(new UserValueInjector());
  110. }
  111. });
  112. final UserInjectable4 injectable = new UserInjectable4();
  113. injector.injectMembers(injectable);
  114. assertEquals("User1", injectable.user1.getName());
  115. assertEquals(1, injectable.user1.getId());
  116. assertTrue(listener.executed[0]);
  117. assertTrue(listener.executed[1]);
  118. assertEquals("before", listener.orderOfExecution[0]);
  119. assertEquals("after", listener.orderOfExecution[1]);
  120. }
  121. public static class UserInjectable1 {
  122. private UserEntity user1;
  123. @Inject
  124. public void user(@User(byId = 1) final UserEntity user1) {
  125. this.user1 = user1;
  126. }
  127. }
  128. public static class UserInjectable2 {
  129. private final UserEntity user1;
  130. @Inject
  131. public UserInjectable2(@User(byId = 1) final UserEntity user1) {
  132. this.user1 = user1;
  133. }
  134. }
  135. public static class UserInjectable3 {
  136. @Inject
  137. @User(byId = 1)
  138. private UserEntity user1;
  139. }
  140. public static class UserInjectable4 {
  141. @User(byId = 1)
  142. private UserEntity user1;
  143. }
  144. public static class Listener implements
  145. IdentityProvisionListener<UserEntity, User> {
  146. private final boolean[] executed = new boolean[2];
  147. private final String[] orderOfExecution = new String[2];
  148. @Override
  149. public void beforeInjection(
  150. final IdentityProvision<UserEntity, User> identityProvision,
  151. final User provisionAnnotation) {
  152. executed[0] = true;
  153. if (orderOfExecution[0] == null) {
  154. orderOfExecution[0] = "before";
  155. } else {
  156. orderOfExecution[1] = "before";
  157. }
  158. }
  159. @Override
  160. public void afterInjection(
  161. final IdentityProvision<UserEntity, User> identityProvision,
  162. final User provisionAnnotation, final UserEntity value) {
  163. executed[1] = true;
  164. if (orderOfExecution[0] == null) {
  165. orderOfExecution[0] = "after";
  166. } else {
  167. orderOfExecution[1] = "after";
  168. }
  169. }
  170. }
  171. }