package org.apache.ojb.odmg;
import org.apache.ojb.junit.ODMGTestCase;
import org.apache.ojb.odmg.shared.Article;
import org.apache.ojb.odmg.shared.ProductGroup;
import org.odmg.DList;
import org.odmg.ObjectNameNotFoundException;
import org.odmg.ObjectNameNotUniqueException;
import org.odmg.Transaction;
/**
* Demo Application that shows basic concepts for Applications using the OJB ODMG
* implementation as an transactional object server.
*
* @version $Id: NamedRootsTest.java,v 1.1.2.1 2005/03/16 01:11:58 arminw Exp $
*/
public class NamedRootsTest extends ODMGTestCase
{
private ProductGroup testProductGroup;
public static void main(String[] args)
{
String[] arr = {NamedRootsTest.class.getName()};
junit.textui.TestRunner.main(arr);
}
public NamedRootsTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
Transaction tx = odmg.newTransaction();
tx.begin();
testProductGroup = new ProductGroup();
testProductGroup.setGroupName("NamedRootsTest_" + System.currentTimeMillis());
database.makePersistent(testProductGroup);
tx.commit();
}
private Article createArticle()
{
Article example = new Article();
example.setArticleName(testProductGroup.getName());
example.setProductGroupId(testProductGroup.getId());
return example;
}
public void testBind() throws Exception
{
String bindingName = "testBind_" + System.currentTimeMillis();
TransactionImpl tx = (TransactionImpl) odmg.newTransaction();
//bind object to name
tx.begin();
Article example = createArticle();
database.makePersistent(example);
tx.commit();
tx.begin();
try
{
database.bind(example, bindingName);
Article value = (Article) database.lookup(bindingName);
assertTrue("Could not lookup object for binding name: "+bindingName, value != null);
tx.commit();
}
catch (ObjectNameNotFoundException ex)
{
// tx.abort();
fail("name " + bindingName + " should not be unknown. "+ex.getMessage());
}
catch (ObjectNameNotUniqueException e)
{
// tx.abort();
fail("should not have happened: " + e.getMessage());
}
try
{
tx.begin();
database.bind(example, bindingName);
tx.commit();
fail("We expected a ObjectNameNotUniqueException, but was not thrown");
}
catch (ObjectNameNotUniqueException ex)
{
// we wait for this exception
assertTrue(true);
tx.abort();
}
try
{
tx.begin();
database.unbind(bindingName);
tx.commit();
}
catch (ObjectNameNotFoundException ex)
{
fail("Can't unbind " + bindingName + ": "+ex.getMessage());
}
}
public void testDoubleBindInOneTx() throws Exception
{
String bindingName = "testDoubleBindInOneTx_" + System.currentTimeMillis();
DList foundList = null;
Transaction tx = odmg.newTransaction();
tx.begin();
database.bind(odmg.newDList(), bindingName);
foundList = (DList)database.lookup(bindingName);
assertTrue("Could not found bound DList", foundList != null);
foundList = null;
database.unbind(bindingName);
try
{
foundList = (DList) database.lookup(bindingName);
fail("Found unbound DList");
}
catch (ObjectNameNotFoundException ex)
{
}
database.bind(odmg.newDList(), bindingName);
try
{
foundList = (DList) database.lookup(bindingName);
}
catch (ObjectNameNotFoundException ex)
{
fail("Could not found bound DList, binding name was: "+bindingName);
}
foundList = null;
tx.commit();
tx = odmg.newTransaction();
tx.begin();
try
{
DList newList = (DList) database.lookup(bindingName);
assertNotNull(newList);
database.unbind(bindingName);
}
catch (ObjectNameNotFoundException ex)
{
fail("Could not found bound DList in new tx");
}
tx.commit();
}
public void testLookup() throws Exception
{
String bindingName = "testLookup_" + System.currentTimeMillis();
// clear named roots.
TransactionImpl tx = (TransactionImpl) odmg.newTransaction();
//bind object to name
tx.begin();
Article example = createArticle();
database.makePersistent(example);
tx.commit();
tx.begin();
try
{
database.bind(example, bindingName);
tx.commit();
}
catch (ObjectNameNotUniqueException ex)
{
tx.abort();
fail(ex.getMessage());
}
// TestThreadsNLocks look up
Article lookedUp1 = null;
tx = (TransactionImpl) odmg.newTransaction();
tx.begin();
try
{
// lookup by name binding
lookedUp1 = (Article) database.lookup(bindingName);
}
catch (ObjectNameNotFoundException ex)
{
fail("lookup by name: " + bindingName + " should not be unknown");
}
tx.commit();
// looking up object by OID should return same Object as by name
assertEquals("lookups should return identical object", example, lookedUp1);
try
{
tx.begin();
database.unbind(bindingName);
tx.commit();
}
catch (ObjectNameNotFoundException ex)
{
fail("Can't unbind " + bindingName + ": "+ex.getMessage());
}
}
public void testUnBind() throws Exception
{
String name = "testUnBind_" + System.currentTimeMillis();
Transaction tx = odmg.newTransaction();
//bind object to name
tx.begin();
Article example = createArticle();
database.makePersistent(example);
tx.commit();
// 1. perform binding
tx.begin();
try
{
database.bind(example, name);
tx.commit();
}
catch (ObjectNameNotUniqueException ex)
{
tx.abort();
fail(ex.getMessage());
}
// 2. perform unbind
tx = odmg.newTransaction();
tx.begin();
try
{
database.unbind(name);
tx.commit();
}
catch (ObjectNameNotFoundException ex)
{
tx.abort();
fail("name " + name + "should be known");
}
// 3. check if name is really unknown now
tx = odmg.newTransaction();
tx.begin();
try
{
Article value = (Article) database.lookup(name);
assertNotNull("Should not find unbind name '" + name+"'", value);
fail("name " + name + " should not be known after unbind");
}
catch (ObjectNameNotFoundException ex)
{
// OK, expected
assertTrue(true);
}
tx.abort();
}
}