-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Create SurefireClassLoaderModifier * Update version on pom.xml and readme
- Loading branch information
1 parent
1dedec7
commit 2e1948f
Showing
12 changed files
with
150 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...pache/maven/plugin/surefire/extensions/junit5/JUnit5StatelessTestsetInfoReporterBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="mailto:fabriciorby@hotmail.com">Fabrício Yamamoto (fabriciorby)</a> | ||
*/ | ||
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() | ||
+ "}"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/org/apache/maven/plugin/surefire/loader/SurefireClassLoaderModifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="mailto:fabriciorby@hotmail.com">Fabrício Yamamoto (fabriciorby)</a> | ||
*/ | ||
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<URL> getThreadContextClassLoaderURLs() { | ||
return Stream.of(((URLClassLoader) Thread.currentThread().getContextClassLoader()).getURLs()); | ||
} | ||
|
||
private boolean isMavenSurefireTreeReporterJar(URL url) { | ||
return url.getFile().contains(MAVEN_SUREFIRE_JUNIT_5_TREE_REPORTER); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters