Help

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

A typical Seam project (created with seam-gen or JBoss Tools) relies on the default HSQLDB data source that's included with the Embedded JBoss bootstrap configuration, which is assigned the JNDI name java:/DefaultDS. This may be fine if you are seeding the database before running your tests. However, if you need to test against an existing database, you will need to setup your own connection.

To establish your own connection, you need two things:

  1. a data source configuration file
  2. the appropriate JDBC driver.

Let's start with the data source configuration file. Create a standard JBoss data source configuration file ending in -ds.xml. The file prefix does not matter. Here, we are establishing a connection to a MySQL database.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE datasources PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
   "http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
<datasources>
   <local-tx-datasource>
      <jndi-name>MySQLDS</jndi-name>
      <connection-url>jdbc:mysql://localhost/database1</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name>username</user-name>
      <password>password</password>
   </local-tx-datasource>
</datasources>
NOTE - Embedded JBoss does not support the <use-java-context>false</use-java-context> setting. In fact, if this element is present and set to false, Embedded JBoss will fail to start! Therefore, you will always address the JNDI name using the java:/ prefix in your persistence unit descriptor used for tests (in this case java:/MySQLDS).

Put this file in the bootstrap/deploy folder. The bootstrap folder mirrors a JBoss domain directory, so the deploy folder is roughly equivalent to server/default/deploy in a typical JBoss installation.

Next, configure the persistence unit descriptor (e.g., resources/META-INF/persistence-test.xml in seam-gen projects) to reference this data source. It's also wise to set the Hibernate dialect property, though it may not be necessary in all cases:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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" 
   version="1.0">
   <persistence-unit name="vehicles" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/MySQLDS</jta-data-source>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="none"/>
         <property name="hibernate.transaction.manager_lookup_class"
            value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
      </properties>
   </persistence-unit>
</persistence>

Finally, you have to make sure the JDBC driver (in this case the MySQL driver) is available to Embedded JBoss. It just needs to be on the test classpath when TestNG executes. In seam-gen projects, that means simply placing it in the lib or lib/test folder.

lib/mysql-connector-java.jar

Now you should be able to run your tests against your own database.