arquillian persistence and multiple persistence units
yan.langlois Feb 9, 2015 6:06 AMHi,
I try to test a webapp with two persistence units :
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.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_2_0.xsd"> <persistence-unit name="primary"> <jta-data-source>java:/datasources/standardDS</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="none" /> </properties> </persistence-unit> <persistence-unit name="picketlink"> <jta-data-source>java:/datasources/picketlinkDS</jta-data-source> <class>org.picketlink.idm.jpa.model.sample.simple.AttributedTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.AccountTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.RoleTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.GroupTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.IdentityTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.RelationshipTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.RelationshipIdentityTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.PartitionTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.PasswordCredentialTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.DigestCredentialTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.X509CredentialTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.OTPCredentialTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.AttributeTypeEntity</class> <class>org.picketlink.idm.jpa.model.sample.simple.PermissionTypeEntity</class> <properties> <property name="hibernate.hbm2ddl.auto" value="none" /> </properties> </persistence-unit> </persistence>
My test is here :
@DataSource("java:/datasources/picketlinkDS") // I also try with persistence unit names, ie : "primary" or "picketlink". It does not work either.
@CreateSchema({"create.sql"})
@RunWith(Arquillian.class)
public class MyTest {
@Deployment
public static WebArchive initDeployement() throws IOException {...}
@Test
public void testXXX() {...}
}
I always get this error :
INFOS: Unable to deduct data source from test's archive persistence.xml. Multiple persistence units defined. Please specify default data source either in 'arquillian.xml' or by using @DataSource annotation
When I look at the sources of arquillian persistance (version 1.0.0.Alpha7 : PersistenceDescriptorParser) I see that if there is more than 1 persistence unit an exception is thrown :
public String obtainDataSourceName(final String descriptor)
{
Document document = parseXmlDescriptor(descriptor);
final NodeList persistenceUnits = document.getElementsByTagName("persistence-unit");
if (persistenceUnits.getLength() > 1)
{
throw new MultiplePersistenceUnitsException("Multiple persistence units defined. Please specify default data source either in 'arquillian.xml' or by using @DataSource annotation");
}
...
}
And when I look at the catch of the exception (PersistenceExtensionConfigurationTestArchiveEnricher) I notice that arquillian does not look for the default dataSource (@DataSource("java:/datasources/picketlinkDS")) :
private void obtainDataSourceFromPersistenceXml(final Archive<?> applicationArchive)
{
if (configurationInstance.get().isDefaultDataSourceDefined())
{
return; // if defined globally the only way to alter it is on test level using @DataSource annotation
}
final PersistenceDescriptorExtractor persistenceDescriptorArchiveExtractor = new PersistenceDescriptorExtractor();
final InputStream persistenceXmlAsStream = persistenceDescriptorArchiveExtractor.getAsStream(applicationArchive);
if (persistenceXmlAsStream != null)
{
final PersistenceDescriptorParser parser = new PersistenceDescriptorParser();
try
{
final String dataSourceName = parser.obtainDataSourceName(persistenceXmlAsStream); // The excpetion is thrown here
configurationInstance.get().setDefaultDataSource(dataSourceName);
}
catch (MultiplePersistenceUnitsException e)
{
log.info("Unable to deduct data source from test's archive persistence.xml. " + e.getMessage());
}
}
}
Is it really possible to use multiple peristance units within an arquillian test ? If so, what am I doing wrong ?