Search in sources :

Example 1 with IArtifactCollector

use of org.jboss.tools.hibernate.runtime.spi.IArtifactCollector in project jbosstools-hibernate by jbosstools.

the class ConsoleExtension method runExporters.

private IArtifactCollector runExporters(final ExporterAttributes attributes, final ExporterFactory[] exporterFactories, final Set<String> outputDirectories, final IProgressMonitor monitor) throws CoreException {
    monitor.beginTask(HibernateConsoleMessages.CodeGenerationLaunchDelegate_generating_code_for + attributes.getConsoleConfigurationName(), exporterFactories.length + 1);
    if (monitor.isCanceled())
        return null;
    ConsoleConfiguration cc = KnownConfigurations.getInstance().find(attributes.getConsoleConfigurationName());
    if (attributes.isReverseEngineer()) {
        monitor.subTask(HibernateConsoleMessages.CodeGenerationLaunchDelegate_reading_jdbc_metadata);
    }
    final IConfiguration cfg = buildConfiguration(attributes, cc, ResourcesPlugin.getWorkspace().getRoot());
    monitor.worked(1);
    if (monitor.isCanceled())
        return null;
    return (IArtifactCollector) cc.execute(new Command() {

        public Object execute() {
            IArtifactCollector artifactCollector = hibernateExtension.getHibernateService().newArtifactCollector();
            // Global properties
            Properties props = new Properties();
            props.put(CodeGenerationStrings.EJB3, // $NON-NLS-1$
            "" + attributes.isEJB3Enabled());
            props.put(CodeGenerationStrings.JDK5, // $NON-NLS-1$
            "" + attributes.isJDK5Enabled());
            for (int i = 0; i < exporterFactories.length; i++) {
                monitor.subTask(exporterFactories[i].getExporterDefinition().getDescription());
                Properties globalProperties = new Properties();
                globalProperties.putAll(props);
                IExporter exporter;
                try {
                    exporter = exporterFactories[i].createConfiguredExporter(cfg, attributes.getOutputPath(), attributes.getTemplatePath(), globalProperties, outputDirectories, artifactCollector, hibernateExtension.getHibernateService());
                } catch (CoreException e) {
                    throw new HibernateConsoleRuntimeException(HibernateConsoleMessages.CodeGenerationLaunchDelegate_error_while_setting_up + exporterFactories[i].getExporterDefinition(), e);
                }
                try {
                    exporter.start();
                } catch (HibernateException he) {
                    throw new HibernateConsoleRuntimeException(HibernateConsoleMessages.CodeGenerationLaunchDelegate_error_while_running + exporterFactories[i].getExporterDefinition().getDescription(), he);
                }
                monitor.worked(1);
            }
            return artifactCollector;
        }
    });
}
Also used : ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) CoreException(org.eclipse.core.runtime.CoreException) Command(org.hibernate.console.execution.ExecutionContext.Command) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) IExporter(org.jboss.tools.hibernate.runtime.spi.IExporter) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) Properties(java.util.Properties)

Example 2 with IArtifactCollector

use of org.jboss.tools.hibernate.runtime.spi.IArtifactCollector in project jbosstools-hibernate by jbosstools.

the class WripperException method testHbmExportExceptionTest.

