-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
- 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
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
jbt/src/main/java/org/hibernate/tool/orm/jbt/wrp/CollectionPersisterWrapperFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
jbt/src/test/java/org/hibernate/tool/orm/jbt/wrp/CollectionPersisterWrapperFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |