Search in sources :

Example 1 with Cfg2HbmTool

use of org.hibernate.tool.internal.export.hbm.Cfg2HbmTool in project jbosstools-hibernate by jbosstools.

the class HibernateMappingExporterExtensionTest method testSuperExportPOJO.

@Test
public void testSuperExportPOJO() throws Exception {
    initializeTemplateHelper();
    ArtifactCollector artifactCollector = new DefaultArtifactCollector();
    hibernateMappingExporterExtension.getProperties().put(ExporterConstants.ARTIFACT_COLLECTOR, artifactCollector);
    File[] hbmXmlFiles = artifactCollector.getFiles("hbm.xml");
    assertTrue(hbmXmlFiles.length == 0);
    assertFalse(new File("foo" + File.separator + "Bar.hbm.xml").exists());
    Map<String, Object> additionalContext = new HashMap<String, Object>();
    Cfg2HbmTool c2h = new Cfg2HbmTool();
    additionalContext.put("date", new Date().toString());
    additionalContext.put("version", Version.CURRENT_VERSION);
    additionalContext.put("c2h", c2h);
    hibernateMappingExporterExtension.superExportPOJO(additionalContext, createPojoClass());
    hbmXmlFiles = artifactCollector.getFiles("hbm.xml");
    assertTrue(hbmXmlFiles.length == 1);
    assertEquals("foo" + File.separator + "Bar.hbm.xml", hbmXmlFiles[0].getPath());
    assertTrue(new File("foo" + File.separator + "Bar.hbm.xml").exists());
}
Also used : DefaultArtifactCollector(org.hibernate.tool.internal.export.common.DefaultArtifactCollector) HashMap(java.util.HashMap) ArtifactCollector(org.hibernate.tool.api.export.ArtifactCollector) DefaultArtifactCollector(org.hibernate.tool.internal.export.common.DefaultArtifactCollector) Cfg2HbmTool(org.hibernate.tool.internal.export.hbm.Cfg2HbmTool) File(java.io.File) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 2 with Cfg2HbmTool

use of org.hibernate.tool.internal.export.hbm.Cfg2HbmTool in project jbosstools-hibernate by jbosstools.

the class HibernateMappingExporterExtensionTest method testExportPOJO.

@Test
public void testExportPOJO() throws Exception {
    initializeTemplateHelper();
    POJOClass pojoClass = createPojoClass();
    // first without a delegate exporter
    ArtifactCollector artifactCollector = new DefaultArtifactCollector();
    hibernateMappingExporterExtension.getProperties().put(ExporterConstants.ARTIFACT_COLLECTOR, artifactCollector);
    File[] hbmXmlFiles = artifactCollector.getFiles("hbm.xml");
    Map<Object, Object> additionalContext = new HashMap<Object, Object>();
    Cfg2HbmTool c2h = new Cfg2HbmTool();
    additionalContext.put("date", new Date().toString());
    additionalContext.put("version", Version.CURRENT_VERSION);
    additionalContext.put("c2h", c2h);
    assertTrue(hbmXmlFiles.length == 0);
    assertFalse(new File("foo" + File.separator + "Bar.hbm.xml").exists());
    hibernateMappingExporterExtension.exportPOJO(additionalContext, pojoClass);
    hbmXmlFiles = artifactCollector.getFiles("hbm.xml");
    assertTrue(hbmXmlFiles.length == 1);
    assertEquals("foo" + File.separator + "Bar.hbm.xml", hbmXmlFiles[0].getPath());
    assertTrue(new File("foo" + File.separator + "Bar.hbm.xml").exists());
    // then with a delegate exporter
    artifactCollector = new DefaultArtifactCollector();
    hibernateMappingExporterExtension.getProperties().put(ExporterConstants.ARTIFACT_COLLECTOR, artifactCollector);
    final HashMap<Object, Object> arguments = new HashMap<Object, Object>();
    IExportPOJODelegate exportPojoDelegate = new IExportPOJODelegate() {

        @Override
        public void exportPOJO(Map<Object, Object> map, IPOJOClass pojoClass) {
            arguments.put("map", map);
            arguments.put("pojoClass", pojoClass);
        }
    };
    hbmXmlFiles = artifactCollector.getFiles("hbm.xml");
    Field delegateField = HibernateMappingExporterExtension.class.getDeclaredField("delegateExporter");
    delegateField.setAccessible(true);
    delegateField.set(hibernateMappingExporterExtension, exportPojoDelegate);
    assertTrue(hbmXmlFiles.length == 0);
    assertNull(arguments.get("map"));
    assertNull(arguments.get("pojoClass"));
    hibernateMappingExporterExtension.exportPOJO(additionalContext, pojoClass);
    assertTrue(hbmXmlFiles.length == 0);
    assertSame(additionalContext, arguments.get("map"));
    assertSame(pojoClass, ((IFacade) arguments.get("pojoClass")).getTarget());
}
Also used : DefaultArtifactCollector(org.hibernate.tool.internal.export.common.DefaultArtifactCollector) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) HashMap(java.util.HashMap) EntityPOJOClass(org.hibernate.tool.internal.export.java.EntityPOJOClass) POJOClass(org.hibernate.tool.internal.export.java.POJOClass) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) Date(java.util.Date) Field(java.lang.reflect.Field) ArtifactCollector(org.hibernate.tool.api.export.ArtifactCollector) DefaultArtifactCollector(org.hibernate.tool.internal.export.common.DefaultArtifactCollector) IExportPOJODelegate(org.jboss.tools.hibernate.runtime.spi.IExportPOJODelegate) Cfg2HbmTool(org.hibernate.tool.internal.export.hbm.Cfg2HbmTool) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with Cfg2HbmTool

