KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > cascade > RefreshTest


1 // $Id: RefreshTest.java,v 1.2 2005/05/04 02:32:31 steveebersole Exp $
2
package org.hibernate.test.cascade;
3
4 import org.hibernate.test.TestCase;
5 import org.hibernate.Session;
6 import org.hibernate.Transaction;
7
8 import java.util.Date JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.sql.Connection JavaDoc;
11 import java.sql.PreparedStatement JavaDoc;
12
13 import junit.framework.Test;
14 import junit.framework.TestSuite;
15
16 /**
17  * Implementation of RefreshTest.
18  *
19  * @author Steve Ebersole
20  */

21 public class RefreshTest extends TestCase {
22
23     public RefreshTest(String JavaDoc name) {
24         super( name );
25     }
26
27     protected String JavaDoc[] getMappings() {
28         return new String JavaDoc[] {
29             "cascade/Job.hbm.xml",
30             "cascade/JobBatch.hbm.xml"
31         };
32     }
33
34     public static Test suite() {
35         return new TestSuite( RefreshTest.class );
36     }
37
38     public void testRefreshCascade() throws Throwable JavaDoc {
39         Session session = openSession();
40         Transaction txn = session.beginTransaction();
41
42         JobBatch batch = new JobBatch( new Date JavaDoc() );
43         batch.createJob().setProcessingInstructions( "Just do it!" );
44         batch.createJob().setProcessingInstructions( "I know you can do it!" );
45
46         // write the stuff to the database; at this stage all job.status values are zero
47
session.persist( batch );
48         session.flush();
49
50         // behind the session's back, let's modify the statuses
51
updateStatuses( session.connection() );
52
53         // Now lets refresh the persistent batch, and see if the refresh cascaded to the jobs collection elements
54
session.refresh( batch );
55
56         Iterator JavaDoc itr = batch.getJobs().iterator();
57         while( itr.hasNext() ) {
58             Job job = ( Job ) itr.next();
59             assertEquals( "Jobs not refreshed!", 1, job.getStatus() );
60         }
61
62         txn.rollback();
63         session.close();
64     }
65
66     private void updateStatuses(Connection JavaDoc connection) throws Throwable JavaDoc {
67
68         PreparedStatement JavaDoc stmnt = null;
69         try {
70             stmnt = connection.prepareStatement( "UPDATE t_job SET job_status = 1" );
71             stmnt.executeUpdate();
72         }
73         finally {
74             if ( stmnt != null ) {
75                 try {
76                     stmnt.close();
77                 }
78                 catch( Throwable JavaDoc ignore ) {
79                 }
80             }
81         }
82     }
83 }
84
Popular Tags