[jboss-svn-commits] JBoss Common SVN: r3937 - in arquillian/trunk/demo-testng: src/main/java/com/acme and 7 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Jan 20 16:51:34 EST 2010


Author: aslak
Date: 2010-01-20 16:51:34 -0500 (Wed, 20 Jan 2010)
New Revision: 3937

Added:
   arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/
   arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/User.java
   arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/UserRepository.java
   arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/UserRepositoryBean.java
   arquillian/trunk/demo-testng/src/main/resources/com/
   arquillian/trunk/demo-testng/src/main/resources/com/acme/
   arquillian/trunk/demo-testng/src/main/resources/com/acme/jpa/
   arquillian/trunk/demo-testng/src/main/resources/com/acme/jpa/user-test-persistence.xml
   arquillian/trunk/demo-testng/src/test/java/com/acme/jpa/
   arquillian/trunk/demo-testng/src/test/java/com/acme/jpa/UserRepositoryTestCase.java
Modified:
   arquillian/trunk/demo-testng/pom.xml
Log:
ARQ-35 Added JPA/EJB3 example

Modified: arquillian/trunk/demo-testng/pom.xml
===================================================================
--- arquillian/trunk/demo-testng/pom.xml	2010-01-20 18:33:18 UTC (rev 3936)
+++ arquillian/trunk/demo-testng/pom.xml	2010-01-20 21:51:34 UTC (rev 3937)
@@ -106,8 +106,13 @@
          <version>1.1</version>
          <scope>provided</scope>       
       </dependency>
-
       <dependency>
+         <groupId>javax.persistence</groupId> 
+         <artifactId>persistence-api</artifactId> 
+         <version>1.0</version>
+         <scope>provided</scope>       
+      </dependency>
+      <dependency>
          <groupId>org.jboss.arquillian</groupId>
          <artifactId>arquillian-testng</artifactId>
          <version>${project.version}</version>

Added: arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/User.java
===================================================================
--- arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/User.java	                        (rev 0)
+++ arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/User.java	2010-01-20 21:51:34 UTC (rev 3937)
@@ -0,0 +1,70 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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.acme.jpa;
+
+import java.io.Serializable;
+import java.util.UUID;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+/**
+ * User
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+ at Entity
+ at Table
+public class User implements Serializable
+{
+   private static final long serialVersionUID = 1L;
+
+   @Id
+   private String id;
+   
+   private String firstName; 
+   
+   private String lastName;
+   
+   protected User()
+   {
+   }
+   
+   public User(String firstName, String lastName) 
+   {
+      this.id = UUID.randomUUID().toString();
+      this.firstName = firstName;
+      this.lastName = lastName;
+   }
+   
+   public String getId()
+   {
+      return id;
+   }
+   
+   public String getFirstName()
+   {
+      return firstName;
+   }
+   
+   public String getLastName()
+   {
+      return lastName;
+   }
+}

Added: arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/UserRepository.java
===================================================================
--- arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/UserRepository.java	                        (rev 0)
+++ arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/UserRepository.java	2010-01-20 21:51:34 UTC (rev 3937)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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.acme.jpa;
+
+import java.util.List;
+
+/**
+ * UserRepository
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public interface UserRepository
+{
+   List<User> getByFirstName(String firstName);
+   
+   void store(User user);
+   
+}

Added: arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/UserRepositoryBean.java
===================================================================
--- arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/UserRepositoryBean.java	                        (rev 0)
+++ arquillian/trunk/demo-testng/src/main/java/com/acme/jpa/UserRepositoryBean.java	2010-01-20 21:51:34 UTC (rev 3937)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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.acme.jpa;
+
+import java.util.List;
+
+import javax.ejb.Local;
+import javax.ejb.Stateless;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+/**
+ * UserRepositoryBean
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+ at Local(UserRepository.class)
+ at Stateless
+public class UserRepositoryBean implements UserRepository
+{
+
+   @PersistenceContext(unitName = "Domain")
+   private EntityManager entityManager;
+   
+   /* (non-Javadoc)
+    * @see com.acme.cdi.jpa.UserRepository#getByFirstName(java.lang.String)
+    */
+   @SuppressWarnings("unchecked")
+   @Override
+   public List<User> getByFirstName(String firstName)
+   {
+      return entityManager.createQuery("from User user where user.firstName = :firstName")
+                           .setParameter("firstName", firstName)
+                           .getResultList();
+   }
+
+   /* (non-Javadoc)
+    * @see com.acme.cdi.jpa.UserRepository#store(com.acme.cdi.jpa.User)
+    */
+   @Override
+   public void store(User user)
+   {
+      entityManager.persist(user);
+   }
+
+}

