Skip to content

Commit

Permalink
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
Browse files Browse the repository at this point in the history
  - Modify test case 'org.hibernate.tool.orm.jbt.wrp.PersistentClassWrapperFactoryTest#testGetDiscriminator()' to make sure the returned value is wrapped
  - Adapt the implementation of 'org.hibernate.tool.orm.jbt.wrp.PersistentClassWrapperFactory' to override the 'getDiscriminator()' methods and wrap the returned value

Signed-off-by: Koen Aers <[email protected]>
  • Loading branch information
koentsje committed Jun 30, 2023
1 parent 3de97e6 commit ebe7565
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.SingleTableSubclass;
import org.hibernate.mapping.Value;
import org.hibernate.tool.orm.jbt.util.DummyMetadataBuildingContext;
import org.hibernate.tool.orm.jbt.util.SpecialRootClass;

Expand Down Expand Up @@ -69,6 +70,10 @@ static class RootClassWrapperImpl extends RootClass implements PersistentClassWr
public RootClassWrapperImpl() {
super(DummyMetadataBuildingContext.INSTANCE);
}
@Override
public Value getDiscriminator() {
return wrapValueIfNeeded(super.getDiscriminator());
}
}

static class SingleTableSubclassWrapperImpl
Expand All @@ -77,6 +82,10 @@ static class SingleTableSubclassWrapperImpl
public SingleTableSubclassWrapperImpl(PersistentClass superclass) {
super(superclass, DummyMetadataBuildingContext.INSTANCE);
}
@Override
public Value getDiscriminator() {
return wrapValueIfNeeded(super.getDiscriminator());
}
}

static class JoinedSubclassWrapperImpl
Expand All @@ -85,6 +94,10 @@ static class JoinedSubclassWrapperImpl
public JoinedSubclassWrapperImpl(PersistentClass superclass) {
super(superclass, DummyMetadataBuildingContext.INSTANCE);
}
@Override
public Value getDiscriminator() {
return wrapValueIfNeeded(super.getDiscriminator());
}
}

static class SpecialRootClassWrapperImpl
Expand All @@ -93,6 +106,14 @@ static class SpecialRootClassWrapperImpl
public SpecialRootClassWrapperImpl(Property property) {
super(property);
}
@Override
public Value getDiscriminator() {
return wrapValueIfNeeded(super.getDiscriminator());
}
}

private static Value wrapValueIfNeeded(Value v) {
return (v != null) && !(v instanceof Wrapper) ? ValueWrapperFactory.createValueWrapper(v) : v;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Iterator;

import org.hibernate.MappingException;
import org.hibernate.engine.OptimisticLockStyle;
import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.Join;
import org.hibernate.mapping.JoinedSubclass;
Expand All @@ -30,7 +30,7 @@
import org.hibernate.mapping.Value;
import org.hibernate.tool.orm.jbt.util.DummyMetadataBuildingContext;
import org.hibernate.tool.orm.jbt.util.SpecialRootClass;
import org.hibernate.tool.orm.jbt.wrp.PersistentClassWrapperFactory.PersistentClassWrapperInvocationHandler;
import org.hibernate.tool.orm.jbt.wrp.ValueWrapperFactory.ValueWrapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -49,23 +49,16 @@ public class PersistentClassWrapperFactoryTest {

@BeforeEach
public void beforeEach() throws Exception {
InvocationHandler invocationHandler = null;
Field wrapperField = PersistentClassWrapperInvocationHandler.class.getDeclaredField("wrapper");
wrapperField.setAccessible(true);
rootClassWrapper = PersistentClassWrapperFactory.createRootClassWrapper();
invocationHandler = Proxy.getInvocationHandler(rootClassWrapper);
rootClassTarget = (PersistentClass)wrapperField.get(invocationHandler);
rootClassTarget = rootClassWrapper.getWrappedObject();
singleTableSubclassWrapper = PersistentClassWrapperFactory.createSingleTableSubclassWrapper(rootClassWrapper);
invocationHandler = Proxy.getInvocationHandler(singleTableSubclassWrapper);
singleTableSubclassTarget = (PersistentClass)wrapperField.get(invocationHandler);
singleTableSubclassTarget = singleTableSubclassWrapper.getWrappedObject();
joinedSubclassWrapper = PersistentClassWrapperFactory.createJoinedSubclassWrapper(rootClassWrapper);
invocationHandler = Proxy.getInvocationHandler(joinedSubclassWrapper);
joinedSubclassTarget = (PersistentClass)wrapperField.get(invocationHandler);
joinedSubclassTarget = joinedSubclassWrapper.getWrappedObject();
property = new Property();
property.setPersistentClass(rootClassTarget);
specialRootClassWrapper = PersistentClassWrapperFactory.createSpecialRootClassWrapper(property);
invocationHandler = Proxy.getInvocationHandler(specialRootClassWrapper);
specialRootClassTarget = (PersistentClass)wrapperField.get(invocationHandler);
specialRootClassTarget = specialRootClassWrapper.getWrappedObject();
}

@Test
Expand Down Expand Up @@ -378,13 +371,21 @@ public void testGetDiscriminator() {
assertNull(singleTableSubclassWrapper.getDiscriminator());
assertNull(joinedSubclassWrapper.getDiscriminator());
assertNull(specialRootClassWrapper.getDiscriminator());
Value value = createValue();
Value value = new BasicValue(DummyMetadataBuildingContext.INSTANCE);
((RootClass)rootClassTarget).setDiscriminator(value);
assertSame(value, rootClassWrapper.getDiscriminator());
assertSame(value, singleTableSubclassWrapper.getDiscriminator());
assertSame(value, joinedSubclassWrapper.getDiscriminator());
Value valueWrapper = rootClassWrapper.getDiscriminator();
assertTrue(valueWrapper instanceof ValueWrapper);
assertSame(value, ((ValueWrapper)valueWrapper).getWrappedObject());
valueWrapper = singleTableSubclassWrapper.getDiscriminator();
assertTrue(valueWrapper instanceof ValueWrapper);
assertSame(value, ((ValueWrapper)valueWrapper).getWrappedObject());
valueWrapper = joinedSubclassWrapper.getDiscriminator();
assertTrue(valueWrapper instanceof ValueWrapper);
assertSame(value, ((ValueWrapper)valueWrapper).getWrappedObject());
((RootClass)specialRootClassTarget).setDiscriminator(value);
assertSame(value, specialRootClassWrapper.getDiscriminator());
valueWrapper = specialRootClassWrapper.getDiscriminator();
assertTrue(valueWrapper instanceof ValueWrapper);
assertSame(value, ((ValueWrapper)valueWrapper).getWrappedObject());
}

@Test
Expand Down Expand Up @@ -1213,7 +1214,7 @@ public void testGetRootTable() throws Exception {
private KeyValue createValue() {
return (KeyValue)Proxy.newProxyInstance(
getClass().getClassLoader(),
new Class[] { KeyValue.class },
new Class[] { KeyValue.class, ValueWrapper.class },
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Expand Down

0 comments on commit ebe7565

Please sign in to comment.