-
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.
- Loading branch information
1 parent
68be2d7
commit 01cb89b
Showing
7 changed files
with
162 additions
and
16 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
src/test/java/DynamicExampleTest.java → ...n/plugin/surefire/DynamicExampleTest.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
2 changes: 2 additions & 0 deletions
2
src/test/java/ExampleTest.java → ...he/maven/plugin/surefire/ExampleTest.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
2 changes: 2 additions & 0 deletions
2
src/test/java/LongNameExampleTest.java → .../plugin/surefire/LongNameExampleTest.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
2 changes: 2 additions & 0 deletions
2
src/test/java/NestedExampleTest.java → ...en/plugin/surefire/NestedExampleTest.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
2 changes: 2 additions & 0 deletions
2
src/test/java/ParameterizedExampleTest.java → ...in/surefire/ParameterizedExampleTest.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
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
126 changes: 126 additions & 0 deletions
126
src/test/java/org/apache/maven/plugin/surefire/report/SurefireEmulator.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,126 @@ | ||
package org.apache.maven.plugin.surefire.report; | ||
|
||
import org.apache.maven.plugin.surefire.log.PluginConsoleLogger; | ||
import org.apache.maven.surefire.api.report.RunMode; | ||
import org.apache.maven.surefire.api.report.SimpleReportEntry; | ||
import org.codehaus.plexus.DefaultPlexusContainer; | ||
import org.codehaus.plexus.PlexusContainerException; | ||
import org.codehaus.plexus.logging.Logger; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.platform.commons.util.StringUtils; | ||
|
||
import java.lang.reflect.AnnotatedElement; | ||
import java.lang.reflect.Method; | ||
import java.util.*; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation; | ||
|
||
public class SurefireEmulator { | ||
|
||
Utf8RecodingDeferredFileOutputStream stdout = new Utf8RecodingDeferredFileOutputStream("stdout"); | ||
Utf8RecodingDeferredFileOutputStream stderr = new Utf8RecodingDeferredFileOutputStream("stderr"); | ||
|
||
static DefaultPlexusContainer container; | ||
static Logger logger; | ||
static DisplayNameGenerator displayNameGenerator; | ||
|
||
static { | ||
try { | ||
container = new DefaultPlexusContainer(); | ||
logger = container.getLogger(); | ||
displayNameGenerator = DisplayNameGenerator.getDisplayNameGenerator(DisplayNameGenerator.Standard.class); | ||
} catch (PlexusContainerException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public <T> void run(Class<T> clazz) { | ||
TestSetStats testSetStats = new TestSetStats(false, true); | ||
getAllMethods(getAllInnerClasses(clazz)) | ||
.entrySet().stream() | ||
.flatMap((k) -> k.getValue().stream() | ||
.map(i -> this.simpleReportEntryGenerator(k.getKey(), i)) | ||
.map(this::wrappedReportEntryGenerator)) | ||
.forEachOrdered(testSetStats::testSucceeded); | ||
|
||
TestSetStats testSetStatsForClass = new TestSetStats(false, true); | ||
ConsoleTreeReporter consoleTreeReporter = new ConsoleTreeReporter(new PluginConsoleLogger(logger), ReporterOptions.builder().build()); | ||
getAllInnerClasses(clazz).stream() | ||
.map(this::simpleReportEntryGenerator) | ||
.forEachOrdered(consoleTreeReporter::testSetStarting); | ||
|
||
List<WrappedReportEntry> completedWrappedEntries = | ||
getAllInnerClasses(clazz).stream() | ||
.map(this::simpleReportEntryGenerator) | ||
.map(this::wrappedReportEntryGenerator) | ||
.collect(toList()); | ||
|
||
completedWrappedEntries.stream() | ||
.findFirst() | ||
.ifPresent(i -> consoleTreeReporter.testSetCompleted(i, testSetStats, null)); | ||
completedWrappedEntries.stream() | ||
.skip(1) | ||
.forEachOrdered(i -> consoleTreeReporter.testSetCompleted(i, testSetStatsForClass, null)); | ||
} | ||
|
||
private <T> SimpleReportEntry simpleReportEntryGenerator(Class<T> clazz) { | ||
return new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, clazz.getName(), getClassDisplayName(clazz), null, null); | ||
} | ||
|
||
private <T> SimpleReportEntry simpleReportEntryGenerator(Class<T> clazz, Method method) { | ||
return new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, clazz.getName(), getClassDisplayName(clazz), method.getName(), getMethodDisplayName(clazz, method)); | ||
} | ||
|
||
private WrappedReportEntry wrappedReportEntryGenerator(SimpleReportEntry simpleReportEntry) { | ||
return new WrappedReportEntry(simpleReportEntry, ReportEntryType.SUCCESS, 1, stdout, stderr); | ||
} | ||
|
||
private List<Class<?>> getAllInnerClasses(Class<?> clazz) { | ||
return getAllInnerClasses(clazz, new ArrayList<>()); | ||
} | ||
|
||
private List<Class<?>> getAllInnerClasses(Class<?> clazz, List<Class<?>> acc) { | ||
if (clazz.getDeclaredClasses().length == 0) { | ||
acc.add(clazz); | ||
return acc; | ||
} | ||
acc.add(clazz); | ||
acc.addAll(Arrays.stream(clazz.getDeclaredClasses()) | ||
.flatMap(i -> getAllInnerClasses(i, new ArrayList<>()).stream()) | ||
.collect(toList())); | ||
return acc; | ||
} | ||
|
||
private Map<Class<?>, List<Method>> getAllMethods(List<Class<?>> classes) { | ||
return classes.stream() | ||
.collect(Collectors.toMap(Function.identity(), | ||
i -> Arrays.asList(i.getDeclaredMethods()), | ||
(x, y) -> y, LinkedHashMap::new)); | ||
} | ||
|
||
private String getDisplayName(AnnotatedElement element, Supplier<String> displayNameSupplier) { | ||
Optional<DisplayName> displayNameAnnotation = findAnnotation(element, DisplayName.class); | ||
if (displayNameAnnotation.isPresent()) { | ||
String displayName = displayNameAnnotation.get().value().trim(); | ||
if (!StringUtils.isBlank(displayName)) return displayName; | ||
} | ||
return displayNameSupplier.get(); | ||
} | ||
|
||
private <T> String getClassDisplayName(Class<T> clazz) { | ||
if (clazz.getEnclosingClass() == null) { | ||
return getDisplayName(clazz, () -> displayNameGenerator.generateDisplayNameForClass(clazz)); | ||
} else { | ||
return getDisplayName(clazz, () -> displayNameGenerator.generateDisplayNameForNestedClass(clazz)); | ||
} | ||
} | ||
|
||
private <T> String getMethodDisplayName(Class<T> clazz, Method method) { | ||
return getDisplayName(method, () -> displayNameGenerator.generateDisplayNameForMethod(clazz, method)); | ||
} | ||
} |