use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.
the class AutoConfigPersistenceUnitsTest method testFromWebAppContextNonJta.
/**
* Existing data source "orange-web", non-jta managed
*
* Application contains a web module with root context path as "orange-web"
*
* Persistence xml like so:
*
* <persistence-unit name="orange-unit" />
*
* The orange-unit app should automatically use orange-web data source and create a new non-JtaManaged datasource
*
* @throws Exception
*/
public void testFromWebAppContextNonJta() throws Exception {
final ResourceInfo supplied = addDataSource("orange-web", OrangeDriver.class, "jdbc:orange-web:some:stuff", false);
assertSame(supplied, resources.get(0));
final PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
final ClassLoader cl = this.getClass().getClassLoader();
final AppModule app = new AppModule(cl, "orange-app");
app.addPersistenceModule(new PersistenceModule("root", new Persistence(persistenceUnit)));
final WebApp webApp = new WebApp();
webApp.setMetadataComplete(true);
app.getWebModules().add(new WebModule(webApp, "orange-web", cl, "war", "orange-web"));
// Create app
final AppInfo appInfo = config.configureApplication(app);
assembler.createApplication(appInfo);
// Check results
final ResourceInfo generated = resources.get(1);
assertEquals(supplied.id + "Jta", generated.id);
assertEquals(supplied.service, generated.service);
assertEquals(supplied.className, generated.className);
assertEquals(supplied.properties.get("JdbcDriver"), generated.properties.get("JdbcDriver"));
assertEquals(supplied.properties.get("JdbcUrl"), generated.properties.get("JdbcUrl"));
assertEquals("true", generated.properties.get("JtaManaged"));
}
use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.
the class AutoConfigPersistenceUnitsTest method testFromWebAppIdJta.
/**
* Existing data source "orange-web", jta managed
*
* Application contains a web module with id "orange-id"
*
* Persistence xml like so:
*
* <persistence-unit name="orange-unit" />
*
* The orange-unit app should automatically use orange-id data source and create a new non-JtaManaged datasource
*
* @throws Exception
*/
public void testFromWebAppIdJta() throws Exception {
final ResourceInfo supplied = addDataSource("orange-id", OrangeDriver.class, "jdbc:orange-web:some:stuff", true);
assertSame(supplied, resources.get(0));
final PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
final ClassLoader cl = this.getClass().getClassLoader();
final AppModule app = new AppModule(cl, "orange-app");
app.addPersistenceModule(new PersistenceModule("root", new Persistence(persistenceUnit)));
final WebApp webApp = new WebApp();
webApp.setMetadataComplete(true);
app.getWebModules().add(new WebModule(webApp, "orange-web", cl, "war", "orange-id"));
// Create app
final AppInfo appInfo = config.configureApplication(app);
assembler.createApplication(appInfo);
// Check results
final ResourceInfo generated = resources.get(1);
assertEquals(supplied.id + "NonJta", generated.id);
assertEquals(supplied.service, generated.service);
assertEquals(supplied.className, generated.className);
assertEquals(supplied.properties.get("JdbcDriver"), generated.properties.get("JdbcDriver"));
assertEquals(supplied.properties.get("JdbcUrl"), generated.properties.get("JdbcUrl"));
assertEquals("false", generated.properties.get("JtaManaged"));
}
use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.
the class AutoConfigPersistenceUnitsTest method testFromWebAppContextThirdParty.
/**
* Existing data source "orange-web", not controlled by us
*
* Application contains a web module with root context path as "orange-web"
*
* Persistence xml like so:
*
* <persistence-unit name="orange-unit" />
*
* The orange-unit app should automatically use orange-web data source and the non-jta-datasource should be null
*
* @throws Exception
*/
public void testFromWebAppContextThirdParty() throws Exception {
final ResourceInfo supplied = addDataSource("orange-web", OrangeDriver.class, "jdbc:orange-web:some:stuff", null);
assertSame(supplied, resources.get(0));
final PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
final ClassLoader cl = this.getClass().getClassLoader();
final AppModule app = new AppModule(cl, "orange-app");
app.addPersistenceModule(new PersistenceModule("root", new Persistence(persistenceUnit)));
final WebApp webApp = new WebApp();
webApp.setMetadataComplete(true);
app.getWebModules().add(new WebModule(webApp, "orange-web", cl, "war", "orange-web"));
// Create app
final AppInfo appInfo = config.configureApplication(app);
assembler.createApplication(appInfo);
final PersistenceUnitInfo unitInfo = appInfo.persistenceUnits.get(0);
// Check results
assertEquals(supplied.id, unitInfo.jtaDataSource);
assertNull(unitInfo.nonJtaDataSource);
}
use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.
the class LegacyInterfaceTest method testCustomCmpMappingsWithMappingFileDefinedInPersistenceXml.
public void testCustomCmpMappingsWithMappingFileDefinedInPersistenceXml() throws Exception {
System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
final ConfigurationFactory config = new ConfigurationFactory();
final Assembler assembler = new Assembler();
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
final EjbJar ejbJar = new EjbJar();
ejbJar.addEnterpriseBean(new SingletonBean(MySingletonBean.class));
ejbJar.addEnterpriseBean(new EntityBean(MyBmpBean.class, PersistenceType.BEAN));
final EntityBean cmp = ejbJar.addEnterpriseBean(new EntityBean(MyCmpBean.class, PersistenceType.CONTAINER));
cmp.setPrimKeyClass(Integer.class.getName());
cmp.setPrimkeyField("id");
cmp.getCmpField().add(new CmpField("id"));
cmp.getCmpField().add(new CmpField("name"));
final Query query = new Query();
query.setQueryMethod(new QueryMethod("findByPrimaryKey", Integer.class.getName()));
query.setEjbQl("SELECT OBJECT(DL) FROM License DL");
cmp.getQuery().add(query);
final List<ContainerTransaction> transactions = ejbJar.getAssemblyDescriptor().getContainerTransaction();
transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyBmpBean", "*"));
transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyCmpBean", "*"));
transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MySingletonBean", "*"));
final File f = new File("test").getAbsoluteFile();
if (!f.exists() && !f.mkdirs()) {
throw new Exception("Failed to create test directory: " + f);
}
final AppModule module = new AppModule(this.getClass().getClassLoader(), f.getAbsolutePath());
final EjbModule ejbModule = new EjbModule(ejbJar);
Persistence persistence = new Persistence();
PersistenceUnit pu = persistence.addPersistenceUnit("cmp");
pu.setTransactionType(TransactionType.JTA);
pu.setJtaDataSource("fake");
pu.setNonJtaDataSource("fake");
pu.getMappingFile().add("test-orm.xml");
pu.getClazz().add("openejb.org.apache.openejb.core.MyCmpBean");
module.addPersistenceModule(new PersistenceModule("pu", persistence));
module.getEjbModules().add(ejbModule);
assertNull(module.getCmpMappings());
assembler.createApplication(config.configureApplication(module));
assertNotNull(module.getCmpMappings());
// no mapping should be automatically generated
assertTrue(module.getCmpMappings().getEntityMap().isEmpty());
// pu should not be modified, no duplicate classes
assertEquals(1, pu.getClazz().size());
assertEquals("openejb.org.apache.openejb.core.MyCmpBean", pu.getClazz().get(0));
assertEquals(1, pu.getMappingFile().size());
assertEquals("test-orm.xml", pu.getMappingFile().get(0));
}
use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.
the class DynamicEJBImplTest method persistence.
@Module
public Persistence persistence() {
final PersistenceUnit unit = new PersistenceUnit("pu");
unit.addClass(User.class);
unit.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
unit.setExcludeUnlistedClasses(true);
final Persistence persistence = new Persistence(unit);
persistence.setVersion("2.0");
return persistence;
}
Aggregations