Return-Path: Delivered-To: apmail-incubator-openwebbeans-commits-archive@minotaur.apache.org Received: (qmail 30820 invoked from network); 19 Apr 2009 22:27:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 19 Apr 2009 22:27:45 -0000 Received: (qmail 60519 invoked by uid 500); 19 Apr 2009 22:27:45 -0000 Delivered-To: apmail-incubator-openwebbeans-commits-archive@incubator.apache.org Received: (qmail 60500 invoked by uid 500); 19 Apr 2009 22:27:45 -0000 Mailing-List: contact openwebbeans-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: openwebbeans-dev@incubator.apache.org Delivered-To: mailing list openwebbeans-commits@incubator.apache.org Received: (qmail 60490 invoked by uid 99); 19 Apr 2009 22:27:45 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 19 Apr 2009 22:27:45 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 19 Apr 2009 22:27:38 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 6179D2388A69; Sun, 19 Apr 2009 22:27:18 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r766537 - in /incubator/openwebbeans/trunk/webbeans-jpa: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/webbeans/ src/main/java/org/apache/webbeans/jpa/ src/main/java/org/apache/webbea... Date: Sun, 19 Apr 2009 22:27:18 -0000 To: openwebbeans-commits@incubator.apache.org From: struberg@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090419222718.6179D2388A69@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: struberg Date: Sun Apr 19 22:27:17 2009 New Revision: 766537 URL: http://svn.apache.org/viewvc?rev=766537&view=rev Log: OWB-91 factor out JPA stuff into own plugin module Added: incubator/openwebbeans/trunk/webbeans-jpa/ incubator/openwebbeans/trunk/webbeans-jpa/pom.xml incubator/openwebbeans/trunk/webbeans-jpa/src/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/OpenWebBeansJpaPlugin.java incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/JPAService.java incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/EntityManagersManager.java incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/JPAServicePersistenceImpl.java incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/ incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/org.apache.webbeans.plugins.OpenWebBeansPlugin incubator/openwebbeans/trunk/webbeans-jpa/src/test/ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java incubator/openwebbeans/trunk/webbeans-jpa/src/test/resources/ Added: incubator/openwebbeans/trunk/webbeans-jpa/pom.xml URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/pom.xml?rev=766537&view=auto ============================================================================== --- incubator/openwebbeans/trunk/webbeans-jpa/pom.xml (added) +++ incubator/openwebbeans/trunk/webbeans-jpa/pom.xml Sun Apr 19 22:27:17 2009 @@ -0,0 +1,78 @@ + + + + + 4.0.0 + + org.apache.openwebbeans + openwebbeans + 1.0.0-incubating-SNAPSHOT + + + openwebbeans-jpa + OpenWebBeans :: JPA integration + 1.0.0-incubating-SNAPSHOT + JPA plugin for OpenWebBeans + + + + org.apache.openwebbeans + openwebbeans-impl + + + + org.apache.openwebbeans + openwebbeans-impl + 1.0.0-incubating-SNAPSHOT + tests + test + + + + org.apache.geronimo.specs + geronimo-jpa_3.0_spec + + + + junit + junit + + + + log4j + log4j + test + + + + org.apache.openjpa + openjpa + 1.2.0 + test + + + + hsqldb + hsqldb + 1.8.0.7 + test + + + Added: incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/OpenWebBeansJpaPlugin.java URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/OpenWebBeansJpaPlugin.java?rev=766537&view=auto ============================================================================== --- incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/OpenWebBeansJpaPlugin.java (added) +++ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/plugin/OpenWebBeansJpaPlugin.java Sun Apr 19 22:27:17 2009 @@ -0,0 +1,172 @@ +package org.apache.webbeans.jpa.plugin; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.inject.BindingType; + +import org.apache.webbeans.exception.WebBeansConfigurationException; +import org.apache.webbeans.jpa.spi.JPAService; + +import javax.persistence.Entity; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceContext; +import javax.persistence.PersistenceContextType; +import javax.persistence.PersistenceUnit; +import org.apache.webbeans.plugins.OpenWebBeansPlugin; +import org.apache.webbeans.spi.ServiceLoader; +import org.apache.webbeans.util.AnnotationUtil; +import org.apache.webbeans.util.Asserts; +import org.apache.webbeans.xml.XMLAnnotationTypeManager; + +public class OpenWebBeansJpaPlugin implements OpenWebBeansPlugin +{ + + /** {@inheritDoc} */ + public void startUp() throws WebBeansConfigurationException + { + // nothing to do + } + + /** {@inheritDoc} */ + public void shutDown() throws WebBeansConfigurationException + { + // nothing to do + } + + /** {@inheritDoc} */ + public void isSimpleBeanClass( Class clazz ) throws WebBeansConfigurationException + { + if (AnnotationUtil.isAnnotationExistOnClass(clazz, Entity.class)) + throw new WebBeansConfigurationException("Web Beans component implementation class : " + clazz.getName() + " can not be JPA Entity class"); + + if (AnnotationUtil.isAnnotationExistOnClass(clazz, PersistenceContext.class)) + throw new WebBeansConfigurationException("Web Beans component implementation class : " + clazz.getName() + " can not be JPA Entity class"); + + if (AnnotationUtil.isAnnotationExistOnClass(clazz, PersistenceUnit.class)) + throw new WebBeansConfigurationException("Web Beans component implementation class : " + clazz.getName() + " can not be JPA Entity class"); + + } + + /** {@inheritDoc} */ + public Object injectResource(Type type, Annotation[] annotations) + { + if (!isResource(annotations)) + { + return null; + } + + Annotation annot = AnnotationUtil.getAnnotation(annotations, PersistenceUnit.class); + if (annot != null) + { + PersistenceUnit pu = (PersistenceUnit) annot; + String unitName = pu.unitName(); + + //X TODO what if the EntityManagerFactory is null? + return getJPAService().getPersistenceUnit(unitName); + } + + annot = AnnotationUtil.getAnnotation(annotations, PersistenceContext.class); + if (annot != null) + { + PersistenceContext pc = (PersistenceContext) annot; + String unitName = pc.unitName(); + String name = pc.name(); + + //X TODO what if the EntityManager is null? + return getJPAService().getPersistenceContext(unitName, name); + } + return null; + } + + /** + * {@inheritDoc} + * Returns true if the annotation is a valid JPA Resource handled + * by this very Plugin. + * The following annotations indicate such resources: + *
    + *
  1. @CustomerDataservice
  2. + *
  3. @Resource
  4. + *
  5. @PersistenceContext
  6. + *
  7. @PersistenceUnit
  8. + *
  9. @
  10. + *
