Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import com.fasterxml.jackson.databind.testutil.Failing;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.junit.jupiter.api.Assertions.*;


Expand Down Expand Up @@ -36,4 +39,17 @@ public void testRecordAsPropertyUpdate() throws Exception
assertSame(orig, result);
assertNotSame(origRecord, result.value);
}

@Failing // 01-Dec-2022, tatu: Alas, fails on JDK 17
// [databind#3079]: Should be able to Record value directly
@Test
public void testDirectRecordUpdate() throws Exception {
RecordUpdate3079Test.IdNameRecord orig = new RecordUpdate3079Test.IdNameRecord(123, "Bob");
RecordUpdate3079Test.IdNameRecord result = MAPPER.updateValue(orig,
Collections.singletonMap("id", 137));
assertNotNull(result);
assertEquals(137, result.id());
assertEquals("Bob", result.name());
assertNotSame(orig, result);
}
}
89 changes: 89 additions & 0 deletions src/test/java/com/fasterxml/jackson/databind/testutil/Failing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.fasterxml.jackson.databind.testutil;

import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
import org.opentest4j.TestAbortedException;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(Failing.FailingExtension.class) // Comment this out if you want to temporarily let the tests actually fail
public @interface Failing {

/**
* For when a test is failing only when running on specific Java version - e.g. Java 8 & Java 11 - but passing
* for other versions, then:
* <pre><code>
* {@code @}Failing(javaVersion = { "1.8", "11" }
* {@code @}Test
* public void test...() { ... }
* </code></pre>
*/
String[] javaVersion() default {};

class FailingExtension implements BeforeEachCallback, TestExecutionExceptionHandler, AfterEachCallback {

private static final String SHOULD_FAIL = "should fail";

@Override
public void beforeEach(ExtensionContext context) {
getStore(context).put(SHOULD_FAIL, matchesFailingJavaVersion(context));
}

@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
boolean shouldFail = getStore(context).get(SHOULD_FAIL, boolean.class);
if (!shouldFail) {
// the test threw exception even though it's not expected to fail for this test method, let the
// it fail with this exception so maintainers can investigate
throw throwable;
}

// Instead of swallowing the exception and silently passing, better to mark the test as "aborted"
throw new TestAbortedException("Not implemented/fixed yet", throwable);
}

@Override
public void afterEach(ExtensionContext context) {
boolean shouldFail = getStore(context).get(SHOULD_FAIL, boolean.class);
boolean testFailed = context.getExecutionException().isPresent();

if (shouldFail && !testFailed) {
throw new RuntimeException("Test that has been failing is now passing!");
}
}

private ExtensionContext.Store getStore(ExtensionContext context) {
return context.getStore(ExtensionContext.Namespace.create(getClass(), context.getRequiredTestMethod()));
}

private boolean matchesFailingJavaVersion(ExtensionContext context) {
Method method = context.getTestMethod()
// should not happen because @Failing can only be annotated on test methods!
.orElseThrow(() -> new IllegalArgumentException("Cannot get test method!"));

String currentJavaVersion = System.getProperty("java.version");
String[] failingJavaVersions = method.getAnnotation(Failing.class).javaVersion();

if (failingJavaVersions.length == 0) { // failing for all Java versions
return true;
}
for (String version : failingJavaVersions) {
if (currentJavaVersion.startsWith(version)) {
return true;
}
}
return false; // not expected to fail for the current Java version
}
}
}