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
  - Add new initial test class 'org.hibernate.tool.orm.jbt.wrp.CollectionPersisterWrapperFactoryTest'
  - Create new implementation class 'org.hibernate.tool.orm.jbt.wrp.CollectionPersisterWrapperFactory'

Signed-off-by: Koen Aers <[email protected]>
  • Loading branch information
koentsje committed Jun 30, 2023
1 parent 8498b33 commit 0489358
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.hibernate.tool.orm.jbt.wrp;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.hibernate.persister.collection.CollectionPersister;

public class CollectionPersisterWrapperFactory {

public static Object create(CollectionPersister delegate) {
return Proxy.newProxyInstance(
CollectionPersisterWrapperFactory.class.getClassLoader(),
new Class[] { CollectionPersisterWrapper.class },
new CollectionPersisterInvocationHandler(delegate));
}

static interface CollectionPersisterWrapper extends CollectionPersister, Wrapper {}

static class CollectionPersisterInvocationHandler implements InvocationHandler {

private CollectionPersister delegate;

private CollectionPersisterInvocationHandler(CollectionPersister delegate) {
this.delegate = delegate;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("getWrappedObject".equals(method.getName())) {
return delegate;
} else if ("getElementType".equals(method.getName())) {
return TypeWrapperFactory.createTypeWrapper(delegate.getElementType());
} else {
return method.invoke(delegate, args);
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.hibernate.tool.orm.jbt.wrp;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.tool.orm.jbt.util.MockConnectionProvider;
import org.hibernate.tool.orm.jbt.util.MockDialect;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class CollectionPersisterWrapperFactoryTest {

private static final String TEST_CFG_XML_STRING =
"<hibernate-configuration>" +
" <session-factory>" +
" <property name='" + AvailableSettings.DIALECT + "'>" + MockDialect.class.getName() + "</property>" +
" <property name='" + AvailableSettings.CONNECTION_PROVIDER + "'>" + MockConnectionProvider.class.getName() + "</property>" +
" </session-factory>" +
"</hibernate-configuration>";

private static final String TEST_HBM_XML_STRING =
"<hibernate-mapping package='org.hibernate.tool.orm.jbt.wrp'>" +
" <class name='CollectionPersisterWrapperFactoryTest$Foo'>" +
" <id name='id' access='field' />" +
" <set name='bars' access='field' >" +
" <key column='barId' />" +
" <element column='barVal' type='string' />" +
" </set>" +
" </class>" +
"</hibernate-mapping>";

public static class Foo {
public String id;
public Set<String> bars = new HashSet<String>();
}

@TempDir
public File tempDir;

private CollectionPersister collectionPersisterWrapper = null;
private CollectionPersister wrappedCollectionPersister = null;

private SessionFactoryWrapper sessionFactory = null;

@BeforeEach
public void beforeEach() throws Exception {
tempDir = Files.createTempDirectory("temp").toFile();
File cfgXmlFile = new File(tempDir, "hibernate.cfg.xml");
FileWriter fileWriter = new FileWriter(cfgXmlFile);
fileWriter.write(TEST_CFG_XML_STRING);
fileWriter.close();
File hbmXmlFile = new File(tempDir, "Foo.hbm.xml");
fileWriter = new FileWriter(hbmXmlFile);
fileWriter.write(TEST_HBM_XML_STRING);
fileWriter.close();
Configuration configuration = (Configuration)WrapperFactory.createNativeConfigurationWrapper();
configuration.addFile(hbmXmlFile);
configuration.configure(cfgXmlFile);
sessionFactory = (SessionFactoryWrapper)configuration.buildSessionFactory();
collectionPersisterWrapper = sessionFactory.getCollectionMetadata(Foo.class.getName() + ".bars");
wrappedCollectionPersister = (CollectionPersister)((Wrapper)collectionPersisterWrapper).getWrappedObject();
}

@Test
public void testConstruction() {
assertNotNull(collectionPersisterWrapper);
assertNotNull(wrappedCollectionPersister);
}

}

0 comments on commit 0489358

Please sign in to comment.