FileDocCategorySizeDatePackage
MultiRepresentationTest.javaAPI DocHibernate 3.2.55846Tue Dec 12 16:22:26 GMT 2006org.hibernate.test.entitymode.multi

MultiRepresentationTest

public class MultiRepresentationTest extends org.hibernate.junit.functional.FunctionalTestCase
Implementation of MultiRepresentationTest.
author
Steve Ebersole

Fields Summary
Constructors Summary
public MultiRepresentationTest(String name)

		super( name );
	
Methods Summary
public java.lang.String[]getMappings()

		return new String[] { "entitymode/multi/Stock.hbm.xml", "entitymode/multi/Valuation.hbm.xml" };
	
private voidprettyPrint(org.dom4j.Element element)

		//System.out.println( element.asXML() );
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			new XMLWriter( System.out, format ).write( element );
			System.out.println();
		}
		catch ( Throwable t ) {
			System.err.println( "Unable to pretty print element : " + t );
		}
	
public static junit.framework.Testsuite()

		return new FunctionalTestClassTestSuite( MultiRepresentationTest.class );
	
public voidtestDom4jHQL()

		TestData testData = new TestData();
		testData.create();

		Session session = openSession();
		Transaction txn = session.beginTransaction();
		org.hibernate.Session dom4j = session.getSession( EntityMode.DOM4J );

		List result = dom4j.createQuery( "from Stock" ).list();

		assertEquals( "Incorrect result size", 1, result.size() );
		Element element = ( Element ) result.get( 0 );
		assertEquals( "Something wrong!", testData.stockId, Long.valueOf( element.attributeValue( "id" ) ) );

		System.out.println( "**** XML: ****************************************************" );
		prettyPrint( element );
		System.out.println( "**************************************************************" );

		txn.rollback();
		session.close();

		testData.destroy();
	
public voidtestDom4jRetreival()

		TestData testData = new TestData();
		testData.create();

		Session session = openSession();
		Transaction txn = session.beginTransaction();
		org.hibernate.Session dom4j = session.getSession( EntityMode.DOM4J );

		Object rtn = dom4j.get( Stock.class.getName(), testData.stockId );
		Element element = ( Element ) rtn;

		assertEquals( "Something wrong!", testData.stockId, Long.valueOf( element.attributeValue( "id" ) ) );

		System.out.println( "**** XML: ****************************************************" );
		prettyPrint( element );
		System.out.println( "**************************************************************" );

		Element currVal = element.element( "currentValuation" );

		System.out.println( "**** XML: ****************************************************" );
		prettyPrint( currVal );
		System.out.println( "**************************************************************" );

		txn.rollback();
		session.close();

		testData.destroy();
	
public voidtestDom4jSave()

		TestData testData = new TestData();
		testData.create();

		Session pojos = openSession();
		Transaction txn = pojos.beginTransaction();
		org.hibernate.Session dom4j = pojos.getSession( EntityMode.DOM4J );

		Element stock = DocumentFactory.getInstance().createElement( "stock" );
		stock.addElement( "tradeSymbol" ).setText( "IBM" );

		Element val = stock.addElement( "currentValuation" ).addElement( "valuation" );
		val.appendContent( stock );
		val.addElement( "valuationDate" ).setText( new java.util.Date().toString() );
		val.addElement( "value" ).setText( "121.00" );

		dom4j.save( Stock.class.getName(), stock );
		dom4j.flush();

		txn.rollback();
		pojos.close();

		assertTrue( !pojos.isOpen() );
		assertTrue( !dom4j.isOpen() );

		prettyPrint( stock );

		testData.destroy();
	
public voidtestPojoRetreival()

		TestData testData = new TestData();
		testData.create();

		Session session = openSession();
		Transaction txn = session.beginTransaction();

		Stock stock = ( Stock ) session.get( Stock.class, new Long( 1 ) );
		assertEquals( "Something wrong!", new Long( 1 ), stock.getId() );

		txn.commit();
		session.close();

		testData.destroy();