public void testCreateAndLoadLaptop() throws Exception { Database database = _category.getDatabase(); database.begin(); ProductDetail detail = new ProductDetail(); detail.setId(10); detail.setCategory("category 10"); detail.setLocation("location 10"); database.create(detail); database.commit(); database.begin(); Laptop laptop = new Laptop(); laptop.setId(10); laptop.setName("laptop 10"); laptop.setCpu("centrino"); laptop.setResolution("1600"); laptop.setWeight(2750); laptop.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(10))); database.create(laptop); Owner owner = new Owner(); owner.setId(new Integer(10)); owner.setName("owner 10"); owner.setProduct(laptop); database.commit(); database.begin(); laptop = (Laptop) database.load(Laptop.class, new Integer(10)); database.commit(); assertNotNull(laptop); assertEquals("ctf.jdo.tc9x.Laptop", laptop.getClass().getName()); assertEquals(10, laptop.getId()); assertEquals("laptop 10", laptop.getName()); database.begin(); database.remove(database.load(Laptop.class, new Integer(10))); database.remove(database.load(ProductDetail.class, new Integer(10))); database.commit(); database.begin(); try { laptop = (Laptop) database.load(Laptop.class, new Integer(10)); fail("Laptop with id 10 still exists."); } catch (ObjectNotFoundException e) { assertEquals( "The object of type ctf.jdo.tc9x.Laptop with identity <10(10)> " + "was not found in persistent storage", e.getMessage()); } database.commit(); database.close(); }
public void testCreateAndLoadCar() throws Exception { Database database = _category.getDatabase(); database.begin(); Order order = new Order(); order.setId(11); order.setName("order 11"); database.create(order); database.commit(); database.begin(); ProductDetail detail = new ProductDetail(); detail.setId(11); detail.setCategory("category 11"); detail.setLocation("location 11"); database.create(detail); database.commit(); database.begin(); Truck truck = new Truck(); truck.setId(11); truck.setName("truck 11"); truck.setKw(112); truck.setMake("Fiat"); truck.setMaxWeight(3750); truck.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(11))); database.create(truck); Collection orders = new LinkedList(); orders.add(database.load(Order.class, new Integer(11))); truck.setOrders(orders); database.commit(); database.begin(); Object object = database.load(Car.class, new Integer(11)); assertNotNull(object); assertEquals("ctf.jdo.tc9x.Truck", object.getClass().getName()); Truck loadedTruck = (Truck) object; database.commit(); assertNotNull(loadedTruck); assertEquals(11, loadedTruck.getId()); database.begin(); database.remove(database.load(Car.class, new Integer(11))); database.remove(database.load(ProductDetail.class, new Integer(11))); database.remove(database.load(Order.class, new Integer(11))); database.commit(); database.close(); }
public void testCreateAndLoadComputer() throws Exception { Database database = _category.getDatabase(); database.begin(); Order order = new Order(); order.setId(12); order.setName("order 12"); database.create(order); database.commit(); database.begin(); ProductDetail detail = new ProductDetail(); detail.setId(12); detail.setCategory("category 12"); detail.setLocation("location 12"); database.create(detail); database.commit(); database.begin(); Laptop laptop = new Laptop(); laptop.setId(12); laptop.setName("laptop 12"); laptop.setCpu("centrino"); laptop.setResolution("1600"); laptop.setWeight(2450); laptop.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(12))); database.create(laptop); Collection orders = new LinkedList(); orders.add(database.load(Order.class, new Integer(12))); laptop.setOrders(orders); database.commit(); database.begin(); Computer computer = (Computer) database.load(Computer.class, new Integer(12)); database.commit(); assertNotNull(computer); assertEquals("ctf.jdo.tc9x.Laptop", computer.getClass().getName()); assertEquals(12, computer.getId()); assertEquals("laptop 12", computer.getName()); database.begin(); computer = (Computer) database.load(Computer.class, new Integer(12)); database.remove(computer); database.remove(database.load(ProductDetail.class, new Integer(12))); database.remove(database.load(Order.class, new Integer(12))); database.commit(); database.close(); }
public void testQuery() throws Exception { Database database; // create some products database = _category.getDatabase(); database.begin(); database.create(new Product(1, "LCD", KindEnum.MONITOR)); database.create(new Product(2, "Laser", KindEnum.PRINTER)); database.create(new Product(3, "Desktop", KindEnum.COMPUTER)); database.create(new Product(4, "Notebook", KindEnum.COMPUTER)); database.commit(); database.close(); // query and delete all product database = _category.getDatabase(); database.begin(); Product pq; OQLQuery query = database.getOQLQuery( "select p from " + jdo.tc167.Product.class.getName() + " p order by p.id"); QueryResults results = query.execute(); pq = (Product) results.next(); assertEquals(pq, new Product(1, "LCD", KindEnum.MONITOR)); database.remove(pq); pq = (Product) results.next(); assertEquals(pq, new Product(2, "Laser", KindEnum.PRINTER)); database.remove(pq); pq = (Product) results.next(); assertEquals(pq, new Product(3, "Desktop", KindEnum.COMPUTER)); database.remove(pq); pq = (Product) results.next(); assertEquals(pq, new Product(4, "Notebook", KindEnum.COMPUTER)); database.remove(pq); assertFalse(results.hasMore()); results.close(); query.close(); database.commit(); database.close(); }
/** * Removes a new process instance * * @param instanceId instance id */ public void removeProcessInstance(String instanceId) throws WorkflowException { SilverTrace.info( "worflowEngine", "ProcessInstanceManagerImpl.removeProcessInstance()", "root.MSG_GEN_ENTER_METHOD", "InstanceId=" + instanceId); ProcessInstanceImpl instance; Database db = null; try { // Delete forms data associated with this instance removeProcessInstanceData(instanceId); // Constructs the query db = WorkflowJDOManager.getDatabase(); db.begin(); instance = (ProcessInstanceImpl) db.load(ProcessInstanceImpl.class, instanceId); db.remove(instance); db.commit(); } catch (PersistenceException pe) { throw new WorkflowException( "ProcessInstanceManagerImpl.removeProcessInstance", "EX_ERR_CASTOR_REMOVE_INSTANCE", pe); } finally { WorkflowJDOManager.closeDatabase(db); } WorkflowHub.getErrorManager().removeErrorsOfInstance(instanceId); SilverTrace.info( "worflowEngine", "ProcessInstanceManagerImpl.removeProcessInstance()", "root.MSG_GEN_EXIT_METHOD"); }
public void testDepends() throws Exception { Database db = null; MasterObjectNoKeyGen master = new MasterObjectNoKeyGen(); master.setId(100); master.setDescrip("This is the descrip."); db = _category.getDatabase(); db.begin(); db.create(master); db.commit(); assertTrue(master.getId() != 0); assertEquals(100, master.getId()); // THIS Part Works! db.begin(); DependentObjectNoKeyGen depends = new DependentObjectNoKeyGen(); depends.setId(101); depends.setDescrip("Description"); master.setDepends(depends); db.update(master); db.commit(); assertTrue(master.getId() != 0); assertEquals(100, master.getId()); int masterId = master.getId(); db.begin(); master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId)); assertNotNull(master.getDepends()); master.setDepends(null); db.commit(); db.begin(); master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId)); assertNull(master.getDepends()); db.commit(); // THIS part doesn't! db.begin(); master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId)); depends = new DependentObjectNoKeyGen(); depends.setId(102); depends.setDescrip("Description"); master.setDepends(depends); db.commit(); db.begin(); master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId)); assertNotNull(master.getDepends()); db.commit(); db.begin(); master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId)); assertNotNull(master); db.remove(master); db.commit(); }
public void testCreateLoadUpdateDelete() throws Exception { Database database; // create product database = _category.getDatabase(); database.begin(); Product pc = new Product(1, "LCD", KindEnum.MONITOR); database.create(pc); database.commit(); database.close(); // load created product database = _category.getDatabase(); database.begin(); Product pl1 = new Product(1, "LCD", KindEnum.MONITOR); Product pl2 = (Product) database.load(Product.class, new Integer(1)); assertEquals(pl1, pl2); database.commit(); database.close(); // update product database = _category.getDatabase(); database.begin(); Product pu = (Product) database.load(Product.class, new Integer(1)); pu.setName("Laser"); pu.setKind(KindEnum.PRINTER); database.commit(); database.close(); // load updated product database = _category.getDatabase(); database.begin(); Product pl3 = new Product(1, "Laser", KindEnum.PRINTER); Product pl4 = (Product) database.load(Product.class, new Integer(1)); assertEquals(pl3, pl4); database.commit(); database.close(); // delete product database = _category.getDatabase(); database.begin(); Product pd = (Product) database.load(Product.class, new Integer(1)); database.remove(pd); database.commit(); database.close(); }
/** This method deletes the Repository sent in from the system. */ public void deleteLanguage(Integer languageId, Database db) throws SystemException, Bug { try { db.remove(getLanguageWithId(languageId, db)); } catch (Exception e) { throw new SystemException( "An error occurred when we tried to delete Language in the database. Reason: " + e.getMessage(), e); } }
public void testRemoveState() throws Exception { long time = System.currentTimeMillis(); int count = 0; Database db = _jdo.getDatabase(); db.begin(); OQLQuery query = db.getOQLQuery("SELECT o FROM " + State.class.getName() + " o"); QueryResults results = query.execute(); while (results.hasMore()) { db.remove(results.next()); count++; } db.commit(); db.close(); LOG.info( "Removed " + count + " state objects in " + (System.currentTimeMillis() - time) + "ms."); }
public void delete(String userName, Database db) throws ConstraintException, SystemException, Exception { SystemUser systemUser = getSystemUserWithName(userName, db); Collection roles = systemUser.getRoles(); Iterator rolesIterator = roles.iterator(); while (rolesIterator.hasNext()) { Role role = (Role) rolesIterator.next(); role.getSystemUsers().remove(systemUser); } Collection groups = systemUser.getGroups(); Iterator groupsIterator = groups.iterator(); while (groupsIterator.hasNext()) { Group group = (Group) groupsIterator.next(); group.getSystemUsers().remove(systemUser); } db.remove(systemUser); }
public void setUp() throws PersistenceException { _jdo = _category.getJDO(); _jdo.setCallbackInterceptor(_i); _jdo.setInstanceFactory(_i); _db = _category.getDatabase(); OQLQuery oql; QueryResults qres; LOG.debug("Delete everything"); _db.begin(); oql = _db.getOQLQuery("SELECT p FROM " + TimeStampableObject.class.getName() + " p WHERE id=$1"); oql.bind(TimeStampableObject.DEFAULT_ID); qres = oql.execute(); while (qres.hasMore()) { _db.remove(qres.next()); } oql.close(); _db.commit(); }
public void tearDown() throws PersistenceException { if (_db.isActive()) { _db.rollback(); } OQLQuery oql; QueryResults qres; LOG.debug("Delete everything"); _db.begin(); oql = _db.getOQLQuery("SELECT p FROM " + TimeStampableObject.class.getName() + " p WHERE id=$1"); oql.bind(TimeStampableObject.DEFAULT_ID); qres = oql.execute(); while (qres.hasMore()) { _db.remove(qres.next()); } oql.close(); _db.commit(); _db.close(); _jdo.setCallbackInterceptor(null); }
public void runTest() throws PersistenceException { TimeStampableObject object; CallbacksInvoked cbi = new CallbacksInvoked(); cbi.allow(CallbacksInvoked.CREATING); cbi.allow(CallbacksInvoked.USING); cbi.allow(CallbacksInvoked.CREATED); cbi.allow(CallbacksInvoked.STORING); cbi.allow(CallbacksInvoked.RELEASING); _i._callbacksInvoked.init(); _db.begin(); object = new TimeStampableObject(); LOG.debug("Creating new object: " + object); _db.create(object); _db.commit(); if (!cbi.equals(_i._callbacksInvoked)) { LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); } cbi.init(); cbi.allow(CallbacksInvoked.USING); cbi.allow(CallbacksInvoked.LOADED); cbi.allow(CallbacksInvoked.STORING); cbi.allow(CallbacksInvoked.RELEASING); cbi.allow(CallbacksInvoked.INSTANTIATE); _i._callbacksInvoked.init(); _db.begin(); object = (TimeStampableObject) _db.load(TimeStampableObject.class, new Integer(TimeStampableObject.DEFAULT_ID)); object.setValue1("Alan"); _db.commit(); if (!cbi.equals(_i._callbacksInvoked)) { LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); } cbi.init(); cbi.allow(CallbacksInvoked.USING); cbi.allow(CallbacksInvoked.UPDATED); cbi.allow(CallbacksInvoked.STORING); cbi.allow(CallbacksInvoked.RELEASING); _i._callbacksInvoked.init(); object.setValue2("long transaction new value"); _db.begin(); _db.update(object); _db.commit(); if (!cbi.equals(_i._callbacksInvoked)) { LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); } cbi.init(); cbi.allow(CallbacksInvoked.USING); cbi.allow(CallbacksInvoked.LOADED); cbi.allow(CallbacksInvoked.REMOVING); cbi.allow(CallbacksInvoked.REMOVED); cbi.allow(CallbacksInvoked.RELEASING); cbi.allow(CallbacksInvoked.INSTANTIATE); _i._callbacksInvoked.init(); _db.begin(); object = (TimeStampableObject) _db.load(TimeStampableObject.class, new Integer(TimeStampableObject.DEFAULT_ID)); _db.remove(object); _db.commit(); if (!cbi.equals(_i._callbacksInvoked)) { LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); } }
public void run() { try { int num = 0; stream.writeVerbose("start testing"); TestRace tr; TestRace testrace; OQLQuery oql; QueryResults qr; boolean succeed; int trials; Integer id = new Integer(5); out: for ( int i=0; i<trial; i++ ) { // create, modified, delete object try { switch ( cachetype ) { case 0: testrace = new TestRaceCount(); testrace.setId(5); break; case 1: testrace = new TestRaceTime(); testrace.setId(5); break; case 2: testrace = new TestRaceNone(); testrace.setId(5); break; case 3: testrace = new TestRaceUnlimited(); testrace.setId(5); break; default: testrace = null; } // create object //try { db.begin(); db.create( testrace ); // may throw duplicateIdentityException db.commit(); //} catch ( Exception e ) { // e.printStackTrace(); //} // load it and modify it succeed = false; trials = 0; while ( !succeed && trials < NUM_OF_RETRIAL ) { Thread.currentThread().sleep( 0 ); trials++; try { db.begin(); tr = (TestRace) db.load( _classType, id ); // may throw ObjectNotFoundException // LockNotGrantedException tr.incValue1(); db.commit(); succeed = true; } catch ( LockNotGrantedException e ) { succeed = false; // ethernet way of retry if ( db.isActive() ) db.rollback(); Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) ); } catch ( TransactionAbortedException e ) { succeed = false; // ethernet way of retry if ( db.isActive() ) db.rollback(); Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) ); } } if ( db.isActive() ) db.rollback(); // load it and release it succeed = false; trials = 0; while ( !succeed && trials < NUM_OF_RETRIAL ) { Thread.currentThread().sleep( 0 ); trials++; try { db.begin(); tr = (TestRace) db.load( _classType, id ); // may throw ObjectNotFoundException // LockNotGrantedException db.commit(); succeed = true; } catch ( LockNotGrantedException e ) { succeed = false; // ethernet way of retry if ( db.isActive() ) db.rollback(); Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) ); } } if ( db.isActive() ) db.rollback(); // load it and delete it succeed = false; trials = 0; while ( !succeed && trials < NUM_OF_RETRIAL ) { Thread.currentThread().sleep( 0 ); trials++; try { db.begin(); tr = (TestRace) db.load( _classType, id ); // may throw ObjectNotFoundException // LockNotGrantedException db.remove( tr ); db.commit(); succeed = true; } catch ( LockNotGrantedException e ) { succeed = false; if ( db.isActive() ) db.rollback(); Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) ); } catch ( TransactionAbortedException e ) { succeed = false; // ethernet way of retry if ( db.isActive() ) db.rollback(); Thread.currentThread().sleep( (long) ((SLEEP_BASE_TIME^trials) * ran.nextDouble()) ); } } if ( db.isActive() ) db.rollback(); if ( !succeed ) throw new Exception("Transaction can't not lock the object within "+trials+" trials"); } catch ( TransactionNotInProgressException e ) { stream.writeVerbose( "Thread <CreateDelete> will be killed. Unexcepted exception: "+e.getException() ); e.printStackTrace(); if ( db.isActive() ) try { db.rollback(); } catch ( TransactionNotInProgressException ee ) {} _errLeak = true; break out; } catch ( PersistenceException e ) { stream.writeVerbose( "Thread <CreateDelete> will be killed. Unexcepted exception: " ); e.printStackTrace(); if ( db.isActive() ) try { db.rollback(); } catch ( TransactionNotInProgressException ee ) {} _errLeak = true; break out; } catch ( Exception e ) { stream.writeVerbose( "Thread <CreateDelete> will be killed. Element not found: other exception: "+e ); e.printStackTrace(); if ( db.isActive() ) try { db.rollback(); } catch ( TransactionNotInProgressException ee ) {} _errLeak = true; break out; } } } finally { isDone = true; } }
public void testComplex() throws PersistenceException, SQLException { stream.println("Running testComplex..."); Complex fullname = new Complex("First", "Person"); // set up the data object _db.begin(); TestLazyEmployee loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname); Collection projects = loadPerson.getProjects(); TestLazyProject project = new TestLazyProject(); project.setId(1001); project.setName("Project One"); project.setOwner(loadPerson); projects.add(project); _db.create(project); project = new TestLazyProject(); project.setId(1002); project.setName("Project Two"); project.setOwner(loadPerson); projects.add(project); _db.create(project); project = new TestLazyProject(); project.setId(1003); project.setName("Project Three"); project.setOwner(loadPerson); projects.add(project); _db.create(project); _db.commit(); // reload and test bug 823 _db.begin(); project = (TestLazyProject) _db.load(TestLazyProject.class, new Integer(1002)); _db.remove(project); loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname); projects = loadPerson.getProjects(); Iterator itor = projects.iterator(); while (itor.hasNext()) { project = (TestLazyProject) itor.next(); if (project.getId() == 1002) { itor.remove(); break; } } itor = projects.iterator(); while (itor.hasNext()) { project = (TestLazyProject) itor.next(); if (project.getId() == 1002) { itor.remove(); break; } } _db.commit(); // reload and make sure the cache is consistent _db.begin(); loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname); projects = loadPerson.getProjects(); itor = projects.iterator(); int id1 = 0; if (itor.hasNext()) { project = (TestLazyProject) itor.next(); id1 = project.getId(); if (project.getId() != 1001 && project.getId() != 1003) { stream.println("Error: found project1 with unexpected id " + project.getId()); fail("Error: found project1 with unexpected id " + project.getId()); } } else { stream.println("Error: expected project is not found"); fail("Error: expected project is not found"); } if (itor.hasNext()) { project = (TestLazyProject) itor.next(); if (project.getId() == id1 || (project.getId() != 1001 && project.getId() != 1003)) { stream.println("Error: found project2 with unexpected id " + project.getId()); fail("Error: found project2 with unexpected id " + project.getId()); } } else { stream.println("Error: expected project is not found"); fail("Error: expected project is not found"); } if (itor.hasNext()) { project = (TestLazyProject) itor.next(); stream.println("Error: unexpected project is found: " + project.getId()); fail("Error: unexpected project is found: " + project.getId()); } _db.commit(); }
public void testLongTransaction() throws Exception { Product product = null; ProductGroup group; QueryResults results; Database db = getJDOManager(DBNAME, MAPPING).getDatabase(); db.begin(); LOG.info("Begin transaction to remove Product objects"); // Look up the products and if found delete them from the database OQLQuery productOql = db.getOQLQuery("SELECT p FROM " + Product.class.getName() + " p WHERE p.id = $1"); for (int i = 4; i < 10; ++i) { productOql.bind(i); results = productOql.execute(); while (results.hasMore()) { product = (Product) results.next(); LOG.debug("Deleting existing product: " + product); db.remove(product); } } LOG.info("End transaction to remove Product objects"); db.commit(); db.begin(); LOG.info("Begin transaction: one-to-one, one-to-many, and dependent relations"); // If no such group exists in the database, create a new object and persist it OQLQuery groupOql = db.getOQLQuery("SELECT g FROM " + ProductGroup.class.getName() + " g WHERE id = $1"); groupOql.bind(3); results = groupOql.execute(); if (!results.hasMore()) { group = new ProductGroup(); group.setId(3); group.setName("a group"); db.create(group); LOG.debug("Creating new group: " + group); } else { group = (ProductGroup) results.next(); LOG.debug("Query result: " + group); } // If no such product exists in the database, create a new object and persist it // Note: product uses group, so group object has to be created first, but can // be persisted later productOql.bind(4); results = productOql.execute(); if (!results.hasMore()) { product = new Product(); product.setId(4); product.setName("product4"); product.setPrice(200); product.setGroup(group); LOG.debug("Creating new product: " + product); db.create(product); } else { LOG.debug("Query result: " + results.next()); } LOG.info("End transaction: one-to-one, one-to-many and dependent relations"); db.commit(); db.begin(); LOG.info("Begin transaction: one-to-one and dependent relations"); group = (ProductGroup) db.load(ProductGroup.class, new Integer(3)); // If no such products with ids 5-8 exist, create new objects and persist them for (int i = 5; i < 10; ++i) { int j = i + 1; productOql.bind(j); results = productOql.execute(); if (!results.hasMore()) { product = new Product(); product.setId(i); product.setName("product" + product.getId()); product.setPrice(300); product.setGroup(group); LOG.debug("Creating new product: " + product); db.create(product); } else { LOG.debug("Query result: " + results.next()); } } LOG.info("End transaction: one-to-one and dependent relations"); db.commit(); product.setPrice(333); LOG.info("Updated Product price: " + product); db.begin(); LOG.info("Begin transaction: long transaction"); // Don't forget to implement TimeStampable for the long transaction!!! db.update(product); LOG.info("End transaction: long transaction"); db.commit(); db.close(); LOG.info("Test complete"); }