public void testHbmExportExceptionTest() throws Exception {
    try {
        Object[] persClassesInit = getPersistenceClasses(false);
        final ConsoleConfiguration consCFG = getConsoleConfig();
        IConfiguration config = consCFG.getConfiguration();
        // delete old hbm files
        assertNotNull(testPackage);
        int nDeleted = 0;
        if (testPackage.getNonJavaResources().length > 0) {
            Object[] ress = testPackage.getNonJavaResources();
            for (int i = 0; i < ress.length; i++) {
                if (ress[i] instanceof IFile) {
                    IFile res = (IFile) ress[i];
                    if (res.getName().endsWith(".hbm.xml")) {
                        // $NON-NLS-1$
                        res.delete(true, false, null);
                        nDeleted++;
                    }
                }
            }
        }
        IHibernateMappingExporter hce = service.newHibernateMappingExporter(config, getSrcFolder());
        try {
            hce.start();
            IArtifactCollector collector = service.newArtifactCollector();
            collector.formatFiles();
            try {
                // build generated configuration
                testPackage.getResource().refreshLocal(IResource.DEPTH_INFINITE, null);
                testPackage.getJavaProject().getProject().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
                ConsoleConfigUtils.customizeCfgXmlForPack(testPackage);
                assertNotNull(consCFG);
                consCFG.reset();
                consCFG.build();
                assertTrue(consCFG.hasConfiguration());
                consCFG.getConfiguration().buildMappings();
                config = consCFG.getConfiguration();
            } catch (CoreException e) {
                String out = NLS.bind(ConsoleTestMessages.UpdateConfigurationTest_error_customising_file_for_package, new Object[] { ConsoleConfigUtils.CFG_FILE_NAME, testPackage.getPath(), e.getMessage() });
                fail(out);
            }
        } catch (Exception e) {
            throw (Exception) e.getCause();
        }
        // 
        Object[] persClassesReInit = getPersistenceClasses(false);
        // 
        int nCreated = 0;
        if (testPackage.getNonJavaResources().length > 0) {
            Object[] ress = testPackage.getNonJavaResources();
            for (int i = 0; i < ress.length; i++) {
                if (ress[i] instanceof IFile) {
                    IFile res = (IFile) ress[i];
                    if (res.getName().endsWith(".hbm.xml")) {
                        // $NON-NLS-1$
                        nCreated++;
                    }
                }
            }
        }
        // 
        assertTrue(persClassesInit.length == persClassesReInit.length);
        assertTrue(nCreated > 0);
        assertTrue(nDeleted >= 0 && persClassesInit.length > 0);
        assertTrue(nCreated <= persClassesInit.length);
    } catch (Exception e) {
        // $NON-NLS-1$ //$NON-NLS-2$
        String newMessage = "\nPackage " + testPackage.getElementName() + ":";
        throw new WripperException(newMessage, e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) IFile(org.eclipse.core.resources.IFile) JavaModelException(org.eclipse.jdt.core.JavaModelException) CoreException(org.eclipse.core.runtime.CoreException) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) CoreException(org.eclipse.core.runtime.CoreException) IHibernateMappingExporter(org.jboss.tools.hibernate.runtime.spi.IHibernateMappingExporter) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration)

Example 3 with IArtifactCollector

use of org.jboss.tools.hibernate.runtime.spi.IArtifactCollector in project jbosstools-hibernate by jbosstools.

the class ExporterAttributesTest method checkCorrectLaunchConfigurationFile.

