From 0dd57b91abafcfe3219cece378de9bb283a1bd28 Mon Sep 17 00:00:00 2001 From: Vinicius Avellar Date: Tue, 2 Oct 2018 23:27:35 +0100 Subject: [PATCH] #28 Optional reporters tag --- .../org/utplsql/maven/plugin/UtPLSQLMojo.java | 49 ++++++++++--------- .../maven/plugin/test/UtPLSQLMojoTest.java | 25 +++++++++- 2 files changed, 51 insertions(+), 23 deletions(-) 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 3f07121..f0df3d0 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 @@ -24,6 +24,7 @@ import org.utplsql.api.TestRunner; import org.utplsql.api.Version; import org.utplsql.api.exception.SomeTestsFailedException; +import org.utplsql.api.reporter.CoreReporters; import org.utplsql.api.reporter.Reporter; import org.utplsql.api.reporter.ReporterFactory; import org.utplsql.maven.plugin.helper.PluginDefault; @@ -138,21 +139,25 @@ public void execute() throws MojoExecutionException { 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)); - - List reporterList = initReporters(connection); - + + Version utlVersion = DBHelper.getDatabaseFrameworkVersion(connection); + getLog().info("utPLSQL Version = " + utlVersion); + + List reporterList = initReporters(connection, utlVersion, ReporterFactory.createEmpty()); + 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)) { runner.excludeObject(excludeObject); } @@ -170,9 +175,9 @@ public void execute() throws MojoExecutionException { throw new MojoExecutionException(e.getMessage(), e); } finally { try { - // Write Reporters - if (connection != null) + if (null != connection) { reporterWriter.writeReporters(connection); + } } catch (Exception e) { getLog().error(e.getMessage(), e); } @@ -205,7 +210,6 @@ private void loadConfFromEnvironment() { */ private FileMapperOptions buildSourcesOptions() throws MojoExecutionException { try { - // Check if this element is empty if (sources.isEmpty()) { File defaultSourceDirectory = new File(project.getBasedir(),PluginDefault.SOURCE_DIRECTORY); if (defaultSourceDirectory.exists()) { @@ -262,7 +266,6 @@ private FileMapperOptions buildSourcesOptions() throws MojoExecutionException { */ private FileMapperOptions buildTestsOptions() throws MojoExecutionException { try { - // Check if this element is empty if (tests.isEmpty()) { File defaultTestDirectory = new File(project.getBasedir(),PluginDefault.TEST_DIRECTORY); if (defaultTestDirectory.exists()) { @@ -318,15 +321,18 @@ private FileMapperOptions buildTestsOptions() throws MojoExecutionException { * @return * @throws SQLException */ - private List initReporters(Connection connection) throws SQLException { - List reporterList = new ArrayList<>(); - - Version utlVersion = DBHelper.getDatabaseFrameworkVersion(connection); + private List initReporters( + Connection connection, Version utlVersion, ReporterFactory reporterFactory) throws SQLException { - // Initialized Reporters + List reporterList = new ArrayList<>(); reporterWriter = new ReporterWriter(targetDir, utlVersion); - ReporterFactory reporterFactory = ReporterFactory.createEmpty(); + if (reporters.isEmpty()) { + ReporterParameter reporterParameter = new ReporterParameter(); + reporterParameter.setConsoleOutput(true); + reporterParameter.setName(CoreReporters.UT_DOCUMENTATION_REPORTER.name()); + reporters.add(reporterParameter); + } for (ReporterParameter reporterParameter : reporters) { Reporter reporter = reporterFactory.createReporter(reporterParameter.getName()); @@ -358,7 +364,6 @@ private void logParameters(FileMapperOptions sourceMappingOptions, FileMapperOpt Log log = getLog(); log.info("Invoking TestRunner with: " + targetDir); - // Do nothing when the debug is disabled if (!log.isDebugEnabled()) { return; } 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 index 6cd6891..be464b9 100644 --- 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 @@ -266,7 +266,7 @@ public void testDefaultConsoleBehaviour() throws Exception { return mockReporter; }); - Whitebox.invokeMethod(utplsqlMojo, "initReporters", mockConnection); + Whitebox.invokeMethod(utplsqlMojo, "initReporters", mockConnection, mockVersion, mockReporterFactory); // Assert that we called the create reporter with the correct parameters. verify(mockReporterFactory, times(2)).createReporter("UT_DOCUMENTATION_REPORTER"); @@ -298,4 +298,27 @@ public void testDefaultConsoleBehaviour() throws Exception { assertTrue(reporterParameter3.isFileOutput()); } + @Test + public void testAddDefaultReporter() throws Exception { + UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule.lookupConfiguredMojo(new File("src/test/resources/defaultConsoleOutputBehaviour/"), "test"); + Assert.assertNotNull(utplsqlMojo); + + List reporterList = new ArrayList<>(); + when(mockReporterFactory.createReporter(anyString())).thenAnswer(invocation -> { + Reporter mockReporter = mock(Reporter.class); + when(mockReporter.getTypeName()).thenReturn(invocation.getArgument(0)); + reporterList.add(mockReporter); + return mockReporter; + }); + + List reporterParameters = Whitebox.getInternalState(utplsqlMojo, "reporters"); + reporterParameters.clear(); + + Whitebox.invokeMethod(utplsqlMojo, "initReporters", mockConnection, mockVersion, mockReporterFactory); + + assertEquals(1, reporterList.size()); + assertEquals("UT_DOCUMENTATION_REPORTER", reporterList.get(0).getTypeName()); + verify(reporterList.get(0)).init(mockConnection); + } + }