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(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 <[email protected]>
  • Loading branch information
koentsje committed Jun 22, 2023
1 parent 89b7e99 commit 6aef95e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public class ConfigurationWrapperFactoryTest {
" </class>" +
"</hibernate-mapping>";

private static final String TEST_CFG_XML_STRING =
"<hibernate-configuration>" +
" <session-factory name='bar'>" +
" <mapping resource='Foo.hbm.xml' />" +
" </session-factory>" +
"</hibernate-configuration>";

static class Foo {
public String id;
}
Expand Down Expand Up @@ -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());
}
}

}

0 comments on commit 6aef95e

Please sign in to comment.