1 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 import junit.framework.TestSuite; 11 12 import org.hibernate.Transaction; 13 import org.hibernate.classic.Session; 14 import org.hibernate.test.TestCase; 15 16 21 public class JoinedSubclassFilterTest extends TestCase { 22 23 public JoinedSubclassFilterTest(String name) { 24 super( name ); 25 } 26 27 protected final String [] getMappings() { 28 return new String [] { "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 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 itr = results.iterator(); 46 while ( itr.hasNext() ) { 47 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 results = new ArrayList ( new HashSet ( 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 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 ( new HashSet ( 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 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 |