Skip to content

Commit

Permalink
HBX-2742: Create Interface for ConfigurationWrapper
Browse files Browse the repository at this point in the history
  - Add new test case 'org.hibernate.tool.orm.jbt.api.ConfigurationWrapperTest#testGetNamingStrategy()'
  - Add new default interface method 'org.hibernate.tool.orm.jbt.api.ConfigurationWrapper#getNamingStrategy()'

Signed-off-by: Koen Aers <koen.aers@gmail.com>
  • Loading branch information
koentsje committed Mar 7, 2024
1 parent 0764f72 commit d3ffed6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Table;
import org.hibernate.tool.api.reveng.RevengStrategy;
import org.hibernate.tool.orm.jbt.util.JpaConfiguration;
import org.hibernate.tool.orm.jbt.util.NativeConfiguration;
Expand Down Expand Up @@ -99,5 +100,12 @@ default EntityResolver getEntityResolver() {
if (wrappedObject instanceof JpaConfiguration) return ((JpaConfiguration)wrappedObject).getEntityResolver();
return null;
}
default Iterator<Table> getTableMappings() {
Object wrappedObject = getWrappedObject();
if (wrappedObject instanceof NativeConfiguration) return ((NativeConfiguration)wrappedObject).getTableMappings();
if (wrappedObject instanceof RevengConfiguration) return ((RevengConfiguration)wrappedObject).getTableMappings();
if (wrappedObject instanceof JpaConfiguration) return ((JpaConfiguration)wrappedObject).getTableMappings();
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.hibernate.cfg.DefaultNamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Table;
import org.hibernate.tool.api.reveng.RevengStrategy;
import org.hibernate.tool.internal.reveng.strategy.DefaultStrategy;
import org.hibernate.tool.orm.jbt.internal.factory.ConfigurationWrapperFactory;
Expand All @@ -41,7 +42,6 @@
import org.hibernate.tool.orm.jbt.util.MockDialect;
import org.hibernate.tool.orm.jbt.util.NativeConfiguration;
import org.hibernate.tool.orm.jbt.util.RevengConfiguration;
import org.hibernate.tool.orm.jbt.wrp.ConfigurationWrapperFactoryTest.FooBar;
import org.hibernate.tool.orm.jbt.wrp.SessionFactoryWrapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -52,6 +52,9 @@
import org.xml.sax.EntityResolver;
import org.xml.sax.helpers.DefaultHandler;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

public class ConfigurationWrapperTest {

private static final String TEST_HBM_XML_STRING =
Expand Down Expand Up @@ -87,6 +90,10 @@ static class Foo {
public String id;
}

@Entity public class FooBar {
@Id public int id;
}

@TempDir
public File tempRoot;

Expand Down Expand Up @@ -785,6 +792,59 @@ public void testGetEntityResolver() throws Exception {
}
}

@Test
public void testGetTableMappings() throws Exception {
// For native configuration
String fooHbmXmlFilePath = "org/hibernate/tool/orm/jbt/api";
String fooHbmXmlFileName = "ConfigurationWrapperTest$Foo.hbm.xml";
URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
File hbmXmlFileDir = new File(new File(url.toURI()),fooHbmXmlFilePath);
hbmXmlFileDir.deleteOnExit();
hbmXmlFileDir.mkdirs();
File hbmXmlFile = new File(hbmXmlFileDir, fooHbmXmlFileName);
hbmXmlFile.deleteOnExit();
FileWriter fileWriter = new FileWriter(hbmXmlFile);
fileWriter.write(TEST_HBM_XML_STRING);
fileWriter.close();
wrappedNativeConfiguration.addClass(Foo.class);
Iterator<Table> tableMappings = nativeConfigurationWrapper.getTableMappings();
assertTrue(tableMappings.hasNext());
Table fooTableFacade = tableMappings.next();
assertEquals(fooTableFacade.getName(), "ConfigurationWrapperTest$Foo");
tableMappings = null;
assertNull(tableMappings);
fooTableFacade = null;
assertNull(fooTableFacade);
// For reveng configuration
Connection connection = DriverManager.getConnection("jdbc:h2:mem:test");
Statement statement = connection.createStatement();
statement.execute("CREATE TABLE FOO(id int primary key, bar varchar(255))");
wrappedRevengConfiguration.setProperty("hibernate.connection.url", "jdbc:h2:mem:test");
wrappedRevengConfiguration.setProperty("hibernate.default_schema", "PUBLIC");
tableMappings = revengConfigurationWrapper.getTableMappings();
assertNotNull(tableMappings);
assertFalse(tableMappings.hasNext());
((RevengConfiguration)wrappedRevengConfiguration).readFromJDBC();
tableMappings = revengConfigurationWrapper.getTableMappings();
assertNotNull(tableMappings);
assertTrue(tableMappings.hasNext());
fooTableFacade = tableMappings.next();
assertEquals(fooTableFacade.getName(), "FOO");
statement.execute("DROP TABLE FOO");
statement.close();
connection.close();
tableMappings = null;
assertNull(tableMappings);
fooTableFacade = null;
assertNull(fooTableFacade);
// For jpa configuration
tableMappings = jpaConfigurationWrapper.getTableMappings();
assertNotNull(tableMappings);
assertTrue(tableMappings.hasNext());
fooTableFacade = tableMappings.next();
assertEquals(fooTableFacade.getName(), "ConfigurationWrapperTest$FooBar");
}

private void createPersistenceXml() throws Exception {
File metaInf = new File(tempRoot, "META-INF");
metaInf.mkdirs();
Expand Down

0 comments on commit d3ffed6

Please sign in to comment.