private void checkCorrectLaunchConfigurationFile(final String fileName) {
    // IWorkspace ws = ResourcesPlugin.getWorkspace();
    String str1, str2;
    ILaunchConfiguration launchConfig = loadLaunchConfigFromFile(fileName);
    Map<String, Object> attrMap = null;
    try {
        attrMap = launchConfig.getAttributes();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(attrMap);
    ExporterAttributes exporterAttributes = null;
    try {
        exporterAttributes = new ExporterAttributes(launchConfig);
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(exporterAttributes);
    // check is configuration correct
    Assert.assertNull(exporterAttributes.checkExporterAttributes());
    List<ExporterFactory> exporterFactories = exporterAttributes.getExporterFactories();
    Set<ExporterFactory> selectedExporters = new HashSet<ExporterFactory>();
    selectedExporters.addAll(exporterFactories);
    Set<String> deletedExporterIds = new HashSet<String>();
    ILaunchConfigurationWorkingCopy launchConfigWC = null;
    try {
        launchConfigWC = launchConfig.getWorkingCopy();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(launchConfigWC);
    // 
    str1 = project.getSample(fileName);
    str1 = ResourceReadUtils.adjustXmlText(str1);
    // 
    Assert.assertNotNull(project);
    Assert.assertNotNull(project.getIProject());
    Assert.assertNotNull(project.getIProject().getFile(fileName));
    // 
    InputStream is = null;
    try {
        is = project.getIProject().getFile(fileName).getContents();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(is);
    str2 = ResourceReadUtils.readStream(is);
    str2 = ResourceReadUtils.adjustXmlText(str2);
    Assert.assertEquals(str1, str2);
    // update and save lc - so fileName from the project updated
    try {
        ExporterAttributes.saveExporterFactories(launchConfigWC, exporterFactories, selectedExporters, deletedExporterIds);
        launchConfigWC.doSave();
    } catch (CoreException e) {
        Assert.fail(e.getMessage());
    }
    // 
    is = null;
    try {
        is = project.getIProject().getFile(fileName).getContents();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(is);
    str2 = ResourceReadUtils.readStream(is);
    str2 = ResourceReadUtils.adjustXmlText(str2);
    Assert.assertEquals(str1, str2);
    // 
    IArtifactCollector artifactCollector = service.newArtifactCollector();
    ExporterAttributes expAttr = exporterAttributes;
    // Global properties
    Properties props = new Properties();
    // $NON-NLS-1$
    props.put(CodeGenerationStrings.EJB3, "" + expAttr.isEJB3Enabled());
    // $NON-NLS-1$
    props.put(CodeGenerationStrings.JDK5, "" + expAttr.isJDK5Enabled());
    consoleCfg.build();
    IConfiguration cfg = consoleCfg.getConfiguration();
    Assert.assertNotNull(cfg);
    Set<String> outputDirectories = new HashSet<String>();
    for (int i = 0; i < exporterFactories.size(); i++) {
        Properties globalProperties = new Properties();
        globalProperties.putAll(props);
        ExporterFactory ef = exporterFactories.get(i);
        // 
        Properties propsForTesting = new Properties();
        propsForTesting.putAll(globalProperties);
        propsForTesting.putAll(ef.getProperties());
        // 
        IExporter exporter = null;
        outputDirectories.clear();
        try {
            exporter = ef.createConfiguredExporter(cfg, expAttr.getOutputPath(), expAttr.getTemplatePath(), globalProperties, outputDirectories, artifactCollector, service);
        } catch (CoreException e) {
            e.printStackTrace();
        }
        Assert.assertNotNull(exporter);
        Assert.assertTrue(outputDirectories.size() > 0);
        Properties propsFromExporter = exporter.getProperties();
        String exporterDefinitionId = ef.getExporterDefinitionId();
        // test special handling for GenericExporter
        if (exporterDefinitionId.equals("org.hibernate.tools.hbmtemplate")) {
            // $NON-NLS-1$
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.FILE_PATTERN));
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.TEMPLATE_NAME));
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.FOR_EACH));
            IGenericExporter ge = exporter.getGenericExporter();
            Assert.assertNotNull(ge);
            Assert.assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.FILE_PATTERN), ge.getFilePattern());
            Assert.assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.TEMPLATE_NAME), ge.getTemplateName());
        // to test GenericExporter should provide public getter but it doesn't
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.FOR_EACH), ge.getForEach());
        }
        // test special handling for Hbm2DDLExporter
        if (exporterDefinitionId.equals("org.hibernate.tools.hbm2ddl")) {
            // $NON-NLS-1$
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.EXPORTTODATABASE));
            IHbm2DDLExporter ddlExporter = exporter.getHbm2DDLExporter();
            Assert.assertNotNull(ddlExporter);
        // to test Hbm2DDLExporter should provide public getter but it doesn't
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.EXPORTTODATABASE), ddlExporter.getExport());
        }
        // test special handling for QueryExporter
        if (exporterDefinitionId.equals("org.hibernate.tools.query")) {
            // $NON-NLS-1$
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.QUERY_STRING));
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.OUTPUTFILENAME));
        // IQueryExporter queryExporter = exporter.getQueryExporter();
        // to test QueryExporter should provide public getter but it doesn't
        // List<String> queryStrings = queryExporter.getQueries();
        // assertEquals(1, queryStrings.size());
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.QUERY_STRING), queryStrings.get(0));
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.OUTPUTFILENAME), queryExporter.getFileName());
        }
    // try {
    // exporter.start();
    // } catch (HibernateException he) {
    // he.printStackTrace();
    // }
    }
    Assert.assertTrue(artifactCollector.getFileTypes().size() == 0);
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) InputStream(java.io.InputStream) ILaunchConfigurationWorkingCopy(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) Properties(java.util.Properties) ExporterAttributes(org.hibernate.eclipse.launch.ExporterAttributes) CoreException(org.eclipse.core.runtime.CoreException) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) IHbm2DDLExporter(org.jboss.tools.hibernate.runtime.spi.IHbm2DDLExporter) ExporterFactory(org.hibernate.eclipse.console.model.impl.ExporterFactory) IExporter(org.jboss.tools.hibernate.runtime.spi.IExporter) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) IGenericExporter(org.jboss.tools.hibernate.runtime.spi.IGenericExporter) HashSet(java.util.HashSet)

