~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-distribution-3.3.2.GA/project/testsuite/src/test/java/org/hibernate/test/subclassfilter/JoinedSubclassFilterTest.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// $Id: JoinedSubclassFilterTest.java 10977 2006-12-12 23:28:04Z steve.ebersole@jboss.com $
2
 
package org.hibernate.test.subclassfilter;
3
 
 
4
 
import java.util.ArrayList;
5
 
import java.util.HashSet;
6
 
import java.util.Iterator;
7
 
import java.util.List;
8
 
 
9
 
import junit.framework.Test;
10
 
 
11
 
import org.hibernate.Transaction;
12
 
import org.hibernate.classic.Session;
13
 
import org.hibernate.junit.functional.FunctionalTestCase;
14
 
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
15
 
 
16
 
/**
17
 
 * Implementation of JoinedSubclassFilterTest.
18
 
 *
19
 
 * @author Steve Ebersole
20
 
 */
21
 
public class JoinedSubclassFilterTest extends FunctionalTestCase {
22
 
 
23
 
        public JoinedSubclassFilterTest(String name) {
24
 
                super( name );
25
 
        }
26
 
 
27
 
        public final String[] getMappings() {
28
 
                return new String[] { "subclassfilter/joined-subclass.hbm.xml" };
29
 
        }
30
 
 
31
 
        public static Test suite() {
32
 
                return new FunctionalTestClassTestSuite( JoinedSubclassFilterTest.class );
33
 
        }
34
 
 
35
 
        public void testFiltersWithJoinedSubclass() {
36
 
                Session s = openSession();
37
 
                s.enableFilter( "region" ).setParameter( "userRegion", "US" );
38
 
                Transaction t = s.beginTransaction();
39
 
 
40
 
                prepareTestData( s );
41
 
                s.clear();
42
 
 
43
 
                List results = s.createQuery( "from Person" ).list();
44
 
                assertEquals( "Incorrect qry result count", 4, results.size() );
45
 
                s.clear();
46
 
 
47
 
                results = s.createQuery( "from Employee" ).list();
48
 
                assertEquals( "Incorrect qry result count", 2, results.size() );
49
 
                Iterator itr = results.iterator();
50
 
                while ( itr.hasNext() ) {
51
 
                        // find john
52
 
                        final Person p = ( Person ) itr.next();
53
 
                        if ( p.getName().equals( "John Doe" ) ) {
54
 
                                Employee john = ( Employee ) p;
55
 
                                assertEquals( "Incorrect fecthed minions count", 2, john.getMinions().size() );
56
 
                                break;
57
 
                        }
58
 
                }
59
 
                s.clear();
60
 
 
61
 
                // 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;
62
 
                // this is consistent with the behaviour of a collection-level where.
63
 
                // this might be one argument for "pulling" the attached class-level filters into collection assocations,
64
 
                // although we'd need some way to apply the appropriate alias in that scenario.
65
 
                results = new ArrayList( new HashSet( s.createQuery( "from Person as p left join fetch p.minions" ).list() ) );
66
 
                assertEquals( "Incorrect qry result count", 4, results.size() );
67
 
                itr = results.iterator();
68
 
                while ( itr.hasNext() ) {
69
 
                        // find john
70
 
                        final Person p = ( Person ) itr.next();
71
 
                        if ( p.getName().equals( "John Doe" ) ) {
72
 
                                Employee john = ( Employee ) p;
73
 
                                assertEquals( "Incorrect fecthed minions count", 2, john.getMinions().size() );
74
 
                                break;
75
 
                        }
76
 
                }
77
 
                s.clear();
78
 
 
79
 
                results = new ArrayList( new HashSet( s.createQuery( "from Employee as p left join fetch p.minions" ).list() ) );
80
 
                assertEquals( "Incorrect qry result count", 2, results.size() );
81
 
                itr = results.iterator();
82
 
                while ( itr.hasNext() ) {
83
 
                        // find john
84
 
                        final Person p = ( Person ) itr.next();
85
 
                        if ( p.getName().equals( "John Doe" ) ) {
86
 
                                Employee john = ( Employee ) p;
87
 
                                assertEquals( "Incorrect fecthed minions count", 2, john.getMinions().size() );
88
 
                                break;
89
 
                        }
90
 
                }
91
 
 
92
 
                t.commit();
93
 
                s.close();
94
 
 
95
 
                s = openSession();
96
 
                t = s.beginTransaction();
97
 
                s.createQuery( "delete Customer where contactOwner is not null" ).executeUpdate();
98
 
                s.createQuery( "delete Employee where manager is not null" ).executeUpdate();
99
 
                s.createQuery( "delete Person" ).executeUpdate();
100
 
                t.commit();
101
 
                s.close();
102
 
        }
103
 
 
104
 
        private void prepareTestData(Session s) {
105
 
                Employee john = new Employee("John Doe");
106
 
                john.setCompany( "JBoss" );
107
 
                john.setDepartment( "hr" );
108
 
                john.setTitle( "hr guru" );
109
 
                john.setRegion( "US" );
110
 
 
111
 
                Employee polli = new Employee("Polli Wog");
112
 
                polli.setCompany( "JBoss" );
113
 
                polli.setDepartment( "hr" );
114
 
                polli.setTitle( "hr novice" );
115
 
                polli.setRegion( "US" );
116
 
                polli.setManager( john );
117
 
                john.getMinions().add( polli );
118
 
 
119
 
                Employee suzie = new Employee( "Suzie Q" );
120
 
                suzie.setCompany( "JBoss" );
121
 
                suzie.setDepartment( "hr" );
122
 
                suzie.setTitle( "hr novice" );
123
 
                suzie.setRegion( "EMEA" );
124
 
                suzie.setManager( john );
125
 
                john.getMinions().add( suzie );
126
 
 
127
 
                Customer cust = new Customer( "John Q Public" );
128
 
                cust.setCompany( "Acme" );
129
 
                cust.setRegion( "US" );
130
 
                cust.setContactOwner( john );
131
 
 
132
 
                Person ups = new Person( "UPS guy" );
133
 
                ups.setCompany( "UPS" );
134
 
                ups.setRegion( "US" );
135
 
 
136
 
                s.save( john );
137
 
                s.save( cust );
138
 
                s.save( ups );
139
 
 
140
 
                s.flush();
141
 
        }
142
 
 
143
 
}