From 8e6201ee8a948b120540f6306c6c9b85bed8ce95 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Fri, 23 Jul 2010 19:26:08 +0000 Subject: [PATCH] --- .../test/inject/JavaEETestInjector.java | 60 ++++++++++++++ .../inject/JavaEETestInjectorFactory.java | 81 +++++++++++++++++++ .../org/wamblee/test/inject/package-info.java | 21 +++++ 3 files changed, 162 insertions(+) create mode 100644 test/enterprise/src/main/java/org/wamblee/test/inject/JavaEETestInjector.java create mode 100644 test/enterprise/src/main/java/org/wamblee/test/inject/JavaEETestInjectorFactory.java create mode 100644 test/enterprise/src/main/java/org/wamblee/test/inject/package-info.java diff --git a/test/enterprise/src/main/java/org/wamblee/test/inject/JavaEETestInjector.java b/test/enterprise/src/main/java/org/wamblee/test/inject/JavaEETestInjector.java new file mode 100644 index 00000000..9ea05175 --- /dev/null +++ b/test/enterprise/src/main/java/org/wamblee/test/inject/JavaEETestInjector.java @@ -0,0 +1,60 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * 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 org.wamblee.test.inject; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.wamblee.inject.Injector; +import org.wamblee.reflection.Accessor; +import org.wamblee.reflection.AnnotationUtils; + +/** + * Injector that performs additional injection on top of the injections that are standard + * available (e.g. entity manager in Java SE environment). + * + * @author Erik Brakkee + */ +public class JavaEETestInjector implements Injector { + + private EntityManager entityManager; + private Injector delegate; + private List accessors; + + /** + * Constructs the injector. + * @param aClass Class to inject for. + * @param aEntityManager Entity manager. + * @param aDelegate Injecto to delegate to to perform the standard injections. + */ + public JavaEETestInjector(Class aClass, EntityManager aEntityManager, + Injector aDelegate) { + entityManager = aEntityManager; + delegate = aDelegate; + accessors = AnnotationUtils.analyse(aClass, PersistenceContext.class); + } + + @Override + public void inject(Object aComponent) { + delegate.inject(aComponent); + for (Accessor accessor: accessors) { + accessor.set(aComponent, entityManager); + } + } + +} diff --git a/test/enterprise/src/main/java/org/wamblee/test/inject/JavaEETestInjectorFactory.java b/test/enterprise/src/main/java/org/wamblee/test/inject/JavaEETestInjectorFactory.java new file mode 100644 index 00000000..b36dfbf8 --- /dev/null +++ b/test/enterprise/src/main/java/org/wamblee/test/inject/JavaEETestInjectorFactory.java @@ -0,0 +1,81 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * 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 org.wamblee.test.inject; + +import javax.persistence.EntityManager; + +import org.wamblee.inject.Injector; +import org.wamblee.inject.InjectorFactory; +import org.wamblee.inject.InjectorFactoryBuilder; +import org.wamblee.test.persistence.JpaBuilder; + +/** + *

+ * The test injector factory provides dependency injection of a contextual entity manager + * using the support/inject mini framework. It supports dependency injection of fields + * annoted with @PersistenceContext. It only supports one persistence context + * at the moment. This injector can be easily used together with {@link JpaBuilder#getContextualEntityManager()} + * for obtaining an entity manager in unit test. + *

+ * + *

+ * The reason it is needed is because standard injection mechanisms (such as weld CDI) do not support + * entity manager injection in a Java SE environment out of the box. + *

+ * + *

+ * To use it, construct the factory using one of the available constructors and set + * {@link InjectorFactoryBuilder#setInjectorFactory(InjectorFactory)). + *

+ * + * @author Erik Brakkee + * + */ +public class JavaEETestInjectorFactory implements InjectorFactory { + + private EntityManager entityManager; + private InjectorFactory delegate; + + /** + * Constructs the factory. + * @param aEntityManager Contextual entity manager to inject. + * @param aInjectorFactory Injector factory to delegate to. + */ + public JavaEETestInjectorFactory(EntityManager aEntityManager, InjectorFactory aInjectorFactory) { + entityManager = aEntityManager; + delegate = aInjectorFactory; + } + + /** + * Constructs the factory with the default injector factory obtained from + * {@link InjectorFactoryBuilder#getInjector()}. + * @param aEntityManager Contextual entity manager to inject. + */ + public JavaEETestInjectorFactory(EntityManager aEntityManager) { + this(aEntityManager, getDefaultInjectorFactory()); + } + + private static InjectorFactory getDefaultInjectorFactory() { + InjectorFactoryBuilder.setInjectorFactory(null); + return InjectorFactoryBuilder.getInjectorFactory(); + } + + @Override + public Injector create(Class aClass) { + return new JavaEETestInjector(aClass, entityManager, delegate + .create(aClass)); + } +} diff --git a/test/enterprise/src/main/java/org/wamblee/test/inject/package-info.java b/test/enterprise/src/main/java/org/wamblee/test/inject/package-info.java new file mode 100644 index 00000000..ab4d4d80 --- /dev/null +++ b/test/enterprise/src/main/java/org/wamblee/test/inject/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * 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. + */ +/** + * Test support for testing with dependency injection with the support/inject + * mini framework. + * + */ +package org.wamblee.test.inject; \ No newline at end of file -- 2.31.1