fix some warnings in a couple of tests

This commit is contained in:
Gavin King 2022-01-29 09:00:14 +01:00
parent c807aecdb9
commit c086fff1f8
2 changed files with 42 additions and 47 deletions

View File

@ -36,7 +36,7 @@ import static org.junit.Assert.assertTrue;
*/ */
public class ManyToOneTest extends BaseCoreFunctionalTestCase { public class ManyToOneTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void testEager() throws Exception { public void testEager() {
Session s; Session s;
Transaction tx; Transaction tx;
s = openSession(); s = openSession();
@ -52,7 +52,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
car = (Car) s.get( Car.class, car.getId() ); car = s.get( Car.class, car.getId() );
tx.commit(); tx.commit();
s.close(); s.close();
assertNotNull( car ); assertNotNull( car );
@ -61,7 +61,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testDefaultMetadata() throws Exception { public void testDefaultMetadata() {
Session s; Session s;
Transaction tx; Transaction tx;
s = openSession(); s = openSession();
@ -77,7 +77,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
car = (Car) s.get( Car.class, car.getId() ); car = s.get( Car.class, car.getId() );
assertNotNull( car ); assertNotNull( car );
assertNotNull( car.getBodyColor() ); assertNotNull( car.getBodyColor() );
assertEquals( c.getId(), car.getBodyColor().getId() ); assertEquals( c.getId(), car.getBodyColor().getId() );
@ -92,9 +92,9 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
Flight firstOne = new Flight(); Flight firstOne = new Flight();
firstOne.setId( new Long( 1 ) ); firstOne.setId(1L);
firstOne.setName( "AF0101" ); firstOne.setName( "AF0101" );
firstOne.setDuration( new Long( 1000 ) ); firstOne.setDuration(1000L);
Company frenchOne = new Company(); Company frenchOne = new Company();
frenchOne.setName( "Air France" ); frenchOne.setName( "Air France" );
firstOne.setCompany( frenchOne ); firstOne.setCompany( frenchOne );
@ -105,7 +105,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
firstOne = (Flight) s.get( Flight.class, new Long( 1 ) ); firstOne = s.get( Flight.class, 1L);
assertNotNull( firstOne.getCompany() ); assertNotNull( firstOne.getCompany() );
assertEquals( frenchOne.getName(), firstOne.getCompany().getName() ); assertEquals( frenchOne.getName(), firstOne.getCompany().getName() );
tx.commit(); tx.commit();
@ -113,7 +113,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testCascade() throws Exception { public void testCascade() {
Session s; Session s;
Transaction tx; Transaction tx;
s = openSession(); s = openSession();
@ -121,7 +121,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
Discount discount = new Discount(); Discount discount = new Discount();
discount.setDiscount( 20.12 ); discount.setDiscount( 20.12 );
Customer customer = new Customer(); Customer customer = new Customer();
Collection discounts = new ArrayList(); Collection<Discount> discounts = new ArrayList<>();
discounts.add( discount ); discounts.add( discount );
customer.setName( "Quentin Tarantino" ); customer.setName( "Quentin Tarantino" );
discount.setOwner( customer ); discount.setOwner( customer );
@ -132,14 +132,14 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
discount = (Discount) s.get( Discount.class, discount.getId() ); discount = s.get( Discount.class, discount.getId() );
assertNotNull( discount ); assertNotNull( discount );
assertEquals( 20.12, discount.getDiscount(), 0.01 ); assertEquals( 20.12, discount.getDiscount(), 0.01 );
assertNotNull( discount.getOwner() ); assertNotNull( discount.getOwner() );
customer = new Customer(); customer = new Customer();
customer.setName( "Clooney" ); customer.setName( "Clooney" );
discount.setOwner( customer ); discount.setOwner( customer );
discounts = new ArrayList(); discounts = new ArrayList<>();
discounts.add( discount ); discounts.add( discount );
customer.setDiscountTickets( discounts ); customer.setDiscountTickets( discounts );
tx.commit(); tx.commit();
@ -147,7 +147,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
discount = (Discount) s.get( Discount.class, discount.getId() ); discount = s.get( Discount.class, discount.getId() );
assertNotNull( discount ); assertNotNull( discount );
assertNotNull( discount.getOwner() ); assertNotNull( discount.getOwner() );
assertEquals( "Clooney", discount.getOwner().getName() ); assertEquals( "Clooney", discount.getOwner().getName() );
@ -156,14 +156,14 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
customer = (Customer) s.get( Customer.class, customer.getId() ); customer = s.get( Customer.class, customer.getId() );
s.delete( customer ); s.delete( customer );
tx.commit(); tx.commit();
s.close(); s.close();
} }
@Test @Test
public void testFetch() throws Exception { public void testFetch() {
Session s; Session s;
Transaction tx; Transaction tx;
s = openSession(); s = openSession();
@ -171,7 +171,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
Discount discount = new Discount(); Discount discount = new Discount();
discount.setDiscount( 20 ); discount.setDiscount( 20 );
Customer customer = new Customer(); Customer customer = new Customer();
Collection discounts = new ArrayList(); Collection<Discount> discounts = new ArrayList<>();
discounts.add( discount ); discounts.add( discount );
customer.setName( "Quentin Tarantino" ); customer.setName( "Quentin Tarantino" );
discount.setOwner( customer ); discount.setOwner( customer );
@ -182,14 +182,14 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
discount = (Discount) s.get( Discount.class, discount.getId() ); discount = s.get( Discount.class, discount.getId() );
assertNotNull( discount ); assertNotNull( discount );
assertFalse( Hibernate.isInitialized( discount.getOwner() ) ); assertFalse( Hibernate.isInitialized( discount.getOwner() ) );
tx.commit(); tx.commit();
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
discount = (Discount) s.load( Discount.class, discount.getId() ); discount = s.load( Discount.class, discount.getId() );
assertNotNull( discount ); assertNotNull( discount );
assertFalse( Hibernate.isInitialized( discount.getOwner() ) ); assertFalse( Hibernate.isInitialized( discount.getOwner() ) );
tx.commit(); tx.commit();
@ -202,7 +202,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testCompositeFK() throws Exception { public void testCompositeFK() {
Session s; Session s;
Transaction tx; Transaction tx;
s = openSession(); s = openSession();
@ -234,7 +234,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testImplicitCompositeFk() throws Exception { public void testImplicitCompositeFk() {
Session s; Session s;
Transaction tx; Transaction tx;
s = openSession(); s = openSession();
@ -256,7 +256,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
n2 = (Node) s.get( Node.class, n2pk ); n2 = s.get( Node.class, n2pk );
assertNotNull( n2 ); assertNotNull( n2 );
assertNotNull( n2.getParent() ); assertNotNull( n2.getParent() );
assertEquals( 1, n2.getParent().getId().getLevel() ); assertEquals( 1, n2.getParent().getId().getLevel() );
@ -265,7 +265,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testManyToOneNonPk() throws Exception { public void testManyToOneNonPk() {
Session s = openSession(); Session s = openSession();
Transaction tx = s.beginTransaction(); Transaction tx = s.beginTransaction();
Order order = new Order(); Order order = new Order();
@ -277,7 +277,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s.persist( ol ); s.persist( ol );
s.flush(); s.flush();
s.clear(); s.clear();
ol = (OrderLine) s.get( OrderLine.class, ol.getId() ); ol = s.get( OrderLine.class, ol.getId() );
assertNotNull( ol.getOrder() ); assertNotNull( ol.getOrder() );
assertEquals( "123", ol.getOrder().getOrderNbr() ); assertEquals( "123", ol.getOrder().getOrderNbr() );
assertTrue( ol.getOrder().getOrderLines().contains( ol ) ); assertTrue( ol.getOrder().getOrderLines().contains( ol ) );
@ -286,7 +286,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testManyToOneNonPkSecondaryTable() throws Exception { public void testManyToOneNonPkSecondaryTable() {
Session s = openSession(); Session s = openSession();
Transaction tx = s.beginTransaction(); Transaction tx = s.beginTransaction();
Order order = new Order(); Order order = new Order();
@ -298,7 +298,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s.persist( ol ); s.persist( ol );
s.flush(); s.flush();
s.clear(); s.clear();
ol = (OrderLine) s.get( OrderLine.class, ol.getId() ); ol = s.get( OrderLine.class, ol.getId() );
assertNotNull( ol.getReplacementOrder() ); assertNotNull( ol.getReplacementOrder() );
assertEquals( "123", ol.getReplacementOrder().getOrderNbr() ); assertEquals( "123", ol.getReplacementOrder().getOrderNbr() );
assertFalse( ol.getReplacementOrder().getOrderLines().contains( ol ) ); assertFalse( ol.getReplacementOrder().getOrderLines().contains( ol ) );
@ -307,7 +307,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testTwoManyToOneNonPk() throws Exception { public void testTwoManyToOneNonPk() {
//2 many to one non pk pointing to the same referencedColumnName should not fail //2 many to one non pk pointing to the same referencedColumnName should not fail
Session s = openSession(); Session s = openSession();
Transaction tx = s.beginTransaction(); Transaction tx = s.beginTransaction();
@ -323,7 +323,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s.persist( deal ); s.persist( deal );
s.flush(); s.flush();
s.clear(); s.clear();
deal = (Deal) s.get( Deal.class, deal.id ); deal = s.get( Deal.class, deal.id );
assertNotNull( deal.from ); assertNotNull( deal.from );
assertNotNull( deal.to ); assertNotNull( deal.to );
tx.rollback(); tx.rollback();
@ -331,7 +331,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testFormulaOnOtherSide() throws Exception { public void testFormulaOnOtherSide() {
Session s = openSession(); Session s = openSession();
Transaction tx = s.beginTransaction(); Transaction tx = s.beginTransaction();
Frame frame = new Frame(); Frame frame = new Frame();
@ -347,7 +347,7 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase {
s.persist( r ); s.persist( r );
s.flush(); s.flush();
s.clear(); s.clear();
frame = (Frame) s.get( Frame.class, frame.getId() ); frame = s.get( Frame.class, frame.getId() );
assertEquals( 2, frame.getLenses().size() ); assertEquals( 2, frame.getLenses().size() );
assertTrue( frame.getLenses().iterator().next().getLength() <= 1/1.2f ); assertTrue( frame.getLenses().iterator().next().getLength() <= 1/1.2f );
assertTrue( frame.getLenses().iterator().next().getLength() >= 1/2.5f ); assertTrue( frame.getLenses().iterator().next().getLength() >= 1/2.5f );

View File

@ -9,7 +9,7 @@ package org.hibernate.orm.test.dirtiness;
import java.io.Serializable; import java.io.Serializable;
import org.hibernate.CustomEntityDirtinessStrategy; import org.hibernate.CustomEntityDirtinessStrategy;
import org.hibernate.EmptyInterceptor; import org.hibernate.Interceptor;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionBuilder; import org.hibernate.SessionBuilder;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
@ -55,7 +55,7 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
session = openSession(); session = openSession();
session.beginTransaction(); session.beginTransaction();
Thing thing = (Thing) session.get( Thing.class, id ); Thing thing = session.get( Thing.class, id );
thing.setName( SUBSEQUENT_NAME ); thing.setName( SUBSEQUENT_NAME );
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); session.close();
@ -67,7 +67,7 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
session = openSession(); session = openSession();
session.beginTransaction(); session.beginTransaction();
thing = (Thing) session.get( Thing.class, id ); thing = session.get( Thing.class, id );
assertEquals( SUBSEQUENT_NAME, thing.getName() ); assertEquals( SUBSEQUENT_NAME, thing.getName() );
session.delete( thing ); session.delete( thing );
session.getTransaction().commit(); session.getTransaction().commit();
@ -86,7 +86,7 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
session = sessionWithInterceptor().openSession(); session = sessionWithInterceptor().openSession();
session.beginTransaction(); session.beginTransaction();
Thing thing = (Thing) session.get( Thing.class, id ); Thing thing = session.get( Thing.class, id );
thing.setName( SUBSEQUENT_NAME ); thing.setName( SUBSEQUENT_NAME );
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); session.close();
@ -99,7 +99,7 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
session = openSession(); session = openSession();
session.beginTransaction(); session.beginTransaction();
thing = (Thing) session.get( Thing.class, id ); thing = session.get( Thing.class, id );
assertEquals( SUBSEQUENT_NAME, thing.getName() ); assertEquals( SUBSEQUENT_NAME, thing.getName() );
session.delete( thing ); session.delete( thing );
session.getTransaction().commit(); session.getTransaction().commit();
@ -107,7 +107,7 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testOnlyCustomStrategyConsultedOnNonDirty() throws Exception { public void testOnlyCustomStrategyConsultedOnNonDirty() {
Session session = openSession(); Session session = openSession();
session.beginTransaction(); session.beginTransaction();
Long id = (Long) session.save( new Thing( INITIAL_NAME ) ); Long id = (Long) session.save( new Thing( INITIAL_NAME ) );
@ -116,7 +116,7 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
session = openSession(); session = openSession();
session.beginTransaction(); session.beginTransaction();
Thing thing = (Thing) session.get( Thing.class, id ); Thing thing = session.get( Thing.class, id );
// lets change the name // lets change the name
thing.setName( SUBSEQUENT_NAME ); thing.setName( SUBSEQUENT_NAME );
assertTrue( Strategy.INSTANCE.isDirty( thing, null, null ) ); assertTrue( Strategy.INSTANCE.isDirty( thing, null, null ) );
@ -128,7 +128,7 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
session = openSession(); session = openSession();
session.beginTransaction(); session.beginTransaction();
thing = (Thing) session.get( Thing.class, id ); thing = session.get( Thing.class, id );
assertEquals( INITIAL_NAME, thing.getName() ); assertEquals( INITIAL_NAME, thing.getName() );
session.createQuery( "delete Thing" ).executeUpdate(); session.createQuery( "delete Thing" ).executeUpdate();
session.getTransaction().commit(); session.getTransaction().commit();
@ -150,7 +150,7 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
public boolean canDirtyCheck(Object entity, EntityPersister persister, Session session) { public boolean canDirtyCheck(Object entity, EntityPersister persister, Session session) {
canDirtyCheckCount++; canDirtyCheckCount++;
System.out.println( "canDirtyCheck called" ); System.out.println( "canDirtyCheck called" );
return Thing.class.isInstance( entity ); return entity instanceof Thing;
} }
int isDirtyCount = 0; int isDirtyCount = 0;
@ -159,7 +159,7 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
public boolean isDirty(Object entity, EntityPersister persister, Session session) { public boolean isDirty(Object entity, EntityPersister persister, Session session) {
isDirtyCount++; isDirtyCount++;
System.out.println( "isDirty called" ); System.out.println( "isDirty called" );
return ! Thing.class.cast( entity ).changedValues.isEmpty(); return ! ((Thing) entity).changedValues.isEmpty();
} }
int resetDirtyCount = 0; int resetDirtyCount = 0;
@ -168,7 +168,7 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
public void resetDirty(Object entity, EntityPersister persister, Session session) { public void resetDirty(Object entity, EntityPersister persister, Session session) {
resetDirtyCount++; resetDirtyCount++;
System.out.println( "resetDirty called" ); System.out.println( "resetDirty called" );
Thing.class.cast( entity ).changedValues.clear(); ((Thing) entity).changedValues.clear();
} }
int findDirtyCount = 0; int findDirtyCount = 0;
@ -178,12 +178,7 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
findDirtyCount++; findDirtyCount++;
System.out.println( "findDirty called" ); System.out.println( "findDirty called" );
dirtyCheckContext.doDirtyChecking( dirtyCheckContext.doDirtyChecking(
new AttributeChecker() { attributeInformation -> ((Thing) entity).changedValues.containsKey( attributeInformation.getName() )
@Override
public boolean isDirty(AttributeInformation attributeInformation) {
return Thing.class.cast( entity ).changedValues.containsKey( attributeInformation.getName() );
}
}
); );
} }
@ -196,8 +191,8 @@ public class CustomDirtinessStrategyTest extends BaseCoreFunctionalTestCase {
} }
public static class OnFlushDirtyInterceptor extends EmptyInterceptor { public static class OnFlushDirtyInterceptor implements Interceptor {
private static OnFlushDirtyInterceptor INSTANCE = new OnFlushDirtyInterceptor(); private static final OnFlushDirtyInterceptor INSTANCE = new OnFlushDirtyInterceptor();
@Override @Override
public boolean onFlushDirty( public boolean onFlushDirty(