KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > subclassfilter > DiscrimSubclassFilterTest


1 // $Id: DiscrimSubclassFilterTest.java,v 1.3 2005/04/27 07:21:38 oneovthafew Exp $
2
package org.hibernate.test.subclassfilter;
3
4 import org.hibernate.test.TestCase;
5 import org.hibernate.classic.Session;
6 import org.hibernate.Transaction;
7
8 import java.util.List JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashSet JavaDoc;
12
13 import junit.framework.Test;
14 import junit.framework.TestSuite;
15
16 /**
17  * Implementation of DiscrimSubclassFilterTest.
18  *
19  * @author Steve Ebersole
20  */

21 public class DiscrimSubclassFilterTest extends TestCase {
22
23     public DiscrimSubclassFilterTest(String JavaDoc name) {
24         super( name );
25     }
26
27     protected final String JavaDoc[] getMappings() {
28         return new String JavaDoc[] { "subclassfilter/discrim-subclass.hbm.xml" };
29     }
30
31     public void testFiltersWithSubclass() {
32         Session s = openSession();
33         s.enableFilter( "region" ).setParameter( "userRegion", "US" );
34         Transaction t = s.beginTransaction();
35
36         prepareTestData( s );
37         s.clear();
38
39         List JavaDoc results;
40         Iterator JavaDoc itr;
41
42         results = s.createQuery( "from Person" ).list();
43         assertEquals( "Incorrect qry result count", 4, results.size() );
44         s.clear();
45
46         results = s.createQuery( "from Employee" ).list();
47         assertEquals( "Incorrect qry result count", 2, results.size() );
48         s.clear();
49
50         results = new ArrayList JavaDoc( new HashSet JavaDoc( s.createQuery( "from Person as p left join fetch p.minions" ).list() ) );
51         assertEquals( "Incorrect qry result count", 4, results.size() );
52         itr = results.iterator();
53         while ( itr.hasNext() ) {
54             // find john
55
final Person p = ( Person ) itr.next();
56             if ( p.getName().equals( "John Doe" ) ) {
57                 Employee john = ( Employee ) p;
58                 assertEquals( "Incorrect fecthed minions count", 1, john.getMinions().size() );
59                 break;
60             }
61         }
62         s.clear();
63
64         results = new ArrayList JavaDoc( new HashSet JavaDoc( s.createQuery( "from Employee as p left join fetch p.minions" ).list() ) );
65         assertEquals( "Incorrect qry result count", 2, results.size() );
66         itr = results.iterator();
67         while ( itr.hasNext() ) {
68             // find john
69
final Person p = ( Person ) itr.next();
70             if ( p.getName().equals( "John Doe" ) ) {
71                 Employee john = ( Employee ) p;
72                 assertEquals( "Incorrect fecthed minions count", 1, john.getMinions().size() );
73                 break;
74             }
75         }
76
77         t.commit();
78         s.close();
79     }
80
81     private void prepareTestData(Session s) {
82         Employee john = new Employee("John Doe");
83         john.setCompany( "JBoss" );
84         john.setDepartment( "hr" );
85         john.setTitle( "hr guru" );
86         john.setRegion( "US" );
87
88         Employee polli = new Employee("Polli Wog");
89         polli.setCompany( "JBoss" );
90         polli.setDepartment( "hr" );
91         polli.setTitle( "hr novice" );
92         polli.setRegion( "US" );
93         polli.setManager( john );
94         john.getMinions().add( polli );
95
96         Employee suzie = new Employee( "Suzie Q" );
97         suzie.setCompany( "JBoss" );
98         suzie.setDepartment( "hr" );
99         suzie.setTitle( "hr novice" );
100         suzie.setRegion( "EMEA" );
101         suzie.setManager( john );
102         john.getMinions().add( suzie );
103
104         Customer cust = new Customer( "John Q Public" );
105         cust.setCompany( "Acme" );
106         cust.setRegion( "US" );
107         cust.setContactOwner( john );
108
109         Person ups = new Person( "UPS guy" );
110         ups.setCompany( "UPS" );
111         ups.setRegion( "US" );
112
113         s.save( john );
114         s.save( cust );
115         s.save( ups );
116
117         s.flush();
118     }
119
120     public static Test suite() {
121         return new TestSuite(DiscrimSubclassFilterTest.class);
122     }
123 }
124
Popular Tags