Java Code Examples for org.junit.platform.commons.util.AnnotationUtils#findAnnotation()

The following examples show how to use org.junit.platform.commons.util.AnnotationUtils#findAnnotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OsCondition.java    From mastering-junit5 with Apache License 2.0 6 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(
        ExtensionContext context) {

    Optional<AnnotatedElement> element = context.getElement();
    ConditionEvaluationResult out = ConditionEvaluationResult
            .enabled("@DisabledOnOs is not present");

    Optional<DisabledOnOs> disabledOnOs = AnnotationUtils
            .findAnnotation(element, DisabledOnOs.class);

    if (disabledOnOs.isPresent()) {
        Os myOs = Os.determine();
        if (Arrays.asList(disabledOnOs.get().value()).contains(myOs)) {
            out = ConditionEvaluationResult
                    .disabled("Test is disabled on " + myOs);
        } else {
            out = ConditionEvaluationResult
                    .enabled("Test is not disabled on " + myOs);
        }
    }

    System.out.println("--> " + out.getReason().get());
    return out;
}
 
Example 2
Source File: OsCondition.java    From Mastering-Software-Testing-with-JUnit-5 with MIT License 6 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(
        ExtensionContext context) {

    Optional<AnnotatedElement> element = context.getElement();
    ConditionEvaluationResult out = ConditionEvaluationResult
            .enabled("@DisabledOnOs is not present");

    Optional<DisabledOnOs> disabledOnOs = AnnotationUtils
            .findAnnotation(element, DisabledOnOs.class);

    if (disabledOnOs.isPresent()) {
        Os myOs = Os.determine();
        if (Arrays.asList(disabledOnOs.get().value()).contains(myOs)) {
            out = ConditionEvaluationResult
                    .disabled("Test is disabled on " + myOs);
        } else {
            out = ConditionEvaluationResult
                    .enabled("Test is not disabled on " + myOs);
        }
    }

    System.out.println("--> " + out.getReason().get());
    return out;
}
 
Example 3
Source File: DBUnitExtension.java    From database-rider with Apache License 2.0 6 votes vote down vote up
private void executeExpectedDataSetForCallback(ExtensionContext extensionContext, Class callbackAnnotation, Method callbackMethod) throws DatabaseUnitException {
    Class testClass = extensionContext.getTestClass().get();
    // get ExpectedDataSet annotation, if any
    Optional<ExpectedDataSet> expectedDataSetAnnotation = AnnotationUtils.findAnnotation(callbackMethod, ExpectedDataSet.class);
    if (!expectedDataSetAnnotation.isPresent()) {
        Optional<Method> superclassCallbackMethod = findSuperclassCallbackMethod(testClass, callbackAnnotation);
        if (superclassCallbackMethod.isPresent()) {
            expectedDataSetAnnotation = AnnotationUtils.findAnnotation(superclassCallbackMethod.get(), ExpectedDataSet.class);
        }
    }
    if (expectedDataSetAnnotation.isPresent()) {
        ExpectedDataSet expectedDataSet = expectedDataSetAnnotation.get();
        // Resolve DBUnit config from annotation or file
        DBUnitConfig dbUnitConfig = resolveDbUnitConfig(callbackAnnotation, callbackMethod, testClass);
        // Verify expected dataset
        final String executorId = getExecutorId(extensionContext, null);
        ConnectionHolder connectionHolder = getTestConnection(extensionContext, executorId);
        DataSetExecutor dataSetExecutor = DataSetExecutorImpl.instance(executorId, connectionHolder, dbUnitConfig);
        dataSetExecutor.compareCurrentDataSetWith(
                new DataSetConfig(expectedDataSet.value()).disableConstraints(true).datasetProvider(expectedDataSet.provider()),
                expectedDataSet.ignoreCols(),
                expectedDataSet.replacers(),
                expectedDataSet.orderBy(),
                expectedDataSet.compareOperation());
    }
}
 
Example 4
Source File: DBUnitExtension.java    From database-rider with Apache License 2.0 6 votes vote down vote up
private DBUnitConfig resolveDbUnitConfig(Class callbackAnnotation, Method callbackMethod, Class testClass) {
    Optional<DBUnit> dbUnitAnnotation = AnnotationUtils.findAnnotation(callbackMethod, DBUnit.class);
    if (!dbUnitAnnotation.isPresent()) {
        dbUnitAnnotation = AnnotationUtils.findAnnotation(testClass, DBUnit.class);
    }
    if (!dbUnitAnnotation.isPresent()) {
        Optional<Method> superclassCallbackMethod = findSuperclassCallbackMethod(testClass, callbackAnnotation);
        if (superclassCallbackMethod.isPresent()) {
            dbUnitAnnotation = AnnotationUtils.findAnnotation(superclassCallbackMethod.get(), DBUnit.class);
        }
    }
    if (!dbUnitAnnotation.isPresent() && testClass.getSuperclass() != null) {
        dbUnitAnnotation = AnnotationUtils.findAnnotation(testClass.getSuperclass(), DBUnit.class);
    }
    return dbUnitAnnotation.isPresent() ? DBUnitConfig.from(dbUnitAnnotation.get()) : DBUnitConfig.fromGlobalConfig();
}
 
