package org.apache.ojb.jdo;
/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import junit.framework.TestCase;
import org.apache.ojb.otm.Person;
import javax.jdo.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class TestPersistenceManager extends TestCase
{
/** Cheat and use our impl directly */
private PersistenceManagerFactoryImpl factory = new PersistenceManagerFactoryImpl();
public void testLoadExtent() throws Exception
{
Person article = new Person();
PersistenceManager pm = factory.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
pm.makePersistent(article);
tx.commit();
tx.begin();
Extent extent = pm.getExtent(Person.class, true);
Iterator itty = extent.iterator();
assertTrue(itty.hasNext());
tx.commit();
pm.close();
}
public void testIteratorClosedWhenOutsideTx() throws Exception
{
Person article = new Person();
PersistenceManager pm = factory.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
pm.makePersistent(article);
tx.commit();
tx.begin();
Extent extent = pm.getExtent(Person.class, true);
Iterator itty = extent.iterator();
tx.commit();
try
{
itty.next();
fail("Should have thrown exception");
}
catch (NoSuchElementException e)
{
assertTrue("Flow of control will pass through here", true);
}
pm.close();
}
public void testVerifyCannotQueryExtentWithoutSubclasses()
{
PersistenceManager pm = factory.getPersistenceManager();
pm.currentTransaction().begin();
try
{
pm.getExtent(Person.class, false);
fail("Not supposed to be supported!");
}
catch (UnsupportedOperationException e)
{
assertTrue("Flow goes through here", true);
}
pm.currentTransaction().commit();
pm.close();
}
public void testQueryClassCast()
{
PersistenceManager pm = factory.getPersistenceManager();
pm.currentTransaction().begin();
try
{
pm.newQuery(new Object());
fail("Should not be able to pass incorrect argument to newQuery(Object)");
}
catch (IllegalArgumentException e)
{
assertTrue("Flow goes through here", true);
}
pm.currentTransaction().commit();
pm.close();
}
}