Files
java-tutorials/libraries/src/test/java/com/baeldung/jdo/GuideToJDOIntegrationTest.java
T

113 lines
4.1 KiB
Java
Raw Normal View History

2017-04-25 14:54:32 -04:00
package com.baeldung.jdo;
2017-04-29 20:10:52 +02:00
import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
import org.datanucleus.metadata.PersistenceUnitMetaData;
import org.junit.Test;
2017-04-25 14:54:32 -04:00
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;
2017-04-29 20:10:52 +02:00
import java.util.Iterator;
import java.util.List;
2017-04-25 14:54:32 -04:00
2017-04-29 20:10:52 +02:00
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
2017-04-25 14:54:32 -04:00
2017-04-29 20:10:52 +02:00
public class GuideToJDOIntegrationTest {
2017-04-25 14:54:32 -04:00
@Test
public void givenProduct_WhenNewThenPerformTransaction() {
2017-04-26 15:30:04 -04:00
PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
pumd.addClassName("com.baeldung.jdo.Product");
pumd.setExcludeUnlistedClasses();
pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
pumd.addProperty("datanucleus.autoCreateSchema", "true");
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
2017-04-25 14:54:32 -04:00
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
for (int i = 0; i < 100; i++){
String nam = "Product-" + i;
2017-04-29 20:10:52 +02:00
Product productx = new Product(nam, (double) i);
2017-04-25 14:54:32 -04:00
pm.makePersistent(productx);
}
tx.commit();
} catch (Throwable thr) {
fail("Failed test : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.close();
}
@Test
public void givenProduct_WhenQueryThenExist() {
2017-04-26 15:30:04 -04:00
PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
pumd.addClassName("com.baeldung.jdo.Product");
pumd.setExcludeUnlistedClasses();
pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
pumd.addProperty("datanucleus.autoCreateSchema", "true");
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
2017-04-25 14:54:32 -04:00
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Product product = new Product("Tablet", 80.0);
pm.makePersistent(product);
Product product2 = new Product("Phone", 20.0);
pm.makePersistent(product2);
Product product3 = new Product("Laptop", 200.0);
pm.makePersistent(product3);
tx.commit();
} catch (Throwable thr) {
fail("Failed test : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.close();
2017-04-26 15:30:04 -04:00
PersistenceManagerFactory pmf2 = new JDOPersistenceManagerFactory(pumd, null);
2017-04-25 14:54:32 -04:00
PersistenceManager pm2 = pmf2.getPersistenceManager();
Transaction tx2 = pm2.currentTransaction();
try {
tx2.begin();
@SuppressWarnings("rawtypes")
Query q = pm2.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price == 200");
@SuppressWarnings("unchecked")
List<Product> products = (List<Product>) q.execute();
Iterator<Product> iter = products.iterator();
while (iter.hasNext()) {
Product p = iter.next();
assertEquals("Laptop", p.name);
}
tx2.commit();
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
}
}
2017-04-26 15:30:04 -04:00
}