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.
| Online: | 3 Members of 12451 |
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:
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.
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 cleanupClassI am getting the same error using Seam 2.1.2 GA
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:
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:
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?
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.
Fady, your component-test.properties puJndiName isn't bound. Change it to the JNDI of your entity manager.