Skip to content

Commit

Permalink
hamcrest#322 Added MatcherAssume.assumeThat again
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdemaeyer committed Dec 25, 2021
1 parent 8522353 commit 11bf449
Show file tree
Hide file tree
Showing 18 changed files with 572 additions and 1 deletion.
36 changes: 36 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,39 @@ publishing {
'Hamcrest Library',
'A library of Hamcrest matchers - deprecated, please use "hamcrest" instead')
}

def hamcrestJUnit5TestsProject = project(':hamcrest-junit5-tests')
hamcrestJUnit5Tests(MavenPublication) {
from hamcrestJUnit5TestsProject.components.java
artifactId hamcrestJUnit5TestsProject.name
artifact hamcrestJUnit5TestsProject.sourcesJar
artifact hamcrestJUnit5TestsProject.javadocJar
pom pomConfigurationFor(
'Hamcrest JUnit 5 Tests',
'A test suite for Hamcrest assumptions using JUnit 5')
}

def hamcrestJUnit4TestsProject = project(':hamcrest-junit4-tests')
hamcrestJUnit4Tests(MavenPublication) {
from hamcrestJUnit4TestsProject.components.java
artifactId hamcrestJUnit4TestsProject.name
artifact hamcrestJUnit4TestsProject.sourcesJar
artifact hamcrestJUnit4TestsProject.javadocJar
pom pomConfigurationFor(
'Hamcrest JUnit4 Tests',
'A test suite for Hamcrest assumptions using JUnit 4')
}

