KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > entity > MultiRepresentationTest


1 // $Id: MultiRepresentationTest.java,v 1.8 2005/04/03 04:24:23 oneovthafew Exp $
2
package org.hibernate.test.entity;
3
4 import java.sql.Connection JavaDoc;
5 import java.sql.Date JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.sql.Types JavaDoc;
9 import java.util.List JavaDoc;
10
11 import junit.framework.Test;
12 import junit.framework.TestSuite;
13
14 import org.dom4j.DocumentFactory;
15 import org.dom4j.Element;
16 import org.dom4j.io.OutputFormat;
17 import org.dom4j.io.XMLWriter;
18 import org.hibernate.EntityMode;
19 import org.hibernate.HibernateException;
20 import org.hibernate.Transaction;
21 import org.hibernate.classic.Session;
22 import org.hibernate.engine.SessionFactoryImplementor;
23 import org.hibernate.id.IdentifierGenerator;
24 import org.hibernate.impl.SessionImpl;
25 import org.hibernate.test.TestCase;
26
27 /**
28  * Implementation of MultiRepresentationTest.
29  *
30  * @author Steve Ebersole
31  */

32 public class MultiRepresentationTest extends TestCase {
33
34     Long JavaDoc stockId;
35     Long JavaDoc valId;
36
37     public MultiRepresentationTest(String JavaDoc name) {
38         super(name);
39     }
40
41     public void testPojoRetreival() {
42         Session session = openSession();
43         Transaction txn = session.beginTransaction();
44
45         prepareTestData( session );
46
47         Stock stock = ( Stock ) session.get( Stock.class, new Long JavaDoc(1) );
48         assertEquals( "Something wrong!", new Long JavaDoc(1), stock.getId() );
49
50         txn.rollback();
51         session.close();
52     }
53
54     public void testDom4jRetreival() {
55         Session session = openSession();
56         Transaction txn = session.beginTransaction();
57         org.hibernate.Session dom4j = session.getSession( EntityMode.DOM4J );
58
59         prepareTestData( session );
60
61         Object JavaDoc rtn = dom4j.get( Stock.class.getName(), stockId );
62         Element element = ( Element ) rtn;
63
64         assertEquals( "Something wrong!", stockId, Long.valueOf( element.attributeValue("id") ) );
65
66         System.out.println("**** XML: ****************************************************");
67         prettyPrint( element );
68         System.out.println("**************************************************************");
69
70         Element currVal = element.element( "currentValuation");
71
72         System.out.println("**** XML: ****************************************************");
73         prettyPrint( currVal );
74         System.out.println("**************************************************************");
75
76
77         txn.rollback();
78         session.close();
79     }
80
81     public void testDom4jSave() {
82         Session pojos = openSession();
83         Transaction txn = pojos.beginTransaction();
84
85         prepareTestData( pojos );
86
87         org.hibernate.Session dom4j = pojos.getSession( EntityMode.DOM4J );
88
89         Element stock = DocumentFactory.getInstance().createElement( "stock" );
90         stock.addElement( "tradeSymbol" ).setText( "IBM" );
91
92         Element val = stock.addElement( "currentValuation" ).addElement( "valuation" );
93         val.appendContent( stock );
94         val.addElement( "valuationDate" ).setText( new java.util.Date JavaDoc().toString() );
95         val.addElement( "value" ).setText( "121.00" );
96
97         dom4j.save( Stock.class.getName(), stock );
98         dom4j.flush();
99
100         txn.rollback();
101
102         pojos.close();
103
104         assertTrue( !pojos.isOpen() );
105         assertTrue( !dom4j.isOpen() );
106
107         prettyPrint( stock );
108     }
109
110     public void testDom4jHQL() {
111         Session session = openSession();
112         Transaction txn = session.beginTransaction();
113         org.hibernate.Session dom4j = session.getSession( EntityMode.DOM4J );
114
115         prepareTestData( session );
116
117         List JavaDoc result = dom4j.createQuery( "from Stock" ).list();
118
119         assertEquals( "Incorrect result size", 1, result.size() );
120         Element element = ( Element ) result.get( 0 );
121         assertEquals( "Something wrong!", stockId, Long.valueOf( element.attributeValue("id") ) );
122
123         System.out.println("**** XML: ****************************************************");
124         prettyPrint( element );
125         System.out.println("**************************************************************");
126
127         txn.rollback();
128         session.close();
129     }
130
131     private void prepareTestData(Session session) throws HibernateException {
132         try {
133             IdentifierGenerator stockIdGen = ( (SessionFactoryImplementor) session.getSessionFactory() )
134                     .getIdentifierGenerator( Stock.class.getName() );
135             IdentifierGenerator valIdGen = ( (SessionFactoryImplementor) session.getSessionFactory() )
136                     .getIdentifierGenerator( Valuation.class.getName() );
137
138             stockId = ( Long JavaDoc ) stockIdGen.generate( (SessionImpl) session, null );
139             valId = ( Long JavaDoc ) valIdGen.generate( (SessionImpl) session, null );
140
141             Connection JavaDoc conn = session.connection();
142             PreparedStatement JavaDoc ps = conn.prepareStatement( "INSERT INTO STOCK VALUES (?,?,?)");
143             ps.setLong( 1, stockId.longValue() );
144             ps.setString( 2, "JBOSS" );
145             ps.setNull(3, Types.BIGINT);
146             ps.executeUpdate();
147             ps.close();
148
149             ps = conn.prepareStatement( "INSERT INTO STOCK_VAL VALUES (?,?,?,?)" );
150             ps.setLong( 1, valId.longValue() );
151             ps.setLong( 2, stockId.longValue() );
152             ps.setDate( 3, new Date JavaDoc( new java.util.Date JavaDoc().getTime() ) );
153             ps.setDouble( 4, 200.0 );
154             ps.executeUpdate();
155             ps.close();
156
157             ps = conn.prepareStatement( "UPDATE STOCK SET CURR_VAL_ID = ? WHERE STOCK_ID = ?" );
158             ps.setLong( 1, valId.longValue() );
159             ps.setLong( 2, stockId.longValue() );
160             ps.executeUpdate();
161             ps.close();
162         }
163         catch( SQLException JavaDoc e ) {
164             System.err.println( "Error : " + e );
165             e.printStackTrace();
166             throw new HibernateException("Unable to generate test data",e);
167         }
168     }
169
170     private void prettyPrint(Element element) {
171         //System.out.println( element.asXML() );
172
try {
173             OutputFormat format = OutputFormat.createPrettyPrint();
174             new XMLWriter( System.out, format ).write( element );
175             System.out.println();
176         }
177         catch( Throwable JavaDoc t ) {
178             System.err.println("Unable to pretty print element : " + t);
179         }
180     }
181
182     public static Test suite() {
183         return new TestSuite( MultiRepresentationTest.class );
184     }
185
186     protected String JavaDoc[] getMappings() {
187         return new String JavaDoc[] {"entity/Stock.hbm.xml", "entity/Valuation.hbm.xml"};
188     }
189 }
190
Popular Tags