DynamicFilterTestpublic class DynamicFilterTest extends org.hibernate.junit.functional.FunctionalTestCase Implementation of DynamicFilterTest. |
Fields Summary |
---|
private Log | log |
Constructors Summary |
---|
public DynamicFilterTest(String testName)
super( testName );
|
Methods Summary |
---|
public void | configure(org.hibernate.cfg.Configuration cfg)
cfg.setProperty( Environment.MAX_FETCH_DEPTH, "1" );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );
| public java.lang.String[] | getMappings()
return new String[]{
"filter/defs.hbm.xml",
"filter/LineItem.hbm.xml",
"filter/Order.hbm.xml",
"filter/Product.hbm.xml",
"filter/Salesperson.hbm.xml",
"filter/Department.hbm.xml",
"filter/Category.hbm.xml"
};
| public static junit.framework.Test | suite()
return new FunctionalTestClassTestSuite( DynamicFilterTest.class );
| public void | testCombinedClassAndCollectionFiltersEnabled()
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "regionlist" ).setParameterList( "regions", new String[]{"LA", "APAC"} );
session.enableFilter( "fulfilledOrders" ).setParameter( "asOfDate", testData.lastMonth.getTime() );
// test retreival through hql with the collection as non-eager
List salespersons = session.createQuery( "select s from Salesperson as s" ).list();
assertEquals( "Incorrect salesperson count", 1, salespersons.size() );
Salesperson sp = ( Salesperson ) salespersons.get( 0 );
assertEquals( "Incorrect order count", 1, sp.getOrders().size() );
session.clear();
// test retreival through hql with the collection join fetched
salespersons = session.createQuery( "select s from Salesperson as s left join fetch s.orders" ).list();
assertEquals( "Incorrect salesperson count", 1, salespersons.size() );
sp = ( Salesperson ) salespersons.get( 0 );
assertEquals( "Incorrect order count", 1, sp.getOrders().size() );
session.close();
testData.release();
| public void | testCriteriaQueryFilters()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Criteria-query test
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log.info( "Starting Criteria-query filter tests" );
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "region" ).setParameter( "region", "APAC" );
session.enableFilter( "fulfilledOrders" )
.setParameter( "asOfDate", testData.lastMonth.getTime() );
session.enableFilter( "effectiveDate" )
.setParameter( "asOfDate", testData.lastMonth.getTime() );
log.info( "Criteria query against Salesperson..." );
List salespersons = session.createCriteria( Salesperson.class )
.setFetchMode( "orders", FetchMode.JOIN )
.list();
assertEquals( "Incorrect salesperson count", 1, salespersons.size() );
assertEquals( "Incorrect order count", 1, ( ( Salesperson ) salespersons.get( 0 ) ).getOrders().size() );
log.info( "Criteria query against Product..." );
List products = session.createCriteria( Product.class )
.add( Expression.eq( "stockNumber", new Integer( 124 ) ) )
.list();
assertEquals( "Incorrect product count", 1, products.size() );
session.close();
testData.release();
| public void | testGetFilters()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Get() test
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log.info( "Starting get() filter tests (eager assoc. fetching)." );
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "region" ).setParameter( "region", "APAC" );
log.info( "Performing get()..." );
Salesperson salesperson = ( Salesperson ) session.get( Salesperson.class, testData.steveId );
assertNotNull( salesperson );
assertEquals( "Incorrect order count", 1, salesperson.getOrders().size() );
session.close();
testData.release();
| public void | testHqlFilters()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// HQL test
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log.info( "Starting HQL filter tests" );
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "region" ).setParameter( "region", "APAC" );
session.enableFilter( "effectiveDate" )
.setParameter( "asOfDate", testData.lastMonth.getTime() );
log.info( "HQL against Salesperson..." );
List results = session.createQuery( "select s from Salesperson as s left join fetch s.orders" ).list();
assertTrue( "Incorrect filtered HQL result count [" + results.size() + "]", results.size() == 1 );
Salesperson result = ( Salesperson ) results.get( 0 );
assertTrue( "Incorrect collectionfilter count", result.getOrders().size() == 1 );
log.info( "HQL against Product..." );
results = session.createQuery( "from Product as p where p.stockNumber = ?" ).setInteger( 0, 124 ).list();
assertTrue( results.size() == 1 );
session.close();
testData.release();
| public void | testInStyleFilterParameter()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// one-to-many loading tests
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log.info( "Starting one-to-many collection loader filter tests." );
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "regionlist" )
.setParameterList( "regions", new String[]{"LA", "APAC"} );
log.debug( "Performing query of Salespersons" );
List salespersons = session.createQuery( "from Salesperson" ).list();
assertEquals( "Incorrect salesperson count", 1, salespersons.size() );
session.close();
testData.release();
| public void | testManyToManyBase()
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
Product prod = ( Product ) session.get( Product.class, testData.prod1Id );
long initLoadCount = getSessions().getStatistics().getCollectionLoadCount();
long initFetchCount = getSessions().getStatistics().getCollectionFetchCount();
// should already have been initialized...
int size = prod.getCategories().size();
assertEquals( "Incorrect non-filtered collection count", 2, size );
long currLoadCount = getSessions().getStatistics().getCollectionLoadCount();
long currFetchCount = getSessions().getStatistics().getCollectionFetchCount();
assertTrue(
"load with join fetch of many-to-many did not trigger join fetch",
( initLoadCount == currLoadCount ) && ( initFetchCount == currFetchCount )
);
// make sure we did not get back a collection of proxies
long initEntityLoadCount = getSessions().getStatistics().getEntityLoadCount();
Iterator itr = prod.getCategories().iterator();
while ( itr.hasNext() ) {
Category cat = ( Category ) itr.next();
System.out.println( " ===> " + cat.getName() );
}
long currEntityLoadCount = getSessions().getStatistics().getEntityLoadCount();
assertTrue(
"load with join fetch of many-to-many did not trigger *complete* join fetch",
( initEntityLoadCount == currEntityLoadCount )
);
session.close();
testData.release();
| public void | testManyToManyBaseThruCriteria()
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
List result = session.createCriteria( Product.class )
.add( Expression.eq( "id", testData.prod1Id ) )
.list();
Product prod = ( Product ) result.get( 0 );
long initLoadCount = getSessions().getStatistics().getCollectionLoadCount();
long initFetchCount = getSessions().getStatistics().getCollectionFetchCount();
// should already have been initialized...
int size = prod.getCategories().size();
assertEquals( "Incorrect non-filtered collection count", 2, size );
long currLoadCount = getSessions().getStatistics().getCollectionLoadCount();
long currFetchCount = getSessions().getStatistics().getCollectionFetchCount();
assertTrue(
"load with join fetch of many-to-many did not trigger join fetch",
( initLoadCount == currLoadCount ) && ( initFetchCount == currFetchCount )
);
// make sure we did not get back a collection of proxies
long initEntityLoadCount = getSessions().getStatistics().getEntityLoadCount();
Iterator itr = prod.getCategories().iterator();
while ( itr.hasNext() ) {
Category cat = ( Category ) itr.next();
System.out.println( " ===> " + cat.getName() );
}
long currEntityLoadCount = getSessions().getStatistics().getEntityLoadCount();
assertTrue(
"load with join fetch of many-to-many did not trigger *complete* join fetch",
( initEntityLoadCount == currEntityLoadCount )
);
session.close();
testData.release();
| public void | testManyToManyFilterOnCriteria()
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "effectiveDate" ).setParameter( "asOfDate", new Date() );
Product prod = ( Product ) session.createCriteria( Product.class )
.setResultTransformer( new DistinctRootEntityResultTransformer() )
.add( Expression.eq( "id", testData.prod1Id ) )
.uniqueResult();
assertNotNull( prod );
assertEquals( "Incorrect Product.categories count for filter", 1, prod.getCategories().size() );
session.close();
testData.release();
| public void | testManyToManyFilterOnLoad()
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "effectiveDate" ).setParameter( "asOfDate", new Date() );
Product prod = ( Product ) session.get( Product.class, testData.prod1Id );
long initLoadCount = getSessions().getStatistics().getCollectionLoadCount();
long initFetchCount = getSessions().getStatistics().getCollectionFetchCount();
// should already have been initialized...
int size = prod.getCategories().size();
assertEquals( "Incorrect filtered collection count", 1, size );
long currLoadCount = getSessions().getStatistics().getCollectionLoadCount();
long currFetchCount = getSessions().getStatistics().getCollectionFetchCount();
assertTrue(
"load with join fetch of many-to-many did not trigger join fetch",
( initLoadCount == currLoadCount ) && ( initFetchCount == currFetchCount )
);
// make sure we did not get back a collection of proxies
long initEntityLoadCount = getSessions().getStatistics().getEntityLoadCount();
Iterator itr = prod.getCategories().iterator();
while ( itr.hasNext() ) {
Category cat = ( Category ) itr.next();
System.out.println( " ===> " + cat.getName() );
}
long currEntityLoadCount = getSessions().getStatistics().getEntityLoadCount();
assertTrue(
"load with join fetch of many-to-many did not trigger *complete* join fetch",
( initEntityLoadCount == currEntityLoadCount )
);
session.close();
testData.release();
| public void | testManyToManyFilterOnQuery()
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "effectiveDate" ).setParameter( "asOfDate", new Date() );
List result = session.createQuery( "from Product p inner join fetch p.categories" ).list();
assertTrue( "No products returned from HQL many-to-many filter case", !result.isEmpty() );
Product prod = ( Product ) result.get( 0 );
assertNotNull( prod );
assertEquals( "Incorrect Product.categories count for filter with HQL", 1, prod.getCategories().size() );
session.close();
testData.release();
| public void | testManyToManyOnCollectionLoadAfterHQL()
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "effectiveDate" ).setParameter( "asOfDate", new Date() );
// Force the categories to not get initialized here
List result = session.createQuery( "from Product as p where p.id = :id" )
.setLong( "id", testData.prod1Id.longValue() )
.list();
assertTrue( "No products returned from HQL", !result.isEmpty() );
Product prod = ( Product ) result.get( 0 );
assertNotNull( prod );
assertEquals( "Incorrect Product.categories count for filter on collection load", 1, prod.getCategories().size() );
session.close();
testData.release();
| public void | testOneToManyFilters()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// one-to-many loading tests
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log.info( "Starting one-to-many collection loader filter tests." );
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "seniorSalespersons" )
.setParameter( "asOfDate", testData.lastMonth.getTime() );
log.info( "Performing load of Department..." );
Department department = ( Department ) session.load( Department.class, testData.deptId );
Set salespersons = department.getSalespersons();
assertEquals( "Incorrect salesperson count", 1, salespersons.size() );
session.close();
testData.release();
| public void | testSecondLevelCachedCollectionsFiltering()
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
// Force a collection into the second level cache, with its non-filtered elements
Salesperson sp = ( Salesperson ) session.load( Salesperson.class, testData.steveId );
Hibernate.initialize( sp.getOrders() );
CollectionPersister persister = ( ( SessionFactoryImpl ) getSessions() )
.getCollectionPersister( Salesperson.class.getName() + ".orders" );
assertTrue( "No cache for collection", persister.hasCache() );
CollectionCacheEntry cachedData = ( CollectionCacheEntry ) persister.getCache().getCache()
.read( new CacheKey( testData.steveId, persister.getKeyType(), persister.getRole(), EntityMode.POJO, sfi() ) );
assertNotNull( "collection was not in cache", cachedData );
session.close();
session = openSession();
session.enableFilter( "fulfilledOrders" ).setParameter( "asOfDate", testData.lastMonth.getTime() );
sp = ( Salesperson ) session.createQuery( "from Salesperson as s where s.id = :id" )
.setLong( "id", testData.steveId.longValue() )
.uniqueResult();
assertEquals( "Filtered-collection not bypassing 2L-cache", 1, sp.getOrders().size() );
CollectionCacheEntry cachedData2 = ( CollectionCacheEntry ) persister.getCache().getCache()
.read( new CacheKey( testData.steveId, persister.getKeyType(), persister.getRole(), EntityMode.POJO, sfi() ) );
assertNotNull( "collection no longer in cache!", cachedData2 );
assertSame( "Different cache values!", cachedData, cachedData2 );
session.close();
session = openSession();
session.enableFilter( "fulfilledOrders" ).setParameter( "asOfDate", testData.lastMonth.getTime() );
sp = ( Salesperson ) session.load( Salesperson.class, testData.steveId );
assertEquals( "Filtered-collection not bypassing 2L-cache", 1, sp.getOrders().size() );
session.close();
// Finally, make sure that the original cached version did not get over-written
session = openSession();
sp = ( Salesperson ) session.load( Salesperson.class, testData.steveId );
assertEquals( "Actual cached version got over-written", 2, sp.getOrders().size() );
session.close();
testData.release();
|
|