Added: arquillian/trunk/demo-testng/src/main/resources/com/acme/jpa/user-test-persistence.xml
===================================================================
--- arquillian/trunk/demo-testng/src/main/resources/com/acme/jpa/user-test-persistence.xml	                        (rev 0)
+++ arquillian/trunk/demo-testng/src/main/resources/com/acme/jpa/user-test-persistence.xml	2010-01-20 21:51:34 UTC (rev 3937)
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<persistence version="1.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_1_0.xsd">
+   <persistence-unit name="Domain" transaction-type="JTA">
+      <provider>org.hibernate.ejb.HibernatePersistence</provider>
+      <jta-data-source>java:/DefaultDS</jta-data-source>
+      <class>com.acme.jpa.User</class>
+      <exclude-unlisted-classes>true</exclude-unlisted-classes>
+      <properties>
+         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
+         <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
+      </properties>
+   </persistence-unit>
+</persistence>
\ No newline at end of file

Added: arquillian/trunk/demo-testng/src/test/java/com/acme/jpa/UserRepositoryTestCase.java
===================================================================
--- arquillian/trunk/demo-testng/src/test/java/com/acme/jpa/UserRepositoryTestCase.java	                        (rev 0)
+++ arquillian/trunk/demo-testng/src/test/java/com/acme/jpa/UserRepositoryTestCase.java	2010-01-20 21:51:34 UTC (rev 3937)
@@ -0,0 +1,77 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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.acme.jpa;
+
+import java.util.List;
+
+import javax.ejb.EJB;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.testng.Arquillian;
+import org.jboss.shrinkwrap.api.Archives;
+import org.jboss.shrinkwrap.api.Paths;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.acme.jpa.User;
+import com.acme.jpa.UserRepository;
+
+/**
+ * UserRepositoryTest
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+ at Test(groups = "integration")
+public class UserRepositoryTestCase extends Arquillian
+{
+   @Deployment
+   public static JavaArchive createDeployment() {
+      return Archives.create("test.jar", JavaArchive.class)
+               .addPackage(
+                     User.class.getPackage())
+               .addManifestResource(
+                     "com/acme/jpa/user-test-persistence.xml", 
+                     Paths.create("persistence.xml"));
+   }
+   
+   private static final String FIRST_NAME = "first-name";
+   private static final String LAST_NAME = "last-name";
+   
+   @EJB
+   private UserRepository userRepository;
+   
+   @Test
+   public void shouldBeAbleToStoreUser() throws Exception 
+   {
+      userRepository.store(new User(FIRST_NAME, LAST_NAME));
+   }
+   
+   //@Test(dependsOnMethods = "shouldBeAbleToStoreUser")
+   @Test
+   public void shouldBeAbleToFindUser() throws Exception 
+   {
+      List<User> users  = userRepository.getByFirstName(FIRST_NAME);
+      
+      Assert.assertNotNull(users);
+      Assert.assertTrue(users.size() == 1);
+      
+      Assert.assertEquals(users.get(0).getLastName(), LAST_NAME);
+      Assert.assertEquals(users.get(0).getFirstName(), FIRST_NAME);
+   }
+}



More information about the jboss-svn-commits mailing list