use of org.hibernate.tool.internal.export.hbm.Cfg2HbmTool in project jbosstools-hibernate by jbosstools.

the class HibernateMappingExporterFacadeTest method testExportPOJO.

@Test
public void testExportPOJO() throws Exception {
    RootClass persistentClass = new RootClass(DummyMetadataBuildingContext.INSTANCE);
    Table rootTable = new Table();
    rootTable.setName("FOO");
    persistentClass.setTable(rootTable);
    persistentClass.setEntityName("Foo");
    persistentClass.setClassName("Foo");
    IPOJOClass pojoClass = FACADE_FACTORY.createPOJOClass(new EntityPOJOClass(persistentClass, new Cfg2JavaTool()));
    Map<Object, Object> additionalContext = new HashMap<Object, Object>();
    Cfg2HbmTool c2h = new Cfg2HbmTool();
    additionalContext.put("date", new Date().toString());
    additionalContext.put("version", Version.CURRENT_VERSION);
    additionalContext.put("c2h", c2h);
    hibernateMappingExporter.getProperties().put(ExporterConstants.DESTINATION_FOLDER, outputDir);
    Method setTemplateHelperMethod = AbstractExporter.class.getDeclaredMethod("setTemplateHelper", new Class[] { TemplateHelper.class });
    setTemplateHelperMethod.setAccessible(true);
    TemplateHelper templateHelper = new TemplateHelper();
    templateHelper.init(null, new String[0]);
    setTemplateHelperMethod.invoke(hibernateMappingExporter, new Object[] { templateHelper });
    final File fooHbmXml = new File(outputDir, "Foo.hbm.xml");
    assertFalse(fooHbmXml.exists());
    hibernateMappingExporterFacade.exportPOJO(additionalContext, pojoClass);
    assertTrue(fooHbmXml.exists());
    fooHbmXml.delete();
    outputDir.delete();
}
Also used : RootClass(org.hibernate.mapping.RootClass) Table(org.hibernate.mapping.Table) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) HashMap(java.util.HashMap) TemplateHelper(org.hibernate.tool.internal.export.common.TemplateHelper) Method(java.lang.reflect.Method) Date(java.util.Date) EntityPOJOClass(org.hibernate.tool.internal.export.java.EntityPOJOClass) Cfg2HbmTool(org.hibernate.tool.internal.export.hbm.Cfg2HbmTool) Cfg2JavaTool(org.hibernate.tool.internal.export.java.Cfg2JavaTool) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 4 with Cfg2HbmTool

use of org.hibernate.tool.internal.export.hbm.Cfg2HbmTool in project jbosstools-hibernate by jbosstools.

the class FacadeFactoryTest method testCreateCfg2HbmTool.

@Test
public void testCreateCfg2HbmTool() {
    Cfg2HbmTool cfg2HbmTool = new Cfg2HbmTool();
    ICfg2HbmTool facade = facadeFactory.createCfg2HbmTool(cfg2HbmTool);
    assertSame(cfg2HbmTool, ((IFacade) facade).getTarget());
}
Also used : ICfg2HbmTool(org.jboss.tools.hibernate.runtime.spi.ICfg2HbmTool) Cfg2HbmTool(org.hibernate.tool.internal.export.hbm.Cfg2HbmTool) ICfg2HbmTool(org.jboss.tools.hibernate.runtime.spi.ICfg2HbmTool) Test(org.junit.jupiter.api.Test)

Example 5 with Cfg2HbmTool

use of org.hibernate.tool.internal.export.hbm.Cfg2HbmTool in project jbosstools-hibernate by jbosstools.

the class Cfg2HbmToolFacadeTest method before.

@BeforeEach
public void before() {
    cfg2HbmToolTarget = new Cfg2HbmTool();
    cfg2HbmToolFacade = new AbstractCfg2HbmToolFacade(FACADE_FACTORY, cfg2HbmToolTarget) {
    };
}
Also used : AbstractCfg2HbmToolFacade(org.jboss.tools.hibernate.runtime.common.AbstractCfg2HbmToolFacade) Cfg2HbmTool(org.hibernate.tool.internal.export.hbm.Cfg2HbmTool) ICfg2HbmTool(org.jboss.tools.hibernate.runtime.spi.ICfg2HbmTool) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

Cfg2HbmTool (org.hibernate.tool.internal.export.hbm.Cfg2HbmTool)6 Test (org.junit.jupiter.api.Test)5 File (java.io.File)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 ICfg2HbmTool (org.jboss.tools.hibernate.runtime.spi.ICfg2HbmTool)3 ArtifactCollector (org.hibernate.tool.api.export.ArtifactCollector)2 DefaultArtifactCollector (org.hibernate.tool.internal.export.common.DefaultArtifactCollector)2 EntityPOJOClass (org.hibernate.tool.internal.export.java.EntityPOJOClass)2 IPOJOClass (org.jboss.tools.hibernate.runtime.spi.IPOJOClass)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 RootClass (org.hibernate.mapping.RootClass)1 Table (org.hibernate.mapping.Table)1 TemplateHelper (org.hibernate.tool.internal.export.common.TemplateHelper)1 Cfg2JavaTool (org.hibernate.tool.internal.export.java.Cfg2JavaTool)1 POJOClass (org.hibernate.tool.internal.export.java.POJOClass)1 AbstractCfg2HbmToolFacade (org.jboss.tools.hibernate.runtime.common.AbstractCfg2HbmToolFacade)1 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)1