Skip to content

Commit

Permalink
HHH-7666 Replace unit tests' use of configuration()
Browse files Browse the repository at this point in the history
  • Loading branch information
brmeyer committed Oct 12, 2012
1 parent 8ecb9fd commit 9462c52
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
Expand Up @@ -23,34 +23,35 @@
*/
package org.hibernate.test.stats;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

import java.util.HashSet;
import java.util.Iterator;

import org.junit.Test;

import org.hibernate.FetchMode;
import org.hibernate.Hibernate;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.mapping.Collection;
import org.hibernate.engine.FetchStyle;
import org.hibernate.metamodel.spi.binding.PluralAttributeBinding;
import org.hibernate.stat.QueryStatistics;
import org.hibernate.stat.Statistics;
import org.hibernate.test.util.SchemaUtil;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;

/**
* Show the difference between fetch and load
*
* @author Emmanuel Bernard
*/
@FailureExpectedWithNewMetamodel
public class StatsTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
Expand All @@ -65,7 +66,6 @@ public void configure(Configuration cfg) {

@Test
@SuppressWarnings( {"UnusedAssignment"})
@FailureExpectedWithNewMetamodel
public void testCollectionFetchVsLoad() throws Exception {
Statistics stats = sessionFactory().getStatistics();
stats.clear();
Expand Down Expand Up @@ -106,9 +106,10 @@ public void testCollectionFetchVsLoad() throws Exception {
s.close();

// open second SessionFactory
Collection coll = configuration().getCollectionMapping(Continent.class.getName() + ".countries");
coll.setFetchMode(FetchMode.JOIN);
coll.setLazy(false);
PluralAttributeBinding coll = SchemaUtil.getCollection( Continent.class, "countries", metadata() );
coll.setFetchStyle( FetchStyle.JOIN );
// TODO: Is there a way to set this on the metamodel?
// coll.setLazy(false);
SessionFactory sf = configuration().buildSessionFactory();
stats = sf.getStatistics();
stats.clear();
Expand All @@ -129,9 +130,10 @@ public void testCollectionFetchVsLoad() throws Exception {
sf.close();

// open third SessionFactory
coll = configuration().getCollectionMapping(Continent.class.getName() + ".countries");
coll.setFetchMode(FetchMode.SELECT);
coll.setLazy(false);
coll = SchemaUtil.getCollection( Continent.class, "countries", metadata() );
coll.setFetchStyle( FetchStyle.SELECT );
// TODO: Is there a way to set this on the metamodel?
// coll.setLazy(false);
sf = configuration().buildSessionFactory();
stats = sf.getStatistics();
stats.clear();
Expand All @@ -156,7 +158,6 @@ public void testCollectionFetchVsLoad() throws Exception {
}

@Test
@FailureExpectedWithNewMetamodel
public void testQueryStatGathering() {
Statistics stats = sessionFactory().getStatistics();
stats.clear();
Expand Down
Expand Up @@ -23,9 +23,12 @@
*/
package org.hibernate.test.util;

import java.util.Iterator;

import org.hibernate.AssertionFailure;
import org.hibernate.metamodel.Metadata;
import org.hibernate.metamodel.spi.binding.EntityBinding;
import org.hibernate.metamodel.spi.binding.PluralAttributeBinding;
import org.hibernate.metamodel.spi.relational.Column;
import org.hibernate.metamodel.spi.relational.PrimaryKey;
import org.hibernate.metamodel.spi.relational.TableSpecification;
Expand Down Expand Up @@ -83,4 +86,20 @@ public static PrimaryKey getPrimaryKey( Class<?> entityClass,
Metadata metadata ) throws AssertionFailure {
return getTable( entityClass, metadata ).getPrimaryKey();
}

public static PluralAttributeBinding getCollection( Class<?> entityClass, String fieldName,
Metadata metadata ) {
Iterator<PluralAttributeBinding> collectionBindings
= metadata.getCollectionBindings().iterator();
while ( collectionBindings.hasNext() ) {
PluralAttributeBinding collectionBinding
= collectionBindings.next();
if ( collectionBinding.getAttribute().getName().equals( fieldName )
&& collectionBinding.getAttribute().getAttributeContainer()
.getClassReference().equals( entityClass ) ) {
return collectionBinding;
}
}
return null;
}
}

0 comments on commit 9462c52

Please sign in to comment.