Search in sources :

Example 1 with PojoMetaDataConfiguration

use of org.hibernate.cfg.PojoMetaDataConfiguration in project jbosstools-hibernate by jbosstools.

the class HibernateMappingExporterFacadeTest method testStart.

@Test
public void testStart() throws Exception {
    PojoMetaDataConfiguration configuration = new PojoMetaDataConfiguration();
    RootClass persistentClass = new RootClass(null);
    Table table = new Table("FOO");
    Column keyColumn = new Column("BAR");
    SimpleValue key = new SimpleValue(configuration.getMetadataImplementor());
    key.setTypeName("String");
    key.addColumn(keyColumn);
    key.setTable(table);
    persistentClass.setClassName("Foo");
    persistentClass.setEntityName("Foo");
    persistentClass.setJpaEntityName("Foo");
    persistentClass.setTable(table);
    persistentClass.setIdentifier(key);
    configuration.addClass(persistentClass);
    hibernateMappingExporter.setConfiguration(configuration);
    hibernateMappingExporter.setOutputDirectory(outputDir);
    final File fooHbmXml = new File(outputDir, "Foo.hbm.xml");
    // First without a 'delegate' exporter
    Assert.assertFalse(fooHbmXml.exists());
    hibernateMappingExporterFacade.start();
    Assert.assertTrue(fooHbmXml.exists());
    Assert.assertTrue(fooHbmXml.delete());
    // Now set a 'delegate' and invoke 'start' again
    final File dummyDir = new File(outputDir, "dummy");
    dummyDir.mkdir();
    Assert.assertTrue(dummyDir.exists());
    IExportPOJODelegate delegate = new IExportPOJODelegate() {

        @Override
        public void exportPOJO(Map<Object, Object> map, IPOJOClass pojoClass) {
            Assert.assertTrue(dummyDir.delete());
            Map<String, Object> m = new HashMap<>();
            for (Object key : map.keySet()) {
                m.put((String) key, map.get(key));
            }
            hibernateMappingExporter.superExportPOJO(m, (POJOClass) ((IFacade) pojoClass).getTarget());
        }
    };
    Field delegateField = HibernateMappingExporterExtension.class.getDeclaredField("delegateExporter");
    delegateField.setAccessible(true);
    delegateField.set(hibernateMappingExporter, delegate);
    hibernateMappingExporterFacade.start();
    Assert.assertFalse(dummyDir.exists());
    Assert.assertTrue(fooHbmXml.exists());
    Assert.assertTrue(fooHbmXml.delete());
    Assert.assertTrue(outputDir.exists());
}
Also used : RootClass(org.hibernate.mapping.RootClass) Table(org.hibernate.mapping.Table) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) HashMap(java.util.HashMap) PojoMetaDataConfiguration(org.hibernate.cfg.PojoMetaDataConfiguration) SimpleValue(org.hibernate.mapping.SimpleValue) Field(java.lang.reflect.Field) Column(org.hibernate.mapping.Column) IExportPOJODelegate(org.jboss.tools.hibernate.runtime.spi.IExportPOJODelegate) IFacade(org.jboss.tools.hibernate.runtime.common.IFacade) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with PojoMetaDataConfiguration

use of org.hibernate.cfg.PojoMetaDataConfiguration in project jbosstools-hibernate by jbosstools.

the class HibernateMappingExporterFacadeTest method testStart.

@Test
public void testStart() throws Exception {
    PojoMetaDataConfiguration configuration = new PojoMetaDataConfiguration();
    configuration.setProperty(Environment.DIALECT, "org.hibernate.dialect.H2Dialect");
    RootClass persistentClass = new RootClass(null);
    Table table = new Table("FOO");
    Column keyColumn = new Column("BAR");
    SimpleValue key = new SimpleValue(configuration.getMetadataImplementor());
    key.setTypeName("String");
    key.addColumn(keyColumn);
    key.setTable(table);
    persistentClass.setClassName("Foo");
    persistentClass.setEntityName("Foo");
    persistentClass.setJpaEntityName("Foo");
    persistentClass.setTable(table);
    persistentClass.setIdentifier(key);
    configuration.addClass(persistentClass);
    hibernateMappingExporter.setConfiguration(configuration);
    hibernateMappingExporter.setOutputDirectory(outputDir);
    final File fooHbmXml = new File(outputDir, "Foo.hbm.xml");
    // First without a 'delegate' exporter
    Assert.assertFalse(fooHbmXml.exists());
    hibernateMappingExporterFacade.start();
    Assert.assertTrue(fooHbmXml.exists());
    Assert.assertTrue(fooHbmXml.delete());
    // Now set a 'delegate' and invoke 'start' again
    final File dummyDir = new File(outputDir, "dummy");
    dummyDir.mkdir();
    Assert.assertTrue(dummyDir.exists());
    IExportPOJODelegate delegate = new IExportPOJODelegate() {

        @Override
        public void exportPOJO(Map<Object, Object> map, IPOJOClass pojoClass) {
            Assert.assertTrue(dummyDir.delete());
            Map<String, Object> m = new HashMap<>();
            for (Object key : map.keySet()) {
                m.put((String) key, map.get(key));
            }
            hibernateMappingExporter.superExportPOJO(m, (POJOClass) ((IFacade) pojoClass).getTarget());
        }
    };
    Field delegateField = HibernateMappingExporterExtension.class.getDeclaredField("delegateExporter");
    delegateField.setAccessible(true);
    delegateField.set(hibernateMappingExporter, delegate);
    hibernateMappingExporterFacade.start();
    Assert.assertFalse(dummyDir.exists());
    Assert.assertTrue(fooHbmXml.exists());
    Assert.assertTrue(fooHbmXml.delete());
    Assert.assertTrue(outputDir.exists());
}
Also used : RootClass(org.hibernate.mapping.RootClass) Table(org.hibernate.mapping.Table) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) HashMap(java.util.HashMap) PojoMetaDataConfiguration(org.hibernate.cfg.PojoMetaDataConfiguration) SimpleValue(org.hibernate.mapping.SimpleValue) Field(java.lang.reflect.Field) Column(org.hibernate.mapping.Column) IExportPOJODelegate(org.jboss.tools.hibernate.runtime.spi.IExportPOJODelegate) IFacade(org.jboss.tools.hibernate.runtime.common.IFacade) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with PojoMetaDataConfiguration