+ * + * Please note that @PersistenceContext(type=EXTENDED) + * is not supported for simple beans. + * + * @param clazz type of the annotation + * @return true if the annotation is defined in xml or annotated with + * {@link BindingType} false otherwise + */ + public boolean isResourceAnnotation(Class clazz) + { + Asserts.assertNotNull(clazz, "clazz parameter can not be null"); + XMLAnnotationTypeManager manager = XMLAnnotationTypeManager.getInstance(); + if (manager.isResourceExist(clazz)) + { + return true; + } + else if (clazz.equals(PersistenceContext.class) || + clazz.equals(PersistenceUnit.class) ) + { + return true; + } + + return false; + } + + /** {@inheritDoc} */ + public void checkForValidResources(Type type, Class clazz, String name, Annotation[] annotations) + { + for (Annotation a : annotations) + { + if (a.annotationType().equals(PersistenceUnit.class)) + { + if (!type.equals(EntityManagerFactory.class)) + { + throw new WebBeansConfigurationException("@PersistenceUnit must only be injected into field/method with type EntityManagerFactory! class : " + + clazz.getName() + " in field/method : " + name); + } + } + + if (a.annotationType().equals(PersistenceContext.class)) + { + PersistenceContext pc = (PersistenceContext) a; + + if (!type.equals(EntityManager.class)) + { + throw new WebBeansConfigurationException("@PersistenceContext must only be injected into field/method with type EntityManager! class : " + + clazz.getName() + " in field/method : " + name); + } + + if (pc.type().equals(PersistenceContextType.EXTENDED)) + { + throw new WebBeansConfigurationException("type of @PersistenceContext must not be 'EXTENDED'! class : " + + clazz.getName() + " in field/method : " + name); + + } + } + } + } + + private JPAService getJPAService() + { + return ServiceLoader.getService(JPAService.class); + } + + private boolean isResource(Annotation[] annotations) + { + for (Annotation anno : annotations) + { + if (isResourceAnnotation(anno.annotationType())) + { + return true; + } + } + + return false; + } + +} Added: incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/JPAService.java URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/JPAService.java?rev=766537&view=auto ============================================================================== --- incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/JPAService.java (added) +++ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/JPAService.java Sun Apr 19 22:27:17 2009 @@ -0,0 +1,33 @@ +package org.apache.webbeans.jpa.spi; + + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +/** + * SPI for getting EntityManager and EntityManagerFactory depending on the + * System we are running on. This could be plain JPA Persistence or might + * be retrieved from the EJB container. + */ +public interface JPAService +{ + + /** + * get the EntityManagerFactory with the given name. + * @param unitName JPA persistence unit name + * @return EntityManagerFactory or null if not found + */ + public abstract EntityManagerFactory getPersistenceUnit(String unitName); + + /** + * Get a transactional EntityManager for the current thread using a + * ThreadLocal. + * TODO: from the SPEC: the EntityManger must have dependent scope, but this + * does not make sense for e.g. @ApplicationScoped. + * @param unitName the name of the persistence unit. Can be empty or null + * @param name the name of the EntityManager. Can be empty or null + * @return a transactional EntityManager + */ + public abstract EntityManager getPersistenceContext(String unitName, String name); + +} \ No newline at end of file Added: incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/EntityManagersManager.java URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/EntityManagersManager.java?rev=766537&view=auto ============================================================================== --- incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/EntityManagersManager.java (added) +++ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/EntityManagersManager.java Sun Apr 19 22:27:17 2009 @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You 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.apache.webbeans.jpa.spi.se; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.EntityManager; + +public class EntityManagersManager +{ + + /** + * key = unitName + "." + "name" + * value = ThreadLocal<EntityManager> + */ + private Map> entityManagers = new HashMap>(); + + + /** + * @param unitName + * @param name + * @return the EntityManager or null if not yet set + */ + public EntityManager get(String unitName, String name) + { + EntityManager em = null; + String key = getKey(unitName, name); + + ThreadLocal tl = entityManagers.get(key); + if (tl != null) + { + return tl.get(); + } + + return em; + } + + /** + * Set the given EntityManager for the current Thread. + * This will replace any previously stored EntityManager for this + * unitName and name. + * + * @param unitName + * @param name + * @param em + */ + public void set(String unitName, String name, EntityManager em) + { + String key = getKey(unitName, name); + + ThreadLocal tl = entityManagers.get(key); + if (tl == null) + { + tl = new ThreadLocal(); + entityManagers.put(key, tl); + } + + tl.set(em); + } + + + /** + * construct the key for the {@link #entityManagers} Map + * @return String key for the map + */ + private String getKey(String unitName, String name) + { + if (unitName == null) + { + unitName = ""; + } + + if (name == null) + { + name = ""; + } + + return unitName + "." + name; + } + +} Added: incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/JPAServicePersistenceImpl.java URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/JPAServicePersistenceImpl.java?rev=766537&view=auto ============================================================================== --- incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/JPAServicePersistenceImpl.java (added) +++ incubator/openwebbeans/trunk/webbeans-jpa/src/main/java/org/apache/webbeans/jpa/spi/se/JPAServicePersistenceImpl.java Sun Apr 19 22:27:17 2009 @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You 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.apache.webbeans.jpa.spi.se; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.apache.webbeans.config.WebBeansFinder; +import org.apache.webbeans.jpa.spi.JPAService; +import org.apache.webbeans.jpa.spi.se.EntityManagersManager; + +/** + * SPI Implementation which accesses the PersistenceManagerFactory via the Persistence class + */ +public class JPAServicePersistenceImpl implements JPAService +{ + + public static final String SINGLETON_WEBBEANS_ENTITYMANAGERS_MAP = EntityManagersManager.class.getName(); + + /** + * {@inheritDoc} + * + */ + public EntityManagerFactory getPersistenceUnit(String unitName) + { + //X TODO this currently ignores JNDI + EntityManagerFactory emf = Persistence.createEntityManagerFactory(unitName); + + return emf; + } + + /** + * {@inheritDoc} + * TODO: currently this returns an extended EntityManager, so we have to wrap it + */ + public EntityManager getPersistenceContext(String unitName, String name) + { + EntityManagerFactory emf = getPersistenceUnit(unitName); + + EntityManagersManager entityManagersMgr = (EntityManagersManager) WebBeansFinder.getSingletonInstance(SINGLETON_WEBBEANS_ENTITYMANAGERS_MAP); + + EntityManager em = entityManagersMgr.get(unitName, name); + if (em == null) + { + em = emf.createEntityManager(); + entityManagersMgr.set(unitName, name, em); + } + + return em; + } +} Added: incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/org.apache.webbeans.plugins.OpenWebBeansPlugin URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/org.apache.webbeans.plugins.OpenWebBeansPlugin?rev=766537&view=auto ============================================================================== --- incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/org.apache.webbeans.plugins.OpenWebBeansPlugin (added) +++ incubator/openwebbeans/trunk/webbeans-jpa/src/main/resources/META-INF/services/org.apache.webbeans.plugins.OpenWebBeansPlugin Sun Apr 19 22:27:17 2009 @@ -0,0 +1,4 @@ +# this file contains the service implementation for the +# JSF integration into OpenWebBeans +# this file contains information for java.util.ServiceLoader +org.apache.webbeans.jpa.plugin.OpenWebBeansJpaPlugin \ No newline at end of file Added: incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java?rev=766537&view=auto ============================================================================== --- incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java (added) +++ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/component/resource/TstResourcePersistenceBean.java Sun Apr 19 22:27:17 2009 @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You 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.apache.webbeans.test.component.resource; + +import javax.annotation.Named; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceContext; +import javax.persistence.PersistenceUnit; + +/** + * This simple web bean gets a @PersistenceContext injected + * + */ +@Named +public class TstResourcePersistenceBean +{ + + @PersistenceUnit(unitName="openwebbeanstest") + private EntityManagerFactory emf; + + @PersistenceContext(unitName="openwebbeanstest") + private EntityManager em; + + @PersistenceContext(unitName="openwebbeanstest", name="anotherEm") + private EntityManager emNamed; + + private EntityManagerFactory emfMethodInjected; + + private EntityManager emMethodInjected; + + public EntityManagerFactory getFieldInjectedEntityManagerFactory() + { + return emf; + } + + public EntityManager getFieldInjectedEntityManager() + { + return em; + } + + public EntityManager getFieldInjectedEntityManager2() + { + return emNamed; + } + + public EntityManagerFactory getMethodInjectedEntityManagerFactory() + { + return emfMethodInjected; + } + + @PersistenceUnit(unitName="openwebbeanstest") + public void setMethodInjectedEntityManagerFactory(EntityManagerFactory emf2) + { + this.emfMethodInjected = emf2; + } + + public EntityManager getMethodInjectedEntityManager() + { + return emMethodInjected; + } + + @PersistenceContext(unitName="openwebbeanstest") + public void setMethodInjectedEntityManager(EntityManager em3) + { + this.emMethodInjected = em3; + } + +} Added: incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java?rev=766537&view=auto ============================================================================== --- incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java (added) +++ incubator/openwebbeans/trunk/webbeans-jpa/src/test/java/org/apache/webbeans/test/unittests/resource/ResourceInjectionTest.java Sun Apr 19 22:27:17 2009 @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You 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.apache.webbeans.test.unittests.resource; + +import javax.inject.manager.Manager; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import org.apache.webbeans.component.AbstractComponent; +import org.apache.webbeans.container.ManagerImpl; +import org.apache.webbeans.context.ContextFactory; +import org.apache.webbeans.test.component.resource.TstResourcePersistenceBean; +import org.apache.webbeans.test.servlet.TestContext; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ResourceInjectionTest extends TestContext +{ + Manager container = null; + + public ResourceInjectionTest() + { + super(ResourceInjectionTest.class.getSimpleName()); + } + + @Before + public void init() + { + super.init(); + this.container = ManagerImpl.getManager(); + clear(); + } + + @Test + public void testPersistenceContextInjection() throws Exception + { + AbstractComponent tstComponent = defineSimpleWebBean(TstResourcePersistenceBean.class); + Assert.assertNotNull(tstComponent); + + ContextFactory.initRequestContext(null); + ContextFactory.initApplicationContext(null); + + TstResourcePersistenceBean persBean = (TstResourcePersistenceBean) getInstanceByName("tstResourcePersistenceBean"); + Assert.assertNotNull(persBean); + + // test field injection + EntityManagerFactory emf = persBean.getFieldInjectedEntityManagerFactory(); + Assert.assertNotNull(emf); + + EntityManager em = persBean.getFieldInjectedEntityManager(); + Assert.assertNotNull(em); + + EntityManager em2 = persBean.getFieldInjectedEntityManager2(); + Assert.assertNotNull(em2); + + Assert.assertTrue(em != em2); + + // test method injection + EntityManagerFactory emf2 = persBean.getMethodInjectedEntityManagerFactory(); + Assert.assertNotNull(emf2); + + EntityManager em3 = persBean.getMethodInjectedEntityManager(); + Assert.assertNotNull(em3); + } +}