Author: rmannibucau
Date: Thu Jul  5 08:53:09 2012
New Revision: 1357498

URL: http://svn.apache.org/viewvc?rev=1357498&view=rev
Log:
OPENEJB-1857 example with cdi-query

Added:
    openejb/trunk/openejb/examples/cdi-query/
    openejb/trunk/openejb/examples/cdi-query/pom.xml   (with props)
    openejb/trunk/openejb/examples/cdi-query/src/
    openejb/trunk/openejb/examples/cdi-query/src/main/
    openejb/trunk/openejb/examples/cdi-query/src/main/java/
    openejb/trunk/openejb/examples/cdi-query/src/main/java/org/
    openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/
    openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/
    
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/EntityManagerProducer.java
   (with props)
    
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/User.java
   (with props)
    
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/UserDao.java
   (with props)
    openejb/trunk/openejb/examples/cdi-query/src/main/resources/
    openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/
    
openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/beans.xml  
 (with props)
    
openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/persistence.xml
   (with props)
    openejb/trunk/openejb/examples/cdi-query/src/test/
    openejb/trunk/openejb/examples/cdi-query/src/test/java/
    openejb/trunk/openejb/examples/cdi-query/src/test/java/org/
    openejb/trunk/openejb/examples/cdi-query/src/test/java/org/superbiz/
    openejb/trunk/openejb/examples/cdi-query/src/test/java/org/superbiz/dynamic/
    
openejb/trunk/openejb/examples/cdi-query/src/test/java/org/superbiz/dynamic/CdiQueryTest.java
   (with props)
Modified:
    openejb/trunk/openejb/examples/pom.xml

Added: openejb/trunk/openejb/examples/cdi-query/pom.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-query/pom.xml?rev=1357498&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/cdi-query/pom.xml (added)
+++ openejb/trunk/openejb/examples/cdi-query/pom.xml Thu Jul  5 08:53:09 2012
@@ -0,0 +1,121 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.superbiz</groupId>
+  <artifactId>cdi-query</artifactId>
+  <packaging>jar</packaging>
+  <version>1.1-SNAPSHOT</version>
+  <name>OpenEJB :: Examples :: CDI Query</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>javaee-api</artifactId>
+      <version>6.0-4</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>com.ctp.cdi.query</groupId>
+      <artifactId>cdi-query-api</artifactId>
+      <version>1.0.0.Alpha5-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>com.ctp.cdi.query</groupId>
+      <artifactId>cdi-query-impl</artifactId>
+      <version>1.0.0.Alpha5-SNAPSHOT</version>
+      <scope>runtime</scope>
+      <exclusions>
+        <exclusion>
+          <groupId>org.jboss.arquillian.junit</groupId>
+          <artifactId>arquillian-junit-container</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.jboss.arquillian.protocol</groupId>
+          <artifactId>arquillian-protocol-servlet</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.10</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>openejb-core</artifactId>
+      <version>4.1.0-SNAPSHOT</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <defaultGoal>install</defaultGoal>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.4</version>
+        <configuration>
+          <source>1.6</source>
+          <target>1.6</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+  <repositories>
+    <repository>
+      <id>apache-m2-snapshot</id>
+      <name>Apache Snapshot Repository</name>
+      <url>http://repository.apache.org/snapshots</url>
+    </repository>
+    <repository>
+      <id>ctp-consulting-m2-snapshot</id>
+      <name>Ctp Consulting Snapshot Repository</name>
+      <url>https://repository-ctpconsulting.forge.cloudbees.com/snapshot/</url>
+    </repository>
+  </repositories>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <!--
+  This section allows you to configure where to publish libraries for sharing.
+  It is not required and may be deleted.  For more information see:
+  http://maven.apache.org/plugins/maven-deploy-plugin/
+  -->
+  <distributionManagement>
+    <repository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/repo/</url>
+    </repository>
+    <snapshotRepository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/snapshot-repo/</url>
+    </snapshotRepository>
+  </distributionManagement>
+</project>
+

Propchange: openejb/trunk/openejb/examples/cdi-query/pom.xml
------------------------------------------------------------------------------
    svn:executable = *

Added: 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/EntityManagerProducer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/EntityManagerProducer.java?rev=1357498&view=auto
==============================================================================
--- 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/EntityManagerProducer.java
 (added)
+++ 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/EntityManagerProducer.java
 Thu Jul  5 08:53:09 2012