use of org.hibernate.cfg.PojoMetaDataConfiguration in project jbosstools-hibernate by jbosstools.

the class HibernateMappingExporterFacadeTest method testStart.

@Test
public void testStart() throws Exception {
    PojoMetaDataConfiguration configuration = new PojoMetaDataConfiguration();
    RootClass persistentClass = new RootClass(null);
    Table table = new Table("FOO");
    Column keyColumn = new Column("BAR");
    SimpleValue key = new SimpleValue(configuration.getMetadataImplementor());
    key.setTypeName("String");
    key.addColumn(keyColumn);
    key.setTable(table);
    persistentClass.setClassName("Foo");
    persistentClass.setEntityName("Foo");
    persistentClass.setJpaEntityName("Foo");
    persistentClass.setTable(table);
    persistentClass.setIdentifier(key);
    configuration.addClass(persistentClass);
    hibernateMappingExporter.setConfiguration(configuration);
    hibernateMappingExporter.setOutputDirectory(outputDir);
    final File fooHbmXml = new File(outputDir, "Foo.hbm.xml");
    // First without a 'delegate' exporter
    Assert.assertFalse(fooHbmXml.exists());
    hibernateMappingExporterFacade.start();
    Assert.assertTrue(fooHbmXml.exists());
    Assert.assertTrue(fooHbmXml.delete());
    // Now set a 'delegate' and invoke 'start' again
    final File dummyDir = new File(outputDir, "dummy");
    dummyDir.mkdir();
    Assert.assertTrue(dummyDir.exists());
    IExportPOJODelegate delegate = new IExportPOJODelegate() {

        @Override
        public void exportPOJO(Map<Object, Object> map, IPOJOClass pojoClass) {
            Assert.assertTrue(dummyDir.delete());
            Map<String, Object> m = new HashMap<>();
            for (Object key : map.keySet()) {
                m.put((String) key, map.get(key));
            }
            hibernateMappingExporter.superExportPOJO(m, (POJOClass) ((IFacade) pojoClass).getTarget());
        }
    };
    Field delegateField = HibernateMappingExporterExtension.class.getDeclaredField("delegateExporter");
    delegateField.setAccessible(true);
    delegateField.set(hibernateMappingExporter, delegate);
    hibernateMappingExporterFacade.start();
    Assert.assertFalse(dummyDir.exists());
    Assert.assertTrue(fooHbmXml.exists());
    Assert.assertTrue(fooHbmXml.delete());
    Assert.assertTrue(outputDir.exists());
}
Also used : RootClass(org.hibernate.mapping.RootClass) Table(org.hibernate.mapping.Table) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) HashMap(java.util.HashMap) PojoMetaDataConfiguration(org.hibernate.cfg.PojoMetaDataConfiguration) SimpleValue(org.hibernate.mapping.SimpleValue) Field(java.lang.reflect.Field) Column(org.hibernate.mapping.Column) IExportPOJODelegate(org.jboss.tools.hibernate.runtime.spi.IExportPOJODelegate) IFacade(org.jboss.tools.hibernate.runtime.common.IFacade) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

File (java.io.File)3 Field (java.lang.reflect.Field)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 PojoMetaDataConfiguration (org.hibernate.cfg.PojoMetaDataConfiguration)3 Column (org.hibernate.mapping.Column)3 RootClass (org.hibernate.mapping.RootClass)3 SimpleValue (org.hibernate.mapping.SimpleValue)3 Table (org.hibernate.mapping.Table)3 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)3 IExportPOJODelegate (org.jboss.tools.hibernate.runtime.spi.IExportPOJODelegate)3 IPOJOClass (org.jboss.tools.hibernate.runtime.spi.IPOJOClass)3 Test (org.junit.Test)3