From 5938316a66772a9c77e8d54564f6c5298bca2182 Mon Sep 17 00:00:00 2001 From: Alberto Hernandez Date: Fri, 26 Jan 2018 12:38:28 +0100 Subject: [PATCH 01/16] Rebuild the Mojo Plugin in order to add parameters the the Java UtPLSQL plugin --- utplsql-maven-plugin/pom.xml | 232 ++++++++++-------- .../main/java/org/utplsql/UtPLSQLMojo.java | 226 ++++++++++------- .../org/utplsql/helper/PluginDefault.java | 60 +++++ .../org/utplsql/helper/SQLScannerHelper.java | 59 +++++ .../java/org/utplsql/tools/FileWalker.java | 48 ---- .../src/test/java/UtPLSQLMojoTest.java | 58 ----- .../java/org/utpsql/test/UtPLSQLMojoTest.java | 35 +++ .../src/test/resources/pom.xml | 55 +++-- 8 files changed, 452 insertions(+), 321 deletions(-) create mode 100644 utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java create mode 100644 utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java delete mode 100644 utplsql-maven-plugin/src/main/java/org/utplsql/tools/FileWalker.java delete mode 100644 utplsql-maven-plugin/src/test/java/UtPLSQLMojoTest.java create mode 100644 utplsql-maven-plugin/src/test/java/org/utpsql/test/UtPLSQLMojoTest.java diff --git a/utplsql-maven-plugin/pom.xml b/utplsql-maven-plugin/pom.xml index 4354875..c11545f 100644 --- a/utplsql-maven-plugin/pom.xml +++ b/utplsql-maven-plugin/pom.xml @@ -1,110 +1,126 @@ - 4.0.0 - - - org.utplsql - utplsql-maven-plugin-build - 1.0-SNAPSHOT - - - utplsql-maven-plugin - maven-plugin - - utplsql-maven-plugin Maven Plugin - - - http://maven.apache.org - - - - - - - org.utplsql - java-api - 1.0-SNAPSHOT - compile - - - - org.apache.maven - maven-plugin-api - ${maven.version} - - - - org.apache.maven - maven-core - ${maven.version} - - - - org.apache.maven.plugin-tools - maven-plugin-annotations - 3.5 - provided - - - - - - org.apache.maven.plugin-testing - maven-plugin-testing-harness - 3.3.0 - test - - - - org.apache.maven - maven-compat - ${maven.version} - test - - - - junit - junit - 4.12 - test - - - - org.assertj - assertj-core - 3.8.0 - test - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.7.0 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-plugin-plugin - 3.5 - - - default-descriptor - process-classes - - - help-goal - - helpmojo - - - - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.utplsql + utplsql-maven-plugin + maven-plugin + + 1.0.0-SNAPSHOT + + utplsql-maven-plugin Maven Plugin + + + http://maven.apache.org + + + UTF-8 + 1.8 + + + + + + org.utplsql + java-api + 3.0.4 + + + + org.apache.maven + maven-model + 3.0.2 + + + + org.apache.maven + maven-plugin-api + 3.5.2 + + + + org.codehaus.plexus + plexus-utils + 3.0.8 + + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.5 + provided + + + + + + org.apache.maven.plugin-testing + maven-plugin-testing-harness + 3.3.0 + test + + + + org.apache.maven + maven-compat + 3.5.2 + test + + + + junit + junit + 4.12 + test + + + + org.assertj + assertj-core + 3.8.0 + test + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-plugin-plugin + 3.5 + + utplsql + true + + + + mojo-descriptor + + descriptor + + + + help-goal + + helpmojo + + + + + + + + diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index f49ac79..bd578e4 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -1,104 +1,144 @@ package org.utplsql; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.utplsql.api.FileMapperOptions; -import org.utplsql.api.OutputBuffer; import org.utplsql.api.TestRunner; -import org.utplsql.api.exception.SomeTestsFailedException; import org.utplsql.api.reporter.Reporter; import org.utplsql.api.reporter.ReporterFactory; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static org.utplsql.api.CustomTypes.UT_DOCUMENTATION_REPORTER; - -@Mojo(name = "test") -public class UtPLSQLMojo extends AbstractMojo { - - - /** - * List of paths with test suites. - */ - @Parameter(name = "suitePaths", required = true, property = "utplsql.suite_paths") - private List suitePaths; - - /** - * List of paths with source files. - */ - @Parameter(name = "sourcePaths", required = true, property = "utplsql.source_paths") - private List sourcePaths; - - /** - * List of paths with test files. - */ - @Parameter(name = "testPaths", required = true, property = "utplsql.test_paths") - private List testPaths; - - public void execute() throws MojoExecutionException, MojoFailureException { - - List reporterList = new ArrayList<>(); - reporterList.add(ReporterFactory.createReporter(UT_DOCUMENTATION_REPORTER)); - List testPaths = listOfStrings("tests"); - FileMapperOptions sourceMappingOptions = new FileMapperOptions(listOfStrings( - "source/award_bonus/award_bonus.prc" -// "/Users/kamil.berdychowski/workspaces/utPLSQL/utPLSQL-demo-project/source/between_string/betwnstr.fnc" - )); - FileMapperOptions testMappingOptions = new FileMapperOptions(listOfStrings( - "/Users/kamil.berdychowski/workspaces/utPLSQL/utPLSQL-demo-project/test/award_bonus/test_award_bonus.pkb", - "/Users/kamil.berdychowski/workspaces/utPLSQL/utPLSQL-demo-project/test/award_bonus/test_award_bonus.pks" -// "/Users/kamil.berdychowski/workspaces/utPLSQL/utPLSQL-demo-project/test/between_string/test_betwnstr.pkb", -// "/Users/kamil.berdychowski/workspaces/utPLSQL/utPLSQL-demo-project/test/between_string/test_betwnstr.pks" - )); - - Connection connection = null; - - try { - connection = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "tests", "tests"); - - for (Reporter reporter : reporterList) { - reporter.init(connection); - } - - new TestRunner() - .addPathList(testPaths) - .addReporterList(reporterList) - .sourceMappingOptions(sourceMappingOptions) - .testMappingOptions(testMappingOptions) - .colorConsole(true) - .failOnErrors(true) - .run(connection); - } catch (SomeTestsFailedException e) { - getLog().error(e); - throw new MojoExecutionException("Tests failed", e); - } catch (SQLException e) { - getLog().error(e); - throw new MojoFailureException("SQL error occured", e); - } finally { - //TODO: add more security checking, what happens when connection is broken - try { - new OutputBuffer(reporterList.get(0)).printAvailable(connection, System.out); - if (connection != null) { - connection.close(); - } - } catch (SQLException e) { - getLog().error("Error", e); - } - } - - } - - private List listOfStrings(String... values) { - List result = new ArrayList<>(); - Collections.addAll(result, values); - return result; - } +import org.utplsql.helper.PluginDefault; +import org.utplsql.helper.SQLScannerHelper; + +@Mojo(name = "test", defaultPhase = LifecyclePhase.TEST) +public class UtPLSQLMojo extends AbstractMojo +{ + + @Parameter(required = true) + private String url; + + @Parameter(required = true) + private String user; + + @Parameter(required = true) + private String password; + + @Parameter + private boolean failOnErrors; + + @Parameter + private boolean colorConsole; + + @Parameter + private List reporters; + + // Configuration of Sources, Testing + + @Parameter + private List sources = new ArrayList(); + + @Parameter + private List tests = new ArrayList(); + + /** + * + * Execute the plugin + * + */ + @Override + public void execute() throws MojoExecutionException + { + List testPaths = Arrays.asList(new String[] { "tests" }); + + FileMapperOptions sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource()); + FileMapperOptions testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest()); + + Connection connection = null; + List reporterList = null; + + try + { + connection = DriverManager.getConnection(url, user, password); + + // Init Reporters + reporterList = initReporters(connection); + + TestRunner runner = new TestRunner() + .addPathList(testPaths) + .addReporterList(reporterList) + .sourceMappingOptions(sourceMappingOptions) + .testMappingOptions(testMappingOptions) + .colorConsole(colorConsole) + .failOnErrors(failOnErrors); + + runner.run(connection); + + } + catch (Exception e) + { + getLog().error(e); + throw new MojoExecutionException("Unexpected error executing plugin " + e.getMessage()); + } + finally + { + try + { + if (connection != null) + connection.close(); + } + catch (SQLException e) + { + getLog().error("Error", e); + } + } + } + + /** + * + * @param resources + * @return + */ + private FileMapperOptions buildOptions(List resources, Resource defaultResource) + { + // Check if this element is empty + if (resources.isEmpty()) + { + resources.add(defaultResource); + } + + List scripts = SQLScannerHelper.findSQLs(resources); + return new FileMapperOptions(scripts); + } + + /** + * Init all the reports + * + * @param connection + * @return + * @throws SQLException + */ + private List initReporters(Connection connection) throws SQLException + { + List reporterList = new ArrayList(); + + for (String reporterId : reporters) + { + Reporter reporter = ReporterFactory.createReporter(reporterId); + reporter.init(connection); + + reporterList.add(reporter); + } + + return reporterList; + } } \ No newline at end of file diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java new file mode 100644 index 0000000..7dc1d60 --- /dev/null +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java @@ -0,0 +1,60 @@ +package org.utplsql.helper; + +import java.util.Arrays; + +import org.apache.maven.model.Resource; + +/** + * + * @author Alberto Hernández + * + */ +public class PluginDefault +{ + + // Source Directory + private static final String SOURCE_DIRECTORY = "src/main/plsql"; + + // Test Directory + private static final String TEST_DIRECTORY = "src/test/plsql"; + + // Default Extension for Package HEAD + private static final String PACKAGE_HEAD = "**/*.pkg"; + + // Default Extension for Package HEAD + private static final String PACKAGE_BODY = "**/*.pkb"; + + /** + * + * @return + */ + private static Resource buildDirectory(String directory) + { + Resource resource = new Resource(); + + // Configure Resources + resource.setDirectory(directory); + resource.setIncludes(Arrays.asList(PACKAGE_BODY, PACKAGE_HEAD)); + + return resource; + } + + /** + * + * @return + */ + public static Resource buildDefaultSource() + { + return buildDirectory(SOURCE_DIRECTORY); + } + + /** + * + * @return + */ + public static Resource buildDefaultTest() + { + return buildDirectory(TEST_DIRECTORY); + } + +} diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java new file mode 100644 index 0000000..2a1794c --- /dev/null +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java @@ -0,0 +1,59 @@ +package org.utplsql.helper; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.maven.model.Resource; +import org.codehaus.plexus.util.DirectoryScanner; + +/** + * Utility to Scan all resources + * + * @author Alberto Hernández + * + */ +public class SQLScannerHelper +{ + + /** + * + * @param resouces + * @return + */ + public static List findSQLs(List resources) + { + List founds = new ArrayList(); + + for (Resource resource: resources) + { + // Build Scanner + DirectoryScanner scanner = buildScanner(resource); + scanner.scan(); + + // Append all scanned objects + founds.addAll(Arrays.asList(scanner.getIncludedFiles())); + } + + + return founds; + } + + /** + * Build a scanner in forder to Find all Resource files + * @param resource + * @return + */ + private static DirectoryScanner buildScanner(Resource resource) + { + DirectoryScanner scanner = new DirectoryScanner(); + + scanner.setBasedir(resource.getDirectory()); + scanner.setIncludes(resource.getIncludes().toArray(new String[0])); + scanner.setExcludes(resource.getExcludes().toArray(new String[0])); + + + return scanner; + } + +} diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/tools/FileWalker.java b/utplsql-maven-plugin/src/main/java/org/utplsql/tools/FileWalker.java deleted file mode 100644 index bc57c45..0000000 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/tools/FileWalker.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.utplsql.tools; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by Vinicius on 18/06/2017. - taken from API - */ -public class FileWalker { - - public static List getFileList(File baseDir, String inspectPath) { - return getFileList(baseDir, inspectPath, true); - } - - public static List getFileList(File baseDir, String inspectPath, boolean relative) { - File inspectDir = new File(baseDir, inspectPath); - - if (!inspectDir.isDirectory()) - throw new IllegalArgumentException(inspectPath + " is not a directory."); - - List fileList = new ArrayList<>(); - listDirFiles(baseDir, inspectDir, fileList, relative); - - return fileList; - } - - private static void listDirFiles(File baseDir, File directory, List fileList, boolean relative) { - File[] directoryFiles = directory.listFiles(); - - if (directoryFiles == null) - return; - - for (File file : directoryFiles) { - if (file.isFile()) { - String absolutePath = file.getAbsolutePath(); - - if (relative) - absolutePath = absolutePath.substring(baseDir.getAbsolutePath().length() + 1); - - fileList.add(absolutePath); - } else { - listDirFiles(baseDir, file, fileList, relative); - } - } - } - -} diff --git a/utplsql-maven-plugin/src/test/java/UtPLSQLMojoTest.java b/utplsql-maven-plugin/src/test/java/UtPLSQLMojoTest.java deleted file mode 100644 index e79de39..0000000 --- a/utplsql-maven-plugin/src/test/java/UtPLSQLMojoTest.java +++ /dev/null @@ -1,58 +0,0 @@ -import org.apache.maven.plugin.Mojo; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.assertj.core.api.Assertions; - -import java.io.File; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; - -public class UtPLSQLMojoTest extends AbstractMojoTestCase { - - public static final String POM_PATH = "src/test/resources/pom.xml"; - - @Override - public void setUp() throws Exception { - super.setUp(); - } - - @Override - public void tearDown() throws Exception { - super.tearDown(); - } - - public void test_mojo_definition() throws Exception { - File pom = getTestFile(POM_PATH); - assertThat(pom.exists()).isTrue(); - - Mojo testMojo = lookupMojo("test", pom); - assertThat(testMojo).isNotNull(); - } - - public void test_mapping_suite_paths() throws Exception { - File pom = getTestFile("src/test/resources/pom.xml"); - Mojo testMojo = lookupMojo("test", pom); - - List paths = (List) getVariableValueFromObject(testMojo, "suitePaths"); - - assertThat(paths).containsExactly("suite_path1", "suite_path2"); - } - - public void test_mapping_source_paths() throws Exception { - File pom = getTestFile("src/test/resources/pom.xml"); - Mojo testMojo = lookupMojo("test", pom); - - List paths = (List) getVariableValueFromObject(testMojo, "sourcePaths"); - - assertThat(paths).containsExactly("source_path1", "source_path2", "source_path3"); - } - - public void test_mapping_test_paths() throws Exception { - File pom = getTestFile("src/test/resources/pom.xml"); - Mojo testMojo = lookupMojo("test", pom); - - List paths = (List) getVariableValueFromObject(testMojo, "testPaths"); - - assertThat(paths).containsExactly("test_path1", "test_path2"); - } -} diff --git a/utplsql-maven-plugin/src/test/java/org/utpsql/test/UtPLSQLMojoTest.java b/utplsql-maven-plugin/src/test/java/org/utpsql/test/UtPLSQLMojoTest.java new file mode 100644 index 0000000..eac56cc --- /dev/null +++ b/utplsql-maven-plugin/src/test/java/org/utpsql/test/UtPLSQLMojoTest.java @@ -0,0 +1,35 @@ +package org.utpsql.test; + +import org.apache.maven.plugin.testing.MojoRule; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.utplsql.UtPLSQLMojo; + +public class UtPLSQLMojoTest +{ + public static final String POM_PATH = "src/test/resources/pom.xml"; + + @Rule + public MojoRule rule = new MojoRule() + { + @Override + protected void before() throws Throwable + { + } + + @Override + protected void after() + { + } + }; + + @Test + public void testDefinition() throws Exception + { + UtPLSQLMojo myMojo = (UtPLSQLMojo) rule.lookupMojo("test", POM_PATH); + Assert.assertNotNull(myMojo); + myMojo.execute(); + } + +} diff --git a/utplsql-maven-plugin/src/test/resources/pom.xml b/utplsql-maven-plugin/src/test/resources/pom.xml index 41a1498..5f91cad 100644 --- a/utplsql-maven-plugin/src/test/resources/pom.xml +++ b/utplsql-maven-plugin/src/test/resources/pom.xml @@ -4,7 +4,7 @@ org.utplsql utplsql-maven-plugin-test - 1.0-SNAPSHOT + 1.0.0-SNAPSHOT maven-plugin utplsql-maven-plugin Maven Plugin Test @@ -19,19 +19,46 @@ test - - suite_path1 - suite_path2 - - - source_path1 - source_path2 - source_path3 - - - test_path1 - test_path2 - + + + url_of_connection + user + password + + + false + true + + + UT_COVERAGE_SONAR_REPORTER + UT_SONAR_TEST_REPORTER + + + + + SampleDirectory + + **/*pkg + **/*pkb + + + + SampleDirectory2 + + **/*pkb + + + + + + + src/main/test/test1 + + **/*pkg + + + + From e612ad285639223116f3860e27c1712e7c53fdae Mon Sep 17 00:00:00 2001 From: rostskadat Date: Fri, 26 Jan 2018 17:17:26 +0100 Subject: [PATCH 02/16] Adding report handling --- .../main/java/org/utplsql/UtPLSQLMojo.java | 69 +++++++++++++++---- .../org/utplsql/helper/SQLScannerHelper.java | 11 +-- 2 files changed, 63 insertions(+), 17 deletions(-) diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index bd578e4..3fc3850 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -4,8 +4,8 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; +import java.util.function.Consumer; import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; @@ -14,6 +14,7 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.utplsql.api.FileMapperOptions; +import org.utplsql.api.OutputBuffer; import org.utplsql.api.TestRunner; import org.utplsql.api.reporter.Reporter; import org.utplsql.api.reporter.ReporterFactory; @@ -45,10 +46,13 @@ public class UtPLSQLMojo extends AbstractMojo // Configuration of Sources, Testing @Parameter - private List sources = new ArrayList(); + private List sources = new ArrayList<>(); @Parameter - private List tests = new ArrayList(); + private List tests = new ArrayList<>(); + + @Parameter + private List reports = new ArrayList<>(); /** * @@ -58,10 +62,11 @@ public class UtPLSQLMojo extends AbstractMojo @Override public void execute() throws MojoExecutionException { - List testPaths = Arrays.asList(new String[] { "tests" }); + // List testPaths = Arrays.asList(new String[] { "tests" }); - FileMapperOptions sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource()); - FileMapperOptions testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest()); + FileMapperOptions sourceMappingOptions = buildOptions(sources, + PluginDefault.buildDefaultSource()); + FileMapperOptions testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest()); Connection connection = null; List reporterList = null; @@ -72,10 +77,44 @@ public void execute() throws MojoExecutionException // Init Reporters reporterList = initReporters(connection); + if (getLog().isDebugEnabled()) { + StringBuilder msg = new StringBuilder(); + msg.append("Invoking TestRunner with: ").append('\n'); + msg.append("reporters="); + reporterList.forEach(new Consumer() { + @Override + public void accept(Reporter t) { + try { + msg.append(t.getSQLTypeName()).append(", "); + } catch (Exception e) { + // NA + } + } + + }); + msg.append('\n'); + msg.append("sources="); + sourceMappingOptions.getFilePaths().forEach(new Consumer() { + @Override + public void accept(String t) { + msg.append(t).append(", "); + } + }); + msg.append('\n'); + msg.append("tests="); + testMappingOptions.getFilePaths().forEach(new Consumer() { + @Override + public void accept(String t) { + msg.append(t).append(", "); + } + }); + msg.append('\n'); + getLog().debug(msg.toString()); + } TestRunner runner = new TestRunner() - .addPathList(testPaths) - .addReporterList(reporterList) + // .addPathList(testPaths) + .addReporterList(reporterList) .sourceMappingOptions(sourceMappingOptions) .testMappingOptions(testMappingOptions) .colorConsole(colorConsole) @@ -92,11 +131,15 @@ public void execute() throws MojoExecutionException finally { try - { - if (connection != null) + { + for (Reporter reporter : reporterList) { + new OutputBuffer(reporter).printAvailable(connection, reporter.outputFile()); + } + if (connection != null) { connection.close(); + } } - catch (SQLException e) + catch (Exception e) { getLog().error("Error", e); } @@ -108,7 +151,7 @@ public void execute() throws MojoExecutionException * @param resources * @return */ - private FileMapperOptions buildOptions(List resources, Resource defaultResource) + private FileMapperOptions buildOptions(List resources, Resource defaultResource) { // Check if this element is empty if (resources.isEmpty()) @@ -129,7 +172,7 @@ private FileMapperOptions buildOptions(List resources, Resource defaul */ private List initReporters(Connection connection) throws SQLException { - List reporterList = new ArrayList(); + List reporterList = new ArrayList<>(); for (String reporterId : reporters) { diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java index 2a1794c..1863ac1 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java @@ -1,5 +1,6 @@ package org.utplsql.helper; +import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -23,19 +24,21 @@ public class SQLScannerHelper */ public static List findSQLs(List resources) { - List founds = new ArrayList(); + List founds = new ArrayList<>(); for (Resource resource: resources) { // Build Scanner DirectoryScanner scanner = buildScanner(resource); scanner.scan(); - + for (String basename : scanner.getIncludedFiles()) { + founds.add(new File(scanner.getBasedir(), basename).getAbsolutePath()); + } + // Append all scanned objects - founds.addAll(Arrays.asList(scanner.getIncludedFiles())); + founds.addAll(Arrays.asList()); } - return founds; } From 3cb6beeee909b581b46725850f8ecb12b6b5c200 Mon Sep 17 00:00:00 2001 From: rostskadat Date: Fri, 26 Jan 2018 18:33:36 +0100 Subject: [PATCH 03/16] Adding handling of --- .../main/java/org/utplsql/UtPLSQLMojo.java | 37 +++++++++---------- .../org/utplsql/helper/PluginDefault.java | 14 ++----- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index 3fc3850..4f8af2c 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -18,41 +18,38 @@ import org.utplsql.api.TestRunner; import org.utplsql.api.reporter.Reporter; import org.utplsql.api.reporter.ReporterFactory; -import org.utplsql.helper.PluginDefault; import org.utplsql.helper.SQLScannerHelper; @Mojo(name = "test", defaultPhase = LifecyclePhase.TEST) public class UtPLSQLMojo extends AbstractMojo { - - @Parameter(required = true) + @Parameter(defaultValue = "jdbc:oracle:thin:@localhost:1521:ut3") private String url; - @Parameter(required = true) + @Parameter(defaultValue = "ut3") private String user; - @Parameter(required = true) + @Parameter(defaultValue = "XNtxj8eEgA6X6b6f") private String password; - @Parameter + // This parameter should honor the -Dmaven.test.failure.ignore=false + @Parameter(defaultValue = "false") private boolean failOnErrors; - @Parameter + @Parameter(defaultValue = "false") private boolean colorConsole; - @Parameter + @Parameter(defaultValue = "UT_DOCUMENTATION_REPORTER") private List reporters; - // Configuration of Sources, Testing + @Parameter + private List paths; - @Parameter + @Parameter private List sources = new ArrayList<>(); - @Parameter - private List tests = new ArrayList<>(); - @Parameter - private List reports = new ArrayList<>(); + private List tests = new ArrayList<>(); /** * @@ -64,9 +61,9 @@ public void execute() throws MojoExecutionException { // List testPaths = Arrays.asList(new String[] { "tests" }); - FileMapperOptions sourceMappingOptions = buildOptions(sources, - PluginDefault.buildDefaultSource()); - FileMapperOptions testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest()); + FileMapperOptions sourceMappingOptions = new FileMapperOptions(new ArrayList<>()); + FileMapperOptions testMappingOptions = new FileMapperOptions(new ArrayList<>()); // buildOptions(tests, + // PluginDefault.buildDefaultTest()); Connection connection = null; List reporterList = null; @@ -113,7 +110,7 @@ public void accept(String t) { } TestRunner runner = new TestRunner() - // .addPathList(testPaths) + .addPathList(paths) .addReporterList(reporterList) .sourceMappingOptions(sourceMappingOptions) .testMappingOptions(testMappingOptions) @@ -133,7 +130,7 @@ public void accept(String t) { try { for (Reporter reporter : reporterList) { - new OutputBuffer(reporter).printAvailable(connection, reporter.outputFile()); + new OutputBuffer(reporter).printAvailable(connection, System.out); } if (connection != null) { connection.close(); @@ -160,7 +157,7 @@ private FileMapperOptions buildOptions(List resources, Resource defaul } List scripts = SQLScannerHelper.findSQLs(resources); - return new FileMapperOptions(scripts); + return new FileMapperOptions(scripts); } /** diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java index 7dc1d60..b4a9ce6 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java @@ -18,23 +18,17 @@ public class PluginDefault // Test Directory private static final String TEST_DIRECTORY = "src/test/plsql"; - // Default Extension for Package HEAD - private static final String PACKAGE_HEAD = "**/*.pkg"; - - // Default Extension for Package HEAD - private static final String PACKAGE_BODY = "**/*.pkb"; - /** * * @return */ - private static Resource buildDirectory(String directory) + private static Resource buildDirectory(String directory, String includes) { Resource resource = new Resource(); // Configure Resources resource.setDirectory(directory); - resource.setIncludes(Arrays.asList(PACKAGE_BODY, PACKAGE_HEAD)); + resource.setIncludes(Arrays.asList(includes)); return resource; } @@ -45,7 +39,7 @@ private static Resource buildDirectory(String directory) */ public static Resource buildDefaultSource() { - return buildDirectory(SOURCE_DIRECTORY); + return buildDirectory(SOURCE_DIRECTORY, "**/*.*"); } /** @@ -54,7 +48,7 @@ public static Resource buildDefaultSource() */ public static Resource buildDefaultTest() { - return buildDirectory(TEST_DIRECTORY); + return buildDirectory(TEST_DIRECTORY, "**/*.pkg"); } } From 21937556d76e9519e25203a75487d9780a86488f Mon Sep 17 00:00:00 2001 From: rostskadat Date: Mon, 29 Jan 2018 12:02:36 +0100 Subject: [PATCH 04/16] Exposing more parameters --- .../main/java/org/utplsql/UtPLSQLMojo.java | 309 ++++++++++-------- .../org/utplsql/helper/ReporterDefault.java | 22 ++ .../utplsql/model/ReporterConfiguration.java | 24 ++ .../org/utplsql/model/ReporterParameter.java | 28 ++ 4 files changed, 250 insertions(+), 133 deletions(-) create mode 100644 utplsql-maven-plugin/src/main/java/org/utplsql/helper/ReporterDefault.java create mode 100644 utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterConfiguration.java create mode 100644 utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterParameter.java diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index 4f8af2c..e022d32 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -1,12 +1,21 @@ package org.utplsql; +import static java.lang.String.format; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.function.Consumer; +import org.apache.commons.lang3.StringUtils; import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -16,169 +25,203 @@ import org.utplsql.api.FileMapperOptions; import org.utplsql.api.OutputBuffer; import org.utplsql.api.TestRunner; +import org.utplsql.api.exception.SomeTestsFailedException; import org.utplsql.api.reporter.Reporter; import org.utplsql.api.reporter.ReporterFactory; +import org.utplsql.helper.PluginDefault; import org.utplsql.helper.SQLScannerHelper; +import org.utplsql.model.ReporterConfiguration; +import org.utplsql.model.ReporterParameter; + @Mojo(name = "test", defaultPhase = LifecyclePhase.TEST) -public class UtPLSQLMojo extends AbstractMojo -{ +public class UtPLSQLMojo extends AbstractMojo { @Parameter(defaultValue = "jdbc:oracle:thin:@localhost:1521:ut3") - private String url; + private String url; @Parameter(defaultValue = "ut3") - private String user; + private String user; @Parameter(defaultValue = "XNtxj8eEgA6X6b6f") - private String password; + private String password; + + @Parameter + private String includeObject; + + @Parameter + private String excludeObject; + + @Parameter(defaultValue = "${maven.test.failure.ignore}") + private boolean ignoreFailure; - // This parameter should honor the -Dmaven.test.failure.ignore=false @Parameter(defaultValue = "false") - private boolean failOnErrors; + private boolean colorConsole; @Parameter(defaultValue = "false") - private boolean colorConsole; + private boolean skipCompatibilityCheck; - @Parameter(defaultValue = "UT_DOCUMENTATION_REPORTER") - private List reporters; + @Parameter + private List reporters; + private Map mapReporters = new HashMap<>(); @Parameter private List paths; @Parameter - private List sources = new ArrayList<>(); + private List sources = new ArrayList<>(); @Parameter - private List tests = new ArrayList<>(); - - /** - * - * Execute the plugin - * - */ - @Override - public void execute() throws MojoExecutionException - { - // List testPaths = Arrays.asList(new String[] { "tests" }); - - FileMapperOptions sourceMappingOptions = new FileMapperOptions(new ArrayList<>()); - FileMapperOptions testMappingOptions = new FileMapperOptions(new ArrayList<>()); // buildOptions(tests, - // PluginDefault.buildDefaultTest()); - - Connection connection = null; - List reporterList = null; - - try - { - connection = DriverManager.getConnection(url, user, password); - - // Init Reporters - reporterList = initReporters(connection); + private List tests = new ArrayList<>(); + + @Parameter(defaultValue = "${project.build.directory}", readonly = true) + private String targetDir; + + /** + * + * Execute the plugin + * + */ + @Override + public void execute() throws MojoExecutionException { + FileMapperOptions sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource()); + FileMapperOptions testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest()); + + Connection connection = null; + List reporterList = null; + try { + connection = DriverManager.getConnection(url, user, password); + reporterList = initReporters(connection); + if (getLog().isDebugEnabled()) { - StringBuilder msg = new StringBuilder(); - msg.append("Invoking TestRunner with: ").append('\n'); - msg.append("reporters="); - reporterList.forEach(new Consumer() { - @Override - public void accept(Reporter t) { - try { - msg.append(t.getSQLTypeName()).append(", "); - } catch (Exception e) { - // NA - } - } - - }); - msg.append('\n'); - msg.append("sources="); - sourceMappingOptions.getFilePaths().forEach(new Consumer() { - @Override - public void accept(String t) { - msg.append(t).append(", "); - } - }); - msg.append('\n'); - msg.append("tests="); - testMappingOptions.getFilePaths().forEach(new Consumer() { - @Override - public void accept(String t) { - msg.append(t).append(", "); - } - }); - msg.append('\n'); - getLog().debug(msg.toString()); + dumpParameters(sourceMappingOptions, testMappingOptions, reporterList); } - TestRunner runner = new TestRunner() + TestRunner runner = new TestRunner() .addPathList(paths) .addReporterList(reporterList) - .sourceMappingOptions(sourceMappingOptions) - .testMappingOptions(testMappingOptions) - .colorConsole(colorConsole) - .failOnErrors(failOnErrors); - - runner.run(connection); - - } - catch (Exception e) - { - getLog().error(e); - throw new MojoExecutionException("Unexpected error executing plugin " + e.getMessage()); - } - finally - { - try - { + .sourceMappingOptions(sourceMappingOptions) + .testMappingOptions(testMappingOptions) + // .excludeObject(excludeObject) + .includeObject(includeObject) + .skipCompatibilityCheck(skipCompatibilityCheck) + .colorConsole(colorConsole) + .failOnErrors(!ignoreFailure); + + runner.run(connection); + + } catch (SomeTestsFailedException e) { + getLog().error(e); + throw new MojoExecutionException(e.getMessage()); + } catch (SQLException e) { + getLog().error(e); + throw new MojoExecutionException(e.getMessage(), e); + } finally { + try { for (Reporter reporter : reporterList) { - new OutputBuffer(reporter).printAvailable(connection, System.out); + writeReporter(connection, reporter); } if (connection != null) { - connection.close(); + connection.close(); } - } - catch (Exception e) - { - getLog().error("Error", e); - } - } - } - - /** - * - * @param resources - * @return - */ - private FileMapperOptions buildOptions(List resources, Resource defaultResource) - { - // Check if this element is empty - if (resources.isEmpty()) - { - resources.add(defaultResource); - } - - List scripts = SQLScannerHelper.findSQLs(resources); + } catch (Exception e) { + getLog().error(e.getMessage(), e); + } + } + } + + private void writeReporter(Connection connection, Reporter reporter) throws SQLException, IOException { + String outputFile = mapReporters.get(reporter.getReporterId()).getConfiguration().getOutputFile(); + if (StringUtils.isNotBlank(outputFile)) { + File file = new File(outputFile); + if (!file.isAbsolute()) { + file = new File(targetDir, outputFile); + } + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + try (FileOutputStream fout = new FileOutputStream(file)) { + getLog().info(format("Writing report to %s", file.getAbsolutePath())); + new OutputBuffer(reporter).printAvailable(connection, new PrintStream(fout)); + } + } else { + new OutputBuffer(reporter).printAvailable(connection, System.out); + } + } + + /** + * + * @param resources + * @return + */ + private FileMapperOptions buildOptions(List resources, Resource defaultResource) { + // Check if this element is empty + if (resources.isEmpty()) { + resources.add(defaultResource); + } + + List scripts = SQLScannerHelper.findSQLs(resources); return new FileMapperOptions(scripts); - } - - /** - * Init all the reports - * - * @param connection - * @return - * @throws SQLException - */ - private List initReporters(Connection connection) throws SQLException - { - List reporterList = new ArrayList<>(); - - for (String reporterId : reporters) - { - Reporter reporter = ReporterFactory.createReporter(reporterId); - reporter.init(connection); - - reporterList.add(reporter); - } - - return reporterList; - } + } + + /** + * Init all the reports + * + * @param connection + * @return + * @throws SQLException + */ + private List initReporters(Connection connection) throws SQLException { + List reporterList = new ArrayList<>(); + + for (ReporterParameter reporterParameter : reporters) { + String reporterName = reporterParameter.getId().name(); + Reporter reporter = ReporterFactory.createReporter(reporterName); + reporter.init(connection); + reporterList.add(reporter); + if (reporterParameter.getConfiguration() == null + || StringUtils.isEmpty(reporterParameter.getConfiguration().getOutputFile())) { + reporterParameter + .setConfiguration(new ReporterConfiguration(reporterParameter.getId().getOutputFile())); + } + mapReporters.put(reporter.getReporterId(), reporterParameter); + } + + return reporterList; + } + + private void dumpParameters(FileMapperOptions sourceMappingOptions, FileMapperOptions testMappingOptions, + List reporterList) { + StringBuilder msg = new StringBuilder(); + msg.append("Invoking TestRunner with: ").append('\n'); + msg.append("reporters="); + reporterList.forEach(new Consumer() { + @Override + public void accept(Reporter t) { + try { + msg.append(t.getSQLTypeName()).append(", "); + } catch (Exception e) { + // NA + } + } + + }); + msg.append('\n'); + msg.append("sources="); + sourceMappingOptions.getFilePaths().forEach(new Consumer() { + @Override + public void accept(String t) { + msg.append(t).append(", "); + } + }); + msg.append('\n'); + msg.append("tests="); + testMappingOptions.getFilePaths().forEach(new Consumer() { + @Override + public void accept(String t) { + msg.append(t).append(", "); + } + }); + msg.append('\n'); + getLog().debug(msg.toString()); + } } \ No newline at end of file diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/ReporterDefault.java b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/ReporterDefault.java new file mode 100644 index 0000000..7fcd203 --- /dev/null +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/ReporterDefault.java @@ -0,0 +1,22 @@ +package org.utplsql.helper; + +public enum ReporterDefault { + + UT_DOCUMENTATION_REPORTER(""), + UT_COVERAGE_HTML_REPORTER("utplsql/coverage-html-reporter.html"), + UT_TEAMCITY_REPORTER("utplsql/teamcity-reporter.txt"), + UT_XUNIT_REPORTER("utplsql/xunit-reporter.xml"), + UT_COVERALLS_REPORTER("utplsql/coveralls-reporter.json"), + UT_COVERAGE_SONAR_REPORTER("utplsql/coverage-sonar-reporter.xml"), + UT_SONAR_TEST_REPORTER("utplsql/sonar-test-reporter.xml"); + + private String outputFile; + + private ReporterDefault(String outputFile) { + this.outputFile = outputFile; + } + + public String getOutputFile() { + return outputFile; + } +} diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterConfiguration.java b/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterConfiguration.java new file mode 100644 index 0000000..9bab8fe --- /dev/null +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterConfiguration.java @@ -0,0 +1,24 @@ +package org.utplsql.model; + +public class ReporterConfiguration { + + private String outputFile; + + public ReporterConfiguration() { + super(); + } + + public ReporterConfiguration(String outputFile) { + super(); + this.outputFile = outputFile; + } + + public String getOutputFile() { + return outputFile; + } + + public void setOutputFile(String outputFile) { + this.outputFile = outputFile; + } + +} diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterParameter.java b/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterParameter.java new file mode 100644 index 0000000..8a204de --- /dev/null +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterParameter.java @@ -0,0 +1,28 @@ +package org.utplsql.model; + +import org.utplsql.helper.ReporterDefault; + +public class ReporterParameter { + + private ReporterDefault id; + + private ReporterConfiguration configuration; + + public ReporterDefault getId() { + return id; + } + + public void setId(ReporterDefault id) { + this.id = id; + } + + public ReporterConfiguration getConfiguration() { + return configuration; + } + + public void setConfiguration(ReporterConfiguration configuration) { + this.configuration = configuration; + } + + +} From 632446192d4003f3dcf258b7c36ca9bc7d71d529 Mon Sep 17 00:00:00 2001 From: rostskadat Date: Mon, 29 Jan 2018 15:35:07 +0100 Subject: [PATCH 05/16] Fixed path attribute. Can be null, in which case ut/PLSQL need to scan all the sources for the correct annotation --- .../main/java/org/utplsql/UtPLSQLMojo.java | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index e022d32..03168f2 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -19,6 +19,7 @@ import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -64,7 +65,7 @@ public class UtPLSQLMojo extends AbstractMojo { private List reporters; private Map mapReporters = new HashMap<>(); - @Parameter + @Parameter(defaultValue = "") private List paths; @Parameter @@ -83,8 +84,18 @@ public class UtPLSQLMojo extends AbstractMojo { */ @Override public void execute() throws MojoExecutionException { - FileMapperOptions sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource()); - FileMapperOptions testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest()); + FileMapperOptions sourceMappingOptions = null; + try { + sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource()); + } catch (Exception e) { + throw new MojoExecutionException("Invalid in your pom.xml"); + } + FileMapperOptions testMappingOptions = null; + try { + testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest()); + } catch (Exception e) { + throw new MojoExecutionException("Invalid in your pom.xml"); + } Connection connection = null; List reporterList = null; @@ -101,7 +112,7 @@ public void execute() throws MojoExecutionException { .addReporterList(reporterList) .sourceMappingOptions(sourceMappingOptions) .testMappingOptions(testMappingOptions) - // .excludeObject(excludeObject) + .excludeObject(excludeObject) .includeObject(includeObject) .skipCompatibilityCheck(skipCompatibilityCheck) .colorConsole(colorConsole) @@ -191,37 +202,28 @@ private List initReporters(Connection connection) throws SQLException private void dumpParameters(FileMapperOptions sourceMappingOptions, FileMapperOptions testMappingOptions, List reporterList) { - StringBuilder msg = new StringBuilder(); - msg.append("Invoking TestRunner with: ").append('\n'); - msg.append("reporters="); + Log log = getLog(); + log.debug("Invoking TestRunner with: "); + log.debug("reporters="); reporterList.forEach(new Consumer() { @Override public void accept(Reporter t) { - try { - msg.append(t.getSQLTypeName()).append(", "); - } catch (Exception e) { - // NA - } + log.debug(t.getSelfType()); } - }); - msg.append('\n'); - msg.append("sources="); + log.debug("sources="); sourceMappingOptions.getFilePaths().forEach(new Consumer() { @Override public void accept(String t) { - msg.append(t).append(", "); + log.debug(t); } }); - msg.append('\n'); - msg.append("tests="); + log.debug("tests="); testMappingOptions.getFilePaths().forEach(new Consumer() { @Override public void accept(String t) { - msg.append(t).append(", "); + log.debug(t); } }); - msg.append('\n'); - getLog().debug(msg.toString()); } } \ No newline at end of file From 963e128cfcd2c614d386adef42b566a060f4f3b0 Mon Sep 17 00:00:00 2001 From: rostskadat Date: Tue, 30 Jan 2018 09:55:43 +0100 Subject: [PATCH 06/16] Added some output when writing report --- utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index 03168f2..f03a504 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -151,7 +151,7 @@ private void writeReporter(Connection connection, Reporter reporter) throws SQLE file.getParentFile().mkdirs(); } try (FileOutputStream fout = new FileOutputStream(file)) { - getLog().info(format("Writing report to %s", file.getAbsolutePath())); + getLog().info(format("Writing report %s to %s", reporter.getSelfType(), file.getAbsolutePath())); new OutputBuffer(reporter).printAvailable(connection, new PrintStream(fout)); } } else { From a0c0fd1bde89a11decf8855399063440b8e459e9 Mon Sep 17 00:00:00 2001 From: rostskadat Date: Tue, 30 Jan 2018 11:33:38 +0100 Subject: [PATCH 07/16] Using lambda function for dumping parameters --- .../main/java/org/utplsql/UtPLSQLMojo.java | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index f03a504..01b01a5 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -13,7 +13,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.Consumer; import org.apache.commons.lang3.StringUtils; import org.apache.maven.model.Resource; @@ -205,25 +204,10 @@ private void dumpParameters(FileMapperOptions sourceMappingOptions, FileMapperOp Log log = getLog(); log.debug("Invoking TestRunner with: "); log.debug("reporters="); - reporterList.forEach(new Consumer() { - @Override - public void accept(Reporter t) { - log.debug(t.getSelfType()); - } - }); + reporterList.forEach((Reporter r) -> log.debug(r.getSelfType())); log.debug("sources="); - sourceMappingOptions.getFilePaths().forEach(new Consumer() { - @Override - public void accept(String t) { - log.debug(t); - } - }); + sourceMappingOptions.getFilePaths().forEach(log::debug); log.debug("tests="); - testMappingOptions.getFilePaths().forEach(new Consumer() { - @Override - public void accept(String t) { - log.debug(t); - } - }); + testMappingOptions.getFilePaths().forEach(log::debug); } } \ No newline at end of file From 7f6cf2769a636ee73f02aa6cc14c100b8b2af645 Mon Sep 17 00:00:00 2001 From: rostskadat Date: Tue, 30 Jan 2018 12:17:38 +0100 Subject: [PATCH 08/16] Adding possibility to output to stdout --- utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index 01b01a5..d4d824b 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -141,7 +141,7 @@ public void execute() throws MojoExecutionException { private void writeReporter(Connection connection, Reporter reporter) throws SQLException, IOException { String outputFile = mapReporters.get(reporter.getReporterId()).getConfiguration().getOutputFile(); - if (StringUtils.isNotBlank(outputFile)) { + if (StringUtils.isNotBlank(outputFile) && !StringUtils.equals("-", outputFile)) { File file = new File(outputFile); if (!file.isAbsolute()) { file = new File(targetDir, outputFile); From 1b0fa02014683778ac1e260b17f142e9fce0096f Mon Sep 17 00:00:00 2001 From: rostskadat Date: Tue, 30 Jan 2018 13:07:26 +0100 Subject: [PATCH 09/16] Added some documentation --- .../main/java/org/utplsql/UtPLSQLMojo.java | 7 +- .../org/utplsql/helper/PluginDefault.java | 67 +++++++++---------- .../org/utplsql/helper/ReporterDefault.java | 11 ++- .../org/utplsql/helper/SQLScannerHelper.java | 4 ++ 4 files changed, 52 insertions(+), 37 deletions(-) diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index d4d824b..d9523ed 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -33,7 +33,12 @@ import org.utplsql.model.ReporterConfiguration; import org.utplsql.model.ReporterParameter; - +/** + * This class expose the {@link TestRunner} interface to Maven. + * + * @author Alberto Hernández + * + */ @Mojo(name = "test", defaultPhase = LifecyclePhase.TEST) public class UtPLSQLMojo extends AbstractMojo { @Parameter(defaultValue = "jdbc:oracle:thin:@localhost:1521:ut3") diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java index b4a9ce6..5cbf597 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java @@ -5,50 +5,47 @@ import org.apache.maven.model.Resource; /** + * This class provides methods to retrieve the list of resources in the default and directories. * * @author Alberto Hernández * */ -public class PluginDefault -{ +public class PluginDefault { - // Source Directory - private static final String SOURCE_DIRECTORY = "src/main/plsql"; + // Source Directory + private static final String SOURCE_DIRECTORY = "src/main/plsql"; - // Test Directory - private static final String TEST_DIRECTORY = "src/test/plsql"; + // Test Directory + private static final String TEST_DIRECTORY = "src/test/plsql"; - /** - * - * @return - */ - private static Resource buildDirectory(String directory, String includes) - { - Resource resource = new Resource(); + private PluginDefault() { + // NA + } - // Configure Resources - resource.setDirectory(directory); - resource.setIncludes(Arrays.asList(includes)); - - return resource; - } - - /** - * - * @return - */ - public static Resource buildDefaultSource() - { + /** + * This method returns {@link Resource} for the default {@code source} directory + * + * @return a {@link Resource} + */ + public static Resource buildDefaultSource() { return buildDirectory(SOURCE_DIRECTORY, "**/*.*"); - } - - /** - * - * @return - */ - public static Resource buildDefaultTest() - { + } + + /** + * This method returns {@link Resource} for the default {@code test} directory + * + * @return a {@link Resource} + */ + public static Resource buildDefaultTest() { return buildDirectory(TEST_DIRECTORY, "**/*.pkg"); - } + } + + + private static Resource buildDirectory(String directory, String includes) { + Resource resource = new Resource(); + resource.setDirectory(directory); + resource.setIncludes(Arrays.asList(includes)); + return resource; + } } diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/ReporterDefault.java b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/ReporterDefault.java index 7fcd203..3fe204d 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/ReporterDefault.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/ReporterDefault.java @@ -1,8 +1,17 @@ package org.utplsql.helper; +import org.utplsql.api.reporter.Reporter; + +/** + * This class is an enumeration of all the known reporter in {@code utPLSQL}. It further more defines the default output + * file for each {@link Reporter}. In case the output file is set to {@code -}, it will mean the stdout of the process. + * + * @author Alberto Hernández + * + */ public enum ReporterDefault { - UT_DOCUMENTATION_REPORTER(""), + UT_DOCUMENTATION_REPORTER("-"), UT_COVERAGE_HTML_REPORTER("utplsql/coverage-html-reporter.html"), UT_TEAMCITY_REPORTER("utplsql/teamcity-reporter.txt"), UT_XUNIT_REPORTER("utplsql/xunit-reporter.xml"), diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java index 1863ac1..4ed1386 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java @@ -17,6 +17,10 @@ public class SQLScannerHelper { + private SQLScannerHelper() { + // NA + } + /** * * @param resouces From 3c59e9797763b6c81b504bc5d20142291c5fd5f9 Mon Sep 17 00:00:00 2001 From: rostskadat Date: Tue, 30 Jan 2018 16:08:22 +0100 Subject: [PATCH 10/16] Removed handling of and . It seems to make the coverage fails, since there no good example of sane default value. Removing until there is a use case for it --- .../src/main/java/org/utplsql/UtPLSQLMojo.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index d9523ed..7084e0b 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -115,9 +115,9 @@ public void execute() throws MojoExecutionException { .addPathList(paths) .addReporterList(reporterList) .sourceMappingOptions(sourceMappingOptions) + // .excludeObject(excludeObject) + // .includeObject(includeObject) .testMappingOptions(testMappingOptions) - .excludeObject(excludeObject) - .includeObject(includeObject) .skipCompatibilityCheck(skipCompatibilityCheck) .colorConsole(colorConsole) .failOnErrors(!ignoreFailure); From 16b60d8bf56d1edb98e9e16ff313f3c410f386f6 Mon Sep 17 00:00:00 2001 From: rostskadat Date: Tue, 30 Jan 2018 16:34:30 +0100 Subject: [PATCH 11/16] Log which report logs to the stdout --- utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index 7084e0b..d24ea5e 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -155,10 +155,12 @@ private void writeReporter(Connection connection, Reporter reporter) throws SQLE file.getParentFile().mkdirs(); } try (FileOutputStream fout = new FileOutputStream(file)) { + getLog().info(format("Writing report %s to %s", reporter.getSelfType(), file.getAbsolutePath())); new OutputBuffer(reporter).printAvailable(connection, new PrintStream(fout)); } } else { + getLog().info(format("Reporter %s:", reporter.getSelfType())); new OutputBuffer(reporter).printAvailable(connection, System.out); } } From 37d45913dbcc5fd804926d0f0cd58f4b7aae0a4d Mon Sep 17 00:00:00 2001 From: rostskadat Date: Mon, 5 Feb 2018 11:34:49 +0100 Subject: [PATCH 12/16] Added friendly message for invalid and resources --- .../main/java/org/utplsql/UtPLSQLMojo.java | 4 +- .../org/utplsql/helper/SQLScannerHelper.java | 81 ++++++++++--------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index d24ea5e..e27b6e1 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -92,13 +92,13 @@ public void execute() throws MojoExecutionException { try { sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource()); } catch (Exception e) { - throw new MojoExecutionException("Invalid in your pom.xml"); + throw new MojoExecutionException(format("Invalid in your pom.xml: %s", e.getMessage())); } FileMapperOptions testMappingOptions = null; try { testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest()); } catch (Exception e) { - throw new MojoExecutionException("Invalid in your pom.xml"); + throw new MojoExecutionException(format("Invalid in your pom.xml: %s", e.getMessage())); } Connection connection = null; diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java index 4ed1386..6be7baa 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/SQLScannerHelper.java @@ -1,5 +1,7 @@ package org.utplsql.helper; +import static java.lang.String.format; + import java.io.File; import java.util.ArrayList; import java.util.Arrays; @@ -9,58 +11,61 @@ import org.codehaus.plexus.util.DirectoryScanner; /** - * Utility to Scan all resources + * Utility to Scan all resources * * @author Alberto Hernández * */ -public class SQLScannerHelper -{ - +public class SQLScannerHelper { + private SQLScannerHelper() { // NA } - /** - * - * @param resouces - * @return - */ - public static List findSQLs(List resources) - { + /** + * + * @param resouces + * @return + */ + public static List findSQLs(List resources) { List founds = new ArrayList<>(); - - for (Resource resource: resources) - { - // Build Scanner - DirectoryScanner scanner = buildScanner(resource); - scanner.scan(); + + for (Resource resource : resources) { + // Build Scanner + DirectoryScanner scanner = buildScanner(resource); + scanner.scan(); for (String basename : scanner.getIncludedFiles()) { founds.add(new File(scanner.getBasedir(), basename).getAbsolutePath()); } - // Append all scanned objects + // Append all scanned objects founds.addAll(Arrays.asList()); - } - - return founds; - } + } + + return founds; + } + + /** + * Build a scanner in forder to Find all Resource files + * + * @param resource + * @return + */ + private static DirectoryScanner buildScanner(Resource resource) { + if (resource != null) { + File baseDir = new File(resource.getDirectory()); + if (!baseDir.exists() || !baseDir.isDirectory() || !baseDir.canRead()) { + throw new IllegalArgumentException( + format("Invalid %s in resource. Check your pom.xml", resource.getDirectory())); + } - /** - * Build a scanner in forder to Find all Resource files - * @param resource - * @return - */ - private static DirectoryScanner buildScanner(Resource resource) - { - DirectoryScanner scanner = new DirectoryScanner(); - - scanner.setBasedir(resource.getDirectory()); - scanner.setIncludes(resource.getIncludes().toArray(new String[0])); - scanner.setExcludes(resource.getExcludes().toArray(new String[0])); - - - return scanner; - } + DirectoryScanner scanner = new DirectoryScanner(); + scanner.setBasedir(resource.getDirectory()); + scanner.setIncludes(resource.getIncludes().toArray(new String[0])); + scanner.setExcludes(resource.getExcludes().toArray(new String[0])); + return scanner; + } + throw new IllegalArgumentException(); + } } From d74cbb49491b946d76bbd9fba76e86abf0fbc47d Mon Sep 17 00:00:00 2001 From: Alberto Hernandez Date: Tue, 6 Feb 2018 12:05:11 +0100 Subject: [PATCH 13/16] Update README.md --- README.md | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/README.md b/README.md index b681574..96db034 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,96 @@ # utPLSQL-maven-plugin A maven plugin for running Unit Tests with utPLSQL v3+ + + +### Configuration + +* `url` + * URL of the Connection to the database + * Default: `jdbc:oracle:thin:@localhost:1521:ut3` +* `user` + * Credential of the connection to the database + * Default: `ut3` +* `password` + * Password of the connection to the database + * Default: `XNtxj8eEgA6X6b6f` +* `ignoreFailure` + * Ignore or continue when a test fail + * Default: `${maven.test.failure.ignore}` +* `colorConsole` + * Colors in the console + * `true` | `false` (default: `false`) +* `skipCompatibilityCheck` + * Skip the Compatibility Checks + * `true` | `false` (default: `false`) +* `reporters` + * List of the Reporters + * Enumeration: +* `paths` + * Paths of the resources + * Default: +* `sources` + * Sources of the scripts at the style of the maven resources +* `tests` + * Test fo the scripts at the style of the maven resources +* `targetDir` + * Target dir, this is a readonly property + * Default: `${project.build.directory}` +* `includeObject` + * Include Object +* `excludeObject` + * Exclude Objects + + + +### Example +```xml + + ${pom.groupId} + utplsql-maven-plugin + ${pom.version} + + test + + + + url_of_connection + user + password + + false + true + + + UT_COVERAGE_SONAR_REPORTER + UT_SONAR_TEST_REPORTER + + + + + SampleDirectory + + **/*pkg + **/*pkb + + + + SampleDirectory2 + + **/*pkb + + + + + + + src/main/test/test1 + + **/*pkg + + + + + + +``` + From b214363eaf8a07edfa2cf4004386c3e63d2bc36b Mon Sep 17 00:00:00 2001 From: Alberto Hernandez Date: Tue, 6 Feb 2018 12:47:46 +0100 Subject: [PATCH 14/16] Added Optional parameters to plugin Improve the Readme --- README.md | 6 +- .../main/java/org/utplsql/UtPLSQLMojo.java | 405 ++++++++++-------- 2 files changed, 231 insertions(+), 180 deletions(-) diff --git a/README.md b/README.md index 96db034..daa35be 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ A maven plugin for running Unit Tests with utPLSQL v3+ * `true` | `false` (default: `false`) * `reporters` * List of the Reporters - * Enumeration: + * Enumeration : `UT_DOCUMENTATION_REPORTER`, `UT_COVERAGE_HTML_REPORTER`, `UT_TEAMCITY_REPORTER`, `UT_XUNIT_REPORTER`, `UT_COVERALLS_REPORTER`, `UT_COVERAGE_SONAR_REPORTER`, `UT_SONAR_TEST_REPORTER` + * `paths` * Paths of the resources - * Default: * `sources` * Sources of the scripts at the style of the maven resources * `tests` @@ -57,7 +57,7 @@ A maven plugin for running Unit Tests with utPLSQL v3+ user password - false + false true diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index e27b6e1..162f950 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -40,181 +40,232 @@ * */ @Mojo(name = "test", defaultPhase = LifecyclePhase.TEST) -public class UtPLSQLMojo extends AbstractMojo { - @Parameter(defaultValue = "jdbc:oracle:thin:@localhost:1521:ut3") - private String url; - - @Parameter(defaultValue = "ut3") - private String user; - - @Parameter(defaultValue = "XNtxj8eEgA6X6b6f") - private String password; - - @Parameter - private String includeObject; - - @Parameter - private String excludeObject; - - @Parameter(defaultValue = "${maven.test.failure.ignore}") - private boolean ignoreFailure; - - @Parameter(defaultValue = "false") - private boolean colorConsole; - - @Parameter(defaultValue = "false") - private boolean skipCompatibilityCheck; - - @Parameter - private List reporters; - private Map mapReporters = new HashMap<>(); - - @Parameter(defaultValue = "") - private List paths; - - @Parameter - private List sources = new ArrayList<>(); - - @Parameter - private List tests = new ArrayList<>(); - - @Parameter(defaultValue = "${project.build.directory}", readonly = true) - private String targetDir; - - /** - * - * Execute the plugin - * - */ - @Override - public void execute() throws MojoExecutionException { - FileMapperOptions sourceMappingOptions = null; - try { - sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource()); - } catch (Exception e) { - throw new MojoExecutionException(format("Invalid in your pom.xml: %s", e.getMessage())); - } - FileMapperOptions testMappingOptions = null; - try { - testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest()); - } catch (Exception e) { - throw new MojoExecutionException(format("Invalid in your pom.xml: %s", e.getMessage())); - } - - Connection connection = null; - List reporterList = null; - try { - connection = DriverManager.getConnection(url, user, password); - reporterList = initReporters(connection); - - if (getLog().isDebugEnabled()) { - dumpParameters(sourceMappingOptions, testMappingOptions, reporterList); - } - - TestRunner runner = new TestRunner() - .addPathList(paths) - .addReporterList(reporterList) - .sourceMappingOptions(sourceMappingOptions) - // .excludeObject(excludeObject) - // .includeObject(includeObject) - .testMappingOptions(testMappingOptions) - .skipCompatibilityCheck(skipCompatibilityCheck) - .colorConsole(colorConsole) - .failOnErrors(!ignoreFailure); - - runner.run(connection); - - } catch (SomeTestsFailedException e) { - getLog().error(e); - throw new MojoExecutionException(e.getMessage()); - } catch (SQLException e) { - getLog().error(e); - throw new MojoExecutionException(e.getMessage(), e); - } finally { - try { - for (Reporter reporter : reporterList) { - writeReporter(connection, reporter); - } - if (connection != null) { - connection.close(); - } - } catch (Exception e) { - getLog().error(e.getMessage(), e); - } - } - } - - private void writeReporter(Connection connection, Reporter reporter) throws SQLException, IOException { - String outputFile = mapReporters.get(reporter.getReporterId()).getConfiguration().getOutputFile(); - if (StringUtils.isNotBlank(outputFile) && !StringUtils.equals("-", outputFile)) { - File file = new File(outputFile); - if (!file.isAbsolute()) { - file = new File(targetDir, outputFile); - } - if (!file.getParentFile().exists()) { - file.getParentFile().mkdirs(); - } - try (FileOutputStream fout = new FileOutputStream(file)) { - - getLog().info(format("Writing report %s to %s", reporter.getSelfType(), file.getAbsolutePath())); - new OutputBuffer(reporter).printAvailable(connection, new PrintStream(fout)); - } - } else { - getLog().info(format("Reporter %s:", reporter.getSelfType())); - new OutputBuffer(reporter).printAvailable(connection, System.out); - } - } - - /** - * - * @param resources - * @return - */ - private FileMapperOptions buildOptions(List resources, Resource defaultResource) { - // Check if this element is empty - if (resources.isEmpty()) { - resources.add(defaultResource); - } - - List scripts = SQLScannerHelper.findSQLs(resources); - return new FileMapperOptions(scripts); - } - - /** - * Init all the reports - * - * @param connection - * @return - * @throws SQLException - */ - private List initReporters(Connection connection) throws SQLException { - List reporterList = new ArrayList<>(); - - for (ReporterParameter reporterParameter : reporters) { - String reporterName = reporterParameter.getId().name(); - Reporter reporter = ReporterFactory.createReporter(reporterName); - reporter.init(connection); - reporterList.add(reporter); - if (reporterParameter.getConfiguration() == null - || StringUtils.isEmpty(reporterParameter.getConfiguration().getOutputFile())) { - reporterParameter - .setConfiguration(new ReporterConfiguration(reporterParameter.getId().getOutputFile())); - } - mapReporters.put(reporter.getReporterId(), reporterParameter); - } - - return reporterList; - } - - private void dumpParameters(FileMapperOptions sourceMappingOptions, FileMapperOptions testMappingOptions, - List reporterList) { - Log log = getLog(); - log.debug("Invoking TestRunner with: "); - log.debug("reporters="); - reporterList.forEach((Reporter r) -> log.debug(r.getSelfType())); - log.debug("sources="); - sourceMappingOptions.getFilePaths().forEach(log::debug); - log.debug("tests="); - testMappingOptions.getFilePaths().forEach(log::debug); - } +public class UtPLSQLMojo extends AbstractMojo +{ + @Parameter(defaultValue = "jdbc:oracle:thin:@localhost:1521:ut3") + private String url; + + @Parameter(defaultValue = "ut3") + private String user; + + @Parameter(defaultValue = "XNtxj8eEgA6X6b6f") + private String password; + + @Parameter + private String includeObject; + + @Parameter + private String excludeObject; + + @Parameter(defaultValue = "${maven.test.failure.ignore}") + private boolean ignoreFailure; + + @Parameter(defaultValue = "false") + private boolean colorConsole; + + @Parameter(defaultValue = "false") + private boolean skipCompatibilityCheck; + + @Parameter + private List reporters; + private Map mapReporters = new HashMap<>(); + + @Parameter(defaultValue = "") + private List paths; + + @Parameter + private List sources = new ArrayList<>(); + + @Parameter + private List tests = new ArrayList<>(); + + @Parameter(defaultValue = "${project.build.directory}", readonly = true) + private String targetDir; + + /** + * + * Execute the plugin + * + */ + @Override + public void execute() throws MojoExecutionException + { + FileMapperOptions sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource(), "sources"); + ; + FileMapperOptions testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest(), "test"); + + Connection connection = null; + List reporterList = null; + try + { + connection = DriverManager.getConnection(url, user, password); + reporterList = initReporters(connection); + + if (getLog().isDebugEnabled()) + { + dumpParameters(sourceMappingOptions, testMappingOptions, reporterList); + } + + TestRunner runner = new TestRunner() + .addPathList(paths) + .addReporterList(reporterList) + .sourceMappingOptions(sourceMappingOptions) + .testMappingOptions(testMappingOptions) + .skipCompatibilityCheck(skipCompatibilityCheck) + .colorConsole(colorConsole) + .failOnErrors(!ignoreFailure); + + // Setting Optional Parameters + if (StringUtils.isNotEmpty(excludeObject)) + { + runner.excludeObject(excludeObject); + } + if (StringUtils.isNotEmpty(includeObject)) + { + runner.includeObject(includeObject); + } + + runner.run(connection); + + } + catch (SomeTestsFailedException e) + { + getLog().error(e); + throw new MojoExecutionException(e.getMessage()); + } + catch (SQLException e) + { + getLog().error(e); + throw new MojoExecutionException(e.getMessage(), e); + } + finally + { + try + { + // Write Reporters + for (Reporter reporter : reporterList) + { + writeReporter(connection, reporter); + } + if (connection != null) + { + connection.close(); + } + } + catch (Exception e) + { + getLog().error(e.getMessage(), e); + } + } + } + + /** + * + * @param connection + * @param reporter + * @throws SQLException + * @throws IOException + */ + private void writeReporter(Connection connection, Reporter reporter) throws SQLException, IOException + { + String outputFile = mapReporters.get(reporter.getReporterId()).getConfiguration().getOutputFile(); + if (StringUtils.isNotBlank(outputFile) && !StringUtils.equals("-", outputFile)) + { + File file = new File(outputFile); + if (!file.isAbsolute()) + { + file = new File(targetDir, outputFile); + } + if (!file.getParentFile().exists()) + { + file.getParentFile().mkdirs(); + } + try (FileOutputStream fout = new FileOutputStream(file)) + { + + getLog().info(format("Writing report %s to %s", reporter.getSelfType(), file.getAbsolutePath())); + new OutputBuffer(reporter).printAvailable(connection, new PrintStream(fout)); + } + } + else + { + getLog().info(format("Reporter %s:", reporter.getSelfType())); + new OutputBuffer(reporter).printAvailable(connection, System.out); + } + } + + /** + * + * @param resources + * @return + * @throws MojoExecutionException + */ + private FileMapperOptions buildOptions(List resources, Resource defaultResource, String msg) throws MojoExecutionException + { + try + { + // Check if this element is empty + if (resources.isEmpty()) + { + resources.add(defaultResource); + } + + List scripts = SQLScannerHelper.findSQLs(resources); + return new FileMapperOptions(scripts); + + } + catch (Exception e) + { + throw new MojoExecutionException(format("Invalid <%s> in your pom.xml: %s", msg, e.getMessage())); + + } + } + + /** + * Init all the reports + * + * @param connection + * @return + * @throws SQLException + */ + private List initReporters(Connection connection) throws SQLException + { + List reporterList = new ArrayList<>(); + + for (ReporterParameter reporterParameter : reporters) + { + String reporterName = reporterParameter.getId().name(); + Reporter reporter = ReporterFactory.createReporter(reporterName); + reporter.init(connection); + reporterList.add(reporter); + if (reporterParameter.getConfiguration() == null + || StringUtils.isEmpty(reporterParameter.getConfiguration().getOutputFile())) + { + reporterParameter + .setConfiguration(new ReporterConfiguration(reporterParameter.getId().getOutputFile())); + } + mapReporters.put(reporter.getReporterId(), reporterParameter); + } + + return reporterList; + } + + /** + * + * @param sourceMappingOptions + * @param testMappingOptions + * @param reporterList + */ + private void dumpParameters(FileMapperOptions sourceMappingOptions, FileMapperOptions testMappingOptions, + List reporterList) + { + Log log = getLog(); + log.debug("Invoking TestRunner with: "); + log.debug("reporters="); + reporterList.forEach((Reporter r) -> log.debug(r.getSelfType())); + log.debug("sources="); + sourceMappingOptions.getFilePaths().forEach(log::debug); + log.debug("tests="); + testMappingOptions.getFilePaths().forEach(log::debug); + } } \ No newline at end of file From 6b179c024fa2bd57e5b767b481f8e308a57de533 Mon Sep 17 00:00:00 2001 From: Alberto Hernandez Date: Tue, 6 Feb 2018 12:52:17 +0100 Subject: [PATCH 15/16] Update README.md Improved readability --- README.md | 76 +++++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index daa35be..2e6d400 100644 --- a/README.md +++ b/README.md @@ -44,53 +44,51 @@ A maven plugin for running Unit Tests with utPLSQL v3+ ### Example ```xml - - ${pom.groupId} - utplsql-maven-plugin - ${pom.version} + + ${pom.groupId} + utplsql-maven-plugin + ${pom.version} test - url_of_connection - user - password + url_of_connection + user + password - false - true + false + true - - UT_COVERAGE_SONAR_REPORTER - UT_SONAR_TEST_REPORTER - - - - - SampleDirectory - - **/*pkg - **/*pkb - - - - SampleDirectory2 - - **/*pkb - - - - - - - src/main/test/test1 - - **/*pkg - - - + + UT_COVERAGE_SONAR_REPORTER + UT_SONAR_TEST_REPORTER + + + + SampleDirectory + + **/*pkg + **/*pkb + + + + SampleDirectory2 + + **/*pkb + + + + + + + src/main/test/test1 + + **/*pkg + + + ``` - From bdb8c10c524fd84cadc103fe07e92de6d436787d Mon Sep 17 00:00:00 2001 From: Alberto Hernandez Date: Wed, 7 Feb 2018 18:06:18 +0100 Subject: [PATCH 16/16] * Modified Reporters Configuration - Parametrized Name, OutputFile, Console * Removed default values for connections and use the same as the Java API * Added fully maven test in order to accomplish maven testing - The only drawback is that we have the parameter connection inside the pom test. * Updated the Readme of project --- README.md | 67 ++++----- .../main/java/org/utplsql/UtPLSQLMojo.java | 120 ++++++----------- .../org/utplsql/helper/PluginDefault.java | 112 ++++++++++----- .../utplsql/model/ReporterConfiguration.java | 24 ---- .../org/utplsql/model/ReporterParameter.java | 92 ++++++++++--- .../org/utplsql/reporter/ReporterWriter.java | 127 ++++++++++++++++++ .../java/org/utpsql/test/UtPLSQLMojoTest.java | 44 ++++-- .../src/test/resources/pom.xml | 46 ++++--- .../resources/scripts/sources/PKG_TEST_ME.bdy | 28 ++++ .../resources/scripts/sources/PKG_TEST_ME.spc | 8 ++ .../resources/scripts/sources/TO_TEST_ME.tab | 8 ++ .../scripts/test/TEST_PKG_TEST_ME.bdy | 126 +++++++++++++++++ .../scripts/test/TEST_PKG_TEST_ME.spc | 86 ++++++++++++ 13 files changed, 669 insertions(+), 219 deletions(-) delete mode 100644 utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterConfiguration.java create mode 100644 utplsql-maven-plugin/src/main/java/org/utplsql/reporter/ReporterWriter.java create mode 100644 utplsql-maven-plugin/src/test/resources/scripts/sources/PKG_TEST_ME.bdy create mode 100644 utplsql-maven-plugin/src/test/resources/scripts/sources/PKG_TEST_ME.spc create mode 100644 utplsql-maven-plugin/src/test/resources/scripts/sources/TO_TEST_ME.tab create mode 100644 utplsql-maven-plugin/src/test/resources/scripts/test/TEST_PKG_TEST_ME.bdy create mode 100644 utplsql-maven-plugin/src/test/resources/scripts/test/TEST_PKG_TEST_ME.spc diff --git a/README.md b/README.md index 2e6d400..53d457e 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,36 @@ # utPLSQL-maven-plugin A maven plugin for running Unit Tests with utPLSQL v3+ +### Compatibility -### Configuration +This plugin is compatible with the Java-API 3.0.4. + +### Prerequisites +You have to be a fully utPLSQL environment available compatible with the Java API. + + +### Plugin Parameters * `url` * URL of the Connection to the database - * Default: `jdbc:oracle:thin:@localhost:1521:ut3` + * Default: `${dbURL}` * `user` * Credential of the connection to the database - * Default: `ut3` + * Default: `${dbUser}` * `password` * Password of the connection to the database - * Default: `XNtxj8eEgA6X6b6f` + * Default: `${dbPass}` * `ignoreFailure` * Ignore or continue when a test fail * Default: `${maven.test.failure.ignore}` -* `colorConsole` - * Colors in the console - * `true` | `false` (default: `false`) * `skipCompatibilityCheck` * Skip the Compatibility Checks * `true` | `false` (default: `false`) * `reporters` - * List of the Reporters - * Enumeration : `UT_DOCUMENTATION_REPORTER`, `UT_COVERAGE_HTML_REPORTER`, `UT_TEAMCITY_REPORTER`, `UT_XUNIT_REPORTER`, `UT_COVERALLS_REPORTER`, `UT_COVERAGE_SONAR_REPORTER`, `UT_SONAR_TEST_REPORTER` + * List of the Reporters (Check the example below). + * You can pass the name of the reporter and/or the output file of the reporter and/or if the report is logged to the console + * This is a current list of the names of the reporters (See UtPLSQL documentation in order to check the final list): `UT_DOCUMENTATION_REPORTER`, `UT_COVERAGE_HTML_REPORTER`, `UT_TEAMCITY_REPORTER`, `UT_XUNIT_REPORTER`, `UT_COVERALLS_REPORTER`, `UT_COVERAGE_SONAR_REPORTER`, `UT_SONAR_TEST_REPORTER` + * Check the example below * `paths` * Paths of the resources @@ -42,53 +48,54 @@ A maven plugin for running Unit Tests with utPLSQL v3+ -### Example +### Sample of use +The next snippet is a sample of declaration of the pom ```xml - - ${pom.groupId} + + org.utplsql utplsql-maven-plugin - ${pom.version} + 1.0.0-SNAPSHOT test - url_of_connection user password - false - true - - UT_COVERAGE_SONAR_REPORTER - UT_SONAR_TEST_REPORTER + + UT_COVERAGE_SONAR_REPORTER + utplsql/coverage-sonar-reporter.xml + true + + + UT_SONAR_TEST_REPORTER + utplsql/sonar-test-reporter.xml + false + + + UT_TEAMCITY_REPORTER + - SampleDirectory + src/test/resources/scripts/sources **/*pkg **/*pkb - - - SampleDirectory2 - - **/*pkb - - - src/main/test/test1 + src/test/resources/scripts/test **/*pkg + **/*pkb - - + ``` diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java index 162f950..8ddd083 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java @@ -2,17 +2,11 @@ import static java.lang.String.format; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.PrintStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.maven.model.Resource; @@ -23,15 +17,14 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.utplsql.api.FileMapperOptions; -import org.utplsql.api.OutputBuffer; import org.utplsql.api.TestRunner; import org.utplsql.api.exception.SomeTestsFailedException; import org.utplsql.api.reporter.Reporter; import org.utplsql.api.reporter.ReporterFactory; import org.utplsql.helper.PluginDefault; import org.utplsql.helper.SQLScannerHelper; -import org.utplsql.model.ReporterConfiguration; import org.utplsql.model.ReporterParameter; +import org.utplsql.reporter.ReporterWriter; /** * This class expose the {@link TestRunner} interface to Maven. @@ -42,13 +35,13 @@ @Mojo(name = "test", defaultPhase = LifecyclePhase.TEST) public class UtPLSQLMojo extends AbstractMojo { - @Parameter(defaultValue = "jdbc:oracle:thin:@localhost:1521:ut3") + @Parameter(defaultValue = "${dbUrl}") private String url; - @Parameter(defaultValue = "ut3") + @Parameter(defaultValue = "${dbUser}") private String user; - @Parameter(defaultValue = "XNtxj8eEgA6X6b6f") + @Parameter(defaultValue = "${dbPass}") private String password; @Parameter @@ -57,18 +50,11 @@ public class UtPLSQLMojo extends AbstractMojo @Parameter private String excludeObject; - @Parameter(defaultValue = "${maven.test.failure.ignore}") - private boolean ignoreFailure; - - @Parameter(defaultValue = "false") - private boolean colorConsole; - @Parameter(defaultValue = "false") private boolean skipCompatibilityCheck; @Parameter private List reporters; - private Map mapReporters = new HashMap<>(); @Parameter(defaultValue = "") private List paths; @@ -82,6 +68,15 @@ public class UtPLSQLMojo extends AbstractMojo @Parameter(defaultValue = "${project.build.directory}", readonly = true) private String targetDir; + @Parameter(defaultValue = "${maven.test.failure.ignore}") + private boolean ignoreFailure; + + // Color in the console, loaded by environment variables + private boolean colorConsole = PluginDefault.resolveColor(); + + // Reporter Writer + private ReporterWriter reporterWriter; + /** * * Execute the plugin @@ -90,21 +85,17 @@ public class UtPLSQLMojo extends AbstractMojo @Override public void execute() throws MojoExecutionException { - FileMapperOptions sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource(), "sources"); - ; - FileMapperOptions testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest(), "test"); - Connection connection = null; - List reporterList = null; try { + FileMapperOptions sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource(), "sources"); + FileMapperOptions testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest(), "test"); + + // Create the Connection to the Database connection = DriverManager.getConnection(url, user, password); - reporterList = initReporters(connection); + List reporterList = initReporters(connection); - if (getLog().isDebugEnabled()) - { - dumpParameters(sourceMappingOptions, testMappingOptions, reporterList); - } + logParameters(sourceMappingOptions, testMappingOptions, reporterList); TestRunner runner = new TestRunner() .addPathList(paths) @@ -116,11 +107,11 @@ public void execute() throws MojoExecutionException .failOnErrors(!ignoreFailure); // Setting Optional Parameters - if (StringUtils.isNotEmpty(excludeObject)) + if (StringUtils.isNotBlank(excludeObject)) { runner.excludeObject(excludeObject); } - if (StringUtils.isNotEmpty(includeObject)) + if (StringUtils.isNotBlank(includeObject)) { runner.includeObject(includeObject); } @@ -143,10 +134,8 @@ public void execute() throws MojoExecutionException try { // Write Reporters - for (Reporter reporter : reporterList) - { - writeReporter(connection, reporter); - } + reporterWriter.writeReporters(connection); + if (connection != null) { connection.close(); @@ -159,41 +148,6 @@ public void execute() throws MojoExecutionException } } - /** - * - * @param connection - * @param reporter - * @throws SQLException - * @throws IOException - */ - private void writeReporter(Connection connection, Reporter reporter) throws SQLException, IOException - { - String outputFile = mapReporters.get(reporter.getReporterId()).getConfiguration().getOutputFile(); - if (StringUtils.isNotBlank(outputFile) && !StringUtils.equals("-", outputFile)) - { - File file = new File(outputFile); - if (!file.isAbsolute()) - { - file = new File(targetDir, outputFile); - } - if (!file.getParentFile().exists()) - { - file.getParentFile().mkdirs(); - } - try (FileOutputStream fout = new FileOutputStream(file)) - { - - getLog().info(format("Writing report %s to %s", reporter.getSelfType(), file.getAbsolutePath())); - new OutputBuffer(reporter).printAvailable(connection, new PrintStream(fout)); - } - } - else - { - getLog().info(format("Reporter %s:", reporter.getSelfType())); - new OutputBuffer(reporter).printAvailable(connection, System.out); - } - } - /** * * @param resources @@ -217,12 +171,11 @@ private FileMapperOptions buildOptions(List resources, Resource defaul catch (Exception e) { throw new MojoExecutionException(format("Invalid <%s> in your pom.xml: %s", msg, e.getMessage())); - } } /** - * Init all the reports + * Init all the reporters * * @param connection * @return @@ -232,19 +185,20 @@ private List initReporters(Connection connection) throws SQLException { List reporterList = new ArrayList<>(); + // Initializate Reporters + reporterWriter = new ReporterWriter(targetDir); + for (ReporterParameter reporterParameter : reporters) { - String reporterName = reporterParameter.getId().name(); - Reporter reporter = ReporterFactory.createReporter(reporterName); + Reporter reporter = ReporterFactory.createReporter(reporterParameter.getName()); reporter.init(connection); reporterList.add(reporter); - if (reporterParameter.getConfiguration() == null - || StringUtils.isEmpty(reporterParameter.getConfiguration().getOutputFile())) + + // Only added the reporter if at least one of the output is required + if (StringUtils.isNotBlank(reporterParameter.getFileOutput()) || reporterParameter.isConsoleOutput()) { - reporterParameter - .setConfiguration(new ReporterConfiguration(reporterParameter.getId().getOutputFile())); + reporterWriter.addReporter(reporterParameter, reporter); } - mapReporters.put(reporter.getReporterId(), reporterParameter); } return reporterList; @@ -256,10 +210,18 @@ private List initReporters(Connection connection) throws SQLException * @param testMappingOptions * @param reporterList */ - private void dumpParameters(FileMapperOptions sourceMappingOptions, FileMapperOptions testMappingOptions, + private void logParameters(FileMapperOptions sourceMappingOptions, FileMapperOptions testMappingOptions, List reporterList) { Log log = getLog(); + log.info("Invoking TestRunner with: " + targetDir); + + // Do nothing when the debug is disabled + if (!log.isDebugEnabled()) + { + return; + } + log.debug("Invoking TestRunner with: "); log.debug("reporters="); reporterList.forEach((Reporter r) -> log.debug(r.getSelfType())); diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java index 5cbf597..252e80c 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/helper/PluginDefault.java @@ -1,6 +1,7 @@ package org.utplsql.helper; import java.util.Arrays; +import java.util.Map; import org.apache.maven.model.Resource; @@ -10,42 +11,79 @@ * @author Alberto Hernández * */ -public class PluginDefault { - - // Source Directory - private static final String SOURCE_DIRECTORY = "src/main/plsql"; - - // Test Directory - private static final String TEST_DIRECTORY = "src/test/plsql"; - - private PluginDefault() { - // NA - } - - /** - * This method returns {@link Resource} for the default {@code source} directory - * - * @return a {@link Resource} - */ - public static Resource buildDefaultSource() { - return buildDirectory(SOURCE_DIRECTORY, "**/*.*"); - } - - /** - * This method returns {@link Resource} for the default {@code test} directory - * - * @return a {@link Resource} - */ - public static Resource buildDefaultTest() { - return buildDirectory(TEST_DIRECTORY, "**/*.pkg"); - } - - - private static Resource buildDirectory(String directory, String includes) { - Resource resource = new Resource(); - resource.setDirectory(directory); - resource.setIncludes(Arrays.asList(includes)); - return resource; - } +public class PluginDefault +{ + + private static final String STYLE_COLOR_PROPERTY = "style.color"; + + private static final String BATCH_MODE = "B"; + + private static final String LOG_FILE = "l"; + + // Source Directory + private static final String SOURCE_DIRECTORY = "src/main/plsql"; + + // Test Directory + private static final String TEST_DIRECTORY = "src/test/plsql"; + + private PluginDefault() + { + // NA + } + + /** + * This method returns {@link Resource} for the default {@code source} directory + * + * @return a {@link Resource} + */ + public static Resource buildDefaultSource() + { + return buildDirectory(SOURCE_DIRECTORY, "**/*.*"); + } + + /** + * This method returns {@link Resource} for the default {@code test} directory + * + * @return a {@link Resource} + */ + public static Resource buildDefaultTest() + { + return buildDirectory(TEST_DIRECTORY, "**/*.pkg"); + } + + private static Resource buildDirectory(String directory, String includes) + { + Resource resource = new Resource(); + resource.setDirectory(directory); + resource.setIncludes(Arrays.asList(includes)); + return resource; + } + + /** + * + * @return + */ + public static boolean resolveColor() + { + final Map env = System.getenv(); + String color = env.get(STYLE_COLOR_PROPERTY); + + if ("always".equals(color)) + { + return true; + } + + if ("never".equals(color)) + { + return false; + } + + if (env.containsKey(BATCH_MODE) || env.containsKey(LOG_FILE)) + { + return false; + } + + return false; + } } diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterConfiguration.java b/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterConfiguration.java deleted file mode 100644 index 9bab8fe..0000000 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterConfiguration.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.utplsql.model; - -public class ReporterConfiguration { - - private String outputFile; - - public ReporterConfiguration() { - super(); - } - - public ReporterConfiguration(String outputFile) { - super(); - this.outputFile = outputFile; - } - - public String getOutputFile() { - return outputFile; - } - - public void setOutputFile(String outputFile) { - this.outputFile = outputFile; - } - -} diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterParameter.java b/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterParameter.java index 8a204de..56ebc60 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterParameter.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/model/ReporterParameter.java @@ -1,28 +1,84 @@ package org.utplsql.model; -import org.utplsql.helper.ReporterDefault; +import org.codehaus.plexus.util.StringUtils; -public class ReporterParameter { - - private ReporterDefault id; +public class ReporterParameter +{ - private ReporterConfiguration configuration; + // Name of the registered reported in UtPLSQL + private String name; - public ReporterDefault getId() { - return id; - } + // File Output of the reporter + private String fileOutput; - public void setId(ReporterDefault id) { - this.id = id; - } + // Writes the report to console + private boolean consoleOutput; - public ReporterConfiguration getConfiguration() { - return configuration; - } + /** + * + */ + public ReporterParameter() + { + super(); + } - public void setConfiguration(ReporterConfiguration configuration) { - this.configuration = configuration; - } - + /** + * @return the name + */ + public String getName() + { + return name; + } + + /** + * @param name + * the name to set + */ + public void setName(String name) + { + this.name = name; + } + + /** + * @return the fileOutput + */ + public String getFileOutput() + { + return fileOutput; + } + + /** + * @param fileOutput + * the fileOutput to set + */ + public void setFileOutput(String fileOutput) + { + this.fileOutput = fileOutput; + } + + /** + * @return the consoleOutput + */ + public boolean isConsoleOutput() + { + return consoleOutput; + } + + /** + * @param consoleOutput + * the consoleOutput to set + */ + public void setConsoleOutput(boolean consoleOutput) + { + this.consoleOutput = consoleOutput; + } + + /** + * @return the consoleOutput + */ + public boolean isFileOutput() + { + return StringUtils.isNotBlank(fileOutput); + } } diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/reporter/ReporterWriter.java b/utplsql-maven-plugin/src/main/java/org/utplsql/reporter/ReporterWriter.java new file mode 100644 index 0000000..2673480 --- /dev/null +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/reporter/ReporterWriter.java @@ -0,0 +1,127 @@ +package org.utplsql.reporter; + +import static java.lang.String.format; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.sql.Connection; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.tuple.Pair; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.plugin.logging.SystemStreamLog; +import org.utplsql.api.OutputBuffer; +import org.utplsql.api.reporter.Reporter; +import org.utplsql.model.ReporterParameter; + +public class ReporterWriter +{ + private static final Log LOG = new SystemStreamLog(); + + // Reporter Parameters + private List> listReporters; + + // Output Directory + private String outputDirectory; + + /** + * + */ + public ReporterWriter(String outputDirectory) + { + listReporters = new ArrayList<>(); + this.outputDirectory = outputDirectory; + } + + /** + * + * @param connection + * @throws MojoExecutionException + */ + public void writeReporters(Connection connection) throws MojoExecutionException + { + for (Pair pair : listReporters) + { + writeReports(connection, pair.getLeft(), pair.getRight()); + } + } + + /** + * + * @param id + * @throws MojoExecutionException + */ + private void writeReports(Connection connection, Reporter reporter, ReporterParameter reporterParameter) + throws MojoExecutionException + { + List printStreams = new ArrayList<>(); + FileOutputStream fout = null; + + // + try + { + OutputBuffer buffer = new OutputBuffer(reporter); + + if (reporterParameter.isFileOutput()) + { + + File file = new File(reporterParameter.getFileOutput()); + if (!file.isAbsolute()) + { + file = new File(outputDirectory, reporterParameter.getFileOutput()); + } + + if (!file.getParentFile().exists()) + { + LOG.debug("Creating directory for reporter file " + file.getAbsolutePath()); + file.getParentFile().mkdirs(); + } + + fout = new FileOutputStream(file); + LOG.info(format("Writing report %s to %s", reporter.getSelfType(), file.getAbsolutePath())); + + // Added to the Report + printStreams.add(new PrintStream(fout)); + } + + if (reporterParameter.isConsoleOutput()) + { + LOG.info(format("Writing report %s to Console", reporter.getSelfType())); + printStreams.add(System.out); + } + buffer.printAvailable(connection, printStreams); + } + catch (Exception e) + { + throw new MojoExecutionException("Unexpected error opening file ouput ", e); + } + finally + { + if (fout != null) + { + try + { + fout.close(); + } + catch (IOException e) + { + LOG.info(format("Failed to closing the reporting %s", reporterParameter.getClass())); + } + } + } + + } + + /** + * + */ + public void addReporter(ReporterParameter parameter, Reporter reporter) + { + listReporters.add(Pair.of(reporter, parameter)); + } + +} diff --git a/utplsql-maven-plugin/src/test/java/org/utpsql/test/UtPLSQLMojoTest.java b/utplsql-maven-plugin/src/test/java/org/utpsql/test/UtPLSQLMojoTest.java index eac56cc..dc891a2 100644 --- a/utplsql-maven-plugin/src/test/java/org/utpsql/test/UtPLSQLMojoTest.java +++ b/utplsql-maven-plugin/src/test/java/org/utpsql/test/UtPLSQLMojoTest.java @@ -1,5 +1,7 @@ package org.utpsql.test; +import java.io.File; + import org.apache.maven.plugin.testing.MojoRule; import org.junit.Assert; import org.junit.Rule; @@ -8,28 +10,44 @@ public class UtPLSQLMojoTest { - public static final String POM_PATH = "src/test/resources/pom.xml"; + public static final String POM_PATH = "src/test/resources/"; + + public static final String OUTPUT_DIRECTORY = "target/test-classes"; @Rule - public MojoRule rule = new MojoRule() + public MojoRule rule = new MojoRule(); + + @Test + public void testDefinition() throws Exception { - @Override - protected void before() throws Throwable + + try { - } + UtPLSQLMojo myMojo = (UtPLSQLMojo) rule.lookupConfiguredMojo(new File(POM_PATH), "test"); - @Override - protected void after() + Assert.assertNotNull(myMojo); + myMojo.execute(); + + checkCoverReportsGenerated("utplsql/coverage-sonar-reporter.xml", "utplsql/sonar-test-reporter.xml"); + } + catch (Exception e) { + e.printStackTrace(); + Assert.fail("Unexpected Exception running the test of Definition"); } - }; + } - @Test - public void testDefinition() throws Exception + /** + * + * @param files + */ + private void checkCoverReportsGenerated(String... files) { - UtPLSQLMojo myMojo = (UtPLSQLMojo) rule.lookupMojo("test", POM_PATH); - Assert.assertNotNull(myMojo); - myMojo.execute(); + for (String filename : files) + { + File file = new File(OUTPUT_DIRECTORY, filename); + Assert.assertTrue("The reporter for " + filename + " was not generated", file.exists()); + } } } diff --git a/utplsql-maven-plugin/src/test/resources/pom.xml b/utplsql-maven-plugin/src/test/resources/pom.xml index 5f91cad..3eeae3d 100644 --- a/utplsql-maven-plugin/src/test/resources/pom.xml +++ b/utplsql-maven-plugin/src/test/resources/pom.xml @@ -8,8 +8,17 @@ maven-plugin utplsql-maven-plugin Maven Plugin Test - + + + + jdbc:oracle:thin:@180.129.3.101:1521:xe + ut3 + XNtxj8eEgA6X6b6f + + + ../../../target/test-classes + ${pom.groupId} @@ -19,42 +28,43 @@ test - - url_of_connection - user - password - - false - true + false + + + :plsql + - UT_COVERAGE_SONAR_REPORTER - UT_SONAR_TEST_REPORTER + + UT_COVERAGE_SONAR_REPORTER + utplsql/coverage-sonar-reporter.xml + true + + + UT_SONAR_TEST_REPORTER + utplsql/sonar-test-reporter.xml + true + - SampleDirectory + src/test/resources/scripts/sources **/*pkg **/*pkb - - SampleDirectory2 - - **/*pkb - - - src/main/test/test1 + src/test/resources/scripts/test **/*pkg + **/*pkb diff --git a/utplsql-maven-plugin/src/test/resources/scripts/sources/PKG_TEST_ME.bdy b/utplsql-maven-plugin/src/test/resources/scripts/sources/PKG_TEST_ME.bdy new file mode 100644 index 0000000..a2ff185 --- /dev/null +++ b/utplsql-maven-plugin/src/test/resources/scripts/sources/PKG_TEST_ME.bdy @@ -0,0 +1,28 @@ +CREATE OR REPLACE PACKAGE BODY PKG_TEST_ME IS + + -- + -- This + -- + FUNCTION FC_TEST_ME(PPARAM1 IN VARCHAR2) RETURN NUMBER IS + BEGIN + IF PPARAM1 IS NULL THEN + RETURN NULL; + ELSIF PPARAM1 = '1' THEN + RETURN 1; + ELSE + RETURN 0; + END IF; + END FC_TEST_ME; + + PROCEDURE PR_TEST_ME(PSNAME IN VARCHAR2) IS + BEGIN + IF PSNAME IS NULL THEN + NULL; + ELSE + INSERT INTO TO_TEST_ME (SNAME) VALUES (PSNAME); + COMMIT; + END IF; + END PR_TEST_ME; + +END PKG_TEST_ME; +/ \ No newline at end of file diff --git a/utplsql-maven-plugin/src/test/resources/scripts/sources/PKG_TEST_ME.spc b/utplsql-maven-plugin/src/test/resources/scripts/sources/PKG_TEST_ME.spc new file mode 100644 index 0000000..67fe1f8 --- /dev/null +++ b/utplsql-maven-plugin/src/test/resources/scripts/sources/PKG_TEST_ME.spc @@ -0,0 +1,8 @@ +-- +-- This package is used TO demonstrate the utPL/SQL possibilities +-- +CREATE OR REPLACE PACKAGE PKG_TEST_ME AS + FUNCTION FC_TEST_ME(PPARAM1 IN VARCHAR2) RETURN NUMBER; + PROCEDURE PR_TEST_ME(PSNAME IN VARCHAR2); +END PKG_TEST_ME; +/ \ No newline at end of file diff --git a/utplsql-maven-plugin/src/test/resources/scripts/sources/TO_TEST_ME.tab b/utplsql-maven-plugin/src/test/resources/scripts/sources/TO_TEST_ME.tab new file mode 100644 index 0000000..1cd6f12 --- /dev/null +++ b/utplsql-maven-plugin/src/test/resources/scripts/sources/TO_TEST_ME.tab @@ -0,0 +1,8 @@ +-- +-- This is a table used to demonstrate the UNIT test framework. +-- +CREATE TABLE TO_TEST_ME +( + SNAME VARCHAR2(10) +) +/ diff --git a/utplsql-maven-plugin/src/test/resources/scripts/test/TEST_PKG_TEST_ME.bdy b/utplsql-maven-plugin/src/test/resources/scripts/test/TEST_PKG_TEST_ME.bdy new file mode 100644 index 0000000..6f347b6 --- /dev/null +++ b/utplsql-maven-plugin/src/test/resources/scripts/test/TEST_PKG_TEST_ME.bdy @@ -0,0 +1,126 @@ +CREATE OR REPLACE PACKAGE BODY TEST_PKG_TEST_ME AS + + --------------------------------------------------------------------------- + PROCEDURE SETUP_GLOBAL IS + BEGIN + -- Put here the code which is valid for all tests and that should be + -- executed once. + NULL; + END SETUP_GLOBAL; + + --------------------------------------------------------------------------- + PROCEDURE TEARDOWN_GLOBAL IS + BEGIN + -- Put here the code that should be called only once after all the test + -- have executed + NULL; + END TEARDOWN_GLOBAL; + + --------------------------------------------------------------------------- + PROCEDURE SETUP_TEST IS + BEGIN + -- Nothing to clean up globally + NULL; + END SETUP_TEST; + + PROCEDURE TEARDOWN_TEST IS + BEGIN + -- Nothing to clean up globally + NULL; + END TEARDOWN_TEST; + + PROCEDURE TEST_FC_INPUT_1 IS + BEGIN + -- Ok this is a real test where I check that the function return 1 + -- when called with a '1' parameter + UT.EXPECT(PKG_TEST_ME.FC_TEST_ME('1')).TO_EQUAL(1); + END; + + PROCEDURE SETUP_TEST_FC_INPUT_1 IS + BEGIN + -- Nothing to be done really + NULL; + END; + + PROCEDURE TEARDOWN_TEST_FC_INPUT_1 IS + BEGIN + -- Nothing to be done really + NULL; + END; + + PROCEDURE TEST_FC_INPUT_0 IS + BEGIN + -- Ok this is a real test where I check that the function return 0 + -- when called with a '0' parameter + UT.EXPECT(PKG_TEST_ME.FC_TEST_ME('0')).TO_EQUAL(0); + END; + + PROCEDURE TEST_FC_INPUT_NULL IS + BEGIN + -- Ok I check that the function return NULL + -- when called with a NULL parameter + UT.EXPECT(PKG_TEST_ME.FC_TEST_ME(NULL)).TO_BE_NULL; + END TEST_FC_INPUT_NULL; + + PROCEDURE TEST_PR_TEST_ME_NULL IS + VNCOUNT1 PLS_INTEGER; + VNCOUNT2 PLS_INTEGER; + BEGIN + -- In this example I check that the procedure does + -- not insert anything when passing it a NULL parameter + SELECT COUNT(1) INTO VNCOUNT1 FROM TO_TEST_ME; + PKG_TEST_ME.PR_TEST_ME(NULL); + SELECT COUNT(1) INTO VNCOUNT2 FROM TO_TEST_ME; + UT.EXPECT(VNCOUNT1).TO_EQUAL(VNCOUNT2); + END; + + PROCEDURE TEST_PR_TEST_ME_NOT_NULL IS + VNCOUNT1 PLS_INTEGER; + VNCOUNT2 PLS_INTEGER; + VSNAME TO_TEST_ME.SNAME%TYPE; + BEGIN + -- In this test I will check that I do insert a value + -- when the parameter is not null. I futher check that + -- the procedure has inserted the value I specified. + SELECT COUNT(1) INTO VNCOUNT1 FROM TO_TEST_ME; + VSNAME := TO_CHAR(VNCOUNT1); + PKG_TEST_ME.PR_TEST_ME(VSNAME); + SELECT COUNT(1) INTO VNCOUNT2 FROM TO_TEST_ME; + + -- Check that I have inserted the value + UT.EXPECT(VNCOUNT1 + 1).TO_EQUAL(VNCOUNT2); + SELECT COUNT(1) INTO VNCOUNT2 FROM TO_TEST_ME T WHERE T.SNAME = VSNAME; + + -- Check that I inserted the one I said I would insert + UT.EXPECT(VNCOUNT2).TO_EQUAL(1); + DELETE FROM TO_TEST_ME T WHERE T.SNAME = VSNAME; + COMMIT; + END; + + PROCEDURE TEST_PR_TEST_ME_EXISTS IS + BEGIN + -- In case the value exists the procedure should fail with an exception. + BEGIN + PKG_TEST_ME.PR_TEST_ME('EXISTS'); + PKG_TEST_ME.PR_TEST_ME('EXISTS'); + EXCEPTION + WHEN OTHERS THEN + UT.FAIL('Unexpected exception raised'); + END; + END; + + PROCEDURE TEST_PR_TEST_ME_CURSOR IS + TYPE REF_CURSOR IS REF CURSOR; + VEXPECTED REF_CURSOR; + VACTUAL REF_CURSOR; + BEGIN + EXECUTE IMMEDIATE 'TRUNCATE TABLE TO_TEST_ME'; + OPEN VEXPECTED FOR + SELECT T.SNAME FROM TO_TEST_ME T; + OPEN VACTUAL FOR + SELECT T.SNAME FROM TO_TEST_ME T; + UT.EXPECT(VEXPECTED).TO_(EQUAL(VACTUAL)); + END; + +END; +/ diff --git a/utplsql-maven-plugin/src/test/resources/scripts/test/TEST_PKG_TEST_ME.spc b/utplsql-maven-plugin/src/test/resources/scripts/test/TEST_PKG_TEST_ME.spc new file mode 100644 index 0000000..a2b5f6d --- /dev/null +++ b/utplsql-maven-plugin/src/test/resources/scripts/test/TEST_PKG_TEST_ME.spc @@ -0,0 +1,86 @@ +CREATE OR REPLACE PACKAGE TEST_PKG_TEST_ME AS + -- + -- This package shows all the possibilities to unit test + -- your PL/SQL package. NOTE that it is not limited to + -- testing your package. You can do that on all your + -- procedure/functions... + -- + + /** + * This two parameters are used by the test framework in + * order to identify the test suite to run + */ + -- %suite(TEST_PKG_TEST_ME) + -- %suitepath(plsql.examples) + + /* + * This method is invoked once before any other method. + * It should contain all the setup code that is relevant + * for all your test. It might be inserting a register, + * creating a type, etc... + */ + -- %beforeall + PROCEDURE SETUP_GLOBAL; + + /* + * This method is invoked once after all other method. + * It can be used to clean up all the resources that + * you created in your script + */ + -- %afterall + PROCEDURE TEARDOWN_GLOBAL; + + /* + * This method is called once before each test. + */ + -- %beforeeach + PROCEDURE SETUP_TEST; + + /* + * This method is called once after each test. + */ + -- %aftereach + PROCEDURE TEARDOWN_TEST; + + /** + * This is a real test. The main test can declare a setup + * and teardown method in order to setup and cleanup things + * for that specific test. + */ + -- %test + -- %displayname(Checking if function ('1') returns 1) + -- %beforetest(SETUP_TEST_FC_INPUT_1) + -- %aftertest(TEARDOWN_TEST_FC_INPUT_1) + PROCEDURE TEST_FC_INPUT_1; + PROCEDURE SETUP_TEST_FC_INPUT_1; + PROCEDURE TEARDOWN_TEST_FC_INPUT_1; + + -- %test + -- %displayname(Checking if function ('0') returns 0) + PROCEDURE TEST_FC_INPUT_0; + + -- %test + -- %displayname(Checking if function (NULL) returns NULL) + PROCEDURE TEST_FC_INPUT_NULL; + + -- %test + -- %displayname(Checking if procedure (NULL) insert) + PROCEDURE TEST_PR_TEST_ME_NULL; + + -- %test + -- %displayname(Checking if procedure (NOT NULL) insert) + -- %rollback(manual) + PROCEDURE TEST_PR_TEST_ME_NOT_NULL; + + -- %test + -- %displayname(Checking if procedure (NOT NULL) insert while existing) + -- %rollback(manual) + PROCEDURE TEST_PR_TEST_ME_EXISTS; + + -- %test + -- %displayname(Demonstrating the use of cursor) + -- %rollback(manual) + PROCEDURE TEST_PR_TEST_ME_CURSOR; + +END; +/