Skip to content

Commit

Permalink
Add test for XML deployed datasources when used with JPA
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Jan 31, 2012
1 parent 737538d commit c0a5a34
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class DeployedXmlDataSourceManagementTestCase {
@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class, "testDsXmlDeployment.jar")
.addPackage(DeployedXmlDataSourceManagementTestCase.class.getPackage())
.addClass(DeployedXmlDataSourceManagementTestCase.class)
.addAsManifestResource(DeployedXmlDataSourceManagementTestCase.class.getPackage(), "MANIFEST.MF", "MANIFEST.MF");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class DeployedXmlDataSourceTestCase {
@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class, "testDsXmlDeployment.jar")
.addPackage(DeployedXmlDataSourceTestCase.class.getPackage())
.addClass(DeployedXmlDataSourceTestCase.class)
.addAsManifestResource(DeployedXmlDataSourceTestCase.class.getPackage(), "MANIFEST.MF", "MANIFEST.MF");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2010, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.test.integration.deployment.xml.datasource;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.naming.InitialContext;

import org.jboss.arquillian.container.test.api.Deployer;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.standalone.DeploymentPlan;
import org.jboss.as.controller.client.helpers.standalone.ServerDeploymentActionResult;
import org.jboss.as.controller.client.helpers.standalone.ServerDeploymentManager;
import org.jboss.as.controller.client.helpers.standalone.ServerDeploymentPlanResult;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.jboss.as.arquillian.container.Authentication.getCallbackHandler;

/**
* Test deployment of -ds.xml files as JPA data sources
*
* @author Stuart Douglas
*/
@RunWith(Arquillian.class)
public class DeployedXmlJpaDataSourceTestCase {


private static ModelControllerClient client;
private static ServerDeploymentManager manager;
public static final String TEST_DS_XML = "test-ds.xml";
public static final String JPA_DEPLOYMENT_NAME = "jpaDeployment";

@Deployment(name = "runner")
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class, "testDsXmlJpaDeployment.jar")
.addClasses(DeployedXmlJpaDataSourceTestCase.class, JpaRemote.class)
.addAsManifestResource(DeployedXmlJpaDataSourceTestCase.class.getPackage(),
"MANIFEST.MF", "MANIFEST.MF");
}

@Deployment(name = JPA_DEPLOYMENT_NAME, testable = false, managed = false)
public static Archive<?> deployJpa() {
return ShrinkWrap.create(JavaArchive.class, JPA_DEPLOYMENT_NAME + ".jar")
.addClasses(Employee.class, JpaRemoteBean.class,
JpaRemote.class)
.addAsManifestResource(DeployedXmlJpaDataSourceTestCase.class.getPackage(),
"MANIFEST.MF", "MANIFEST.MF")
.addAsManifestResource(DeployedXmlJpaDataSourceTestCase.class.getPackage(),
"persistence.xml", "persistence.xml");
}

@ArquillianResource
private InitialContext initialContext;

@ArquillianResource
private Deployer deployer;

@BeforeClass
public static void deployDatasource() throws Throwable {
try {
client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9999, getCallbackHandler());
manager = ServerDeploymentManager.Factory.create(client);
final String packageName = DeployedXmlJpaDataSourceTestCase.class.getPackage().getName().replace(".", "/");
final DeploymentPlan plan = manager.newDeploymentPlan().add(DeployedXmlJpaDataSourceTestCase.class.getResource("/" + packageName + "/" + TEST_DS_XML)).andDeploy().build();
final Future<ServerDeploymentPlanResult> future = manager.execute(plan);
final ServerDeploymentPlanResult result = future.get(20, TimeUnit.SECONDS);
final ServerDeploymentActionResult actionResult = result.getDeploymentActionResult(plan.getId());
if (actionResult != null) {
if (actionResult.getDeploymentException() != null) {
throw actionResult.getDeploymentException();
}
}
} catch (Throwable e) {
if (client != null) {
client.close();
}
}
}

@AfterClass
public static void undeployDatasource() throws IOException {
if (client != null) {
final DeploymentPlan undeployPlan = manager.newDeploymentPlan().undeploy(TEST_DS_XML).andRemoveUndeployed().build();
manager.execute(undeployPlan);
client.close();
client = null;
manager = null;
}
}

@Test
public void testJpaUsedWithXMLXaDataSource() throws Throwable {
deployer.deploy(JPA_DEPLOYMENT_NAME);
try {
final JpaRemote remote = (JpaRemote) initialContext.lookup("java:global/" + JPA_DEPLOYMENT_NAME + "/" + "JpaRemoteBean");
remote.addEmployee("Bob");
remote.addEmployee("Sue");
final Set<String> emps = remote.getEmployees();
Assert.assertEquals(2, emps.size());
Assert.assertTrue(emps.contains("Bob"));
Assert.assertTrue(emps.contains("Sue"));
} finally {
deployer.undeploy(JPA_DEPLOYMENT_NAME);
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.test.integration.deployment.xml.datasource;

import javax.persistence.Entity;
import javax.persistence.Id;

/**
* Employee entity class
*
* @author Scott Marlow
*/
@Entity
public class Employee {
@Id
private int id;

private String name;

private String address;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getId() {

return id;
}

public void setId(int id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jboss.as.test.integration.deployment.xml.datasource;

import java.util.Set;

import javax.ejb.Remote;

/**
* @author Stuart Douglas
*/
@Remote
public interface JpaRemote {

public void addEmployee(String name);

public Set<String> getEmployees();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jboss.as.test.integration.deployment.xml.datasource;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
* @author Stuart Douglas
*/
@Stateless
public class JpaRemoteBean implements JpaRemote {

private static final AtomicInteger idGenerator = new AtomicInteger(0);

@PersistenceContext
private EntityManager entityManager;

@Override
public void addEmployee(final String name) {
Employee e = new Employee();
e.setId(idGenerator.incrementAndGet());
e.setName(name);
entityManager.persist(e);
}

@Override
public Set<String> getEmployees() {
final List<Employee> emps = entityManager.createQuery("from Employee").getResultList();
final Set<String> ret = new HashSet<String>();
for (Employee e : emps) {
ret.add(e.getName());
}
return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="mypc">
<description>Persistence Unit.
</description>
<jta-data-source>java:/H2XADS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class DeployedXmlJMSManagementTestCase {
@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class, "testJMSXmlDeployment.jar")
.addPackage(DeployedXmlJMSManagementTestCase.class.getPackage())
.addClass(DeployedXmlJMSManagementTestCase.class)
.addAsManifestResource(DeployedXmlJMSManagementTestCase.class.getPackage(), "MANIFEST.MF", "MANIFEST.MF");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class DeployedXmlJMSTestCase {
@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class, "testDsXmlDeployment.jar")
.addPackage(DeployedXmlDataSourceTestCase.class.getPackage())
.addClass(DeployedXmlJMSTestCase.class)
.addAsManifestResource(DeployedXmlDataSourceTestCase.class.getPackage(), "MANIFEST.MF", "MANIFEST.MF");
}

Expand Down

0 comments on commit c0a5a34

Please sign in to comment.