diff --git a/README.md b/README.md index 96f95a0..56e33b5 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Configure your POM like the following me.fabriciorby maven-surefire-junit5-tree-reporter - 1.0.0 + 1.0.1 @@ -57,7 +57,7 @@ Configure your POM like the following me.fabriciorby maven-surefire-junit5-tree-reporter - 1.0.0 + 1.0.1 diff --git a/pom.xml b/pom.xml index 4ab88d1..ef6867c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.fabriciorby maven-surefire-junit5-tree-reporter - 1.0.0 + 1.0.1 jar maven-surefire-junit5-tree-reporter diff --git a/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoReporterBase.java b/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoReporterBase.java new file mode 100644 index 0000000..5ceed10 --- /dev/null +++ b/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoReporterBase.java @@ -0,0 +1,32 @@ +package org.apache.maven.plugin.surefire.extensions.junit5; + +import org.apache.maven.plugin.surefire.loader.SurefireClassLoaderModifier; + +/** + * Extension of {@link JUnit5StatelessTestsetInfoReporter file and console reporter of test-set} for JUnit5. + * + * @author Fabrício Yamamoto (fabriciorby) + */ +public abstract class JUnit5StatelessTestsetInfoReporterBase extends JUnit5StatelessTestsetInfoReporter { + + @Override + public Object clone(ClassLoader target) { + try { + new SurefireClassLoaderModifier(target).addThisToSurefireClassLoader(); + return super.clone(target); + } catch (ReflectiveOperationException e) { + throw new IllegalStateException(e.getLocalizedMessage()); + } + } + + @Override + public String toString() { + return this.getClass().getSimpleName() + "{" + + "disable=" + isDisable() + + ", usePhrasedFileName=" + isUsePhrasedFileName() + + ", usePhrasedClassNameInRunning=" + isUsePhrasedClassNameInRunning() + + ", usePhrasedClassNameInTestCaseSummary=" + isUsePhrasedClassNameInTestCaseSummary() + + "}"; + } + +} diff --git a/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoTreeReporter.java b/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoTreeReporter.java index d32b1b4..46732e9 100644 --- a/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoTreeReporter.java +++ b/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoTreeReporter.java @@ -30,25 +30,13 @@ * * @author Fabrício Yamamoto (fabriciorby) */ -public class JUnit5StatelessTestsetInfoTreeReporter extends JUnit5StatelessTestsetInfoReporter -{ +public class JUnit5StatelessTestsetInfoTreeReporter extends JUnit5StatelessTestsetInfoReporterBase { @Override public StatelessTestsetInfoConsoleReportEventListener createListener( - ConsoleLogger logger ) - { - return new ConsoleTreeReporter( logger, isUsePhrasedClassNameInRunning(), - isUsePhrasedClassNameInTestCaseSummary() ); + ConsoleLogger logger) { + return new ConsoleTreeReporter(logger, isUsePhrasedClassNameInRunning(), + isUsePhrasedClassNameInTestCaseSummary()); } - @Override - public String toString() - { - return "JUnit5StatelessTestsetInfoTreeReporter{" - + "disable=" + isDisable() - + ", usePhrasedFileName=" + isUsePhrasedFileName() - + ", usePhrasedClassNameInRunning=" + isUsePhrasedClassNameInRunning() - + ", usePhrasedClassNameInTestCaseSummary=" + isUsePhrasedClassNameInTestCaseSummary() - + "}"; - } } diff --git a/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoTreeReporterUnicode.java b/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoTreeReporterUnicode.java index d4695e1..45c80a0 100644 --- a/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoTreeReporterUnicode.java +++ b/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoTreeReporterUnicode.java @@ -20,7 +20,9 @@ */ import org.apache.maven.plugin.surefire.log.api.ConsoleLogger; -import org.apache.maven.plugin.surefire.report.*; +import org.apache.maven.plugin.surefire.report.ConsoleTreeReporterUnicode; +import org.apache.maven.plugin.surefire.report.TestSetStats; +import org.apache.maven.plugin.surefire.report.WrappedReportEntry; import org.apache.maven.surefire.extensions.StatelessTestsetInfoConsoleReportEventListener; /** @@ -28,25 +30,13 @@ * * @author Fabrício Yamamoto (fabriciorby) */ -public class JUnit5StatelessTestsetInfoTreeReporterUnicode extends JUnit5StatelessTestsetInfoTreeReporter -{ +public class JUnit5StatelessTestsetInfoTreeReporterUnicode extends JUnit5StatelessTestsetInfoReporterBase { @Override public StatelessTestsetInfoConsoleReportEventListener createListener( - ConsoleLogger logger ) - { - return new ConsoleTreeReporterUnicode( logger, isUsePhrasedClassNameInRunning(), - isUsePhrasedClassNameInTestCaseSummary() ); + ConsoleLogger logger) { + return new ConsoleTreeReporterUnicode(logger, isUsePhrasedClassNameInRunning(), + isUsePhrasedClassNameInTestCaseSummary()); } - @Override - public String toString() - { - return "JUnit5StatelessTestsetInfoTreeReporterUnicode{" - + "disable=" + isDisable() - + ", usePhrasedFileName=" + isUsePhrasedFileName() - + ", usePhrasedClassNameInRunning=" + isUsePhrasedClassNameInRunning() - + ", usePhrasedClassNameInTestCaseSummary=" + isUsePhrasedClassNameInTestCaseSummary() - + "}"; - } } diff --git a/src/main/java/org/apache/maven/plugin/surefire/loader/SurefireClassLoaderModifier.java b/src/main/java/org/apache/maven/plugin/surefire/loader/SurefireClassLoaderModifier.java new file mode 100644 index 0000000..185cc28 --- /dev/null +++ b/src/main/java/org/apache/maven/plugin/surefire/loader/SurefireClassLoaderModifier.java @@ -0,0 +1,68 @@ +package org.apache.maven.plugin.surefire.loader; + +import org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoReporterBase; + +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.stream.Stream; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Class created to modify the Surefire ClassLoader during Runtime. + * Needed for {@link JUnit5StatelessTestsetInfoReporterBase#clone()} method, + * which is used when the unit tests are running using multiple threads. + * + * @author Fabrício Yamamoto (fabriciorby) + */ +public class SurefireClassLoaderModifier { + + private final ClassLoader surefireClassLoader; + private final Method addUrlMethod; + + public static final String MAVEN_SUREFIRE_JUNIT_5_TREE_REPORTER = "maven-surefire-junit5-tree-reporter"; + public static final String ADD_URL_METHOD = "addURL"; + + public SurefireClassLoaderModifier(ClassLoader surefireClassLoader) throws NoSuchMethodException { + this.surefireClassLoader = surefireClassLoader; + this.addUrlMethod = surefireClassLoader.getClass().getDeclaredMethod(ADD_URL_METHOD, URL.class); + this.addUrlMethod.setAccessible(true); + } + + public void addThisToSurefireClassLoader() throws ReflectiveOperationException { + this.addUrlMethod.invoke(surefireClassLoader, getJarUrl()); + } + + private URL getJarUrl() throws ReflectiveOperationException { + return getThreadContextClassLoaderURLs() + .filter(this::isMavenSurefireTreeReporterJar) + .findFirst() + .orElseThrow(ReflectiveOperationException::new); + } + + private Stream getThreadContextClassLoaderURLs() { + return Stream.of(((URLClassLoader) Thread.currentThread().getContextClassLoader()).getURLs()); + } + + private boolean isMavenSurefireTreeReporterJar(URL url) { + return url.getFile().contains(MAVEN_SUREFIRE_JUNIT_5_TREE_REPORTER); + } +} diff --git a/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporter.java b/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporter.java index d31570a..5a2067a 100644 --- a/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporter.java +++ b/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporter.java @@ -22,7 +22,7 @@ import org.apache.maven.plugin.surefire.log.api.ConsoleLogger; import org.apache.maven.surefire.api.report.TestSetReportEntry; -import java.util.*; +import java.util.List; /** * Tree view class for console reporters. @@ -31,8 +31,8 @@ */ public class ConsoleTreeReporter extends ConsoleReporter { - public ConsoleTreeReporter(ConsoleLogger logger, - boolean usePhrasedClassNameInRunning, boolean usePhrasedClassNameInTestCaseSummary) { + public ConsoleTreeReporter(ConsoleLogger logger, boolean usePhrasedClassNameInRunning, + boolean usePhrasedClassNameInTestCaseSummary) { super(logger, usePhrasedClassNameInRunning, usePhrasedClassNameInTestCaseSummary); } diff --git a/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporterUnicode.java b/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporterUnicode.java index b984634..c12274b 100644 --- a/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporterUnicode.java +++ b/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporterUnicode.java @@ -20,7 +20,6 @@ */ import org.apache.maven.plugin.surefire.log.api.ConsoleLogger; -import org.apache.maven.surefire.api.report.TestSetReportEntry; import java.util.List; @@ -31,8 +30,8 @@ */ public class ConsoleTreeReporterUnicode extends ConsoleTreeReporter { - public ConsoleTreeReporterUnicode(ConsoleLogger logger, - boolean usePhrasedClassNameInRunning, boolean usePhrasedClassNameInTestCaseSummary) { + public ConsoleTreeReporterUnicode(ConsoleLogger logger, boolean usePhrasedClassNameInRunning, + boolean usePhrasedClassNameInTestCaseSummary) { super(logger, usePhrasedClassNameInRunning, usePhrasedClassNameInTestCaseSummary); } diff --git a/src/main/java/org/apache/maven/plugin/surefire/report/TextFormatter.java b/src/main/java/org/apache/maven/plugin/surefire/report/TextFormatter.java index 7a3f384..0c46cef 100644 --- a/src/main/java/org/apache/maven/plugin/surefire/report/TextFormatter.java +++ b/src/main/java/org/apache/maven/plugin/surefire/report/TextFormatter.java @@ -3,6 +3,30 @@ import static org.apache.maven.surefire.shared.lang3.StringUtils.abbreviate; import static org.apache.maven.surefire.shared.lang3.StringUtils.normalizeSpace; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Text formatter to use when print to the console is needed. + * + * @author Fabrício Yamamoto + */ public class TextFormatter { public static final int MAX_WIDTH = 180; diff --git a/src/main/java/org/apache/maven/plugin/surefire/report/Theme.java b/src/main/java/org/apache/maven/plugin/surefire/report/Theme.java index 834fcb6..4e2ef01 100644 --- a/src/main/java/org/apache/maven/plugin/surefire/report/Theme.java +++ b/src/main/java/org/apache/maven/plugin/surefire/report/Theme.java @@ -23,9 +23,10 @@ import java.nio.charset.StandardCharsets; /** - * Tree view printer. - * @see Used reference + * Theme to use when printing using the TreePrinter. + * * @author Fabrício Yamamoto + * @see Used reference */ public enum Theme { diff --git a/src/main/java/org/apache/maven/plugin/surefire/report/TreePrinter.java b/src/main/java/org/apache/maven/plugin/surefire/report/TreePrinter.java index 1fdea0e..b86671a 100644 --- a/src/main/java/org/apache/maven/plugin/surefire/report/TreePrinter.java +++ b/src/main/java/org/apache/maven/plugin/surefire/report/TreePrinter.java @@ -20,7 +20,6 @@ */ import org.apache.maven.plugin.surefire.log.api.ConsoleLogger; -import org.apache.maven.surefire.shared.lang3.StringUtils; import org.apache.maven.surefire.shared.utils.logging.MessageBuilder; import java.util.List; @@ -31,8 +30,6 @@ import static java.util.stream.Collectors.toSet; import static org.apache.maven.plugin.surefire.report.TestSetStats.concatenateWithTestGroup; import static org.apache.maven.plugin.surefire.report.TextFormatter.abbreviateName; -import static org.apache.maven.surefire.shared.lang3.StringUtils.abbreviate; -import static org.apache.maven.surefire.shared.lang3.StringUtils.normalizeSpace; import static org.apache.maven.surefire.shared.utils.StringUtils.isBlank; import static org.apache.maven.surefire.shared.utils.logging.MessageUtils.buffer; @@ -82,9 +79,9 @@ public void printTests() { .map(TestPrinter::new) .forEach(TestPrinter::printTest); } - + private class TestPrinter { - + private final WrappedReportEntry testResult; private final long treeLength; @@ -137,7 +134,7 @@ private void printFailure() { } private void printClass() { - if(!distinctSourceName.contains(testResult.getSourceName())) return; + if (!distinctSourceName.contains(testResult.getSourceName())) return; distinctSourceName.remove(testResult.getSourceName()); MessageBuilder builder = buffer(); @@ -198,7 +195,7 @@ private void println(MessageBuilder builder) { private void println(String message) { consoleLogger.info(message); } - + } } diff --git a/src/test/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporterTest.java b/src/test/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporterTest.java index 9043b75..bdca1c6 100644 --- a/src/test/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporterTest.java +++ b/src/test/java/org/apache/maven/plugin/surefire/report/ConsoleTreeReporterTest.java @@ -9,8 +9,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; -import static org.mockito.Mockito.mock; - @ExtendWith(MockitoExtension.class) class ConsoleTreeReporterTest {