//$Id: FooBarTest.java 10976 2006-12-12 23:22:26Z steve.ebersole@jboss.com $
package org.hibernate.test.legacy;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.SortedSet;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.TreeSet;
import junit.framework.Test;
import junit.textui.TestRunner;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.FlushMode;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LazyInitializationException;
import org.hibernate.LockMode;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.Query;
import org.hibernate.QueryException;
import org.hibernate.ScrollableResults;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.classic.Session;
import org.hibernate.connection.ConnectionProvider;
import org.hibernate.connection.DriverManagerConnectionProvider;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.InterbaseDialect;
import org.hibernate.dialect.MckoiDialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.Oracle9Dialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.PointbaseDialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.dialect.SAPDBDialect;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.dialect.TimesTenDialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.jmx.HibernateService;
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
import org.hibernate.mapping.RootClass;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.type.Type;
import org.hibernate.util.JoinedIterator;
import org.hibernate.util.SerializationHelper;
public class FooBarTest extends LegacyTestCase {
public FooBarTest(String arg) {
super(arg);
}
public String[] getMappings() {
return new String[] {
"legacy/FooBar.hbm.xml",
"legacy/Baz.hbm.xml",
"legacy/Qux.hbm.xml",
"legacy/Glarch.hbm.xml",
"legacy/Fum.hbm.xml",
"legacy/Fumm.hbm.xml",
"legacy/Fo.hbm.xml",
"legacy/One.hbm.xml",
"legacy/Many.hbm.xml",
"legacy/Immutable.hbm.xml",
"legacy/Fee.hbm.xml",
"legacy/Vetoer.hbm.xml",
"legacy/Holder.hbm.xml",
"legacy/Location.hbm.xml",
"legacy/Stuff.hbm.xml",
"legacy/Container.hbm.xml",
"legacy/Simple.hbm.xml",
"legacy/XY.hbm.xml"
};
}
public void configure(Configuration cfg) {
super.configure( cfg );
if ( Dialect.getDialect() instanceof OracleDialect ) {
( (RootClass) cfg.getClassMapping("org.hibernate.test.legacy.Foo") ).setForceDiscriminator(false);
}
}
public static Test suite() {
return new FunctionalTestClassTestSuite( FooBarTest.class );
}
public static void main(String[] args) throws Exception {
TestRunner.run( suite() );
}
public void testSaveOrUpdateCopyAny() throws Exception {
Session s = openSession();
Bar bar = new Bar();
One one = new One();
bar.setObject(one);
s.save(bar);
GlarchProxy g = bar.getComponent().getGlarch();
bar.getComponent().setGlarch(null);
s.delete(g);
s.flush();
assertTrue( s.contains(one) );
s.connection().commit();
s.close();
s = openSession();
Bar bar2 = (Bar) s.saveOrUpdateCopy(bar);
s.flush();
s.delete(bar2);
s.flush();
s.connection().commit();
s.close();
}
public void testRefreshProxy() throws Exception {
Session s = openSession();
Glarch g = new Glarch();
Serializable gid = s.save(g);
s.flush();
s.clear();
GlarchProxy gp = (GlarchProxy) s.load(Glarch.class, gid);
gp.getName(); //force init
s.refresh(gp);
s.delete(gp);
s.flush();
s.connection().commit();
s.close();
}
public void testOnCascadeDelete() throws Exception {
if ( ! supportsCircularCascadeDelete() ) {
return;
}
Session s = openSession();
Baz baz = new Baz();
baz.subs = new ArrayList();
Baz sub = new Baz();
sub.superBaz = baz;
baz.subs.add(sub);
s.save(baz);
s.flush();
assertTrue( s.createQuery("from Baz").list().size()==2 );
s.connection().commit();
s.delete(baz);
s.flush();
s.connection().commit();
assertTrue( s.createQuery("from Baz").list().size()==0 );
s.connection().commit();
s.close();
}
public void testRemoveFromIdbag() throws Exception {
Session s = openSession();
Baz baz = new Baz();
baz.setByteBag( new ArrayList() );
byte[] bytes = { 12, 13 };
baz.getByteBag().add( new byte[] { 10, 45 } );
baz.getByteBag().add(bytes);
baz.getByteBag().add( new byte[] { 1, 11 } );
baz.getByteBag().add( new byte[] { 12 } );
s.save(baz);
s.flush();
baz.getByteBag().remove(bytes);
s.flush();
baz.getByteBag().add(bytes);
s.flush();
s.delete(baz);
s.flush();
s.connection().commit();
s.close();
}
public void testLoad() throws Exception {
Session s = openSession();
Qux q = new Qux();
s.save(q);
BarProxy b = new Bar();
s.save(b);
s.flush();
s.connection().commit();
s.close();
s = openSession();
q = (Qux) s.load(Qux.class, q.getKey() );
b = (BarProxy) s.load( Foo.class, b.getKey() );
b.getKey();
assertFalse( Hibernate.isInitialized(b) );
b.getBarString();
assertTrue( Hibernate.isInitialized(b) );
BarProxy b2 = (BarProxy) s.load( Bar.class, new String( b.getKey() ) );
Qux q2 = (Qux) s.load( Qux.class, q.getKey() );
assertTrue( "loaded same object", q==q2 );
assertTrue( "loaded same object", b==b2 );
assertTrue( Math.round( b.getFormula() ) == b.getInt()/2 );
s.delete(q2);
s.delete(b2);
s.flush();
s.connection().commit();
s.close();
}
public void testJoin() throws Exception {
Session s = openSession();
Foo foo = new Foo();
foo.setJoinedProp("foo");
s.save(foo);
s.flush();
foo.setJoinedProp("bar");
s.flush();
String fid = foo.getKey();
s.delete(foo);
s.flush();
s.connection().commit();
s.close();
s = openSession();
Foo foo2 = new Foo();
foo2.setJoinedProp("foo");
s.save(foo2);
s.find("select foo.id from Foo foo where foo.joinedProp = 'foo'");
assertNull( s.get(Foo.class, fid) );
s.delete(foo2);
s.flush();
s.connection().commit();
s.close();
}
public void testDereferenceLazyCollection() throws Exception {
Session s = openSession();
Baz baz = new Baz();
baz.setFooSet( new HashSet() );
Foo foo = new Foo();
baz.getFooSet().add(foo);
s.save(foo);
s.save(baz);
foo.setBytes( "foobar".getBytes() );
s.flush();
s.connection().commit();
s.close();
s = openSession();
foo = (Foo) s.get( Foo.class, foo.getKey() );
assertTrue( Hibernate.isInitialized( foo.getBytes() ) );
assertTrue( foo.getBytes().length==6 );
baz = (Baz) s.get( Baz.class, baz.getCode() );
assertTrue( baz.getFooSet().size()==1 );
s.flush();
s.connection().commit();
s.close();
getSessions().evictCollection("org.hibernate.test.legacy.Baz.fooSet");
s = openSession();
baz = (Baz) s.get( Baz.class, baz.getCode() );
assertFalse( Hibernate.isInitialized( baz.getFooSet() ) );
baz.setFooSet(null);
s.flush();
s.connection().commit(