Example 4 with IArtifactCollector

use of org.jboss.tools.hibernate.runtime.spi.IArtifactCollector in project jbosstools-hibernate by jbosstools.

the class ExporterFacadeTest method testSetArtifactCollector.

@Test
public void testSetArtifactCollector() {
    ArtifactCollector ac = new ArtifactCollector();
    IArtifactCollector artifactCollector = new AbstractArtifactCollectorFacade(FACADE_FACTORY, ac) {
    };
    exporterFacade.setArtifactCollector(artifactCollector);
    Assert.assertEquals("setArtifactCollector", methodName);
    Assert.assertArrayEquals(new Object[] { ac }, arguments);
}
Also used : AbstractArtifactCollectorFacade(org.jboss.tools.hibernate.runtime.common.AbstractArtifactCollectorFacade) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) ArtifactCollector(org.hibernate.tool.hbm2x.ArtifactCollector) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) Test(org.junit.Test)

Example 5 with IArtifactCollector

use of org.jboss.tools.hibernate.runtime.spi.IArtifactCollector in project jbosstools-hibernate by jbosstools.

the class ExporterFacadeTest method testSetArtifactCollector.

@Test
public void testSetArtifactCollector() {
    ArtifactCollector ac = new ArtifactCollector();
    IArtifactCollector artifactCollector = new AbstractArtifactCollectorFacade(FACADE_FACTORY, ac) {
    };
    exporterFacade.setArtifactCollector(artifactCollector);
    Assert.assertEquals("setArtifactCollector", methodName);
    Assert.assertArrayEquals(new Object[] { ac }, arguments);
}
Also used : AbstractArtifactCollectorFacade(org.jboss.tools.hibernate.runtime.common.AbstractArtifactCollectorFacade) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) ArtifactCollector(org.hibernate.tool.hbm2x.ArtifactCollector) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) Test(org.junit.Test)

Aggregations

IArtifactCollector (org.jboss.tools.hibernate.runtime.spi.IArtifactCollector)27 ArtifactCollector (org.hibernate.tool.hbm2x.ArtifactCollector)20 Test (org.junit.Test)20 AbstractArtifactCollectorFacade (org.jboss.tools.hibernate.runtime.common.AbstractArtifactCollectorFacade)8 CoreException (org.eclipse.core.runtime.CoreException)7 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)6 ConsoleConfiguration (org.hibernate.console.ConsoleConfiguration)5 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)4 File (java.io.File)3 Properties (java.util.Properties)3 IFile (org.eclipse.core.resources.IFile)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 JavaModelException (org.eclipse.jdt.core.JavaModelException)3 HibernateConsoleRuntimeException (org.hibernate.console.HibernateConsoleRuntimeException)3 HibernateException (org.jboss.tools.hibernate.runtime.spi.HibernateException)3 IExporter (org.jboss.tools.hibernate.runtime.spi.IExporter)3 IHibernateMappingExporter (org.jboss.tools.hibernate.runtime.spi.IHibernateMappingExporter)3 IService (org.jboss.tools.hibernate.runtime.spi.IService)3 FileNotFoundException (java.io.FileNotFoundException)2 HashSet (java.util.HashSet)2