@@ -0,0 +1,30 @@
+/*
+ * 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.superbiz.dynamic;
+
+
+import javax.ejb.Stateless;
+import javax.enterprise.inject.Produces;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+@Stateless
+public class EntityManagerProducer {
+    @PersistenceContext
+    @Produces
+    private EntityManager em;
+}

Propchange: 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/EntityManagerProducer.java
------------------------------------------------------------------------------
    svn:executable = *

Added: 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/User.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/User.java?rev=1357498&view=auto
==============================================================================
--- 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/User.java
 (added)
+++ 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/User.java
 Thu Jul  5 08:53:09 2012
@@ -0,0 +1,54 @@
+/*
+ * 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.superbiz.dynamic;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+@Entity
+public class User {
+    @Id
+    @GeneratedValue
+    private long id;
+    private String name;
+    private int age;
+
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+}

Propchange: 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/User.java
------------------------------------------------------------------------------
    svn:executable = *

Added: 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/UserDao.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/UserDao.java?rev=1357498&view=auto
==============================================================================
--- 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/UserDao.java
 (added)
+++ 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/UserDao.java
 Thu Jul  5 08:53:09 2012
@@ -0,0 +1,24 @@
+/**
+ * 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.superbiz.dynamic;
+
+import com.ctp.cdi.query.Dao;
+import com.ctp.cdi.query.EntityDao;
+
+@Dao
+public interface UserDao extends EntityDao<User, Long> {
+}

Propchange: 
openejb/trunk/openejb/examples/cdi-query/src/main/java/org/superbiz/dynamic/UserDao.java
------------------------------------------------------------------------------
    svn:executable = *

Added: 
openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/beans.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/beans.xml?rev=1357498&view=auto
==============================================================================
--- 
openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/beans.xml 
(added)
+++ 
openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/beans.xml 
Thu Jul  5 08:53:09 2012
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://java.sun.com/xml/ns/javaee";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"; />

Propchange: 
openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/beans.xml
------------------------------------------------------------------------------
    svn:executable = *

Added: 
openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/persistence.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/persistence.xml?rev=1357498&view=auto
==============================================================================
--- 
openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/persistence.xml
 (added)
+++ 
openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/persistence.xml
 Thu Jul  5 08:53:09 2012
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+  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.
+-->
+<persistence version="2.0"
+             xmlns="http://java.sun.com/xml/ns/persistence";
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+             xsi:schemaLocation="
+      http://java.sun.com/xml/ns/persistence
+      http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd";>
+  <persistence-unit name="dynamic" transaction-type="JTA">
+    <jta-data-source>jdbc/dynamicDB</jta-data-source>
+    <class>org.superbiz.dynamic.User</class>
+    <properties>
+      <property name="openjpa.jdbc.SynchronizeMappings" 
value="buildSchema(ForeignKeys=true)"/>
+    </properties>
+  </persistence-unit>
+</persistence>

Propchange: 
openejb/trunk/openejb/examples/cdi-query/src/main/resources/META-INF/persistence.xml
------------------------------------------------------------------------------
    svn:executable = *

Added: 
openejb/trunk/openejb/examples/cdi-query/src/test/java/org/superbiz/dynamic/CdiQueryTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/cdi-query/src/test/java/org/superbiz/dynamic/CdiQueryTest.java?rev=1357498&view=auto
==============================================================================
--- 
openejb/trunk/openejb/examples/cdi-query/src/test/java/org/superbiz/dynamic/CdiQueryTest.java
 (added)
+++ 
openejb/trunk/openejb/examples/cdi-query/src/test/java/org/superbiz/dynamic/CdiQueryTest.java
 Thu Jul  5 08:53:09 2012
@@ -0,0 +1,103 @@
+/*
+ * 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.superbiz.dynamic;
+
+import java.util.Collection;
+import java.util.Properties;
+import javax.ejb.Lock;
+import javax.ejb.LockType;
+import javax.ejb.Singleton;
+import javax.ejb.embeddable.EJBContainer;
+import javax.inject.Inject;
+import javax.naming.NamingException;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static junit.framework.Assert.assertEquals;
+
+/**
+ * the UserDao can be injected but doesn't manage transaction so we create
+ * a TxProvider bean which provides the tx context.
+ *
+ * In this sample it simply delegates but in real life it often aggregates 
multiple calls.
+ */
+public class CdiQueryTest {
+    private static EJBContainer container;
+    private static boolean initialized = false;
+
+    @Inject
+    private TxProvider dao;
+
+    @Test
+    public void findAll() {
+        Collection<User> users = dao.findAll();
+        assertEquals(10, users.size());
+    }
+
+    @BeforeClass
+    public static void start() throws Exception {
+        final Properties p = new Properties();
+
+        p.setProperty("jdbc/CdiQueryTest", "new://Resource?type=DataSource");
+        p.setProperty("jdbc/CdiQueryTest.JdbcDriver", "org.hsqldb.jdbcDriver");
+        p.setProperty("jdbc/CdiQueryTest.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+        p.setProperty("jdbc/CdiQueryTest.UserName", "sa");
+        p.setProperty("jdbc/CdiQueryTest.Password", "");
+
+        container = EJBContainer.createEJBContainer(p);
+    }
+
+    @Before
+    public void injectAndInit() throws NamingException {
+        container.getContext().bind("inject", this);
+        if (!initialized) {
+            for (int i = 0; i < 10; i++) {
+                final User u = new User();
+                u.setAge(i % 4);
+                if (i % 3 == 0) {
+                    u.setName("foo");
+                } else {
+                    u.setName("bar-" + i);
+                }
+                dao.save(u);
+            }
+            initialized = true;
+        }
+    }
+
+    @AfterClass
+    public static void close() {
+        container.close();
+    }
+
+    @Singleton
+    @Lock(LockType.READ)
+    public static class TxProvider { // just here to provide the transactional 
context
+        @Inject
+        private UserDao dao;
+
+        public Collection<User> findAll() {
+            return dao.findAll();
+        }
+
+        public void save(final User u) {
+            dao.save(u);
+        }
+    }
+}

Propchange: 
openejb/trunk/openejb/examples/cdi-query/src/test/java/org/superbiz/dynamic/CdiQueryTest.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: openejb/trunk/openejb/examples/pom.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/pom.xml?rev=1357498&r1=1357497&r2=1357498&view=diff
==============================================================================
--- openejb/trunk/openejb/examples/pom.xml (original)
+++ openejb/trunk/openejb/examples/pom.xml Thu Jul  5 08:53:09 2012
@@ -46,6 +46,7 @@
     <module>cdi-application-scope</module>
     <module>cdi-produces-disposes</module>
     <module>cdi-produces-field</module>
+    <module>cdi-query</module>
     <module>component-interfaces</module>
     <module>custom-injection</module>
     <module>datasource-ciphered-password</module>


Reply via email to