Skip to content

Commit

Permalink
HHH-16049 Test setting a property to its current value with bytecode …
Browse files Browse the repository at this point in the history
…enhancement enabled
  • Loading branch information
yrodiere authored and dreab8 committed Jan 24, 2023
1 parent 84662bf commit ac55bb2
Show file tree
Hide file tree
Showing 4 changed files with 511 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
package org.hibernate.test.bytecode.enhancement.lazy.basic;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext;

import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
Expand All @@ -16,7 +19,9 @@
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand All @@ -29,7 +34,7 @@

@RunWith(BytecodeEnhancerRunner.class)
@CustomEnhancementContext( {EnhancerTestContext.class, NoDirtyCheckingContext.class} )
@TestForIssue(jiraKey = "HHH-15634")
@TestForIssue(jiraKey = { "HHH-15634", "HHH-16049" })
public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {

private Long entityId;
Expand All @@ -39,6 +44,16 @@ public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { LazyEntity.class };
}

@Override
protected void prepareBasicRegistryBuilder(StandardServiceRegistryBuilder serviceRegistryBuilder) {
super.prepareBasicRegistryBuilder( serviceRegistryBuilder );
serviceRegistryBuilder.applySetting( AvailableSettings.STATEMENT_INSPECTOR, SQLStatementInspector.class.getName() );
}

SQLStatementInspector statementInspector() {
return (SQLStatementInspector) sessionFactory().getSessionFactoryOptions().getStatementInspector();
}

private void initNull() {
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = new LazyEntity();
Expand All @@ -58,6 +73,23 @@ private void initNonNull() {
} );
}

@Before
public void clearStatementInspector() {
statementInspector().clear();
}

@Test
public void updateOneLazyProperty_nullToNull() {
initNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( null );
} );

// We should not update entities when property values did not change
statementInspector().assertNoUpdate();
}

@Test
public void updateOneLazyProperty_nullToNonNull() {
initNull();
Expand All @@ -75,7 +107,7 @@ public void updateOneLazyProperty_nullToNonNull() {
}

@Test
public void updateOneLazyProperty_nonNullToNonNull() {
public void updateOneLazyProperty_nonNullToNonNull_differentValues() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
Expand All @@ -90,6 +122,18 @@ public void updateOneLazyProperty_nonNullToNonNull() {
} );
}

@Test
public void updateOneLazyProperty_nonNullToNonNull_sameValues() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "lazy1_update" );
} );

// We should not update entities when property values did not change
statementInspector().assertNoUpdate();
}

@Test
public void updateOneLazyProperty_nonNullToNull() {
initNonNull();
Expand All @@ -106,6 +150,91 @@ public void updateOneLazyProperty_nonNullToNull() {
} );
}

@Test
public void updateOneEagerProperty_nullToNull() {
initNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( null );
} );

// We should not update entities when property values did not change
statementInspector().assertNoUpdate();
}

@Test
public void updateOneEagerProperty_nullToNonNull() {
initNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( "eager_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "eager_update", entity.getEagerProperty() );

assertNull( entity.getLazyProperty1() );
assertNull( entity.getLazyProperty2() );
} );
}

@Test
public void updateOneEagerProperty_nonNullToNonNull_differentValues() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( "eager_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "eager_update", entity.getEagerProperty() );

assertEquals( "lazy1_initial", entity.getLazyProperty1() );
assertEquals( "lazy2_initial", entity.getLazyProperty2() );
} );
}

@Test
public void updateOneEagerProperty_nonNullToNonNull_sameValues() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( "eager_update" );
} );

// We should not update entities when property values did not change
statementInspector().assertNoUpdate();
}

@Test
public void updateOneEagerProperty_nonNullToNull() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( null );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertNull( entity.getEagerProperty() );

assertEquals( "lazy1_initial", entity.getLazyProperty1() );
assertEquals( "lazy2_initial", entity.getLazyProperty2() );
} );
}

@Test
public void updateOneEagerPropertyAndOneLazyProperty_nullToNull() {
initNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( null );
entity.setLazyProperty1( null );
} );

// We should not update entities when property values did not change
statementInspector().assertNoUpdate();
}

@Test
public void updateOneEagerPropertyAndOneLazyProperty_nullToNonNull() {
initNull();
Expand All @@ -124,7 +253,7 @@ public void updateOneEagerPropertyAndOneLazyProperty_nullToNonNull() {
}

@Test
public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull() {
public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull_differentValues() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
Expand All @@ -140,6 +269,19 @@ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull() {
} );
}

@Test
public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull_sameValues() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( entity.getEagerProperty() );
entity.setLazyProperty1( entity.getLazyProperty1() );
} );

// We should not update entities when property values did not change
statementInspector().assertNoUpdate();
}

@Test
public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNull() {
initNonNull();
Expand All @@ -157,6 +299,19 @@ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNull() {
} );
}

@Test
public void updateAllLazyProperties_nullToNull() {
initNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( null );
entity.setLazyProperty2( null );
} );

// We should not update entities when property values did not change
statementInspector().assertNoUpdate();
}

@Test
public void updateAllLazyProperties_nullToNonNull() {
initNull();
Expand All @@ -175,7 +330,7 @@ public void updateAllLazyProperties_nullToNonNull() {
}

@Test
public void updateAllLazyProperties_nonNullToNonNull() {
public void updateAllLazyProperties_nonNullToNonNull_differentValues() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
Expand All @@ -191,6 +346,19 @@ public void updateAllLazyProperties_nonNullToNonNull() {
} );
}

@Test
public void updateAllLazyProperties_nonNullToNonNull_sameValues() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( entity.getLazyProperty1() );
entity.setLazyProperty2( entity.getLazyProperty2() );
} );

// We should not update entities when property values did not change
statementInspector().assertNoUpdate();
}

@Test
public void updateAllLazyProperties_nonNullToNull() {
initNonNull();
Expand Down

0 comments on commit ac55bb2

Please sign in to comment.