Example 5
Source File: DBUnitExtension.java    From database-rider with Apache License 2.0 5 votes vote down vote up
private Optional<DataSet> findDataSetAnnotation(ExtensionContext extensionContext) {
    Optional<Method> testMethod = extensionContext.getTestMethod();
    if (testMethod.isPresent()) {
        Optional<DataSet> annDataSet = AnnotationUtils.findAnnotation(testMethod.get(), DataSet.class);
        if (!annDataSet.isPresent()) {
            annDataSet = AnnotationUtils.findAnnotation(extensionContext.getRequiredTestClass(), DataSet.class);
        }
        return annDataSet;
    } else {
        return Optional.empty();
    }
}
 
Example 6
Source File: DBUnitExtension.java    From database-rider with Apache License 2.0 5 votes vote down vote up
private static String getConfiguredDataSourceBeanName(ExtensionContext extensionContext) {
    Optional<Method> testMethod = extensionContext.getTestMethod();
    if (testMethod.isPresent()) {
        Optional<DBRider> annotation = AnnotationUtils.findAnnotation(testMethod.get(), DBRider.class);
        if (!annotation.isPresent()) {
            annotation = AnnotationUtils.findAnnotation(extensionContext.getRequiredTestClass(), DBRider.class);
        }
        return annotation.map(DBRider::dataSourceBeanName).orElse(EMPTY_STRING);
    } else {
        return EMPTY_STRING;
    }
}
 
Example 7
Source File: DBUnitExtension.java    From database-rider with Apache License 2.0 5 votes vote down vote up
private void executeDataSetForCallback(ExtensionContext extensionContext, Class callbackAnnotation, Method callbackMethod) {
    Class testClass = extensionContext.getTestClass().get();
    // get DataSet annotation, if any
    Optional<DataSet> dataSetAnnotation = AnnotationUtils.findAnnotation(callbackMethod, DataSet.class);
    if (!dataSetAnnotation.isPresent()) {
        Optional<Method> superclassCallbackMethod = findSuperclassCallbackMethod(testClass, callbackAnnotation);
        if (superclassCallbackMethod.isPresent()) {
            dataSetAnnotation = AnnotationUtils.findAnnotation(superclassCallbackMethod.get(), DataSet.class);
        }
    }
    if (dataSetAnnotation.isPresent()) {
        clearEntityManager();
        DBUnitConfig dbUnitConfig = resolveDbUnitConfig(callbackAnnotation, callbackMethod, testClass);
        DataSet dataSet;
        if (dbUnitConfig.isMergeDataSets()) {
            Optional<DataSet> classLevelDataSetAnnotation = AnnotationUtils.findAnnotation(testClass, DataSet.class);
            dataSet = resolveDataSet(dataSetAnnotation, classLevelDataSetAnnotation);
        } else {
            dataSet = dataSetAnnotation.get();
        }
        // Execute dataset
        final String executorId = getExecutorId(extensionContext, dataSet);
        ConnectionHolder connectionHolder = getTestConnection(extensionContext, executorId);
        DataSetExecutor dataSetExecutor = DataSetExecutorImpl.instance(executorId, connectionHolder, dbUnitConfig);
        dataSetExecutor.createDataSet(new DataSetConfig().from(dataSet));
    }
}
 
Example 8
Source File: VaultVersionExtension.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {

	Optional<RequiresVaultVersion> optional = AnnotationUtils.findAnnotation(context.getElement(),
			RequiresVaultVersion.class);

	if (!optional.isPresent()) {
		return ENABLED_BY_DEFAULT;
	}

	ExtensionContext.Store store = context.getStore(VAULT);

	Version runningVersion = store.getOrComputeIfAbsent(Version.class, versionClass -> {

		VaultInitializer initializer = new VaultInitializer();
		initializer.initialize();
		return initializer.prepare().getVersion();
	}, Version.class);

	RequiresVaultVersion requiredVersion = optional.get();

	Version required = Version.parse(requiredVersion.value());

	if (runningVersion.isGreaterThanOrEqualTo(required)) {
		return ConditionEvaluationResult
				.enabled(String.format("@VaultVersion check passed current Vault version is %s", runningVersion));
	}

	return ConditionEvaluationResult.disabled(String.format(
			"@VaultVersion requires since version %s, current Vault version is %s", required, runningVersion));
}
 
Example 9
Source File: VideoExtension.java    From video-recorder-java with MIT License 5 votes vote down vote up
private boolean videoDisabled(Method testMethod) {
  Optional<com.automation.remarks.video.annotations.Video> video = AnnotationUtils.findAnnotation(testMethod, com.automation.remarks.video.annotations.Video.class);

  return video.map(v -> !videoEnabled(v))
          .orElseGet(() -> true);

  //return !video.isPresent() && !videoEnabled(video.get());
}
 
Example 10
Source File: TestcontainersExtension.java    From testcontainers-java with MIT License 5 votes vote down vote up
private Optional<Testcontainers> findTestcontainers(ExtensionContext context) {
    Optional<ExtensionContext> current = Optional.of(context);
    while (current.isPresent()) {
        Optional<Testcontainers> testcontainers = AnnotationUtils.findAnnotation(current.get().getRequiredTestClass(), Testcontainers.class);
        if (testcontainers.isPresent()) {
            return testcontainers;
        }
        current = current.get().getParent();
    }
    return Optional.empty();
}
 
Example 11
Source File: FakeParameterContext.java    From junit-servers with MIT License 4 votes vote down vote up
@Override
public <A extends Annotation> Optional<A> findAnnotation(Class<A> annotationType) {
	return AnnotationUtils.findAnnotation(parameter, annotationType);
}