Source code: org/hibernate/test/cascade/RefreshTest.java
1 // $Id: RefreshTest.java 8991 2006-01-09 11:14:27Z maxcsaucdk $
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;
9 import java.util.Iterator;
10 import java.sql.Connection;
11 import java.sql.PreparedStatement;
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 name) {
24 super( name );
25 }
26
27 protected String[] getMappings() {
28 return new String[] {
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 {
39 Session session = openSession();
40 Transaction txn = session.beginTransaction();
41
42 JobBatch batch = new JobBatch( new Date() );
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 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 connection) throws Throwable {
67
68 PreparedStatement 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 ignore ) {
79 }
80 }
81 }
82 }
83 }