From 6aef95ecac277bd8d9ca0f32a8339bc9e9d1999a Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Thu, 22 Jun 2023 12:54:28 +0200 Subject: [PATCH] HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools - Add new interface method 'org.hibernate.tool.orm.jbt.wrp.ConfigurationWrapperFactory.ConfigurationWrapper#configure(File)' - Override method 'org.hibernate.tool.orm.jbt.util.RevengConfiguration#configure(File)' - Override method 'org.hibernate.tool.orm.jbt.util.JpaConfiguration#configure(File)' - Add new test case 'org.hibernate.tool.orm.jbt.wrp.ConfigurationWrapperFactoryTest#testConfigureFile()' Signed-off-by: Koen Aers --- .../tool/orm/jbt/util/JpaConfiguration.java | 6 ++- .../orm/jbt/util/RevengConfiguration.java | 2 +- .../jbt/wrp/ConfigurationWrapperFactory.java | 1 + .../orm/jbt/util/JpaConfigurationTest.java | 2 +- .../orm/jbt/util/RevengConfigurationTest.java | 2 +- .../wrp/ConfigurationWrapperFactoryTest.java | 53 +++++++++++++++++++ 6 files changed, 61 insertions(+), 5 deletions(-) diff --git a/jbt/src/main/java/org/hibernate/tool/orm/jbt/util/JpaConfiguration.java b/jbt/src/main/java/org/hibernate/tool/orm/jbt/util/JpaConfiguration.java index 7ff352e4e6..3393cf035b 100644 --- a/jbt/src/main/java/org/hibernate/tool/orm/jbt/util/JpaConfiguration.java +++ b/jbt/src/main/java/org/hibernate/tool/orm/jbt/util/JpaConfiguration.java @@ -87,7 +87,9 @@ public Configuration addClass(Class pc) { @Override public Configuration configure() { - return this.configure(new Object()); + throw new RuntimeException( + "Method 'configure' should not be called on instances of " + + this.getClass().getName()); } public Configuration configure(Document document) { @@ -96,7 +98,7 @@ public Configuration configure(Document document) { this.getClass().getName()); } - public Configuration configure(Object... object) { + public Configuration configure(File file) { throw new RuntimeException( "Method 'configure' should not be called on instances of " + this.getClass().getName()); diff --git a/jbt/src/main/java/org/hibernate/tool/orm/jbt/util/RevengConfiguration.java b/jbt/src/main/java/org/hibernate/tool/orm/jbt/util/RevengConfiguration.java index a5d5bef199..1d11707001 100644 --- a/jbt/src/main/java/org/hibernate/tool/orm/jbt/util/RevengConfiguration.java +++ b/jbt/src/main/java/org/hibernate/tool/orm/jbt/util/RevengConfiguration.java @@ -95,7 +95,7 @@ public void setNamingStrategy(NamingStrategy namingStrategy) { this.getClass().getName()); } - public Configuration configure(Object... object) { + public Configuration configure(File object) { throw new RuntimeException( "Method 'configure' should not be called on instances of " + this.getClass().getName()); diff --git a/jbt/src/main/java/org/hibernate/tool/orm/jbt/wrp/ConfigurationWrapperFactory.java b/jbt/src/main/java/org/hibernate/tool/orm/jbt/wrp/ConfigurationWrapperFactory.java index a4331bdd80..b0d83f03e1 100644 --- a/jbt/src/main/java/org/hibernate/tool/orm/jbt/wrp/ConfigurationWrapperFactory.java +++ b/jbt/src/main/java/org/hibernate/tool/orm/jbt/wrp/ConfigurationWrapperFactory.java @@ -52,6 +52,7 @@ static interface ConfigurationWrapper extends Wrapper { Properties getProperties(); Configuration addProperties(Properties testProperties); Configuration configure(Document document); + Configuration configure(File cfgXmlFile); } static class ConfigurationWrapperInvocationHandler implements InvocationHandler { diff --git a/jbt/src/test/java/org/hibernate/tool/orm/jbt/util/JpaConfigurationTest.java b/jbt/src/test/java/org/hibernate/tool/orm/jbt/util/JpaConfigurationTest.java index e99202b33b..36bf7fb603 100644 --- a/jbt/src/test/java/org/hibernate/tool/orm/jbt/util/JpaConfigurationTest.java +++ b/jbt/src/test/java/org/hibernate/tool/orm/jbt/util/JpaConfigurationTest.java @@ -234,7 +234,7 @@ public void testSetNamingStrategy() { public void testConfigure() { JpaConfiguration jpaConfiguration = new JpaConfiguration("foobar", null); try { - jpaConfiguration.configure(new Object()); + jpaConfiguration.configure(new File("")); fail(); } catch (RuntimeException e) { assertEquals( diff --git a/jbt/src/test/java/org/hibernate/tool/orm/jbt/util/RevengConfigurationTest.java b/jbt/src/test/java/org/hibernate/tool/orm/jbt/util/RevengConfigurationTest.java index 12b5ee7ade..079ea0e49e 100644 --- a/jbt/src/test/java/org/hibernate/tool/orm/jbt/util/RevengConfigurationTest.java +++ b/jbt/src/test/java/org/hibernate/tool/orm/jbt/util/RevengConfigurationTest.java @@ -268,7 +268,7 @@ public void testSetNamingStrategy() { @Test public void testConfigure() { try { - revengConfiguration.configure(new Object()); + revengConfiguration.configure(new File("")); fail(); } catch (RuntimeException e) { assertEquals( diff --git a/jbt/src/test/java/org/hibernate/tool/orm/jbt/wrp/ConfigurationWrapperFactoryTest.java b/jbt/src/test/java/org/hibernate/tool/orm/jbt/wrp/ConfigurationWrapperFactoryTest.java index 72470e110b..20201dc9d6 100644 --- a/jbt/src/test/java/org/hibernate/tool/orm/jbt/wrp/ConfigurationWrapperFactoryTest.java +++ b/jbt/src/test/java/org/hibernate/tool/orm/jbt/wrp/ConfigurationWrapperFactoryTest.java @@ -48,6 +48,13 @@ public class ConfigurationWrapperFactoryTest { " " + ""; + private static final String TEST_CFG_XML_STRING = + "" + + " " + + " " + + " " + + ""; + static class Foo { public String id; } @@ -314,4 +321,50 @@ public void testConfigureDocument() throws Exception { } } + @Test + public void testConfigureFile() throws Exception { + // For native configuration + URL url = getClass().getProtectionDomain().getCodeSource().getLocation(); + File cfgXmlFile = new File(new File(url.toURI()), "foobarfile.cfg.xml"); + FileWriter fileWriter = new FileWriter(cfgXmlFile); + fileWriter.write(TEST_CFG_XML_STRING); + fileWriter.close(); + File hbmXmlFile = new File(new File(url.toURI()), "Foo.hbm.xml"); + fileWriter = new FileWriter(hbmXmlFile); + fileWriter.write(TEST_HBM_XML_STRING); + fileWriter.close(); + + String fooClassName = + "org.hibernate.tool.orm.jbt.wrp.ConfigurationWrapperFactoryTest$Foo"; + Metadata metadata = MetadataHelper.getMetadata(wrappedNativeConfiguration); + assertNull(metadata.getEntityBinding(fooClassName)); + Field metadataField = NativeConfiguration.class.getDeclaredField("metadata"); + metadataField.setAccessible(true); + metadataField.set(wrappedNativeConfiguration, null); + nativeConfigurationWrapper.configure(cfgXmlFile); + metadata = MetadataHelper.getMetadata(wrappedNativeConfiguration); + assertNotNull(metadata.getEntityBinding(fooClassName)); + assertTrue(cfgXmlFile.delete()); + assertTrue(hbmXmlFile.delete()); + + // For reveng configuration + try { + revengConfigurationWrapper.configure(cfgXmlFile); + 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(cfgXmlFile); + fail(); + } catch (RuntimeException e) { + assertEquals( + e.getMessage(), + "Method 'configure' should not be called on instances of " + JpaConfigurationWrapperImpl.class.getName()); + } + } + }