diff --git a/src/main/java/eu/kidf/diversicon/cli/commands/ImportXmlCommand.java b/src/main/java/eu/kidf/diversicon/cli/commands/ImportXmlCommand.java index f34014e..9fbc4e6 100644 --- a/src/main/java/eu/kidf/diversicon/cli/commands/ImportXmlCommand.java +++ b/src/main/java/eu/kidf/diversicon/cli/commands/ImportXmlCommand.java @@ -42,6 +42,12 @@ public class ImportXmlCommand implements DiverCliCommand { @Parameter(names = { "--skip-augment", "-s" }, description = "Skips augmenting the db graph to speed up " + " operations requiring the transitive closure.") boolean skipAugment = false; + + @Parameter(names = { "--dry-run", "-r" }, description = "A dry run simulates the import without writing anything into the database.") + boolean dryRun = false; + + @Parameter(names = { "--force", "-f" }, description = "Forces import even on warnings (in particular, missing external references).") + boolean force = false; @Parameter(required = true, variableArity = true, description = "a space separated list of XML files in UBY-LMF format." + " Lexical resources must have a 'name' attribute. If there are already present resources with the" @@ -74,10 +80,12 @@ public void configure() { checkNotBlank(author, "Tried to import files without '--author' parameter! "); checkNotBlank(description, "Tried to import files without '--description' parameter! "); - importConfig = new ImportConfig(); - importConfig.setAuthor(author); - importConfig.setDescription(description); - importConfig.setSkipAugment(skipAugment); + importConfig = new ImportConfig() + .setAuthor(author) + .setDescription(description) + .setSkipAugment(skipAugment) + .setForce(force) + .setDryRun(dryRun); for (String fileUrl : importXmlPaths) { importConfig.addLexResFileUrl(fileUrl); diff --git a/src/main/java/eu/kidf/diversicon/cli/commands/InitCommand.java b/src/main/java/eu/kidf/diversicon/cli/commands/InitCommand.java index ef01280..31dad14 100644 --- a/src/main/java/eu/kidf/diversicon/cli/commands/InitCommand.java +++ b/src/main/java/eu/kidf/diversicon/cli/commands/InitCommand.java @@ -136,11 +136,9 @@ public void run(){ System.setOut(new PrintStream( new OutputStream() { @Override public void write(int b) { }} )); - Diversicons.dropCreateTables(dbCfg); - } catch (Exception ex){ - ///LOG.setLevel(savedLevel); + Diversicons.dropCreateTables(dbCfg); + } finally { System.setOut(savedOut); - throw ex; } } diff --git a/src/test/java/eu/kidf/diversicon/cli/test/DiverCliTest.java b/src/test/java/eu/kidf/diversicon/cli/test/DiverCliTest.java index 888aef2..4cc6620 100644 --- a/src/test/java/eu/kidf/diversicon/cli/test/DiverCliTest.java +++ b/src/test/java/eu/kidf/diversicon/cli/test/DiverCliTest.java @@ -12,7 +12,6 @@ import org.ini4j.InvalidFileFormatException; import org.ini4j.Wini; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,12 +39,12 @@ import eu.kidf.diversicon.core.Diversicon; import eu.kidf.diversicon.core.Diversicons; import eu.kidf.diversicon.core.ImportJob; +import eu.kidf.diversicon.core.exceptions.InvalidImportException; import eu.kidf.diversicon.core.exceptions.InvalidXmlException; import eu.kidf.diversicon.core.internal.Internals; import eu.kidf.diversicon.core.test.DivTester; import eu.kidf.diversicon.data.DivUpper; import eu.kidf.diversicon.data.DivWn31; -import eu.kidf.diversicon.data.Examplicon; import static eu.kidf.diversicon.cli.MainCommand.PRJ_OPTION; import static eu.kidf.diversicon.cli.test.CliTester.initEmpty; @@ -319,12 +318,12 @@ public void testIni() throws InvalidFileFormatException, IOException { */ @Test public void testImportXmlBadParams() { - DiverCli.of(PRJ_OPTION, "db", InitCommand.CMD).run(); + DiverCli.of(PRJ_OPTION, "db", InitCommand.CMD).run(); File xmlFile = DivTester.writeXml(DivTester.GRAPH_1_HYPERNYM); try { - DiverCli.of("--import ", xmlFile.getAbsolutePath()) + DiverCli.of(ImportXmlCommand.CMD, xmlFile.getAbsolutePath()) .run(); Assert.fail("Should need author!"); } catch (Exception ex) { @@ -332,7 +331,7 @@ public void testImportXmlBadParams() { } try { - DiverCli.of("--import ", xmlFile.getAbsolutePath(), "--author", " ") + DiverCli.of(ImportXmlCommand.CMD, xmlFile.getAbsolutePath(), "--author", " ") .run(); Assert.fail("Should need author!"); } catch (Exception ex) { @@ -340,7 +339,7 @@ public void testImportXmlBadParams() { } try { - DiverCli.of("--import ", xmlFile.getAbsolutePath(), "--author", "") + DiverCli.of(ImportXmlCommand.CMD, xmlFile.getAbsolutePath(), "--author", "") .run(); Assert.fail("Should need author!"); } catch (Exception ex) { @@ -348,7 +347,7 @@ public void testImportXmlBadParams() { } try { - DiverCli.of("--import ", xmlFile.getAbsolutePath(), "--author", "a") + DiverCli.of(ImportXmlCommand.CMD, xmlFile.getAbsolutePath(), "--author", "a") .run(); Assert.fail("Need description!"); } catch (Exception ex) { @@ -356,7 +355,7 @@ public void testImportXmlBadParams() { } try { - DiverCli.of("--import ", xmlFile.getAbsolutePath(), "--author", "a", "--description", "") + DiverCli.of(ImportXmlCommand.CMD, xmlFile.getAbsolutePath(), "--author", "a", "--description", "") .run(); Assert.fail("Need description!"); } catch (Exception ex) { @@ -372,6 +371,40 @@ public void testImportXmlBadParams() { } } + + /** + * @since 0.1.0 + */ + @Test + public void testImportXmlWarning(){ + initEmpty(); + + File xml = DivTester.writeXml(DivTester.GRAPH_WARNING, + DivTester.createLexResPackage(DivTester.GRAPH_WARNING, DivTester.TOO_LONG_PREFIX)); + DiverCli cli = DiverCli.of(ImportXmlCommand.CMD, "-a", "a", "-d","d", xml.getAbsolutePath()); + try { + cli.run(); + Assert.fail("Shouldn't arrive here!"); + } catch (InvalidImportException ex){ + LOG.debug("Caught expected exception: ", ex); + } + } + + /** + * @since 0.1.0 + */ + @Test + public void testImportXmlWarningForce(){ + initEmpty(); + + File xml = DivTester.writeXml(DivTester.GRAPH_WARNING, + DivTester.createLexResPackage(DivTester.GRAPH_WARNING, DivTester.TOO_LONG_PREFIX)); + DiverCli cli = DiverCli.of(ImportXmlCommand.CMD, "-a", "a", "-d","d", "--force", xml.getAbsolutePath()); + + cli.run(); + + } + /** * This also tests MainCommand is working @@ -384,6 +417,9 @@ public void testDebug() { cli.run(); } + /** + * @since 0.1.0 + */ @Test public void testImportXml() { @@ -404,6 +440,29 @@ public void testImportXml() { .close(); } + /** + * @since 0.1.0 + */ + @Test + public void testImportXmlDry() { + initEmpty(); + + File xmlFile = DivTester.writeXml(DivTester.GRAPH_1_HYPERNYM); + + DiverCli cli = DiverCli.of(ImportXmlCommand.CMD, + "--author", "a", + "--description", "d", + "--dry-run", + xmlFile.getAbsolutePath()); + + cli.run(); + + Diversicon div = Diversicon.connectToDb(cli.divConfig()); + assertEquals(null, div.getLexicalResource(DivTester.GRAPH_1_HYPERNYM.getName())); + div.getSession() + .close(); + + } /** @@ -478,6 +537,9 @@ public void testDbAugment() { cli2.disconnect(); } + /** + * @since 0.1.0 + */ @Test public void testExportXmlMissingXmlPath() { initEmpty(); @@ -490,6 +552,9 @@ public void testExportXmlMissingXmlPath() { } } + /** + * @since 0.1.0 + */ @Test public void testExportXmlMissingLexicalResourceName() throws IOException { @@ -506,6 +571,9 @@ public void testExportXmlMissingLexicalResourceName() throws IOException { } } + /** + * @since 0.1.0 + */ @Test public void testExportXmlWrongLexicalResource() throws IOException { @@ -522,6 +590,9 @@ public void testExportXmlWrongLexicalResource() throws IOException { } } + /** + * @since 0.1.0 + */ @Test public void testExportXmlExistingXml() throws IOException { @@ -542,6 +613,9 @@ public void testExportXmlExistingXml() throws IOException { } } + /** + * @since 0.1.0 + */ @Test public void testExportXml() throws IOException { @@ -561,6 +635,9 @@ public void testExportXml() throws IOException { } + /** + * @since 0.1.0 + */ @Test public void testExportXmlCompressed() throws IOException { initEmpty(); @@ -582,6 +659,9 @@ public void testExportXmlCompressed() throws IOException { } + /** + * @since 0.1.0 + */ @Test public void testExportSqlMissingSqlPath() { initEmpty(); @@ -595,6 +675,9 @@ public void testExportSqlMissingSqlPath() { } } + /** + * @since 0.1.0 + */ @Test public void testExportSqlExistingSql() throws IOException { initEmpty(); @@ -614,6 +697,9 @@ public void testExportSqlExistingSql() throws IOException { } } + /** + * @since 0.1.0 + */ @Test public void testExportSql() throws IOException { initEmpty(); @@ -856,6 +942,6 @@ public void testDiversiconBuildInfo(){ LOG.debug(bfWn31.getScmUrl()); assertTrue(bfWn31.getScmUrl().toLowerCase().contains("diversicon-wordnet-3.1")); } - + }