Skip to content

Commit

Permalink
Small improvements and fixes (#790 / #800)
Browse files Browse the repository at this point in the history
Numerous small improvements and polishing,
for example touch-up of the "demo" project.

Closes: #790 
PR: #800
  • Loading branch information
Michael1993 committed Mar 26, 2024
1 parent 2753134 commit 1251593
Show file tree
Hide file tree
Showing 25 changed files with 306 additions and 409 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -98,7 +98,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: "61ab2579215aa8a0024a2f9368fc1298fdecfd18"
run: ./gradlew jacocoTestReport sonarqube --stacktrace -i
run: ./gradlew jacocoTestReport sonar --stacktrace -i

# Code format check
code-format-check:
Expand Down
1 change: 1 addition & 0 deletions .infra/checkstyle/import-control.xml
Expand Up @@ -5,6 +5,7 @@

<allow pkg=".*" regex="true" />
<disallow pkg="com.fasterxml.jackson.*" regex="true"/>
<disallow pkg="org.junit.jupiter.api.Assertions.*" regex="true"/>

<subpackage name="jupiter.json">
<file name="JacksonNode.java">
Expand Down
3 changes: 1 addition & 2 deletions build.gradle.kts
Expand Up @@ -304,8 +304,7 @@ tasks {
javadoc {
if (releaseBuild) {
javadocTool.set(project.javaToolchains.javadocToolFor {
// create Javadoc with least Java version to get all features
// (e.g. search result page on 20)
// create Javadoc with the minimum Java version needed for our desired features (e.g. search)
languageVersion.set(JavaLanguageVersion.of(21))
})
}
Expand Down
Expand Up @@ -10,8 +10,8 @@

package org.junitpioneer.jupiter;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

import java.io.IOException;

Expand All @@ -38,7 +38,7 @@ void test1() {
@Test
@Order(2)
void test2() {
fail();
fail("fails");
}

@Test
Expand All @@ -64,7 +64,7 @@ void test1() {
@Order(2)
void test2() {
// fails test with assertion
assertTrue(false);
assertThat(false).isTrue();
}

@Test
Expand Down
Expand Up @@ -10,7 +10,7 @@

package org.junitpioneer.jupiter;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

Expand All @@ -21,7 +21,7 @@ public class ExpectedToFailExtensionDemo {
@ExpectedToFail
void test() {
int actual = brokenMethod();
assertEquals(10, actual);
assertThat(actual).isEqualTo(10);
}
// end::expected_to_fail[]

Expand All @@ -30,7 +30,7 @@ void test() {
@ExpectedToFail("Implementation bug in brokenMethod()")
void doSomething() {
int actual = brokenMethod();
assertEquals(10, actual);
assertThat(actual).isEqualTo(10);
}
// end::expected_to_fail_message[]

Expand Down
Expand Up @@ -101,10 +101,10 @@ void testReadLines(StdIn in) throws IOException {
String[] lines = in.capturedLines();

// This is failing
// assertEquals(lines, "line1", "line2");
// assertThat(lines).containsExactly("line1", "line2");

// This is passing
// assertEquals(lines, "line1", "line2", "line3");
// assertThat(lines).containsExactly("line1", "line2", "line3");
}

}
Expand Down
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.extension.ExtensionContext;

// implementation example, does not contain tests
public class BitArgumentProvider {

// tag::cartesian_bit_source_annotation[]
Expand Down
Expand Up @@ -23,6 +23,7 @@
import java.time.temporal.TemporalUnit;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
Expand Down Expand Up @@ -178,6 +179,7 @@ static ArgumentSets stringIntFactory() {
}
// end::cartesian_argument_sets_reuse[]

// these tests fail intentionally ~> no @Nested
static class MisconfiguredExamples {

// tag::cartesian_bad_examples[]
Expand Down Expand Up @@ -236,7 +238,7 @@ static ArgumentSets resolveTestReporterParam() {

// tag::cartesian_argument_sets_int_argument_provider[]
class IntArgumentsProvider
implements CartesianParameterArgumentsProvider {
implements CartesianParameterArgumentsProvider<Integer> {

@Override
public Stream<Integer> provideArguments(
Expand Down Expand Up @@ -282,5 +284,13 @@ ArgumentSets provideArguments() {
}
// end::cartesian_argument_sets_with_non_static_factory[]

static class MyTestReporter implements TestReporter {

@Override
public void publishEntry(Map<String, String> map) {
}

}

}
// @formatter:on

This file was deleted.

Expand Up @@ -19,6 +19,7 @@
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.support.AnnotationConsumer;

// implementation example, does not contain tests
public class NumberArgumentProvider {

// tag::cartesian_number_argument_provider[]
Expand All @@ -31,7 +32,7 @@ public class NumberArgumentProvider {

}

class NumberArgumentsProvider implements CartesianMethodArgumentsProvider, AnnotationConsumer<NumberSource> {
static class NumberArgumentsProvider implements CartesianMethodArgumentsProvider, AnnotationConsumer<NumberSource> {

private int[] numbers;

Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.support.AnnotationConsumer;

// implementation example, does not contain tests
public class PeopleProviderSources {

// tag::cartesian_people_provider_with_CartesianParameterArgumentsProvider[]
Expand All @@ -36,7 +37,7 @@ public class PeopleProviderSources {

}

class PeopleProvider implements CartesianParameterArgumentsProvider {
class PeopleProvider implements CartesianParameterArgumentsProvider<Person> {

@Override
public Stream<Person> provideArguments(ExtensionContext context, Parameter parameter) {
Expand All @@ -49,7 +50,7 @@ public Stream<Person> provideArguments(ExtensionContext context, Parameter param
}
// end::cartesian_people_provider_with_CartesianParameterArgumentsProvider[]

class Person {
static class Person {

String name;
int age;
Expand All @@ -63,7 +64,7 @@ public Person(String name, int age) {

// tag::cartesian_people_provider_with_AnnotationConsumer[]
class PeopleProviderWithAnnotationConsumer
implements CartesianParameterArgumentsProvider, AnnotationConsumer<People> {
implements CartesianParameterArgumentsProvider<Person>, AnnotationConsumer<People> {

private People source;

Expand Down
Expand Up @@ -10,7 +10,7 @@

package org.junitpioneer.jupiter.params;

import static org.junit.jupiter.api.Assertions.fail;
import static org.assertj.core.api.Assertions.fail;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
Expand Down
Expand Up @@ -23,8 +23,8 @@
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;

// implementation example, does not contain tests
// tag::in_memory_directory[]

public final class InMemoryDirectory implements ResourceFactory<Path> {

private static final AtomicInteger DIRECTORY_NAME = new AtomicInteger();
Expand All @@ -42,16 +42,16 @@ public Resource<Path> create(List<String> arguments) throws Exception {
Path newInMemoryDirectory = this.fileSystem.getPath("/" + directoryPrefix + DIRECTORY_NAME.getAndIncrement());
Files.createDirectory(newInMemoryDirectory);

return new Resource<Path>() {
return new Resource<>() {

@Override
public Path get() throws Exception {
public Path get() {
return newInMemoryDirectory;
}

@Override
public void close() throws Exception {
Files.walkFileTree(newInMemoryDirectory, new SimpleFileVisitor<Path>() {
Files.walkFileTree(newInMemoryDirectory, new SimpleFileVisitor<>() {

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Expand Down
Expand Up @@ -10,12 +10,12 @@

package org.junitpioneer.jupiter.resource;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

public class ResourceExtensionDemo {
Expand All @@ -24,7 +24,7 @@ public class ResourceExtensionDemo {
@Test
void test1(@New(TemporaryDirectory.class) Path tempDir) {
// Test code goes here, e.g.,
assertTrue(Files.exists(tempDir));
assertThat(tempDir).exists();
}

@Test
Expand All @@ -37,7 +37,7 @@ void test2(@New(TemporaryDirectory.class) Path tempDir) {
@Test
void dirTest1(@Dir Path tempDir) {
// Test code goes here, e.g.,
assertTrue(Files.exists(tempDir));
assertThat(tempDir).exists();
}

@Test
Expand All @@ -54,7 +54,7 @@ void testWithArg(
Path tempDir) {
// Test code goes here, e.g.,
Path rootTempDir = Paths.get(System.getProperty("java.io.tmpdir"));
assertTrue(rootTempDir.relativize(tempDir).toString().startsWith("customPrefix"));
assertThat(rootTempDir.relativize(tempDir)).asString().startsWith("customPrefix");
}
// end::create_new_resource_with_arg_demo[]
// @formatter:on
Expand All @@ -66,7 +66,7 @@ void sharedResourceTest1(
@Shared(factory = TemporaryDirectory.class, name = "sharedTempDir")
Path sharedTempDir) {
// Test code goes here, e.g.,
assertTrue(Files.exists(sharedTempDir));
assertThat(sharedTempDir).exists();
}

@Test
Expand Down Expand Up @@ -105,40 +105,42 @@ void secondSharedResource(
// end::create_multiple_shared_resources_demo[]
// @formatter:on

}

// @formatter:off
// tag::create_global_shared_resource_demo_first[]
class FirstTest {
@Nested
// @formatter:off
// tag::create_global_shared_resource_demo_first[]
class FirstTest {

@Test
void test(
@Shared(
factory = TemporaryDirectory.class,
name = "globalTempDir",
scope = Shared.Scope.GLOBAL)
Path tempDir) {
// Test code using the global shared resource...
}

@Test
void test(
@Shared(
factory = TemporaryDirectory.class,
name = "globalTempDir",
scope = Shared.Scope.GLOBAL)
Path tempDir) {
// Test code using the global shared resource...
}
// end::create_global_shared_resource_demo_first[]
// @formatter:on

}
// end::create_global_shared_resource_demo_first[]
// @formatter:on

// @formatter:off
// tag::create_global_shared_resource_demo_second[]
class SecondTest {
@Nested
// @formatter:off
// tag::create_global_shared_resource_demo_second[]
class SecondTest {

@Test
void test(
@Shared(
factory = TemporaryDirectory.class,
name = "globalTempDir",
scope = Shared.Scope.GLOBAL)
Path tempDir) {
// Test code using the global shared resource...
}

@Test
void test(
@Shared(
factory = TemporaryDirectory.class,
name = "globalTempDir",
scope = Shared.Scope.GLOBAL)
Path tempDir) {
// Test code using the global shared resource...
}
// end::create_global_shared_resource_demo_second[]
// @formatter:on

}
// end::create_global_shared_resource_demo_second[]
// @formatter:on

0 comments on commit 1251593

Please sign in to comment.