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.

FAQ Category:

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.

5 comments:
 
14. Jun 2009, 02:42 America/New_York | Link
LSL

I tried your suggestions and it doesn't work for me (Seam 2.1.2 GA). I get the following exception stacktrace:

FAILED CONFIGURATION: @BeforeSuite startSeam
org.jboss.deployers.client.spi.IncompleteDeploymentException: Summary of incomplete deployments (SEE PREVIOUS ERRORS FOR DETAILS):

*** CONTEXTS MISSING DEPENDENCIES: Name -> Dependency{Required State:Actual State}

jboss.jca:name=JmsXA,service=ConnectionFactoryBinding
 -> jboss.jca:name=JmsXA,service=TxCM{Start:Configured}
 -> jboss.jca:name=JmsXA,service=TxCM{Create:Configured}

jboss.jca:name=JmsXA,service=ManagedConnectionFactory
 -> jboss.messaging:service=ServerPeer{Create:Configured}
 -> jboss.messaging:service=ServerPeer{Start:Configured}

jboss.jca:name=JmsXA,service=ManagedConnectionPool
 -> jboss.jca:name=JmsXA,service=ManagedConnectionFactory{Create:Configured}
 -> jboss.jca:name=JmsXA,service=ManagedConnectionFactory{Start:Configured}

jboss.jca:name=JmsXA,service=TxCM
 -> jboss.jca:name=JmsXA,service=ManagedConnectionPool{Start:Configured}
 -> jboss.jca:name=JmsXA,service=ManagedConnectionPool{Create:Configured}

jboss.messaging.connectionfactory:service=ClusterPullConnectionFactory
 -> jboss.messaging:service=PostOffice{Create:Configured}
 -> jboss.messaging:service=ServerPeer{Create:Configured}
 -> jboss.messaging:service=PostOffice{Start:Configured}
 -> jboss.messaging:service=ServerPeer{Start:Configured}

jboss.messaging.connectionfactory:service=ClusteredConnectionFactory
 -> jboss.messaging:service=ServerPeer{Start:Configured}
 -> jboss.messaging:service=PostOffice{Start:Configured}
 -> jboss.messaging:service=ServerPeer{Create:Configured}
 -> jboss.messaging:service=PostOffice{Create:Configured}

jboss.messaging.connectionfactory:service=ConnectionFactory
 -> jboss.messaging:service=PostOffice{Start:Configured}
 -> jboss.messaging:service=PostOffice{Create:Configured}
 -> jboss.messaging:service=ServerPeer{Start:Configured}
 -> jboss.messaging:service=ServerPeer{Create:Configured}

jboss.messaging.destination:name=DLQ,service=Queue
 -> jboss.messaging:service=ServerPeer{Create:Configured}
 -> jboss.messaging:service=PostOffice{Create:Configured}
 -> jboss.messaging:service=PostOffice{Start:Configured}
 -> jboss.messaging:service=ServerPeer{Start:Configured}

jboss.messaging.destination:name=ExpiryQueue,service=Queue
 -> jboss.messaging:service=PostOffice{Create:Configured}
 -> jboss.messaging:service=ServerPeer{Start:Configured}
 -> jboss.messaging:service=PostOffice{Start:Configured}
 -> jboss.messaging:service=ServerPeer{Create:Configured}

jboss.messaging:service=JMSUserManager
 -> jboss.jca:name=DefaultDS,service=DataSourceBinding{Start:** NOT FOUND **}
 -> jboss.jca:name=DefaultDS,service=DataSourceBinding{Create:** NOT FOUND **}

jboss.messaging:service=PersistenceManager
 -> jboss.jca:name=DefaultDS,service=DataSourceBinding{Create:** NOT FOUND **}
 -> jboss.jca:name=DefaultDS,service=DataSourceBinding{Start:** NOT FOUND **}

jboss.messaging:service=PostOffice
 -> jboss.jca:name=DefaultDS,service=DataSourceBinding{Create:** NOT FOUND **}
 -> jboss.messaging:service=ServerPeer{Start:Configured}
 -> jboss.messaging:service=ServerPeer{Create:Configured}
 -> jboss.jca:name=DefaultDS,service=DataSourceBinding{Start:** NOT FOUND **}

