/src/test/java/com/google/inject/identityinjection/test/ListenerSingleInjectionTestCase.java
Java | 217 lines | 156 code | 46 blank | 15 comment | 6 complexity | 9119cb49cb334db65d1963b384067d01 MD5 | raw file
- /*
- * Copyright (C) 2011 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.identityinjection.test;
-
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertTrue;
-
- import org.junit.Test;
-
- import com.google.inject.AbstractModule;
- import com.google.inject.Guice;
- import com.google.inject.Inject;
- import com.google.inject.Injector;
- import com.google.inject.identityinjection.IdentityModule;
- import com.google.inject.identityinjection.IdentityProvision;
- import com.google.inject.identityinjection.IdentityProvisionListener;
- import com.google.inject.identityinjection.test.annotations.User;
- import com.google.inject.identityinjection.test.impl.UserEntity;
- import com.google.inject.identityinjection.test.impl.UserValueInjector;
-
- public class ListenerSingleInjectionTestCase {
-
- @Test
- public void testInjectionListenerValueProvider1() {
- final Listener listener = new Listener();
-
- final Injector injector = Guice.createInjector(new IdentityModule() {
- @Override
- protected void configure() {
- bindIdentity(UserEntity.class).annotatedWith(User.class)
- .applyListener(listener)
- .toIdentityProvider(new UserValueInjector());
- }
- });
-
- final UserInjectable1 injectable = new UserInjectable1();
- injector.injectMembers(injectable);
-
- assertEquals("User1", injectable.user1.getName());
- assertEquals(1, injectable.user1.getId());
-
- assertTrue(listener.executed[0]);
- assertTrue(listener.executed[1]);
-
- assertEquals("before", listener.orderOfExecution[0]);
- assertEquals("after", listener.orderOfExecution[1]);
- }
-
- @Test
- public void testInjectionListenerValueProvider2() {
- final Listener listener = new Listener();
-
- final Injector injector = Guice.createInjector(new IdentityModule() {
- @Override
- protected void configure() {
- bindIdentity(UserEntity.class).annotatedWith(User.class)
- .applyListener(listener)
- .toIdentityProvider(new UserValueInjector());
- }
- }, new AbstractModule() {
- @Override
- protected void configure() {
- bind(UserInjectable2.class);
- }
- });
-
- final UserInjectable2 injectable = injector
- .getInstance(UserInjectable2.class);
-
- assertEquals("User1", injectable.user1.getName());
- assertEquals(1, injectable.user1.getId());
-
- assertTrue(listener.executed[0]);
- assertTrue(listener.executed[1]);
-
- assertEquals("before", listener.orderOfExecution[0]);
- assertEquals("after", listener.orderOfExecution[1]);
- }
-
- @Test
- public void testInjectionListenerValueProvider3() {
- final Listener listener = new Listener();
-
- final Injector injector = Guice.createInjector(new IdentityModule() {
- @Override
- protected void configure() {
- bindIdentity(UserEntity.class).annotatedWith(User.class)
- .applyListener(listener)
- .toIdentityProvider(new UserValueInjector());
- }
- }, new AbstractModule() {
- @Override
- protected void configure() {
- bind(UserInjectable3.class);
- }
- });
-
- final UserInjectable3 injectable = injector
- .getInstance(UserInjectable3.class);
-
- assertEquals("User1", injectable.user1.getName());
- assertEquals(1, injectable.user1.getId());
-
- assertTrue(listener.executed[0]);
- assertTrue(listener.executed[1]);
-
- assertEquals("before", listener.orderOfExecution[0]);
- assertEquals("after", listener.orderOfExecution[1]);
- }
-
- @Test
- public void testInjectionListenerValueProvider4() {
- final Listener listener = new Listener();
-
- final Injector injector = Guice.createInjector(new IdentityModule() {
- @Override
- protected void configure() {
- bindIdentity(UserEntity.class).annotatedWith(User.class)
- .applyListener(listener)
- .toIdentityProvider(new UserValueInjector());
- }
- });
-
- final UserInjectable4 injectable = new UserInjectable4();
- injector.injectMembers(injectable);
-
- assertEquals("User1", injectable.user1.getName());
- assertEquals(1, injectable.user1.getId());
-
- assertTrue(listener.executed[0]);
- assertTrue(listener.executed[1]);
-
- assertEquals("before", listener.orderOfExecution[0]);
- assertEquals("after", listener.orderOfExecution[1]);
- }
-
- public static class UserInjectable1 {
- private UserEntity user1;
-
- @Inject
- public void user(@User(byId = 1) final UserEntity user1) {
- this.user1 = user1;
- }
- }
-
- public static class UserInjectable2 {
- private final UserEntity user1;
-
- @Inject
- public UserInjectable2(@User(byId = 1) final UserEntity user1) {
- this.user1 = user1;
- }
- }
-
- public static class UserInjectable3 {
- @Inject
- @User(byId = 1)
- private UserEntity user1;
- }
-
- public static class UserInjectable4 {
- @User(byId = 1)
- private UserEntity user1;
- }
-
- public static class Listener implements
- IdentityProvisionListener<UserEntity, User> {
-
- private final boolean[] executed = new boolean[2];
- private final String[] orderOfExecution = new String[2];
-
- @Override
- public void beforeInjection(
- final IdentityProvision<UserEntity, User> identityProvision,
- final User provisionAnnotation) {
-
- executed[0] = true;
-
- if (orderOfExecution[0] == null) {
- orderOfExecution[0] = "before";
-
- } else {
- orderOfExecution[1] = "before";
- }
- }
-
- @Override
- public void afterInjection(
- final IdentityProvision<UserEntity, User> identityProvision,
- final User provisionAnnotation, final UserEntity value) {
-
- executed[1] = true;
-
- if (orderOfExecution[0] == null) {
- orderOfExecution[0] = "after";
-
- } else {
- orderOfExecution[1] = "after";
- }
- }
- }
-
- }