org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-testing Show documentation
Show all versions of hibernate-testing Show documentation
Support for testing Hibernate ORM functionality
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.testing.bytecode.enhancement;
import org.hibernate.LockMode;
import org.hibernate.engine.internal.MutableEntityEntryFactory;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.SelfDirtinessTracker;
import org.hibernate.engine.spi.Status;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import java.lang.reflect.Field;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* utility class to use in bytecode enhancement tests
*
* @author Steve Ebersole
* @author Luis Barreiro
*/
public abstract class EnhancerTestUtils extends BaseUnitTestCase {
public static Object getFieldByReflection(Object entity, String fieldName) {
try {
Field field = entity.getClass().getDeclaredField( fieldName );
ReflectHelper.ensureAccessibility( field );
return field.get( entity );
}
catch (NoSuchFieldException | IllegalAccessException e) {
fail( "Fail to get field '" + fieldName + "' in entity " + entity + ": " + e.getMessage() );
}
return null;
}
/**
* clears the dirty set for an entity
*/
public static void clearDirtyTracking(Object entityInstance) {
( (SelfDirtinessTracker) entityInstance ).$$_hibernate_clearDirtyAttributes();
}
/**
* compares the dirty fields of an entity with a set of expected values
*/
public static void checkDirtyTracking(Object entityInstance, String... dirtyFields) {
SelfDirtinessTracker selfDirtinessTracker = (SelfDirtinessTracker) entityInstance;
assertEquals( dirtyFields.length > 0, selfDirtinessTracker.$$_hibernate_hasDirtyAttributes() );
String[] tracked = selfDirtinessTracker.$$_hibernate_getDirtyAttributes();
assertEquals( dirtyFields.length, tracked.length );
assertTrue( Arrays.asList( tracked ).containsAll( Arrays.asList( dirtyFields ) ) );
}
public static EntityEntry makeEntityEntry() {
return MutableEntityEntryFactory.INSTANCE.createEntityEntry(
Status.MANAGED,
null,
null,
1,
null,
LockMode.NONE,
false,
null,
false,
null
);
}
}