tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1153863 - in /openejb/trunk/openejb3/examples: ./ dynamic-dao-implementation/ dynamic-dao-implementation/src/ dynamic-dao-implementation/src/main/ dynamic-dao-implementation/src/main/java/ dynamic-dao-implementation/src/main/java/org/ dyna...
Date Thu, 04 Aug 2011 12:30:41 GMT
Author: rmannibucau
Date: Thu Aug  4 12:30:40 2011
New Revision: 1153863

URL: http://svn.apache.org/viewvc?rev=1153863&view=rev
Log:
adding dynamic-dao-implementation example

Added:
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/README.md
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/pom.xml
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/dynamic/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/dynamic/User.java
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/dynamic/UserDao.java
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/resources/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/resources/META-INF/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/resources/META-INF/persistence.xml
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/test/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/test/java/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/test/java/org/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/test/java/org/superbiz/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/test/java/org/superbiz/dynamic/
    openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java
Modified:
    openejb/trunk/openejb3/examples/pom.xml

Added: openejb/trunk/openejb3/examples/dynamic-dao-implementation/README.md
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-dao-implementation/README.md?rev=1153863&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-dao-implementation/README.md (added)
+++ openejb/trunk/openejb3/examples/dynamic-dao-implementation/README.md Thu Aug  4 12:30:40
2011
@@ -0,0 +1,21 @@
+# Dynamic DAO Implementation
+
+Coming soon
+
+# Download
+
+[Download as zip](${zip})
+
+# Files
+
+## APIs
+
+${apis}
+
+## Java files
+
+${javas}
+
+## Resource files
+
+${resources}

Added: openejb/trunk/openejb3/examples/dynamic-dao-implementation/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-dao-implementation/pom.xml?rev=1153863&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-dao-implementation/pom.xml (added)
+++ openejb/trunk/openejb3/examples/dynamic-dao-implementation/pom.xml Thu Aug  4 12:30:40
2011
@@ -0,0 +1,94 @@
+<?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>dynamic-dao-implementation</artifactId>
+  <packaging>jar</packaging>
+  <version>1.1-SNAPSHOT</version>
+  <name>OpenEJB :: Examples :: Dynamic DAO Implementation</name>
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+  <build>
+    <defaultGoal>install</defaultGoal>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.3.2</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>
+  </repositories>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>javaee-api-embedded</artifactId>
+      <version>6.0-SNAPSHOT</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.8.1</version>
+      <scope>test</scope>
+    </dependency>
+
+    <!--
+    The <scope>test</scope> guarantees that non of your runtime
+    code is dependent on any OpenEJB classes.
+    -->
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>openejb-core</artifactId>
+      <version>4.0.0-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+  <!--
+  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>
+

Added: openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/dynamic/User.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/dynamic/User.java?rev=1153863&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/dynamic/User.java
(added)
+++ openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/dynamic/User.java
Thu Aug  4 12:30:40 2011
@@ -0,0 +1,45 @@
+package org.superbiz.dynamic;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+
+/**
+ * @author rmannibucau
+ */
+@Entity
+@NamedQueries({
+    @NamedQuery(name = "dynamic-ejb-impl-test.query", query = "SELECT u FROM User AS u WHERE
u.name LIKE :name"),
+    @NamedQuery(name = "dynamic-ejb-impl-test.all", query = "SELECT u FROM User AS u")
+})
+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;
+    }
+}

Added: openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/dynamic/UserDao.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/dynamic/UserDao.java?rev=1153863&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/dynamic/UserDao.java
(added)
+++ openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/java/org/superbiz/dynamic/UserDao.java
Thu Aug  4 12:30:40 2011
@@ -0,0 +1,40 @@
+package org.superbiz.dynamic;
+
+/**
+ * @author rmannibucau
+ */
+
+import javax.ejb.Stateless;
+import javax.persistence.PersistenceContext;
+import java.util.Collection;
+import java.util.Map;
+
+@Stateless
+@PersistenceContext(name = "dynamic")
+public interface UserDao {
+    User findById(long id);
+
+    Collection<User> findByName(String name);
+
+    Collection<User> findByNameAndAge(String name, int age);
+
+    Collection<User> findAll();
+
+    Collection<User> findAll(int first, int max);
+
+    Collection<User> namedQuery(String name, Map<String, ?> params, int first,
int max);
+
+    Collection<User> namedQuery(String name, int first, int max, Map<String, ?>
params);
+
+    Collection<User> namedQuery(String name, Map<String, ?> params);
+
+    Collection<User> namedQuery(String name);
+
+    Collection<User> query(String value, Map<String, ?> params);
+
+    void save(User u);
+
+    void delete(User u);
+
+    User update(User u);
+}

Added: openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/resources/META-INF/persistence.xml?rev=1153863&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/resources/META-INF/persistence.xml
(added)
+++ openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/main/resources/META-INF/persistence.xml
Thu Aug  4 12:30:40 2011
@@ -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>