jboss.messaging:service=ServerPeer
 -> jboss.messaging:service=PersistenceManager{Create:Configured}
 -> jboss.messaging:service=JMSUserManager{Start:Configured}
 -> jboss.messaging:service=PersistenceManager{Start:Configured}
 -> jboss.messaging:service=JMSUserManager{Create:Configured}

jboss.mq:service=DestinationManager
 -> jboss.messaging:service=ServerPeer{Create:Configured}
 -> jboss.messaging:service=ServerPeer{Start:Configured}


*** CONTEXTS IN ERROR: Name -> Error

jboss.jca:name=DefaultDS,service=DataSourceBinding -> ** NOT FOUND **


        at org.jboss.deployers.plugins.deployers.DeployersImpl.checkComplete(DeployersImpl.java:576)
        at org.jboss.deployers.plugins.main.MainDeployerImpl.checkComplete(MainDeployerImpl.java:559)
        at org.jboss.embedded.Bootstrap.bootstrapURL(Bootstrap.java:149)
        at org.jboss.embedded.Bootstrap.bootstrap(Bootstrap.java:183)
        at org.jboss.embedded.Bootstrap.bootstrap(Bootstrap.java:195)
        at org.jboss.seam.mock.EmbeddedBootstrap.startAndDeployResources(EmbeddedBootstrap.java:11)
        at org.jboss.seam.mock.AbstractSeamTest.startJbossEmbeddedIfNecessary(AbstractSeamTest.java:1024)
        at org.jboss.seam.mock.AbstractSeamTest.startSeam(AbstractSeamTest.java:915)
        at org.jboss.seam.mock.SeamTest.startSeam(SeamTest.java:58)
... Removed 15 stack frames
SKIPPED CONFIGURATION: @BeforeClass setupClass
SKIPPED CONFIGURATION: @BeforeMethod begin
SKIPPED CONFIGURATION: @AfterMethod end
SKIPPED CONFIGURATION: @AfterClass cleanupClass
 
04. Aug 2009, 05:39 America/New_York | Link
Adam Wells

I am getting the same error using Seam 2.1.2 GA

 
03. Mar 2010, 12:12 America/New_York | Link

Hi Dan, I have followed your approach on a fresh seam-gen project but still I can't access the entityManager, the datasource is deployed fine I can access it from the component test. Whenever I try to access the entity manager using

EntityManager entityManager = (EntityManager) getInstance("entityManager");

I get the following exception:

