KickJava   Java API By Example, From Geeks To Geeks.

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


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

21 public class JoinedSubclassFilterTest extends TestCase {
22
23     public JoinedSubclassFilterTest(String JavaDoc name) {
24         super( name );
25     }
26
27     protected final String JavaDoc[] getMappings() {
28         return new String JavaDoc[] { "subclassfilter/joined-subclass.hbm.xml" };
29     }
30
31     public void testFiltersWithJoinedSubclass() {
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 = s.createQuery( "from Person" ).list();
40         assertEquals( "Incorrect qry result count", 4, results.size() );
41         s.clear();
42
43         results = s.createQuery( "from Employee" ).list();
44         assertEquals( "Incorrect qry result count", 2, results.size() );
45         Iterator JavaDoc itr = results.iterator();
46         while ( itr.hasNext() ) {
47             // find john
48
final Person p = ( Person ) itr.next();
49             if ( p.getName().equals( "John Doe" ) ) {
50                 Employee john = ( Employee ) p;
51                 assertEquals( "Incorrect fecthed minions count", 2, john.getMinions().size() );
52                 break;
53             }
54         }
55         s.clear();
56
57         // TODO : currently impossible to define a collection-level filter w/ joined-subclass elements that will filter based on a superclass column and function correctly in (theta only?) outer joins;
58
// this is consistent with the behaviour of a collection-level where.
59
// this might be one argument for "pulling" the attached class-level filters into collection assocations,
60
// although we'd need some way to apply the appropriate alias in that scenario.
61
results = new ArrayList JavaDoc( new HashSet JavaDoc( s.createQuery( "from Person as p left join fetch p.minions" ).list() ) );
62         assertEquals( "Incorrect qry result count", 4, results.size() );
63         itr = results.iterator();
64         while ( itr.hasNext() ) {
65             // find john
66
final Person p = ( Person ) itr.next();
67             if ( p.getName().equals( "John Doe" ) ) {
68                 Employee john = ( Employee ) p;
69                 assertEquals( "Incorrect fecthed minions count", 2, john.getMinions().size() );
70                 break;
71             }
72         }
73         s.clear();
74
75         results = new ArrayList JavaDoc( new HashSet JavaDoc( s.createQuery( "from Employee as p left join fetch p.minions" ).list() ) );
76         assertEquals( "Incorrect qry result count", 2, results.size() );
77         itr = results.iterator();
78         while ( itr.hasNext() ) {
79             // find john
80
final Person p = ( Person ) itr.next();
81             if ( p.getName().equals( "John Doe" ) ) {
82                 Employee john = ( Employee ) p;
83                 assertEquals( "Incorrect fecthed minions count", 2, john.getMinions().size() );
84                 break;
85             }
86         }
87
88         t.commit();
89         s.close();
90     }
91
92     private void prepareTestData(Session s) {
93         Employee john = new Employee("John Doe");
94         john.setCompany( "JBoss" );
95         john.setDepartment( "hr" );
96         john.setTitle( "hr guru" );
97         john.setRegion( "US" );
98
99         Employee polli = new Employee("Polli Wog");
100         polli.setCompany( "JBoss" );
101         polli.setDepartment( "hr" );
102         polli.setTitle( "hr novice" );
103         polli.setRegion( "US" );
104         polli.setManager( john );
105         john.getMinions().add( polli );
106
107         Employee suzie = new Employee( "Suzie Q" );
108         suzie.setCompany( "JBoss" );
109         suzie.setDepartment( "hr" );
110         suzie.setTitle( "hr novice" );
111         suzie.setRegion( "EMEA" );
112         suzie.setManager( john );
113         john.getMinions().add( suzie );
114
115         Customer cust = new Customer( "John Q Public" );
116         cust.setCompany( "Acme" );
117         cust.setRegion( "US" );
118         cust.setContactOwner( john );
119
120         Person ups = new Person( "UPS guy" );
121         ups.setCompany( "UPS" );
122         ups.setRegion( "US" );
123
124         s.save( john );
125         s.save( cust );
126         s.save( ups );
127
128         s.flush();
129     }
130
131     public static Test suite() {
132         return new TestSuite(JoinedSubclassFilterTest.class);
133     }
134 }
135
Popular Tags