KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > hql > ScrollableCollectionFetchingTest


1 // $Id: ScrollableCollectionFetchingTest.java,v 1.2 2005/06/22 18:58:15 oneovthafew Exp $
2
package org.hibernate.test.hql;
3
4 import org.hibernate.test.TestCase;
5 import org.hibernate.Session;
6 import org.hibernate.Transaction;
7 import org.hibernate.ScrollableResults;
8 import org.hibernate.HibernateException;
9 import junit.framework.Test;
10 import junit.framework.TestSuite;
11
12 /**
13  * Tests the new functionality of allowing scrolling of results which
14  * contain collection fetches.
15  *
16  * @author Steve Ebersole
17  */

18 public class ScrollableCollectionFetchingTest extends TestCase {
19
20     public ScrollableCollectionFetchingTest(String JavaDoc name) {
21         super( name );
22     }
23
24     public static Test suite() {
25         return new TestSuite( ScrollableCollectionFetchingTest.class );
26     }
27
28     protected String JavaDoc[] getMappings() {
29         return new String JavaDoc[] { "hql/Animal.hbm.xml" };
30     }
31
32     public void testTupleReturnFails() {
33         Session s = openSession();
34         Transaction txn = s.beginTransaction();
35
36         try {
37             s.createQuery( "select a, a.weight from Animal a inner join fetch a.offspring" ).scroll();
38             fail( "scroll allowed with collection fetch and reurning tuples" );
39         }
40         catch( HibernateException e ) {
41             // expected result...
42
}
43
44         txn.commit();
45         s.close();
46     }
47
48     public void testScrollingJoinFetchesForward() {
49         TestData data = new TestData();
50         data.prepare();
51
52         Session s = openSession();
53         Transaction txn = s.beginTransaction();
54
55         ScrollableResults results = s
56                 .createQuery( "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
57                 .setString( "desc", "root%" )
58                 .scroll();
59
60         int counter = 0;
61         while ( results.next() ) {
62             counter++;
63             Animal animal = ( Animal ) results.get( 0 );
64             checkResult( animal );
65         }
66         assertEquals( "unexpected result count", 2, counter );
67
68         txn.commit();
69         s.close();
70
71         data.cleanup();
72     }
73
74     public void testScrollingJoinFetchesReverse() {
75         TestData data = new TestData();
76         data.prepare();
77
78         Session s = openSession();
79         Transaction txn = s.beginTransaction();
80
81         ScrollableResults results = s
82                 .createQuery( "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
83                 .setString( "desc", "root%" )
84                 .scroll();
85
86         results.afterLast();
87
88         int counter = 0;
89         while ( results.previous() ) {
90             counter++;
91             Animal animal = ( Animal ) results.get( 0 );
92             checkResult( animal );
93         }
94         assertEquals( "unexpected result count", 2, counter );
95
96         txn.commit();
97         s.close();
98
99         data.cleanup();
100     }
101
102     public void testScrollingJoinFetchesPositioning() {
103         TestData data = new TestData();
104         data.prepare();
105
106         Session s = openSession();
107         Transaction txn = s.beginTransaction();
108
109         ScrollableResults results = s
110                 .createQuery( "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
111                 .setString( "desc", "root%" )
112                 .scroll();
113
114         results.first();
115         Animal animal = ( Animal ) results.get( 0 );
116         assertEquals( "first() did not return expected row", data.root1Id, animal.getId() );
117
118         results.scroll( 1 );
119         animal = ( Animal ) results.get( 0 );
120         assertEquals( "scroll(1) did not return expected row", data.root2Id, animal.getId() );
121
122         results.scroll( -1 );
123         animal = ( Animal ) results.get( 0 );
124         assertEquals( "scroll(-1) did not return expected row", data.root1Id, animal.getId() );
125
126         results.setRowNumber( 1 );
127         animal = ( Animal ) results.get( 0 );
128         assertEquals( "setRowNumber(1) did not return expected row", data.root1Id, animal.getId() );
129
130         results.setRowNumber( 2 );
131         animal = ( Animal ) results.get( 0 );
132         assertEquals( "setRowNumber(2) did not return expected row", data.root2Id, animal.getId() );
133
134         txn.commit();
135         s.close();
136
137         data.cleanup();
138     }
139
140     private void checkResult(Animal animal) {
141         if ( "root-1".equals( animal.getDescription() ) ) {
142             assertEquals( "root-1 did not contain both children", 2, animal.getOffspring().size() );
143         }
144         else if ( "root-2".equals( animal.getDescription() ) ) {
145             assertEquals( "root-2 did not contain zero children", 0, animal.getOffspring().size() );
146         }
147     }
148
149     private class TestData {
150
151         private Long JavaDoc root1Id;
152         private Long JavaDoc root2Id;
153
154         private void prepare() {
155             Session s = openSession();
156             Transaction txn = s.beginTransaction();
157
158             Animal mother = new Animal();
159             mother.setDescription( "root-1" );
160
161             Animal another = new Animal();
162             another.setDescription( "root-2" );
163
164             Animal son = new Animal();
165             son.setDescription( "son");
166
167             Animal daughter = new Animal();
168             daughter.setDescription( "daughter" );
169
170             Animal grandson = new Animal();
171             grandson.setDescription( "grandson" );
172
173             Animal grandDaughter = new Animal();
174             grandDaughter.setDescription( "granddaughter" );
175
176             son.setMother( mother );
177             mother.addOffspring( son );
178
179             daughter.setMother( mother );
180             mother.addOffspring( daughter );
181
182             grandson.setMother( daughter );
183             daughter.addOffspring( grandson );
184
185             grandDaughter.setMother( daughter );
186             daughter.addOffspring( grandDaughter );
187
188             s.save( mother );
189             s.save( another );
190             s.save( son );
191             s.save( daughter );
192             s.save( grandson );
193             s.save( grandDaughter );
194
195             txn.commit();
196             s.close();
197
198             root1Id = mother.getId();
199             root2Id = another.getId();
200         }
201
202         private void cleanup() {
203             Session s = openSession();
204             Transaction txn = s.beginTransaction();
205             
206             s.createQuery( "delete Animal where description like 'grand%'" ).executeUpdate();
207             s.createQuery( "delete Animal where not description like 'root%'" ).executeUpdate();
208             s.createQuery( "delete Animal" ).executeUpdate();
209
210             txn.commit();
211             s.close();
212         }
213     }
214 }
215
Popular Tags