test:
   [testng] [Parser] Running:
   [testng]   D:\workspace\src\eelab\test-build\eelabTest.xml
   [testng]
   [testng] WARN  [org.jboss.seam.security.permission.PersistentPermissionResolver] no permission store available - please install a PermissionStore with the name 'org.jboss.seam.security.jpaPermissionStore' if persistent permissions are required.
   [testng] FAILED: entityManagerComponentTest
   [testng] java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : java:/entityManager
   [testng]     at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:245)
   [testng]     at org.jboss.seam.persistence.ManagedPersistenceContext.initEntityManager(ManagedPersistenceContext.java:78)
   [testng]     at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManager(ManagedPersistenceContext.java:107)
   [testng]     at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
   [testng]     at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)
   [testng]     at org.jboss.seam.Component.callComponentMethod(Component.java:2249)
   [testng]     at org.jboss.seam.Component.unwrap(Component.java:2275)
   [testng]     at org.jboss.seam.Component.getInstance(Component.java:2041)
   [testng]     at org.jboss.seam.Component.getInstance(Component.java:1983)
   [testng]     at org.jboss.seam.Component.getInstance(Component.java:1977)
   [testng]     at org.jboss.seam.Component.getInstanceInAllNamespaces(Component.java:2349)
   [testng]     at org.jboss.seam.Component.getValueToInject(Component.java:2301)
   [testng]     at org.jboss.seam.Component.injectAttributes(Component.java:1736)
   [testng]     at org.jboss.seam.Component.inject(Component.java:1554)
   [testng]     at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:61)
   [testng]     at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
   [testng]     at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
   [testng]     at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
   [testng]     at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
   [testng]     at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
   [testng]     at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
   [testng]     at org.webpatterns.agora.lab.action.EntityManagerTest_$$_javassist_seam_1.getOneFromDual(EntityManagerTest_$$_javassist_seam_1.java)
   [testng]     at org.webpatterns.agora.lab.test.DummyTest$1.testComponents(DummyTest.java:20)
   [testng]     at org.jboss.seam.mock.AbstractSeamTest$ComponentTest.run(AbstractSeamTest.java:162)
   [testng]     at org.webpatterns.agora.lab.test.DummyTest.entityManagerComponentTest(DummyTest.java:22)
   [testng] Caused by: javax.naming.NameNotFoundException: entityManager not bound
   [testng]     at org.jnp.server.NamingServer.getBinding(NamingServer.java:542)
   [testng]     at org.jnp.server.NamingServer.getBinding(NamingServer.java:550)
   [testng]     at org.jnp.server.NamingServer.getObject(NamingServer.java:556)
   [testng]     at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
   [testng]     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:669)
   [testng]     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:629)
   [testng]     at javax.naming.InitialContext.lookup(InitialContext.java:392)
   [testng]     at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:241)
   [testng]     ... 50 more
   [testng] ... Removed 26 stack frames
   [testng]
   [testng] ===============================================
   [testng]     EE Lab: Model Unit Tests
   [testng]     Tests run: 1, Failures: 1, Skips: 0
   [testng] ===============================================
   [testng]
   [testng]
   [testng] ===============================================
   [testng] DVD Store
   [testng] Total tests run: 1, Failures: 1, Skips: 0
   [testng] ===============================================
   [testng]

My test case is the following:

public class DummyTest extends SeamTest {

    @Test
    public void entityManagerTest() throws Exception {
	new ComponentTest() {

	    @Override
	    protected void testComponents() throws Exception {
		assert 1 == 1;

		EntityManager entityManager = (EntityManager) getInstance("entityManager");
		assert (entityManager != null);
	    }
	}.run();
    }
}

the following configuration illustrates my test environment:

component-test.properties:

jndiPattern=#{ejbName}/local
debug=true
seamBootstrapsPu=true
seamEmfRef=#{entityManagerFactory}
puJndiName=#{null}

persistence-test.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <!-- Persistence deployment descriptor for tests -->
<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="eelab" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/eelabTestDatasource</jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

My test datasource is under bootstrap/deploy and consists of the following:

<?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>eelabTestDatasource</jndi-name>
      <use-java-context>false</use-java-context>
      <connection-url>jdbc:h2:tcp://localhost/~/eelab_testdb</connection-url>
      <driver-class>org.h2.Driver</driver-class>
      <user-name>eelab</user-name>
      <password>eelab</password>
   </local-tx-datasource>
</datasources>

My environment consists of:

  • jdk-1.6u18
  • apache-ant-1.7.1
  • jboss-seam-2.2.0.GA
  • JBoss AS 5.1.0.GA

And finally the h2.jar file is on the classpath inside the lib folder hence it's exposed.

Can someone tell me what slight detail I'm missing in here? Am I the only one who's having problem with this?

 
30. Apr 2010, 12:43 America/New_York | Link

Hello ,

does queue service in jms messeging with seam persist data in DB ? bcz if we restart the server the queue will get empty ,right so is there any way to persist the messages from queue to DB?

2- how to get the SERVERPEERID from messaging-service. XML file ? do i need parsing the XML file? or is there any API those helps in the same to retrieve the same. Actually my requirement is to get generate the unique id on different JBOS servers in cluster manner. can anybody help me.

 
09. Feb 2011, 16:58 America/New_York | Link

Fady, your component-test.properties puJndiName isn't bound. Change it to the JNDI of your entity manager.