Added: openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java?rev=1153863&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java
(added)
+++ openejb/trunk/openejb3/examples/dynamic-dao-implementation/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java
Thu Aug  4 12:30:40 2011
@@ -0,0 +1,162 @@
+package org.superbiz.dynamic;
+
+import junit.framework.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import javax.ejb.EJBException;
+import javax.ejb.Stateless;
+import javax.ejb.embeddable.EJBContainer;
+import javax.naming.Context;
+import javax.persistence.EntityManager;
+import javax.persistence.NoResultException;
+import javax.persistence.PersistenceContext;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+
+/**
+ * @author rmannibucau
+ */
+public class DynamicUserDaoTest {
+    private static UserDao dao;
+    private static Util util;
+
+    @BeforeClass public static void init() throws Exception {
+        final Properties p = new Properties();
+        p.put("jdbc/dynamicDB", "new://Resource?type=DataSource");
+        p.put("jdbc/dynamicDB.JdbcDriver", "org.hsqldb.jdbcDriver");
+        p.put("jdbc/dynamicDB.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+        p.put("jdbc/dynamicDB.UserName", "sa");
+        p.put("jdbc/dynamicDB.Password", "");
+
+        final Context context = EJBContainer.createEJBContainer(p).getContext();
+        dao = (UserDao) context.lookup("java:global/dynamic-dao-implementation/UserDao");
+        util = (Util) context.lookup("java:global/dynamic-dao-implementation/Util");
+
+        util.init(); // init database
+    }
+
+    @Test public void simple() {
+        User user = dao.findById(1);
+        assertNotNull(user);
+        assertEquals(1, user.getId());
+    }
+
+    @Test public void findAll() {
+        Collection<User> users = dao.findAll();
+        assertEquals(10, users.size());
+    }
+
+    @Test public void pagination() {
+        Collection<User> users = dao.findAll(0, 5);
+        assertEquals(5, users.size());
+
+        users = dao.findAll(6, 1);
+        assertEquals(1, users.size());
+        assertEquals(7, users.iterator().next().getId());
+    }
+
+    @Test public void persist() {
+        User u = new User();
+        dao.save(u);
+        assertNotNull(u.getId());
+        util.remove(u);
+    }
+
+    @Test public void remove() {
+        User u = new User();
+        dao.save(u);
+        assertNotNull(u.getId());
+        dao.delete(u);
+        try {
+            dao.findById(u.getId());
+            Assert.fail();
+        } catch (EJBException ee) {
+            assertTrue(ee.getCause() instanceof NoResultException);
+        }
+    }
+
+    @Test public void merge() {
+        User u = new User();
+        u.setAge(1);
+        dao.save(u);
+        assertEquals(1, u.getAge());
+        assertNotNull(u.getId());
+
+        u.setAge(2);
+        dao.update(u);
+        assertEquals(2, u.getAge());
+
+        dao.delete(u);
+    }
+
+    @Test public void oneCriteria() {
+        Collection<User> users = dao.findByName("foo");
+        assertEquals(4, users.size());
+        for (User user : users) {
+            assertEquals("foo", user.getName());
+        }
+    }
+
+    @Test public void twoCriteria() {
+        Collection<User> users = dao.findByNameAndAge("bar-1", 1);
+        assertEquals(1, users.size());
+
+        User user = users.iterator().next();
+        assertEquals("bar-1", user.getName());
+        assertEquals(1, user.getAge());
+    }
+
+    @Test public void query() {
+        Map<String, Object> params = new HashMap<String, Object> ();
+        params.put("name", "foo");
+
+        Collection<User> users = dao.namedQuery("dynamic-ejb-impl-test.query", params,
0, 100);
+        assertEquals(4, users.size());
+
+        users = dao.namedQuery("dynamic-ejb-impl-test.query", params);
+        assertEquals(4, users.size());
+
+        users = dao.namedQuery("dynamic-ejb-impl-test.query", params, 0, 2);
+        assertEquals(2, users.size());
+
+        users = dao.namedQuery("dynamic-ejb-impl-test.query", 0, 2, params);
+        assertEquals(2, users.size());
+
+        users = dao.namedQuery("dynamic-ejb-impl-test.all");
+        assertEquals(10, users.size());
+
+        params.remove("name");
+        params.put("age", 1);
+        users = dao.query("SELECT u FROM User AS u WHERE u.age LIKE :age", params);
+        assertEquals(3, users.size());
+    }
+
+    @Stateless public static class Util {
+        @PersistenceContext private EntityManager em;
+
+        public void remove(Object o) {
+            em.remove(o);
+        }
+
+        public void init() {
+            for (int i = 0; i < 10; i++) {
+                User u = new User();
+                u.setAge(i % 4);
+                if (i % 3 == 0) {
+                    u.setName("foo");
+                } else {
+                    u.setName("bar-" + i);
+                }
+                em.persist(u);
+            }
+        }
+
+    }
+}

Modified: openejb/trunk/openejb3/examples/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/pom.xml?rev=1153863&r1=1153862&r2=1153863&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/pom.xml (original)
+++ openejb/trunk/openejb3/examples/pom.xml Thu Aug  4 12:30:40 2011
@@ -85,6 +85,7 @@
     <module>cdi-example</module>
 	<module>cdi-interceptors</module>
     <module>rest-on-ejb</module>
+    <module>dynamic-dao-implementation</module>
   </modules>
   <profiles>
     <profile>



Mime
View raw message