git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19405 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Hardy Ferentschik 2010-05-07 13:46:32 +00:00
parent 0884b16946
commit 1a8a602e3c
6 changed files with 169 additions and 158 deletions

View File

@ -5,6 +5,7 @@ import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.junit.RequiresDialectFeature;
import org.hibernate.mapping.Column;
import org.hibernate.test.annotations.TestCase;
import org.hibernate.test.annotations.id.sequences.entities.Ball;
@ -29,27 +30,28 @@ import org.hibernate.test.annotations.id.sequences.entities.Tree;
* @author Emmanuel Bernard
*/
@SuppressWarnings("unchecked")
@RequiresDialectFeature("supportsSequences")
public class IdTest extends TestCase {
public void testGenericGenerator() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
SoundSystem system = new SoundSystem();
system.setBrand("Genelec");
system.setModel("T234");
system.setBrand( "Genelec" );
system.setModel( "T234" );
Furniture fur = new Furniture();
s.persist(system);
s.persist(fur);
s.persist( system );
s.persist( fur );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
system = (SoundSystem) s.get(SoundSystem.class, system.getId());
fur = (Furniture) s.get(Furniture.class, fur.getId());
assertNotNull(system);
assertNotNull(fur);
s.delete(system);
s.delete(fur);
system = ( SoundSystem ) s.get( SoundSystem.class, system.getId() );
fur = ( Furniture ) s.get( Furniture.class, fur.getId() );
assertNotNull( system );
assertNotNull( fur );
s.delete( system );
s.delete( fur );
tx.commit();
s.close();
@ -59,13 +61,14 @@ public class IdTest extends TestCase {
* Ensures that GenericGenerator annotations wrapped inside a
* GenericGenerators holder are bound correctly
*/
public void testGenericGenerators() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Monkey monkey = new Monkey();
s.persist(monkey);
s.persist( monkey );
s.flush();
assertNotNull(monkey.getId());
assertNotNull( monkey.getId() );
tx.rollback();
s.close();
}
@ -77,21 +80,23 @@ public class IdTest extends TestCase {
Ball b = new Ball();
Dog d = new Dog();
Computer c = new Computer();
s.persist(b);
s.persist(d);
s.persist(c);
s.persist( b );
s.persist( d );
s.persist( c );
tx.commit();
s.close();
assertEquals("table id not generated", new Integer(1), b.getId());
assertEquals("generator should not be shared", new Integer(1), d
.getId());
assertEquals("default value should work", new Long(1), c.getId());
assertEquals( "table id not generated", new Integer( 1 ), b.getId() );
assertEquals(
"generator should not be shared", new Integer( 1 ), d
.getId()
);
assertEquals( "default value should work", new Long( 1 ), c.getId() );
s = openSession();
tx = s.beginTransaction();
s.delete(s.get(Ball.class, new Integer(1)));
s.delete(s.get(Dog.class, new Integer(1)));
s.delete(s.get(Computer.class, new Long(1)));
s.delete( s.get( Ball.class, new Integer( 1 ) ) );
s.delete( s.get( Dog.class, new Integer( 1 ) ) );
s.delete( s.get( Computer.class, new Long( 1 ) ) );
tx.commit();
s.close();
}
@ -100,14 +105,14 @@ public class IdTest extends TestCase {
Session s = openSession();
Transaction tx = s.beginTransaction();
Shoe b = new Shoe();
s.persist(b);
s.persist( b );
tx.commit();
s.close();
assertNotNull(b.getId());
assertNotNull( b.getId() );
s = openSession();
tx = s.beginTransaction();
s.delete(s.get(Shoe.class, b.getId()));
s.delete( s.get( Shoe.class, b.getId() ) );
tx.commit();
s.close();
}
@ -116,14 +121,14 @@ public class IdTest extends TestCase {
Session s = openSession();
Transaction tx = s.beginTransaction();
Store b = new Store();
s.persist(b);
s.persist( b );
tx.commit();
s.close();
assertNotNull(b.getId());
assertNotNull( b.getId() );
s = openSession();
tx = s.beginTransaction();
s.delete(s.get(Store.class, b.getId()));
s.delete( s.get( Store.class, b.getId() ) );
tx.commit();
s.close();
}
@ -132,14 +137,14 @@ public class IdTest extends TestCase {
Session s = openSession();
Transaction tx = s.beginTransaction();
Department b = new Department();
s.persist(b);
s.persist( b );
tx.commit();
s.close();
assertNotNull(b.getId());
assertNotNull( b.getId() );
s = openSession();
tx = s.beginTransaction();
s.delete(s.get(Department.class, b.getId()));
s.delete( s.get( Department.class, b.getId() ) );
tx.commit();
s.close();
}
@ -150,16 +155,16 @@ public class IdTest extends TestCase {
s = openSession();
tx = s.beginTransaction();
Home h = new Home();
s.persist(h);
s.persist( h );
tx.commit();
s.close();
assertNotNull(h.getId());
assertNotNull( h.getId() );
s = openSession();
tx = s.beginTransaction();
Home reloadedHome = (Home) s.get(Home.class, h.getId());
assertEquals(h.getId(), reloadedHome.getId());
s.delete(reloadedHome);
Home reloadedHome = ( Home ) s.get( Home.class, h.getId() );
assertEquals( h.getId(), reloadedHome.getId() );
s.delete( reloadedHome );
tx.commit();
s.close();
}
@ -170,16 +175,16 @@ public class IdTest extends TestCase {
s = openSession();
tx = s.beginTransaction();
Home h = new Home();
s.persist(h);
s.persist( h );
tx.commit();
s.close();
assertNotNull(h.getId());
assertNotNull( h.getId() );
s = openSession();
tx = s.beginTransaction();
Home reloadedHome = (Home) s.get(Home.class, h.getId());
assertEquals(h.getId(), reloadedHome.getId());
s.delete(reloadedHome);
Home reloadedHome = ( Home ) s.get( Home.class, h.getId() );
assertEquals( h.getId(), reloadedHome.getId() );
s.delete( reloadedHome );
tx.commit();
s.close();
}
@ -190,13 +195,13 @@ public class IdTest extends TestCase {
s = openSession();
tx = s.beginTransaction();
FirTree chrismasTree = new FirTree();
s.persist(chrismasTree);
s.persist( chrismasTree );
tx.commit();
s.clear();
tx = s.beginTransaction();
chrismasTree = (FirTree) s.get(FirTree.class, chrismasTree.getId());
assertNotNull(chrismasTree);
s.delete(chrismasTree);
chrismasTree = ( FirTree ) s.get( FirTree.class, chrismasTree.getId() );
assertNotNull( chrismasTree );
s.delete( chrismasTree );
tx.commit();
s.close();
}
@ -206,55 +211,58 @@ public class IdTest extends TestCase {
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Footballer fb = new Footballer("David", "Beckam", "Arsenal");
GoalKeeper keeper = new GoalKeeper("Fabien", "Bartez", "OM");
s.persist(fb);
s.persist(keeper);
Footballer fb = new Footballer( "David", "Beckam", "Arsenal" );
GoalKeeper keeper = new GoalKeeper( "Fabien", "Bartez", "OM" );
s.persist( fb );
s.persist( keeper );
tx.commit();
s.clear();
// lookup by id
tx = s.beginTransaction();
FootballerPk fpk = new FootballerPk("David", "Beckam");
fb = (Footballer) s.get(Footballer.class, fpk);
FootballerPk fpk2 = new FootballerPk("Fabien", "Bartez");
keeper = (GoalKeeper) s.get(GoalKeeper.class, fpk2);
assertNotNull(fb);
assertNotNull(keeper);
assertEquals("Beckam", fb.getLastname());
assertEquals("Arsenal", fb.getClub());
assertEquals(1, s.createQuery(
"from Footballer f where f.firstname = 'David'").list().size());
FootballerPk fpk = new FootballerPk( "David", "Beckam" );
fb = ( Footballer ) s.get( Footballer.class, fpk );
FootballerPk fpk2 = new FootballerPk( "Fabien", "Bartez" );
keeper = ( GoalKeeper ) s.get( GoalKeeper.class, fpk2 );
assertNotNull( fb );
assertNotNull( keeper );
assertEquals( "Beckam", fb.getLastname() );
assertEquals( "Arsenal", fb.getClub() );
assertEquals(
1, s.createQuery(
"from Footballer f where f.firstname = 'David'"
).list().size()
);
tx.commit();
// reattach by merge
tx = s.beginTransaction();
fb.setClub("Bimbo FC");
s.merge(fb);
fb.setClub( "Bimbo FC" );
s.merge( fb );
tx.commit();
// reattach by saveOrUpdate
tx = s.beginTransaction();
fb.setClub("Bimbo FC SA");
s.saveOrUpdate(fb);
fb.setClub( "Bimbo FC SA" );
s.saveOrUpdate( fb );
tx.commit();
// clean up
s.clear();
tx = s.beginTransaction();
fpk = new FootballerPk("David", "Beckam");
fb = (Footballer) s.get(Footballer.class, fpk);
assertEquals("Bimbo FC SA", fb.getClub());
s.delete(fb);
s.delete(keeper);
fpk = new FootballerPk( "David", "Beckam" );
fb = ( Footballer ) s.get( Footballer.class, fpk );
assertEquals( "Bimbo FC SA", fb.getClub() );
s.delete( fb );
s.delete( keeper );
tx.commit();
s.close();
}
public void testColumnDefinition() {
Column idCol = (Column) getCfg().getClassMapping(Ball.class.getName())
Column idCol = ( Column ) getCfg().getClassMapping( Ball.class.getName() )
.getIdentifierProperty().getValue().getColumnIterator().next();
assertEquals("ball_id", idCol.getName());
assertEquals( "ball_id", idCol.getName() );
}
public void testLowAllocationSize() throws Exception {
@ -264,42 +272,39 @@ public class IdTest extends TestCase {
tx = s.beginTransaction();
int size = 4;
BreakDance[] bds = new BreakDance[size];
for (int i = 0; i < size; i++) {
for ( int i = 0; i < size; i++ ) {
bds[i] = new BreakDance();
s.persist(bds[i]);
s.persist( bds[i] );
}
s.flush();
for (int i = 0; i < size; i++) {
assertEquals(i + 1, bds[i].id.intValue());
for ( int i = 0; i < size; i++ ) {
assertEquals( i + 1, bds[i].id.intValue() );
}
tx.rollback();
s.close();
}
@Override
protected boolean runForCurrentDialect() {
return super.runForCurrentDialect() && getDialect().supportsSequences();
}
/**
* @see org.hibernate.test.annotations.TestCase#getAnnotatedClasses()
*/
protected Class[] getAnnotatedClasses() {
return new Class[] { Ball.class, Shoe.class, Store.class,
return new Class[] {
Ball.class, Shoe.class, Store.class,
Department.class, Dog.class, Computer.class, Home.class,
Phone.class, Tree.class, FirTree.class, Footballer.class,
SoundSystem.class, Furniture.class, GoalKeeper.class,
BreakDance.class, Monkey.class};
BreakDance.class, Monkey.class
};
}
/**
* @see org.hibernate.test.annotations.TestCase#getAnnotatedPackages()
*/
protected String[] getAnnotatedPackages() {
return new String[] { "org.hibernate.test.annotations",
"org.hibernate.test.annotations.id" };
return new String[] {
"org.hibernate.test.annotations",
"org.hibernate.test.annotations.id"
};
}
@Override

View File

@ -3,12 +3,13 @@ package org.hibernate.test.annotations.lob;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.dialect.Dialect;
import org.hibernate.junit.RequiresDialectFeature;
import org.hibernate.test.annotations.TestCase;
/**
* @author Emmanuel Bernard
*/
@RequiresDialectFeature("supportsExpectedLobUsagePattern")
public class LobTest extends TestCase {
public void testSerializableToBlob() throws Exception {
Book book = new Book();
@ -118,11 +119,6 @@ public class LobTest extends TestCase {
super( x );
}
@Override
protected boolean runForCurrentDialect() {
return super.runForCurrentDialect() && getDialect().supportsExpectedLobUsagePattern();
}
protected Class[] getAnnotatedClasses() {
return new Class[] {
Book.class,

View File

@ -1,29 +1,31 @@
//$
// $Id$
package org.hibernate.test.annotations.manytoone.referencedcolumnname;
import java.math.BigDecimal;
import org.hibernate.test.annotations.TestCase;
import org.hibernate.Session;
import org.hibernate.junit.RequiresDialectFeature;
import org.hibernate.test.annotations.TestCase;
/**
* @author Emmanuel Bernard
*/
public class ManyToOneReferencedColumnNameTest extends TestCase {
@RequiresDialectFeature("supportsIdentityColumns")
public void testReoverableExceptionInFkOrdering() throws Exception {
//SF should not blow up
Vendor v = new Vendor();
Item i = new Item();
ZItemCost ic = new ZItemCost();
ic.setCost( new BigDecimal(2) );
ic.setCost( new BigDecimal( 2 ) );
ic.setItem( i );
ic.setVendor( v );
WarehouseItem wi = new WarehouseItem();
wi.setDefaultCost( ic );
wi.setItem( i );
wi.setVendor( v );
wi.setQtyInStock( new BigDecimal(2) );
Session s = openSession( );
wi.setQtyInStock( new BigDecimal( 2 ) );
Session s = openSession();
s.getTransaction().begin();
s.save( i );
s.save( v );
@ -32,15 +34,8 @@ public class ManyToOneReferencedColumnNameTest extends TestCase {
s.flush();
s.getTransaction().rollback();
s.close();
}
@Override
protected boolean runForCurrentDialect() {
return super.runForCurrentDialect() && getDialect().supportsIdentityColumns();
}
}
protected Class[] getAnnotatedClasses() {
return new Class[] {

View File

@ -2,6 +2,7 @@
package org.hibernate.test.annotations.xml.hbm;
import org.hibernate.Session;
import org.hibernate.junit.RequiresDialectFeature;
import org.hibernate.test.annotations.TestCase;
/**
@ -9,6 +10,7 @@ import org.hibernate.test.annotations.TestCase;
*/
public class HbmWithIdentityTest extends TestCase {
@RequiresDialectFeature("supportsIdentityColumns")
public void testManyToOneAndInterface() throws Exception {
Session s = openSession();
s.getTransaction().begin();
@ -23,11 +25,6 @@ public class HbmWithIdentityTest extends TestCase {
s.close();
}
@Override
protected boolean runForCurrentDialect() {
return super.runForCurrentDialect() && getDialect().supportsIdentityColumns();
}
protected Class[] getAnnotatedClasses() {
return new Class[] {
Sky.class,

View File

@ -0,0 +1,44 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.junit;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation used to indicate that a test should be run only when the current dialect suppports the
* specified feature.
*
* @author Hardy Ferentschik
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresDialectFeature {
/**
* @return The name of the dialect feature.
*/
String value();
}

View File

@ -1,4 +1,4 @@
// $Id:$
// $Id$
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
@ -30,8 +30,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import junit.framework.TestCase;
import org.slf4j.Logger;
@ -42,6 +40,7 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.jdbc.Work;
import org.hibernate.junit.FailureExpected;
import org.hibernate.junit.RequiresDialect;
import org.hibernate.junit.RequiresDialectFeature;
import org.hibernate.junit.SkipForDialect;
import org.hibernate.junit.SkipLog;
import org.hibernate.tool.hbm2ddl.SchemaExport;
@ -60,23 +59,6 @@ public abstract class HibernateTestCase extends TestCase {
protected static Configuration cfg;
private static Class<?> lastTestClass;
/**
* Flag indicating whether the test should be run or skipped.
*/
private boolean runTest = true;
/**
* List of required dialect for the current {@code runMethod}. If the list is empty any dialect is allowed.
* Otherwise the current dialect or a superclass of the current dialect must be in the list.
*/
private final Set<Class<? extends Dialect>> requiredDialectList = new HashSet<Class<? extends Dialect>>();
/**
* List of dialects for which the current {@code runMethod} should be skipped.
*/
private final Set<Class<? extends Dialect>> skipForDialectList = new HashSet<Class<? extends Dialect>>();
public HibernateTestCase() {
super();
}
@ -172,7 +154,7 @@ public abstract class HibernateTestCase extends TestCase {
}
}
protected final Skip determineSkipByDialect(Dialect dialect, Method runMethod) {
protected final Skip determineSkipByDialect(Dialect dialect, Method runMethod) throws Exception {
// skips have precedence, so check them first
SkipForDialect skipForDialectAnn = locateAnnotation( SkipForDialect.class, runMethod );
if ( skipForDialectAnn != null ) {
@ -195,18 +177,34 @@ public abstract class HibernateTestCase extends TestCase {
if ( requiresDialectAnn != null ) {
for ( Class<? extends Dialect> dialectClass : requiresDialectAnn.value() ) {
if ( requiresDialectAnn.strictMatching() ) {
if ( dialectClass.equals( dialect.getClass() ) ) {
if ( !dialectClass.equals( dialect.getClass() ) ) {
return buildSkip( dialect, null, null );
}
}
else {
if ( dialectClass.isInstance( dialect ) ) {
if ( !dialectClass.isInstance( dialect ) ) {
return buildSkip( dialect, null, null );
}
}
}
}
// then check against a dialect feature
RequiresDialectFeature requiresDialectFeatureAnn = locateAnnotation( RequiresDialectFeature.class, runMethod );
if ( requiresDialectFeatureAnn != null ) {
String feature = requiresDialectFeatureAnn.value();
boolean skip = false;
try {
Method m = dialect.getClass().getMethod( feature );
skip = (Boolean) m.invoke( dialect );
}
catch ( NoSuchMethodException e ) {
fail( "Dialect does not have a method: " + feature );
}
if ( skip ) {
return buildSkip( dialect, null, null );
}
}
return null;
}
@ -244,30 +242,6 @@ public abstract class HibernateTestCase extends TestCase {
return this.getClass().getName() + "#" + this.getName();
}
protected boolean runForCurrentDialect() {
boolean runTestForCurrentDialect = true;
// check whether the current dialect is assignableFrom from any of the specified required dialects.
for ( Class<? extends Dialect> dialect : requiredDialectList ) {
if ( dialect.isAssignableFrom( Dialect.getDialect().getClass() ) ) {
runTestForCurrentDialect = true;
break;
}
runTestForCurrentDialect = false;
}
// check whether the current dialect is assignableFrom from any of the specified skip for dialects.
for ( Class<? extends Dialect> dialect : skipForDialectList ) {
if ( dialect.isAssignableFrom( Dialect.getDialect().getClass() ) ) {
runTestForCurrentDialect = false;
break;
}
runTestForCurrentDialect = true;
}
return runTestForCurrentDialect;
}
private Method findTestMethod() {
String fName = getName();
assertNotNull( fName );
@ -289,7 +263,7 @@ public abstract class HibernateTestCase extends TestCase {
protected abstract Class<?>[] getAnnotatedClasses();
protected String[] getMappings() {
return new String[]{};
return new String[] { };
}
protected abstract void handleUnclosedResources();