-
Notifications
You must be signed in to change notification settings - Fork 79
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
019eb5d
commit 2125368
Showing
4 changed files
with
151 additions
and
14 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
tmail_selenium/src/main/java/com/tmail/base/CustomParameterResolver.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,70 @@ | ||
package com.tmail.base; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolutionException; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
import org.junit.jupiter.engine.execution.BeforeEachMethodAdapter; | ||
import org.junit.jupiter.engine.extension.ExtensionRegistry; | ||
|
||
public class CustomParameterResolver implements BeforeEachMethodAdapter, ParameterResolver { | ||
|
||
private ParameterResolver parameterisedTestParameterResolver = null; | ||
|
||
@Override | ||
public void invokeBeforeEachMethod(ExtensionContext context, ExtensionRegistry registry) | ||
throws Throwable { | ||
Optional<ParameterResolver> resolverOptional = registry.getExtensions(ParameterResolver.class) | ||
.stream() | ||
.filter(parameterResolver -> parameterResolver.getClass().getName() | ||
.contains("ParameterizedTestParameterResolver")) | ||
.findFirst(); | ||
if (!resolverOptional.isPresent()) { | ||
throw new IllegalStateException( | ||
"ParameterizedTestParameterResolver missed in the registry. Probably it's not a Parameterized Test"); | ||
} else { | ||
parameterisedTestParameterResolver = resolverOptional.get(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, | ||
ExtensionContext extensionContext) throws ParameterResolutionException { | ||
if (isExecutedOnAfterOrBeforeMethod(parameterContext)) { | ||
ParameterContext pContext = getMappedContext(parameterContext, extensionContext); | ||
return parameterisedTestParameterResolver.supportsParameter(pContext, extensionContext); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, | ||
ExtensionContext extensionContext) throws ParameterResolutionException { | ||
return parameterisedTestParameterResolver.resolveParameter( | ||
getMappedContext(parameterContext, extensionContext), extensionContext); | ||
} | ||
|
||
private MappedParameterContext getMappedContext(ParameterContext parameterContext, | ||
ExtensionContext extensionContext) { | ||
return new MappedParameterContext( | ||
parameterContext.getIndex(), | ||
extensionContext.getRequiredTestMethod().getParameters()[parameterContext.getIndex()], | ||
Optional.of(parameterContext.getTarget())); | ||
} | ||
|
||
private boolean isExecutedOnAfterOrBeforeMethod(ParameterContext parameterContext) { | ||
return Arrays.stream(parameterContext.getDeclaringExecutable().getDeclaredAnnotations()) | ||
.anyMatch(this::isAfterEachOrBeforeEachAnnotation); | ||
} | ||
|
||
private boolean isAfterEachOrBeforeEachAnnotation(Annotation annotation) { | ||
return annotation.annotationType() == BeforeEach.class | ||
|| annotation.annotationType() == AfterEach.class; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
tmail_selenium/src/main/java/com/tmail/base/MappedParameterContext.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,53 @@ | ||
package com.tmail.base; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Parameter; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.platform.commons.util.AnnotationUtils; | ||
|
||
public class MappedParameterContext implements ParameterContext { | ||
|
||
private final int index; | ||
private final Parameter parameter; | ||
private final Optional<Object> target; | ||
|
||
public MappedParameterContext(int index, Parameter parameter, | ||
Optional<Object> target) { | ||
this.index = index; | ||
this.parameter = parameter; | ||
this.target = target; | ||
} | ||
|
||
@Override | ||
public boolean isAnnotated(Class<? extends Annotation> annotationType) { | ||
return AnnotationUtils.isAnnotated(parameter, annotationType); | ||
} | ||
|
||
@Override | ||
public <A extends Annotation> Optional<A> findAnnotation(Class<A> annotationType) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public <A extends Annotation> List<A> findRepeatableAnnotations(Class<A> annotationType) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getIndex() { | ||
return index; | ||
} | ||
|
||
@Override | ||
public Parameter getParameter() { | ||
return parameter; | ||
} | ||
|
||
@Override | ||
public Optional<Object> getTarget() { | ||
return target; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
junit.jupiter.execution.parallel.enabled = true | ||
junit.jupiter.execution.parallel.mode.default = same_thread | ||
junit.jupiter.execution.parallel.mode.classes.default = concurrent | ||
junit.jupiter.execution.parallel.config.strategy=dynamic | ||
junit.jupiter.execution.parallel.config.dynamic.factor=0.5 |