Search in sources :

Example 1 with InvalidConfigurationException

use of org.mybatis.generator.exception.InvalidConfigurationException in project generator by mybatis.

the class MyBatisGeneratorTest method testGenerateIbatis2WithInvalidConfig.

@Test(expected = InvalidConfigurationException.class)
public void testGenerateIbatis2WithInvalidConfig() throws Exception {
    List<String> warnings = new ArrayList<String>();
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(this.getClass().getClassLoader().getResourceAsStream("generatorConfigIbatis2_badConfig.xml"));
    DefaultShellCallback shellCallback = new DefaultShellCallback(true);
    try {
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
        myBatisGenerator.generate(null, null, null, false);
    } catch (InvalidConfigurationException e) {
        assertEquals(1, e.getErrors().size());
        throw e;
    }
}
Also used : Configuration(org.mybatis.generator.config.Configuration) ConnectionFactoryConfiguration(org.mybatis.generator.config.ConnectionFactoryConfiguration) JDBCConnectionConfiguration(org.mybatis.generator.config.JDBCConnectionConfiguration) ArrayList(java.util.ArrayList) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) Test(org.junit.Test)

Example 2 with InvalidConfigurationException

use of org.mybatis.generator.exception.InvalidConfigurationException in project jeesuite-libs by vakinge.

the class MyBatisGeneratorTool method main.

public static void main(String[] args) {
    List<String> warnings = new ArrayList<String>();
    String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    boolean overwrite = true;
    File configFile = new File(path + "/../../src/test/resources/generator/generatorConfig.xml");
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = null;
    try {
        config = cp.parseConfiguration(configFile);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMLParserException e) {
        e.printStackTrace();
    }
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = null;
    try {
        myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    } catch (InvalidConfigurationException e) {
        e.printStackTrace();
    }
    try {
        myBatisGenerator.generate(null);
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : Configuration(org.mybatis.generator.config.Configuration) XMLParserException(org.mybatis.generator.exception.XMLParserException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) File(java.io.File) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 3 with InvalidConfigurationException

use of org.mybatis.generator.exception.InvalidConfigurationException in project generator by mybatis.

the class MyBatisGeneratorTest method testGenerateInvalidConfigWithTwoConnectionSources.

@Test
void testGenerateInvalidConfigWithTwoConnectionSources() {
    List<String> warnings = new ArrayList<>();
    Configuration config = new Configuration();
    Context context = new Context(ModelType.HIERARCHICAL);
    context.setId("MyContext");
    context.setConnectionFactoryConfiguration(new ConnectionFactoryConfiguration());
    context.setJdbcConnectionConfiguration(new JDBCConnectionConfiguration());
    config.addContext(context);
    DefaultShellCallback shellCallback = new DefaultShellCallback(true);
    InvalidConfigurationException e = assertThrows(InvalidConfigurationException.class, () -> {
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
        myBatisGenerator.generate(null, null, null, false);
    });
    assertEquals(3, e.getErrors().size());
}
Also used : Context(org.mybatis.generator.config.Context) Configuration(org.mybatis.generator.config.Configuration) ConnectionFactoryConfiguration(org.mybatis.generator.config.ConnectionFactoryConfiguration) JDBCConnectionConfiguration(org.mybatis.generator.config.JDBCConnectionConfiguration) ArrayList(java.util.ArrayList) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) JDBCConnectionConfiguration(org.mybatis.generator.config.JDBCConnectionConfiguration) ConnectionFactoryConfiguration(org.mybatis.generator.config.ConnectionFactoryConfiguration) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator) Test(org.junit.jupiter.api.Test)

Example 4 with InvalidConfigurationException

use of org.mybatis.generator.exception.InvalidConfigurationException in project generator by mybatis.

the class ShellRunner method main.

public static void main(String[] args) {
    if (args.length == 0) {
        usage();
        System.exit(0);
        // only to satisfy compiler, never returns
        return;
    }
    Map<String, String> arguments = parseCommandLine(args);
    if (arguments.containsKey(HELP_1)) {
        usage();
        System.exit(0);
        // only to satisfy compiler, never returns
        return;
    }
    if (!arguments.containsKey(CONFIG_FILE)) {
        // $NON-NLS-1$
        writeLine(getString("RuntimeError.0"));
        return;
    }
    List<String> warnings = new ArrayList<>();
    String configfile = arguments.get(CONFIG_FILE);
    File configurationFile = new File(configfile);
    if (!configurationFile.exists()) {
        // $NON-NLS-1$
        writeLine(getString("RuntimeError.1", configfile));
        return;
    }
    Set<String> fullyqualifiedTables = new HashSet<>();
    if (arguments.containsKey(TABLES)) {
        // $NON-NLS-1$
        StringTokenizer st = new StringTokenizer(arguments.get(TABLES), ",");
        while (st.hasMoreTokens()) {
            String s = st.nextToken().trim();
            if (s.length() > 0) {
                fullyqualifiedTables.add(s);
            }
        }
    }
    Set<String> contexts = new HashSet<>();
    if (arguments.containsKey(CONTEXT_IDS)) {
        StringTokenizer st = new StringTokenizer(arguments.get(CONTEXT_IDS), // $NON-NLS-1$
        ",");
        while (st.hasMoreTokens()) {
            String s = st.nextToken().trim();
            if (s.length() > 0) {
                contexts.add(s);
            }
        }
    }
    try {
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configurationFile);
        DefaultShellCallback shellCallback = new DefaultShellCallback(arguments.containsKey(OVERWRITE));
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
        ProgressCallback progressCallback = arguments.containsKey(VERBOSE) ? new VerboseProgressCallback() : null;
        myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables);
    } catch (XMLParserException e) {
        // $NON-NLS-1$
        writeLine(getString("Progress.3"));
        writeLine();
        for (String error : e.getErrors()) {
            writeLine(error);
        }
        return;
    } catch (SQLException | IOException e) {
        e.printStackTrace(System.out);
        return;
    } catch (InvalidConfigurationException e) {
        // $NON-NLS-1$
        writeLine(getString("Progress.16"));
        for (String error : e.getErrors()) {
            writeLine(error);
        }
        return;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    for (String warning : warnings) {
        writeLine(warning);
    }
    if (warnings.isEmpty()) {
        // $NON-NLS-1$
        writeLine(getString("Progress.4"));
    } else {
        writeLine();
        // $NON-NLS-1$
        writeLine(getString("Progress.5"));
    }
}
Also used : Configuration(org.mybatis.generator.config.Configuration) XMLParserException(org.mybatis.generator.exception.XMLParserException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) IOException(java.io.IOException) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) StringTokenizer(java.util.StringTokenizer) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) File(java.io.File) HashSet(java.util.HashSet)