def hamcrestJUnit4JUnit5TestsProject = project(':hamcrest-junit4-junit5-tests')
hamcrestJUnit4JUnit5Tests(MavenPublication) {
from hamcrestJUnit4JUnit5TestsProject.components.java
artifactId hamcrestJUnit4JUnit5TestsProject.name
artifact hamcrestJUnit4JUnit5TestsProject.sourcesJar
artifact hamcrestJUnit4JUnit5TestsProject.javadocJar
pom pomConfigurationFor(
'Hamcrest Hybrid JUnit 4/JUnit 5 Tests',
'A test suite for Hamcrest assumptions using hybrid JUnit 4/JUnit 5')
}
}
repositories {
if (publishToOssrh) {
Expand All @@ -150,4 +183,7 @@ signing {
sign publishing.publications.hamcrest
sign publishing.publications.hamcrestCore
sign publishing.publications.hamcrestLibrary
sign publishing.publications.hamcrestJUnit5Tests
sign publishing.publications.hamcrestJUnit4Tests
sign publishing.publications.hamcrestJUnit4JUnit5Tests
}
20 changes: 20 additions & 0 deletions hamcrest-junit4-junit5-tests/hamcrest-junit4-junit5-tests.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
id 'java'
}

group 'org.hamcrest'
version '2.3-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
testImplementation project(':hamcrest')
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testImplementation 'org.junit.vintage:junit-vintage-engine:5.8.2'
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.hamcrest;

import org.junit.Test;
import org.junit.AssumptionViolatedException;
import org.opentest4j.TestAbortedException;

import static org.hamcrest.MatcherAssume.assumeThat;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
* Tests compatibility with JUnit 4 <i>and</i> JUnit 5 on the classpath.
* The equivalent test with only JUnit 4 on the classpath is in another module.
*/
public class JUnit4MatcherAssumeTest {

@Test public void
assumptionFailsWithMessage() {
try {
assumeThat("Custom assumption", "a", startsWith("abc"));
fail("should have failed");
}
catch (AssumptionViolatedException e) {
assertEquals("Custom assumption: got: \"a\", expected: a string starting with \"abc\"", e.getMessage());
}
catch (TestAbortedException e) {
throw new AssertionError("Illegal JUnit 5 assumption", e);
}
}

@Test public void
assumptionFailsWithDefaultMessage() {
try {
assumeThat("a", startsWith("abc"));
fail("should have failed");
}
catch (AssumptionViolatedException e) {
assertEquals(": got: \"a\", expected: a string starting with \"abc\"", e.getMessage());
}
catch (TestAbortedException e) {
throw new AssertionError("Illegal JUnit 5 assumption", e);
}
}

@Test public void
assumptionSucceeds() {
try {
assumeThat("xyz", startsWith("xy"));
} catch (TestAbortedException e) {
throw new AssertionError("Illegal JUnit 5 assumption", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.hamcrest;

import org.junit.AssumptionViolatedException;
import org.junit.jupiter.api.Test;
import org.opentest4j.TestAbortedException;

import static org.hamcrest.MatcherAssume.assumeThat;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Tests compatibility with JUnit 5 with JUnit 4 <i>and</i> JUnit 5 on the classpath.
* The equivalent test with only JUnit 4 on the classpath is in another module.
*/
class JUnit5MatcherAssumeTest {

@Test
void
assumptionFailsWithMessage() {
try {
assumeThat("Custom assumption", "a", startsWith("abc"));
fail("should have failed");
}
catch (TestAbortedException e) {
assertEquals("Assumption failed: Custom assumption", e.getMessage());
}
catch (AssumptionViolatedException e) {
// If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually
// a false ignored test.
throw new AssertionError("Illegal JUnit 4 assumption", e);
}
}

@Test void
assumptionFailsWithDefaultMessage() {
try {
assumeThat("a", startsWith("abc"));
fail("should have failed");
}
catch (TestAbortedException e) {
assertEquals("Assumption failed", e.getMessage());
}
catch (AssumptionViolatedException e) {
// If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually
// a false ignored test.
throw new AssertionError("Illegal JUnit 4 assumption", e);
}
}

@Test void
assumptionSucceeds() {
try {
assumeThat("xyz", startsWith("xy"));
}
catch (AssumptionViolatedException e) {
// If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually
// a false ignored test.
throw new AssertionError("Illegal JUnit 4 assumption", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.hamcrest;

import org.junit.AssumptionViolatedException;
import org.junit.jupiter.api.Test;
import org.opentest4j.TestAbortedException;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

import static java.util.concurrent.Executors.newSingleThreadExecutor;
import static org.hamcrest.MatcherAssume.assumeThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.fail;

class MatcherAssumeTest {

@Test
void assumptionFailsWithAssertionErrorWhenNoJUnitInStackTrace() throws Throwable {
// Run the assumption on a separate thread to make sure it has JUnit 4 nor JUnit 5 in its stack trace.
ExecutorService executor = newSingleThreadExecutor();
try {
try {
executor.submit(new Runnable() {

@Override
public void run() {
assumeThat(1, is(2));
}
}).get();
fail("Expected " + ExecutionException.class);
} catch (ExecutionException expected) {
throw expected.getCause();
}
} catch (AssertionError expected) {
} catch (TestAbortedException | AssumptionViolatedException e) {
throw new AssertionError(e);
}
}
}
17 changes: 17 additions & 0 deletions hamcrest-junit4-tests/hamcrest-junit4-tests.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
dependencies {
testImplementation project(':hamcrest')
testImplementation(group: 'junit', name: 'junit', version: '4.13.2') {
transitive = false
}
}

jar {
manifest {
attributes 'Implementation-Title': project.name,
'Implementation-Vendor': 'hamcrest.org',
'Implementation-Version': version,
'Automatic-Module-Name': 'org.hamcrest.junit4-tests'
}
}

javadoc.title = "Hamcrest JUnit 4 Tests $version"
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.hamcrest;

import org.junit.Test;
import org.junit.AssumptionViolatedException;

import static org.hamcrest.MatcherAssume.assumeThat;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
* Tests compatibility with JUnit 4 with only JUnit 4 on the classpath.
* The equivalent test with JUnit 4 <i>and</i> JUnit 5 on the classpath is in another module.
*/
public class JUnit4MatcherAssumeTest {

@Test public void
assumptionFailsWithMessage() {
try {
assumeThat("Custom assumption", "a", startsWith("abc"));
fail("should have failed");
}
catch (AssumptionViolatedException e) {
assertEquals("Custom assumption: got: \"a\", expected: a string starting with \"abc\"", e.getMessage());
}
}

@Test public void
assumptionFailsWithDefaultMessage() {
try {
assumeThat("a", startsWith("abc"));
fail("should have failed");
}
catch (AssumptionViolatedException e) {
assertEquals(": got: \"a\", expected: a string starting with \"abc\"", e.getMessage());
}
}

@Test public void
assumptionSucceeds() {
assumeThat("xyz", startsWith("xy"));
}
}
21 changes: 21 additions & 0 deletions hamcrest-junit5-tests/hamcrest-junit5-tests.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
dependencies {
api project(':hamcrest')
api 'org.opentest4j:opentest4j:1.2.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}

jar {
manifest {
attributes 'Implementation-Title': project.name,
'Implementation-Vendor': 'hamcrest.org',
'Implementation-Version': version,
'Automatic-Module-Name': 'org.hamcrest.junit5-tests'
}
}

test {
useJUnitPlatform()
}

javadoc.title = "Hamcrest JUnit 5 Tests $version"
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.hamcrest;

import org.junit.jupiter.api.Test;
import org.opentest4j.TestAbortedException;

import static org.hamcrest.MatcherAssume.assumeThat;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Tests compatibility with JUnit 5 with only JUnit 5 on the classpath.
* The equivalent test with JUnit 4 <i>and</i> JUnit 5 on the classpath is in another module.
*/
class JUnit5MatcherAssumeTest {

@Test
void
assumptionFailsWithMessage() {
try {
assumeThat("Custom assumption", "a", startsWith("abc"));
fail("should have failed");
}
catch (TestAbortedException e) {
assertEquals("Assumption failed: Custom assumption", e.getMessage());
}
}

@Test void
assumptionFailsWithDefaultMessage() {
try {
assumeThat("a", startsWith("abc"));
fail("should have failed");
}
catch (TestAbortedException e) {
assertEquals("Assumption failed", e.getMessage());
}
}

@Test void
assumptionSucceeds() {
assumeThat("xyz", startsWith("xy"));
}
}
4 changes: 3 additions & 1 deletion hamcrest/hamcrest.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ apply plugin: 'osgi'
version = rootProject.version

dependencies {
testImplementation(group: 'junit', name: 'junit', version: '4.13') {
compileOnly 'org.junit.jupiter:junit-jupiter-api:5.8.2'
compileOnly 'junit:junit:4.13.2'
testImplementation(group: 'junit', name: 'junit', version: '4.13.2') {
transitive = false
}
}
Expand Down
17 changes: 17 additions & 0 deletions hamcrest/src/main/java/org/hamcrest/MatcherAssume.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.hamcrest;

import org.hamcrest.internal.AssumptionProvider;

public final class MatcherAssume {

private MatcherAssume() {
}

public static <T> void assumeThat(T actual, Matcher<? super T> matcher) {
assumeThat("", actual, matcher);
}

public static <T> void assumeThat(String message, T actual, Matcher<? super T> matcher) {
AssumptionProvider.getInstance().assumeThat(message, actual, matcher);
}
}
Loading

0 comments on commit 11bf449

Please sign in to comment.