Search in sources :

Example 1 with ExporterAttributes

use of org.hibernate.eclipse.launch.ExporterAttributes 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 2 with ExporterAttributes

use of org.hibernate.eclipse.launch.ExporterAttributes in project jbosstools-hibernate by jbosstools.

the class ExportAntCodeGenWizardPage method checkCodeGenLaunchConfig.

protected String checkCodeGenLaunchConfig(ILaunchConfiguration lc) {
    String checkMessage = null;
    ExporterAttributes attributes = null;
    try {
        attributes = new ExporterAttributes(lc);
        checkMessage = attributes.checkExporterAttributes();
    } catch (CoreException e) {
        checkMessage = e.getMessage();
    }
    if (checkMessage != null) {
        checkMessage = NLS.bind(HibernateConsoleMessages.ExportAntCodeGenWizardPage_error_in_hibernate_code_generation_configuration, checkMessage);
    }
    if (checkMessage == null && attributes != null) {
        String consoleConfigName = attributes.getConsoleConfigurationName();
        ConsoleConfigurationPreferences consoleConfigPrefs = getConsoleConfigPreferences(consoleConfigName);
        String connProfileName = consoleConfigPrefs == null ? null : consoleConfigPrefs.getConnectionProfileName();
        if (!CodeGenXMLFactory.isEmpty(connProfileName)) {
            IWorkspace workspace = ResourcesPlugin.getWorkspace();
            String externalPropFileName = CodeGenXMLFactory.propFileNameSuffix;
            // $NON-NLS-1$
            externalPropFileName = getFileName() + "." + externalPropFileName;
            String problemMessage = NLS.bind(HibernateConsoleMessages.ExportAntCodeGenWizardPage_warning, externalPropFileName);
            IPath resourcePath = getContainerFullPath().append(externalPropFileName);
            if (workspace.getRoot().getFile(resourcePath).exists()) {
                checkMessage = problemMessage;
            }
        }
    }
    return checkMessage;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IPath(org.eclipse.core.runtime.IPath) ConsoleConfigurationPreferences(org.hibernate.console.preferences.ConsoleConfigurationPreferences) IWorkspace(org.eclipse.core.resources.IWorkspace) ExporterAttributes(org.hibernate.eclipse.launch.ExporterAttributes)

Example 3 with ExporterAttributes

use of org.hibernate.eclipse.launch.ExporterAttributes in project jbosstools-hibernate by jbosstools.

the class ConsoleExtension method launchExporters.

public Map<String, File[]> launchExporters(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
    Assert.isNotNull(configuration);
    Assert.isNotNull(monitor);
    ExporterAttributes attributes = new ExporterAttributes(configuration);
    List<ExporterFactory> exporterFactories = attributes.getExporterFactories();
    for (Iterator<ExporterFactory> iter = exporterFactories.iterator(); iter.hasNext(); ) {
        ExporterFactory exFactory = iter.next();
        if (!exFactory.isEnabled(configuration)) {
            iter.remove();
        }
    }
    try {
        Set<String> outputDirectories = new HashSet<String>();
        ExporterFactory[] exporters = exporterFactories.toArray(new ExporterFactory[exporterFactories.size()]);
        IArtifactCollector collector = runExporters(attributes, exporters, outputDirectories, monitor);
        for (String path : outputDirectories) {
            CodeGenerationUtils.refreshOutputDir(path);
        }
        // eclipse will format the uptodate files!
        if (collector != null) {
            Map<String, File[]> map = new HashMap<String, File[]>();
            Set<String> types = collector.getFileTypes();
            for (String type : types) {
                File[] files = collector.getFiles(type.toString());
                map.put(type, files);
            }
            return map;
        }
    } catch (Exception e) {
        throw new CoreException(HibernatePlugin.throwableToStatus(e, 666));
    } catch (NoClassDefFoundError e) {
        throw new CoreException(HibernatePlugin.throwableToStatus(new HibernateConsoleRuntimeException(HibernateConsoleMessages.CodeGenerationLaunchDelegate_received_noclassdeffounderror, e), 666));
    } finally {
        monitor.done();
    }
    return null;
}
Also used : HashMap(java.util.HashMap) ExporterAttributes(org.hibernate.eclipse.launch.ExporterAttributes) CoreException(org.eclipse.core.runtime.CoreException) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) CoreException(org.eclipse.core.runtime.CoreException) ExporterFactory(org.hibernate.eclipse.console.model.impl.ExporterFactory) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with ExporterAttributes

use of org.hibernate.eclipse.launch.ExporterAttributes in project jbosstools-hibernate by jbosstools.

the class ExporterAttributesTest method checkIncorrectLaunchConfigurationFile.

private void checkIncorrectLaunchConfigurationFile(final String fileName) {
    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.assertNotNull(exporterAttributes.checkExporterAttributes());
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) CoreException(org.eclipse.core.runtime.CoreException) ExporterAttributes(org.hibernate.eclipse.launch.ExporterAttributes)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)4 ExporterAttributes (org.hibernate.eclipse.launch.ExporterAttributes)4 HashSet (java.util.HashSet)2 ILaunchConfiguration (org.eclipse.debug.core.ILaunchConfiguration)2 ExporterFactory (org.hibernate.eclipse.console.model.impl.ExporterFactory)2 IArtifactCollector (org.jboss.tools.hibernate.runtime.spi.IArtifactCollector)2 File (java.io.File)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 IWorkspace (org.eclipse.core.resources.IWorkspace)1 IPath (org.eclipse.core.runtime.IPath)1 ILaunchConfigurationWorkingCopy (org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)1 HibernateConsoleRuntimeException (org.hibernate.console.HibernateConsoleRuntimeException)1 ConsoleConfigurationPreferences (org.hibernate.console.preferences.ConsoleConfigurationPreferences)1 HibernateException (org.jboss.tools.hibernate.runtime.spi.HibernateException)1 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)1 IExporter (org.jboss.tools.hibernate.runtime.spi.IExporter)1 IGenericExporter (org.jboss.tools.hibernate.runtime.spi.IGenericExporter)1 IHbm2DDLExporter (org.jboss.tools.hibernate.runtime.spi.IHbm2DDLExporter)1