diff --git a/utplsql-maven-plugin-it/src/it/java/org/utpsql/maven/plugin/test/UtPLSQLMojoIT.java b/utplsql-maven-plugin-it/src/it/java/org/utpsql/maven/plugin/test/UtPLSQLMojoIT.java index 203af8a..af730c8 100644 --- a/utplsql-maven-plugin-it/src/it/java/org/utpsql/maven/plugin/test/UtPLSQLMojoIT.java +++ b/utplsql-maven-plugin-it/src/it/java/org/utpsql/maven/plugin/test/UtPLSQLMojoIT.java @@ -117,7 +117,7 @@ public void testTypeMappingProject() throws Exception { @Test public void testOwnerParameterProject() throws Exception { try { - final String PROJECT_NAME = "type-mapping-project"; + final String PROJECT_NAME = "owner-param-project"; File testProject = ResourceExtractor.simpleExtractResources(getClass(), "/" + PROJECT_NAME); Verifier verifier; @@ -137,6 +137,27 @@ public void testOwnerParameterProject() throws Exception { Assert.fail("Unexpected Exception running the test : " + e.getMessage()); } } + + @Test + public void testMinimalistProject() throws Exception { + try { + final String PROJECT_NAME = "minimalist-project"; + File testProject = ResourceExtractor.simpleExtractResources(getClass(), "/" + PROJECT_NAME); + + Verifier verifier; + verifier = new Verifier(testProject.getAbsolutePath()); + verifier.addCliOption("-N"); + verifier.addCliOption("-Dutplsql-maven-plugin-version=" + pluginVersion); + verifier.addCliOption("-DdbUrl=" + System.getProperty("dbUrl")); + verifier.addCliOption("-DdbUser=" + System.getProperty("dbUser")); + verifier.addCliOption("-DdbPass=" + System.getProperty("dbPass")); + + verifier.executeGoal("test"); + } catch (Exception e) { + e.printStackTrace(); + Assert.fail("Unexpected Exception running the test : " + e.getMessage()); + } + } /** * diff --git a/utplsql-maven-plugin-it/src/it/resources/minimalist-project/pom.xml b/utplsql-maven-plugin-it/src/it/resources/minimalist-project/pom.xml new file mode 100644 index 0000000..d410eff --- /dev/null +++ b/utplsql-maven-plugin-it/src/it/resources/minimalist-project/pom.xml @@ -0,0 +1,33 @@ + + 4.0.0 + + org.utplsql + utplsql-maven-plugin-minimalist-test + 1.0.0 + pom + + utplsql-maven-plugin IT Minimalist + + + + + + + + ${project.groupId} + utplsql-maven-plugin + ${utplsql-maven-plugin-version} + + + + test + + + + + + + + \ No newline at end of file diff --git a/utplsql-maven-plugin-it/src/it/resources/owner-param-project/expected-output/utplsql/coverage-sonar-reporter.xml b/utplsql-maven-plugin-it/src/it/resources/owner-param-project/expected-output/utplsql/coverage-sonar-reporter.xml index ee91996..6718aa5 100644 --- a/utplsql-maven-plugin-it/src/it/resources/owner-param-project/expected-output/utplsql/coverage-sonar-reporter.xml +++ b/utplsql-maven-plugin-it/src/it/resources/owner-param-project/expected-output/utplsql/coverage-sonar-reporter.xml @@ -1,5 +1,5 @@ - + diff --git a/utplsql-maven-plugin-it/src/it/resources/owner-param-project/expected-output/utplsql/sonar-test-reporter.xml b/utplsql-maven-plugin-it/src/it/resources/owner-param-project/expected-output/utplsql/sonar-test-reporter.xml index ac04b30..e558e2f 100644 --- a/utplsql-maven-plugin-it/src/it/resources/owner-param-project/expected-output/utplsql/sonar-test-reporter.xml +++ b/utplsql-maven-plugin-it/src/it/resources/owner-param-project/expected-output/utplsql/sonar-test-reporter.xml @@ -1,5 +1,5 @@ - + diff --git a/utplsql-maven-plugin-it/src/it/resources/owner-param-project/pom.xml b/utplsql-maven-plugin-it/src/it/resources/owner-param-project/pom.xml index abf7f0b..f11ba46 100644 --- a/utplsql-maven-plugin-it/src/it/resources/owner-param-project/pom.xml +++ b/utplsql-maven-plugin-it/src/it/resources/owner-param-project/pom.xml @@ -20,69 +20,73 @@ ${project.groupId} utplsql-maven-plugin ${utplsql-maven-plugin-version} - - test - - - + + + + test + + + - false + false - - tests_owner - + + tests_owner + - - - UT_COVERAGE_SONAR_REPORTER - utplsql/coverage-sonar-reporter.xml - true - - - UT_SONAR_TEST_REPORTER - utplsql/sonar-test-reporter.xml - true - - + + + UT_COVERAGE_SONAR_REPORTER + utplsql/coverage-sonar-reporter.xml + true + + + UT_SONAR_TEST_REPORTER + utplsql/sonar-test-reporter.xml + true + + - - - scripts/sources - - **/*sql - - - - code_owner - .*/\w+/(\w+)/(\w+)\.\w{3} - 2 - 1 - - - package body - package_bodies - - + + + scripts/sources + + **/*sql + + + + code_owner + .*/\w+/(\w+)/(\w+)\.\w{3} + 2 + 1 + + + package body + package_bodies + + - - - scripts/test - - **/*sql - - - - tests_owner - .*/\w+/(\w+)/(\w+)\.\w{3} - 2 - 1 - - - package body - package_bodies - - - + + + scripts/test + + **/*sql + + + + tests_owner + .*/\w+/(\w+)/(\w+)\.\w{3} + 2 + 1 + + + package body + package_bodies + + + + + diff --git a/utplsql-maven-plugin/pom.xml b/utplsql-maven-plugin/pom.xml index 363be2c..14639dd 100644 --- a/utplsql-maven-plugin/pom.xml +++ b/utplsql-maven-plugin/pom.xml @@ -21,6 +21,12 @@ 1.8 + + + org.apache.maven + maven-core + 3.5.2 + org.utplsql @@ -54,8 +60,13 @@ - + + org.powermock + powermock-module-junit4 + 1.7.4 + test + org.apache.maven.plugin-testing @@ -84,8 +95,6 @@ 3.8.0 test - - diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/UtPLSQLMojo.java b/utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/UtPLSQLMojo.java index bd2d6fd..8673638 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/UtPLSQLMojo.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/UtPLSQLMojo.java @@ -1,5 +1,6 @@ package org.utplsql.maven.plugin; +import java.io.File; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; @@ -14,6 +15,7 @@ import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; import org.utplsql.api.DBHelper; import org.utplsql.api.FileMapperOptions; import org.utplsql.api.JavaApiVersionInfo; @@ -35,8 +37,11 @@ * */ @Mojo(name = "test", defaultPhase = LifecyclePhase.TEST) -public class UtPLSQLMojo extends AbstractMojo -{ +public class UtPLSQLMojo extends AbstractMojo { + + @Parameter(readonly = true, defaultValue = "${project}") + private MavenProject project; + @Parameter(property = "dbUrl") protected String url; @@ -45,7 +50,7 @@ public class UtPLSQLMojo extends AbstractMojo @Parameter(property = "dbPass") protected String password; - + @Parameter protected String includeObject; @@ -56,66 +61,61 @@ public class UtPLSQLMojo extends AbstractMojo protected boolean skipCompatibilityCheck; @Parameter - protected List reporters; + protected List reporters = new ArrayList(); - @Parameter(defaultValue = "") - protected List paths; + @Parameter + protected List paths = new ArrayList(); // Sources Configuration @Parameter - protected List sources = new ArrayList<>(); - + protected List sources = new ArrayList(); + @Parameter private String sourcesOwner; - + @Parameter private String sourcesRegexExpression; - + @Parameter private Integer sourcesOwnerSubexpression; - + @Parameter private Integer sourcesNameSubexpression; - + @Parameter private Integer sourcesTypeSubexpression; - + @Parameter private List sourcesCustomTypeMapping; - + // Tests Configuration - + @Parameter protected List tests = new ArrayList<>(); - + @Parameter private String testsOwner; - + @Parameter private String testsRegexExpression; - + @Parameter private Integer testsOwnerSubexpression; - + @Parameter private Integer testsNameSubexpression; - + @Parameter private Integer testsTypeSubexpression; - + @Parameter - private List testsCustomTypeMapping; - + private List testsCustomTypeMapping; @Parameter(defaultValue = "${project.build.directory}", readonly = true) protected String targetDir; @Parameter(defaultValue = "${maven.test.failure.ignore}") protected boolean ignoreFailure; - - - - // Color in the console, loaded by environment variables private boolean colorConsole = PluginDefault.resolveColor(); @@ -129,90 +129,69 @@ public class UtPLSQLMojo extends AbstractMojo * */ @Override - public void execute() throws MojoExecutionException - { - getLog().debug("Java Api Version = "+JavaApiVersionInfo.getVersion()); + public void execute() throws MojoExecutionException { + getLog().debug("Java Api Version = " + JavaApiVersionInfo.getVersion()); loadConfFromEnvironment(); - + Connection connection = null; - try - { + try { FileMapperOptions sourceMappingOptions = buildSourcesOptions(); FileMapperOptions testMappingOptions = buildTestsOptions(); - + // Create the Connection to the Database connection = DriverManager.getConnection(url, user, password); - getLog().info("utPLSQL Version = "+DBHelper.getDatabaseFrameworkVersion(connection)); - + getLog().info("utPLSQL Version = " + DBHelper.getDatabaseFrameworkVersion(connection)); + List reporterList = initReporters(connection); logParameters(sourceMappingOptions, testMappingOptions, reporterList); - TestRunner runner = new TestRunner() - .addPathList(paths) - .addReporterList(reporterList) - .sourceMappingOptions(sourceMappingOptions) - .testMappingOptions(testMappingOptions) - .skipCompatibilityCheck(skipCompatibilityCheck) - .colorConsole(colorConsole) + TestRunner runner = new TestRunner().addPathList(paths).addReporterList(reporterList) + .sourceMappingOptions(sourceMappingOptions).testMappingOptions(testMappingOptions) + .skipCompatibilityCheck(skipCompatibilityCheck).colorConsole(colorConsole) .failOnErrors(!ignoreFailure); // Setting Optional Parameters - if (StringUtils.isNotBlank(excludeObject)) - { + if (StringUtils.isNotBlank(excludeObject)) { runner.excludeObject(excludeObject); } - if (StringUtils.isNotBlank(includeObject)) - { + if (StringUtils.isNotBlank(includeObject)) { runner.includeObject(includeObject); } runner.run(connection); - - } - catch (SomeTestsFailedException e) - { + + } catch (SomeTestsFailedException e) { getLog().error(e); throw new MojoExecutionException(e.getMessage()); - } - catch (SQLException e) - { + } catch (SQLException e) { getLog().error(e); throw new MojoExecutionException(e.getMessage(), e); - } - finally - { - try - { + } finally { + try { // Write Reporters - if (connection != null) + if (connection != null) reporterWriter.writeReporters(connection); - } - catch (Exception e) - { + } catch (Exception e) { getLog().error(e.getMessage(), e); } } } - + /** * Load some configuration from env variables. * */ - private void loadConfFromEnvironment () - { - if (StringUtils.isEmpty(url)) - { - url = System.getProperty("dbUrl"); - } - - if (StringUtils.isEmpty(user)) - { + private void loadConfFromEnvironment() { + if (StringUtils.isEmpty(url)) { + url = System.getProperty("dbUrl"); + } + + if (StringUtils.isEmpty(user)) { user = System.getProperty("dbUser"); } - - if (StringUtils.isEmpty(password)) - { + + if (StringUtils.isEmpty(password)) { password = System.getProperty("dbPass"); } } @@ -223,122 +202,114 @@ private void loadConfFromEnvironment () * @return * @throws MojoExecutionException */ - private FileMapperOptions buildSourcesOptions() throws MojoExecutionException - { - try - { + private FileMapperOptions buildSourcesOptions() throws MojoExecutionException { + try { // Check if this element is empty - if (sources.isEmpty()) - { - sources.add(PluginDefault.buildDefaultSource()); + if (sources.isEmpty()) { + File defaultSourceDirectory = new File(project.getBasedir(),PluginDefault.SOURCE_DIRECTORY); + if (defaultSourceDirectory.exists()) { + sources.add(PluginDefault.buildDefaultSource(project.getBasedir())); + } else { + return new FileMapperOptions(new ArrayList()); + } } - List scripts = SQLScannerHelper.findSQLs(sources); + List scripts = SQLScannerHelper.findSQLs(project.getBasedir(),sources); FileMapperOptions fileMapperOptions = new FileMapperOptions(scripts); - - if (StringUtils.isNotEmpty(sourcesOwner)) - { + + if (StringUtils.isNotEmpty(sourcesOwner)) { fileMapperOptions.setObjectOwner(sourcesOwner); } - - if (StringUtils.isNotEmpty(sourcesRegexExpression)) - { + + if (StringUtils.isNotEmpty(sourcesRegexExpression)) { fileMapperOptions.setRegexPattern(sourcesRegexExpression); } - - if (sourcesOwnerSubexpression != null) - { + + if (sourcesOwnerSubexpression != null) { fileMapperOptions.setOwnerSubExpression(sourcesOwnerSubexpression); } - - if (sourcesNameSubexpression != null) - { + + if (sourcesNameSubexpression != null) { fileMapperOptions.setNameSubExpression(sourcesNameSubexpression); } - - if (sourcesTypeSubexpression != null) - { + + if (sourcesTypeSubexpression != null) { fileMapperOptions.setTypeSubExpression(sourcesTypeSubexpression); } - + if (sourcesCustomTypeMapping != null && !sourcesCustomTypeMapping.isEmpty()) { - fileMapperOptions.setTypeMappings(new ArrayList()); - for (CustomTypeMapping typeMapping : sourcesCustomTypeMapping) { - fileMapperOptions.getTypeMappings().add(new KeyValuePair(typeMapping.getCustomMapping(),typeMapping.getType())); - } + fileMapperOptions.setTypeMappings(new ArrayList()); + for (CustomTypeMapping typeMapping : sourcesCustomTypeMapping) { + fileMapperOptions.getTypeMappings() + .add(new KeyValuePair(typeMapping.getCustomMapping(), typeMapping.getType())); + } } - + return fileMapperOptions; + } catch (Exception e) { + throw new MojoExecutionException("Invalid in your pom.xml",e); } - catch (Exception e) - { - throw new MojoExecutionException("Invalid in your pom.xml: " + e.getMessage()); - } - + } - - + /** * * @param resources * @return * @throws MojoExecutionException */ - private FileMapperOptions buildTestsOptions() throws MojoExecutionException - { - try - { + private FileMapperOptions buildTestsOptions() throws MojoExecutionException { + try { // Check if this element is empty - if (tests.isEmpty()) - { - tests.add(PluginDefault.buildDefaultTest()); - } + if (tests.isEmpty()) { + File defaultTestDirecyory = new File(project.getBasedir(),PluginDefault.TEST_DIRECTORY); + if (defaultTestDirecyory.exists()) { + tests.add(PluginDefault.buildDefaultTest(project.getBasedir())); + } else { + return new FileMapperOptions(new ArrayList()); + } + } - List scripts = SQLScannerHelper.findSQLs(tests); + List scripts = SQLScannerHelper.findSQLs(project.getBasedir(),tests); FileMapperOptions fileMapperOptions = new FileMapperOptions(scripts); - - if (StringUtils.isNotEmpty(testsOwner)) - { + + if (StringUtils.isNotEmpty(testsOwner)) { fileMapperOptions.setObjectOwner(testsOwner); } - - if (StringUtils.isNotEmpty(testsRegexExpression)) - { + + if (StringUtils.isNotEmpty(testsRegexExpression)) { fileMapperOptions.setRegexPattern(testsRegexExpression); } - - if (testsOwnerSubexpression != null) - { + + if (testsOwnerSubexpression != null) { fileMapperOptions.setOwnerSubExpression(testsOwnerSubexpression); } - - if (testsNameSubexpression != null) - { + + if (testsNameSubexpression != null) { fileMapperOptions.setNameSubExpression(testsNameSubexpression); } - - if (testsTypeSubexpression != null) - { + + if (testsTypeSubexpression != null) { fileMapperOptions.setTypeSubExpression(testsTypeSubexpression); } - + if (testsCustomTypeMapping != null && !testsCustomTypeMapping.isEmpty()) { - fileMapperOptions.setTypeMappings(new ArrayList()); - for (CustomTypeMapping typeMapping : testsCustomTypeMapping) { - fileMapperOptions.getTypeMappings().add(new KeyValuePair(typeMapping.getCustomMapping(),typeMapping.getType())); - } - } - + fileMapperOptions.setTypeMappings(new ArrayList()); + for (CustomTypeMapping typeMapping : testsCustomTypeMapping) { + fileMapperOptions.getTypeMappings() + .add(new KeyValuePair(typeMapping.getCustomMapping(), typeMapping.getType())); + } + } + return fileMapperOptions; - } - catch (Exception e) - { + } catch (Exception e) { throw new MojoExecutionException("Invalid in your pom.xml: " + e.getMessage()); } - + } + /** * Init all the reporters * @@ -346,26 +317,23 @@ private FileMapperOptions buildTestsOptions() throws MojoExecutionException * @return * @throws SQLException */ - private List initReporters(Connection connection) throws SQLException - { + private List initReporters(Connection connection) throws SQLException { List reporterList = new ArrayList<>(); - + Version utlVersion = DBHelper.getDatabaseFrameworkVersion(connection); // Initialized Reporters reporterWriter = new ReporterWriter(targetDir, utlVersion); - + ReporterFactory reporterFactory = ReporterFactory.createEmpty(); - for (ReporterParameter reporterParameter : reporters) - { + for (ReporterParameter reporterParameter : reporters) { Reporter reporter = reporterFactory.createReporter(reporterParameter.getName()); reporter.init(connection); reporterList.add(reporter); // Only added the reporter if at least one of the output is required - if (StringUtils.isNotBlank(reporterParameter.getFileOutput()) || reporterParameter.isConsoleOutput()) - { + if (StringUtils.isNotBlank(reporterParameter.getFileOutput()) || reporterParameter.isConsoleOutput()) { reporterWriter.addReporter(reporterParameter, reporter); } } @@ -380,14 +348,12 @@ private List initReporters(Connection connection) throws SQLException * @param reporterList */ private void logParameters(FileMapperOptions sourceMappingOptions, FileMapperOptions testMappingOptions, - List reporterList) - { + List reporterList) { Log log = getLog(); log.info("Invoking TestRunner with: " + targetDir); // Do nothing when the debug is disabled - if (!log.isDebugEnabled()) - { + if (!log.isDebugEnabled()) { return; } diff --git a/utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/helper/PluginDefault.java b/utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/helper/PluginDefault.java index 0969ab8..eb615fe 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/helper/PluginDefault.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/helper/PluginDefault.java @@ -1,89 +1,90 @@ -package org.utplsql.maven.plugin.helper; - -import java.util.Arrays; -import java.util.Map; - -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 -{ - - 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; - } - -} +package org.utplsql.maven.plugin.helper; + +import java.io.File; +import java.util.Arrays; +import java.util.Map; + +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 +{ + + 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 + public static final String SOURCE_DIRECTORY = "src/main/plsql"; + + // Test Directory + public 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(File baseDir) + { + return buildDirectory(baseDir,SOURCE_DIRECTORY, "**/*.*"); + } + + /** + * This method returns {@link Resource} for the default {@code test} directory + * + * @return a {@link Resource} + */ + public static Resource buildDefaultTest(File baseDir) + { + return buildDirectory(baseDir, TEST_DIRECTORY, "**/*.pkg"); + } + + private static Resource buildDirectory(File baseDir, String directory, String includes) + { + Resource resource = new Resource(); + resource.setDirectory(baseDir.getAbsolutePath()+"/"+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/maven/plugin/helper/SQLScannerHelper.java b/utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/helper/SQLScannerHelper.java index 3026ff1..4a177f8 100644 --- a/utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/helper/SQLScannerHelper.java +++ b/utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/helper/SQLScannerHelper.java @@ -1,71 +1,71 @@ -package org.utplsql.maven.plugin.helper; - -import static java.lang.String.format; - -import java.io.File; -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 { - - private SQLScannerHelper() { - // NA - } - - /** - * - * @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 (String basename : scanner.getIncludedFiles()) { - founds.add(new File(scanner.getBasedir(), basename).getPath()); - } - - // Append all scanned objects - founds.addAll(Arrays.asList()); - } - - 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())); - } - - 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(); - } - -} +package org.utplsql.maven.plugin.helper; + +import static java.lang.String.format; + +import java.io.File; +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 { + + private SQLScannerHelper() { + // NA + } + + /** + * + * @param resouces + * @return + */ + public static List findSQLs(File baseDir,List resources) { + List founds = new ArrayList(); + + for (Resource resource : resources) { + // Build Scanner + DirectoryScanner scanner = buildScanner(resource); + scanner.scan(); + for (String basename : scanner.getIncludedFiles()) { + founds.add(baseDir.toURI().relativize(new File(scanner.getBasedir(), basename).toURI()).getPath()); + } + + // Append all scanned objects + founds.addAll(Arrays.asList()); + } + + 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())); + } + + 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(); + } + +} diff --git a/utplsql-maven-plugin/src/test/java/org/utplsql/maven/plugin/test/UtPLSQLMojoTest.java b/utplsql-maven-plugin/src/test/java/org/utplsql/maven/plugin/test/UtPLSQLMojoTest.java new file mode 100644 index 0000000..bb69e06 --- /dev/null +++ b/utplsql-maven-plugin/src/test/java/org/utplsql/maven/plugin/test/UtPLSQLMojoTest.java @@ -0,0 +1,161 @@ +package org.utplsql.maven.plugin.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.List; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.testing.MojoRule; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.powermock.reflect.Whitebox; +import org.utplsql.api.FileMapperOptions; +import org.utplsql.maven.plugin.UtPLSQLMojo; + +public class UtPLSQLMojoTest { + + @Rule + public MojoRule rule = new MojoRule(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + /** + * testInvalidSourcesDirectory. + * + * Given : a pom.xml with invalid sources directory + * When : pom is read and buildSourcesOptions is run + * Then : it should throw a MojoExecutionException + */ + @Test + public void testInvalidSourcesDirectory() throws Exception { + UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule.lookupConfiguredMojo(new File("src/test/resources/invalidTestsSourcesDirectories/"), "test"); + Assert.assertNotNull(utplsqlMojo); + + // Expected exception + thrown.expect(MojoExecutionException.class); + // Excepted message + thrown.expectMessage("Invalid in your pom.xml"); + + Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions"); + + } + + /** + * testInvalidTestsDirectory. + * + * Given : a pom.xml with invalid tests directory + * When : pom is read and buildTestsOptions is run + * Then : it should throw a MojoExecutionException + */ + @Test + public void testInvalidTestsDirectory() throws Exception { + UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule.lookupConfiguredMojo(new File("src/test/resources/invalidTestsSourcesDirectories/"), "test"); + Assert.assertNotNull(utplsqlMojo); + + // Expected exception + thrown.expect(MojoExecutionException.class); + // Excepted message + thrown.expectMessage("Invalid in your pom.xml"); + + Whitebox.invokeMethod(utplsqlMojo, "buildTestsOptions"); + } + + /** + * testSourcesTestsParameters. + * + * Given : a pom.xml with sources and tests with a lot of parameters + * When : pom is read and buildSourcesOptions / buildTestsOptions are run + * Then : it should fill all parameters correctly + */ + @Test + public void testSourcesTestsParameters() throws Exception { + UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule.lookupConfiguredMojo(new File("src/test/resources/testSourcesTestsParams/"), "test"); + Assert.assertNotNull(utplsqlMojo); + + // TODO : move to another test about reporters + List reporters = Whitebox.>getInternalState(utplsqlMojo, "reporters"); + assertEquals(reporters.size(), 2); + + // check sources + FileMapperOptions sources = Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions"); + assertEquals(1, sources.getFilePaths().size()); + assertEquals("srcs/foo.sql", sources.getFilePaths().get(0)); + assertEquals("code_owner", sources.getObjectOwner() ); + assertEquals(".*/\\w+/(\\w+)/(\\w+)\\.\\w{3}", sources.getRegexPattern()); + assertEquals(new Integer(9), sources.getNameSubExpression()); + assertEquals(new Integer(1), sources.getTypeSubExpression()); + assertEquals(new Integer(4), sources.getOwnerSubExpression()); + assertEquals(1, sources.getTypeMappings().size()); + assertEquals("bar", sources.getTypeMappings().get(0).getKey()); + assertEquals("foo", sources.getTypeMappings().get(0).getValue()); + + // check tests + FileMapperOptions tests = Whitebox.invokeMethod(utplsqlMojo, "buildTestsOptions"); + assertEquals(2, tests.getFilePaths().size()); + assertTrue(tests.getFilePaths().contains("te/st/file.bdy")); + assertTrue(tests.getFilePaths().contains("te/st/spec.spc")); + assertEquals("tests_owner", tests.getObjectOwner() ); + assertEquals(".*/\\w+/(\\w+)/(\\w+)\\.\\w{3}", tests.getRegexPattern()); + assertEquals(new Integer(54), tests.getNameSubExpression()); + assertEquals(new Integer(21), tests.getTypeSubExpression()); + assertEquals(new Integer(24), tests.getOwnerSubExpression()); + assertEquals(1, tests.getTypeMappings().size()); + assertEquals("def", tests.getTypeMappings().get(0).getKey()); + assertEquals("abc", tests.getTypeMappings().get(0).getValue()); + + } + + /** + * testSourcesAndTestsParameterDoesNotExist. + * + * Given : a pom.xml with no sources / tests tags and default directory does not exist. + * When : pom is read and buildSourcesOptions / buildTestsOptions are run + * Then : it should not find any source files + */ + @Test + public void testSourcesAndTestsParameterDoesNotExist() throws Exception { + UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule.lookupConfiguredMojo(new File("src/test/resources/testNoSourcesTestsParams/directoryDoesNotExist/"), "test"); + Assert.assertNotNull(utplsqlMojo); + + + // check sources + FileMapperOptions sources = Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions"); + assertEquals(0, sources.getFilePaths().size()); + + + // check tests + FileMapperOptions tests = Whitebox.invokeMethod(utplsqlMojo, "buildTestsOptions"); + assertEquals(0, tests.getFilePaths().size()); + } + + /** + * testSourcesAndTestsParameterDoesNotExistButDefaultDirectoryExists. + * + * Given : a pom.xml with no sources / tests tags but default directory exists. + * When : pom is read and buildSourcesOptions / buildTestsOptions are run + * Then : it should find all sources/tests files in default directories + */ + @Test + public void testSourcesAndTestsParameterDoesNotExistButDefaultDirectoryExists() throws Exception { + UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule.lookupConfiguredMojo(new File("src/test/resources/testNoSourcesTestsParams/directoryExists/"), "test"); + Assert.assertNotNull(utplsqlMojo); + + // check sources + FileMapperOptions sources = Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions"); + assertEquals(2, sources.getFilePaths().size()); + assertTrue(sources.getFilePaths().contains("src/main/plsql/f1.sql")); + assertTrue(sources.getFilePaths().contains("src/main/plsql/f2.sql")); + + // check tests + FileMapperOptions tests = Whitebox.invokeMethod(utplsqlMojo, "buildTestsOptions"); + assertEquals(2, tests.getFilePaths().size()); + assertTrue(tests.getFilePaths().contains("src/test/plsql/foo/f1.pkg")); + assertTrue(tests.getFilePaths().contains("src/test/plsql/f2.pkg")); + + } +} diff --git a/utplsql-maven-plugin/src/test/resources/invalidTestsSourcesDirectories/pom.xml b/utplsql-maven-plugin/src/test/resources/invalidTestsSourcesDirectories/pom.xml new file mode 100644 index 0000000..1c5e6f0 --- /dev/null +++ b/utplsql-maven-plugin/src/test/resources/invalidTestsSourcesDirectories/pom.xml @@ -0,0 +1,78 @@ + + 4.0.0 + + + org.utplsql + utplsql-maven-plugin-test + 3.1.0-SNAPSHOT + + pom + + utplsql-maven-plugin Maven Plugin Test + + http://utplsql.org + + + jdbc:oracle:thin:@180.129.3.101:1521:xe + ut3 + XNtxj8eEgA6X6b6f + + + + + ../../../target/ + + + org.utplsql + utplsql-maven-plugin + {project.version} + + test + + + + + false + + + :plsql + + + + + UT_COVERAGE_SONAR_REPORTER + utplsql/coverage-sonar-reporter.xml + true + + + UT_SONAR_TEST_REPORTER + utplsql/sonar-test-reporter.xml + true + + + + + + foo + + **/*sql + + + + + + + bar + + **/*.spc + **/*.bdy + + + + + + + + \ No newline at end of file diff --git a/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryDoesNotExist/pom.xml b/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryDoesNotExist/pom.xml new file mode 100644 index 0000000..6606b00 --- /dev/null +++ b/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryDoesNotExist/pom.xml @@ -0,0 +1,62 @@ + + 4.0.0 + + + org.utplsql + utplsql-maven-plugin-test + 3.1.0-SNAPSHOT + + pom + + utplsql-maven-plugin Maven Plugin Test + + http://utplsql.org + + + jdbc:oracle:thin:@180.129.3.101:1521:xe + ut3 + XNtxj8eEgA6X6b6f + + + + + ../../../target/ + + + org.utplsql + utplsql-maven-plugin + {project.version} + + test + + + + + false + + + :plsql + + + + + UT_COVERAGE_SONAR_REPORTER + utplsql/coverage-sonar-reporter.xml + true + + + UT_SONAR_TEST_REPORTER + utplsql/sonar-test-reporter.xml + true + + + + + + + + + + \ No newline at end of file diff --git a/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/pom.xml b/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/pom.xml new file mode 100644 index 0000000..7f38f37 --- /dev/null +++ b/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/pom.xml @@ -0,0 +1,62 @@ + + 4.0.0 + + + org.utplsql + utplsql-maven-plugin-test + 3.1.0-SNAPSHOT + + pom + + utplsql-maven-plugin Maven Plugin Test + + http://utplsql.org + + + jdbc:oracle:thin:@180.129.3.101:1521:xe + ut3 + XNtxj8eEgA6X6b6f + / + + + + + + + org.utplsql + utplsql-maven-plugin + {project.version} + + test + + + + + false + + + :plsql + + + + + UT_COVERAGE_SONAR_REPORTER + utplsql/coverage-sonar-reporter.xml + true + + + UT_SONAR_TEST_REPORTER + utplsql/sonar-test-reporter.xml + true + + + + + + + + + + \ No newline at end of file diff --git a/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/src/main/plsql/f1.sql b/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/src/main/plsql/f1.sql new file mode 100644 index 0000000..e69de29 diff --git a/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/src/main/plsql/f2.sql b/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/src/main/plsql/f2.sql new file mode 100644 index 0000000..e69de29 diff --git a/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/src/test/plsql/f2.pkg b/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/src/test/plsql/f2.pkg new file mode 100644 index 0000000..e69de29 diff --git a/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/src/test/plsql/foo/f1.pkg b/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/src/test/plsql/foo/f1.pkg new file mode 100644 index 0000000..e69de29 diff --git a/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/src/test/plsql/foo/f1.sql b/utplsql-maven-plugin/src/test/resources/testNoSourcesTestsParams/directoryExists/src/test/plsql/foo/f1.sql new file mode 100644 index 0000000..e69de29 diff --git a/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/pom.xml b/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/pom.xml new file mode 100644 index 0000000..69ba53e --- /dev/null +++ b/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/pom.xml @@ -0,0 +1,101 @@ + + 4.0.0 + + + org.utplsql + utplsql-maven-plugin-test + 3.1.0-SNAPSHOT + + pom + + utplsql-maven-plugin Maven Plugin Test + + http://utplsql.org + + + jdbc:oracle:thin:@180.129.3.101:1521:xe + ut3 + XNtxj8eEgA6X6b6f + + + + + ../../../target/ + + + org.utplsql + utplsql-maven-plugin + {project.version} + + test + + + + + false + + + :plsql + + + + + UT_COVERAGE_SONAR_REPORTER + utplsql/coverage-sonar-reporter.xml + true + + + UT_SONAR_TEST_REPORTER + utplsql/sonar-test-reporter.xml + true + + + + + + src/test/resources/testSourcesTestsParams/srcs + + **/*sql + + + + code_owner + .*/\w+/(\w+)/(\w+)\.\w{3} + 9 + 1 + 4 + + + foo + bar + + + + + + src/test/resources/testSourcesTestsParams/te/st + + **/*.spc + **/*.bdy + + + + tests_owner + .*/\w+/(\w+)/(\w+)\.\w{3} + 24 + 54 + 21 + + + abc + def + + + + + + + + \ No newline at end of file diff --git a/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/srcs/file.bdy b/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/srcs/file.bdy new file mode 100644 index 0000000..e69de29 diff --git a/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/srcs/foo.sql b/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/srcs/foo.sql new file mode 100644 index 0000000..e69de29 diff --git a/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/srcs/spec.spc b/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/srcs/spec.spc new file mode 100644 index 0000000..e69de29 diff --git a/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/te/st/file.bdy b/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/te/st/file.bdy new file mode 100644 index 0000000..e69de29 diff --git a/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/te/st/foo.sql b/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/te/st/foo.sql new file mode 100644 index 0000000..e69de29 diff --git a/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/te/st/spec.spc b/utplsql-maven-plugin/src/test/resources/testSourcesTestsParams/te/st/spec.spc new file mode 100644 index 0000000..e69de29