|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | + * accompanies this distribution and is available at |
| 7 | + * |
| 8 | + * http://www.eclipse.org/legal/epl-v20.html |
| 9 | + */ |
| 10 | + |
| 11 | +package org.junitpioneer.jupiter; |
| 12 | + |
| 13 | +import static java.lang.String.format; |
| 14 | +import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled; |
| 15 | +import static org.junitpioneer.internal.PioneerAnnotationUtils.findClosestEnclosingAnnotation; |
| 16 | + |
| 17 | +import java.time.LocalDate; |
| 18 | +import java.time.format.DateTimeFormatter; |
| 19 | +import java.time.format.DateTimeParseException; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 23 | +import org.junit.jupiter.api.extension.ExecutionCondition; |
| 24 | +import org.junit.jupiter.api.extension.ExtensionConfigurationException; |
| 25 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 26 | +import org.opentest4j.AssertionFailedError; |
| 27 | + |
| 28 | +/** |
| 29 | + * This class implements the functionality for the {@code @FailAt} annotation. |
| 30 | + * |
| 31 | + * @see FailAt |
| 32 | + */ |
| 33 | +class FailAtExtension implements ExecutionCondition { |
| 34 | + |
| 35 | + private static final DateTimeFormatter ISO_8601 = DateTimeFormatter.ISO_DATE; |
| 36 | + |
| 37 | + @Override |
| 38 | + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 39 | + return getFailAtDateFromAnnotation(context) |
| 40 | + .map(failAtDate -> evaluateFailAtDate(context, failAtDate)) |
| 41 | + .orElse(enabled("No @FailAt annotation found on element.")); |
| 42 | + } |
| 43 | + |
| 44 | + private Optional<LocalDate> getFailAtDateFromAnnotation(ExtensionContext context) { |
| 45 | + return findClosestEnclosingAnnotation(context, FailAt.class).map(FailAt::date).map(this::parseDate); |
| 46 | + } |
| 47 | + |
| 48 | + private LocalDate parseDate(String dateString) { |
| 49 | + try { |
| 50 | + return LocalDate.parse(dateString, ISO_8601); |
| 51 | + } |
| 52 | + catch (DateTimeParseException ex) { |
| 53 | + throw new ExtensionConfigurationException( |
| 54 | + "The `failAtDate` string '" + dateString + "' is not a valid ISO 8601 string.", ex); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private ConditionEvaluationResult evaluateFailAtDate(ExtensionContext context, LocalDate failAtDate) { |
| 59 | + LocalDate today = LocalDate.now(); |
| 60 | + boolean isBefore = today.isBefore(failAtDate); |
| 61 | + |
| 62 | + String failAtDateString = failAtDate.format(ISO_8601); |
| 63 | + String todayDateString = today.format(ISO_8601); |
| 64 | + |
| 65 | + if (isBefore) { |
| 66 | + String reportEntry = format( |
| 67 | + "The `date` %s is after the current date %s, so `@FailAt` did not fail the test \"%s\". It will do so when the date is reached.", |
| 68 | + failAtDateString, todayDateString, context.getUniqueId()); |
| 69 | + context.publishReportEntry("FailAt", reportEntry); |
| 70 | + return enabled(reportEntry); |
| 71 | + } else { |
| 72 | + String reportEntry = format( |
| 73 | + "The current date %s is after or on the `date` %s, so `@FailAt` fails the test \"%s\". Please remove the annotation.", |
| 74 | + failAtDateString, todayDateString, context.getUniqueId()); |
| 75 | + context.publishReportEntry(FailAtExtension.class.getSimpleName(), reportEntry); |
| 76 | + |
| 77 | + String message = format("The current date %s is after or on the `date` %s", todayDateString, |
| 78 | + failAtDateString); |
| 79 | + |
| 80 | + throw new AssertionFailedError(message); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments