-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from mk868/test-metadata
Abstraction for reading test metadata added
- Loading branch information
Showing
10 changed files
with
778 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
98 changes: 98 additions & 0 deletions
98
src/main/java/app/getxray/xray/junit/customjunitxml/DefaultXrayTestMetadataReader.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,98 @@ | ||
/* | ||
* Copyright 2024-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package app.getxray.xray.junit.customjunitxml; | ||
|
||
import app.getxray.xray.junit.customjunitxml.annotations.Requirement; | ||
import app.getxray.xray.junit.customjunitxml.annotations.XrayTest; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.TestFactory; | ||
import org.junit.platform.commons.support.AnnotationSupport; | ||
import org.junit.platform.engine.support.descriptor.MethodSource; | ||
import org.junit.platform.launcher.TestIdentifier; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class DefaultXrayTestMetadataReader implements XrayTestMetadataReader { | ||
|
||
@Override | ||
public Optional<String> getId(TestIdentifier testIdentifier) { | ||
return getTestMethodAnnotation(testIdentifier, XrayTest.class) | ||
.map(XrayTest::id) | ||
.filter(s -> !s.isEmpty()); | ||
} | ||
|
||
@Override | ||
public Optional<String> getKey(TestIdentifier testIdentifier) { | ||
return getTestMethodAnnotation(testIdentifier, XrayTest.class) | ||
.map(XrayTest::key) | ||
.filter(s -> !s.isEmpty()); | ||
} | ||
|
||
@Override | ||
public Optional<String> getSummary(TestIdentifier testIdentifier) { | ||
Optional<String> testSummary = getTestMethodAnnotation(testIdentifier, XrayTest.class) | ||
.map(XrayTest::summary) | ||
.filter(s -> !s.isEmpty()); | ||
if (testSummary.isPresent()) { | ||
return testSummary; | ||
} | ||
|
||
Optional<DisplayName> displayName = getTestMethodAnnotation(testIdentifier, DisplayName.class); | ||
if (displayName.isPresent()) { | ||
return Optional.of(displayName.get().value()); | ||
} | ||
|
||
|
||
Optional<TestFactory> dynamicTest = getTestMethodAnnotation(testIdentifier, TestFactory.class); | ||
Optional<DisplayNameGeneration> displayNameGenerator = getTestClassAnnotation(testIdentifier, DisplayNameGeneration.class); | ||
if (dynamicTest.isPresent() || displayNameGenerator.isPresent()) { | ||
return Optional.of(testIdentifier.getDisplayName()); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<String> getDescription(TestIdentifier testIdentifier) { | ||
return getTestMethodAnnotation(testIdentifier, XrayTest.class) | ||
.map(XrayTest::description) | ||
.filter(s -> !s.isEmpty()); | ||
} | ||
|
||
@Override | ||
public List<String> getRequirements(TestIdentifier testIdentifier) { | ||
return getTestMethodAnnotation(testIdentifier, Requirement.class) | ||
.map(Requirement::value) | ||
.map(arr -> Collections.unmodifiableList(Arrays.asList(arr))) | ||
.orElse(Collections.emptyList()); | ||
} | ||
|
||
protected <A extends Annotation> Optional<A> getTestMethodAnnotation(TestIdentifier testIdentifier, Class<A> aClass) { | ||
return testIdentifier.getSource() | ||
.filter(a -> a instanceof MethodSource) | ||
.map(MethodSource.class::cast) | ||
.map(MethodSource::getJavaMethod) | ||
.flatMap(a -> AnnotationSupport.findAnnotation(a, aClass)); | ||
} | ||
|
||
protected <A extends Annotation> Optional<A> getTestClassAnnotation(TestIdentifier testIdentifier, Class<A> aClass) { | ||
return testIdentifier.getSource() | ||
.filter(a -> a instanceof MethodSource) | ||
.map(MethodSource.class::cast) | ||
.map(MethodSource::getJavaClass) | ||
.flatMap(a -> AnnotationSupport.findAnnotation(a, aClass)); | ||
} | ||
} |
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
Oops, something went wrong.