Example 5 with InvalidConfigurationException

use of org.mybatis.generator.exception.InvalidConfigurationException in project generator by mybatis.

the class RunGeneratorThread method run.

/*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor)
     */
public void run(IProgressMonitor monitor) throws CoreException {
    SubMonitor subMonitor = SubMonitor.convert(monitor, 1000);
    subMonitor.beginTask("Generating MyBatis/iBATIS Artifacts:", 1000);
    setClassLoader();
    try {
        subMonitor.subTask("Parsing Configuration");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(inputFile.getLocation().toFile());
        subMonitor.worked(50);
        MyBatisGenerator mybatisGenerator = new MyBatisGenerator(config, new EclipseShellCallback(), warnings);
        monitor.subTask("Generating Files from Database Tables");
        SubMonitor spm = subMonitor.newChild(950);
        mybatisGenerator.generate(new EclipseProgressCallback(spm));
    } catch (InterruptedException e) {
        throw new OperationCanceledException();
    } catch (SQLException e) {
        Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e);
        Activator.getDefault().getLog().log(status);
        throw new CoreException(status);
    } catch (IOException e) {
        Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e);
        Activator.getDefault().getLog().log(status);
        throw new CoreException(status);
    } catch (XMLParserException e) {
        List<String> errors = e.getErrors();
        MultiStatus multiStatus = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR, "XML Parser Errors\n  See Details for more Information", null);
        Iterator<String> iter = errors.iterator();
        while (iter.hasNext()) {
            Status message = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, iter.next(), null);
            multiStatus.add(message);
        }
        throw new CoreException(multiStatus);
    } catch (InvalidConfigurationException e) {
        List<String> errors = e.getErrors();
        MultiStatus multiStatus = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR, "Invalid Configuration\n  See Details for more Information", null);
        Iterator<String> iter = errors.iterator();
        while (iter.hasNext()) {
            Status message = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, iter.next(), null);
            multiStatus.add(message);
        }
        throw new CoreException(multiStatus);
    } finally {
        monitor.done();
        restoreClassLoader();
    }
}
Also used : EclipseShellCallback(org.mybatis.generator.eclipse.core.callback.EclipseShellCallback) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Configuration(org.mybatis.generator.config.Configuration) SQLException(java.sql.SQLException) XMLParserException(org.mybatis.generator.exception.XMLParserException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) MultiStatus(org.eclipse.core.runtime.MultiStatus) IOException(java.io.IOException) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) EclipseProgressCallback(org.mybatis.generator.eclipse.core.callback.EclipseProgressCallback) CoreException(org.eclipse.core.runtime.CoreException) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Aggregations

InvalidConfigurationException (org.mybatis.generator.exception.InvalidConfigurationException)12 ArrayList (java.util.ArrayList)11 MyBatisGenerator (org.mybatis.generator.api.MyBatisGenerator)11 Configuration (org.mybatis.generator.config.Configuration)11 ConfigurationParser (org.mybatis.generator.config.xml.ConfigurationParser)9 IOException (java.io.IOException)8 SQLException (java.sql.SQLException)8 DefaultShellCallback (org.mybatis.generator.internal.DefaultShellCallback)8 XMLParserException (org.mybatis.generator.exception.XMLParserException)7 File (java.io.File)5 ConnectionFactoryConfiguration (org.mybatis.generator.config.ConnectionFactoryConfiguration)4 JDBCConnectionConfiguration (org.mybatis.generator.config.JDBCConnectionConfiguration)4 HashSet (java.util.HashSet)3 StringTokenizer (java.util.StringTokenizer)3 Test (org.junit.jupiter.api.Test)3 Properties (java.util.Properties)2 BuildException (org.apache.tools.ant.BuildException)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 Context (org.mybatis.generator.config.Context)2 EclipseProgressCallback (org.mybatis.generator.eclipse.core.callback.EclipseProgressCallback)2