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 interface method 'org.hibernate.tool.orm.jbt.wrp.ConfigurationWrapperFactory.ConfigurationWrapper#configure(Document)'
  - Override method 'org.hibernate.tool.orm.jbt.util.RevengConfiguration#configure(Document)'
  - Override method 'org.hibernate.tool.orm.jbt.util.JpaConfiguration#configure(Document)'
  - Add new test case 'org.hibernate.tool.orm.jbt.wrp.ConfigurationWrapperFactoryTest#testConfigureDocument()'

Signed-off-by: Koen Aers <[email protected]>
  • Loading branch information
koentsje committed Jun 22, 2023
1 parent 8c10a7a commit 89b7e99
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.mapping.Table;
import org.hibernate.tool.api.reveng.RevengStrategy;
import org.hibernate.tool.orm.jbt.wrp.SessionFactoryWrapper;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;

import jakarta.persistence.EntityManagerFactory;
Expand Down Expand Up @@ -89,6 +90,12 @@ public Configuration configure() {
return this.configure(new Object());
}

public Configuration configure(Document document) {
throw new RuntimeException(
"Method 'configure' should not be called on instances of " +
this.getClass().getName());
}

public Configuration configure(Object... object) {
throw new RuntimeException(
"Method 'configure' should not be called on instances of " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.hibernate.tool.api.metadata.MetadataConstants;
import org.hibernate.tool.api.metadata.MetadataDescriptorFactory;
import org.hibernate.tool.api.reveng.RevengStrategy;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;

public class RevengConfiguration extends Configuration {
Expand Down Expand Up @@ -100,6 +101,12 @@ public Configuration configure(Object... object) {
this.getClass().getName());
}

public Configuration configure(Document document) {
throw new RuntimeException(
"Method 'configure' should not be called on instances of " +
this.getClass().getName());
}

public Configuration configure() {
throw new RuntimeException(
"Method 'configure' should not be called on instances of " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.hibernate.tool.orm.jbt.util.JpaConfiguration;
import org.hibernate.tool.orm.jbt.util.NativeConfiguration;
import org.hibernate.tool.orm.jbt.util.RevengConfiguration;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;

public class ConfigurationWrapperFactory {
Expand Down Expand Up @@ -50,6 +51,7 @@ static interface ConfigurationWrapper extends Wrapper {
void setNamingStrategy(NamingStrategy namingStrategy);
Properties getProperties();
Configuration addProperties(Properties testProperties);
Configuration configure(Document document);
}

static class ConfigurationWrapperInvocationHandler implements InvocationHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,48 @@
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilderFactory;

import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.jaxb.spi.Binding;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.DefaultNamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.tool.orm.jbt.util.MetadataHelper;
import org.hibernate.tool.orm.jbt.util.MockConnectionProvider;
import org.hibernate.tool.orm.jbt.util.MockDialect;
import org.hibernate.tool.orm.jbt.util.NativeConfiguration;
import org.hibernate.tool.orm.jbt.wrp.ConfigurationWrapperFactory.ConfigurationWrapper;
import org.hibernate.tool.orm.jbt.wrp.ConfigurationWrapperFactory.JpaConfigurationWrapperImpl;
import org.hibernate.tool.orm.jbt.wrp.ConfigurationWrapperFactory.RevengConfigurationWrapperImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.EntityResolver;
import org.xml.sax.helpers.DefaultHandler;

public class ConfigurationWrapperFactoryTest {

private static final String TEST_HBM_XML_STRING =
"<hibernate-mapping package='org.jboss.tools.hibernate.orm.runtime.exp.internal'>" +
"<hibernate-mapping package='org.hibernate.tool.orm.jbt.wrp'>" +
" <class name='ConfigurationWrapperFactoryTest$Foo'>" +
" <id name='id'/>" +
" </class>" +
"</hibernate-mapping>";

static class Foo {
public String id;
}

private ConfigurationWrapper nativeConfigurationWrapper = null;
private Configuration wrappedNativeConfiguration = null;
private ConfigurationWrapper revengConfigurationWrapper = null;
Expand All @@ -49,6 +63,8 @@ public class ConfigurationWrapperFactoryTest {
public void beforeEach() {
nativeConfigurationWrapper = ConfigurationWrapperFactory.createNativeConfigurationWrapper();
wrappedNativeConfiguration = nativeConfigurationWrapper.getWrappedObject();
wrappedNativeConfiguration.setProperty(AvailableSettings.DIALECT, MockDialect.class.getName());
wrappedNativeConfiguration.setProperty(AvailableSettings.CONNECTION_PROVIDER, MockConnectionProvider.class.getName());
revengConfigurationWrapper = ConfigurationWrapperFactory.createRevengConfigurationWrapper();
wrappedRevengConfiguration = revengConfigurationWrapper.getWrappedObject();
jpaConfigurationWrapper = ConfigurationWrapperFactory.createJpaConfigurationWrapper(null, null);
Expand Down Expand Up @@ -248,4 +264,54 @@ public void testAddProperties() {
assertEquals("bar", wrappedJpaConfiguration.getProperty("foo"));
}

@Test
public void testConfigureDocument() throws Exception {
Document document = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.newDocument();
Element hibernateConfiguration = document.createElement("hibernate-configuration");
document.appendChild(hibernateConfiguration);
Element sessionFactory = document.createElement("session-factory");
sessionFactory.setAttribute("name", "bar");
hibernateConfiguration.appendChild(sessionFactory);
Element mapping = document.createElement("mapping");
mapping.setAttribute("resource", "Foo.hbm.xml");
sessionFactory.appendChild(mapping);

URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
File hbmXmlFile = new File(new File(url.toURI()), "Foo.hbm.xml");
hbmXmlFile.deleteOnExit();
FileWriter fileWriter = new FileWriter(hbmXmlFile);
fileWriter.write(TEST_HBM_XML_STRING);
fileWriter.close();

// For native configuration
String fooClassName =
"org.hibernate.tool.orm.jbt.wrp.ConfigurationWrapperFactoryTest$Foo";
Metadata metadata = MetadataHelper.getMetadata(wrappedNativeConfiguration);
assertNull(metadata.getEntityBinding(fooClassName));
nativeConfigurationWrapper.configure(document);
metadata = MetadataHelper.getMetadata(wrappedNativeConfiguration);
assertNotNull(metadata.getEntityBinding(fooClassName));
// For reveng configuration
try {
revengConfigurationWrapper.configure(document);
fail();
} catch (RuntimeException e) {
assertEquals(
e.getMessage(),
"Method 'configure' should not be called on instances of " + RevengConfigurationWrapperImpl.class.getName());
}
// For jpa configuration
try {
jpaConfigurationWrapper.configure(document);
fail();
} catch (RuntimeException e) {
assertEquals(
e.getMessage(),
"Method 'configure' should not be called on instances of " + JpaConfigurationWrapperImpl.class.getName());
}
}

}

0 comments on commit 89b7e99

Please sign in to comment.