Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ repository on GitHub.

* The `UniqueId.uniqueIdFormat` field has been removed, reducing the size of `UniqueId`
objects.
* Add `DiscoverySelectors.selectClasspathResources(String...)` and `DiscoverySelectors.selectClasspathResources(List<String)`

[[v6.1.0-M2-junit-jupiter]]
=== JUnit Jupiter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,56 @@ public static <T extends Collection<?>> T notEmpty(@Nullable T collection, Suppl
return collection;
}

/**
* Assert that the supplied collection contains no blank elements.
*
* <p><strong>WARNING</strong>: this method does NOT check if the supplied
* collection is {@code null} or <em>empty</em>.
*
* @param collection the collection to check
* @param message precondition violation message
* @return the supplied collection as a convenience
* @throws PreconditionViolationException if the supplied collection contains
* any blank elements
* @since 6.1
* @see #notBlank(String, String)
*/
@API(status = INTERNAL, since = "6.1")
@Contract("null, _ -> null")
public static <T extends Collection<String>> @Nullable T containsNoBlankElements(@Nullable T collection,
String message) throws PreconditionViolationException {

if (collection != null) {
collection.forEach(object -> notBlank(object, message));
}
return collection;
}

/**
* Assert that the supplied collection contains no blank elements.
*
* <p><strong>WARNING</strong>: this method does NOT check if the supplied
* collection is {@code null} or <em>empty</em>.
*
* @param collection the collection to check
* @param messageSupplier precondition violation message supplier
* @return the supplied collection as a convenience
* @throws PreconditionViolationException if the supplied collection contains
* any blank elements
* @since 6.1
* @see #notBlank(String, String)
*/
@API(status = INTERNAL, since = "6.1")
@Contract("null, _ -> null")
public static <T extends Collection<String>> @Nullable T containsNoBlankElements(@Nullable T collection,
Supplier<String> messageSupplier) throws PreconditionViolationException {

if (collection != null) {
collection.forEach(object -> notBlank(object, messageSupplier));
}
return collection;
}

/**
* Assert that the supplied {@link String} is not blank.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -292,6 +293,7 @@ public static List<ClasspathRootSelector> selectClasspathRoots(Set<Path> classpa
* @param classpathResourceName the name of the classpath resource; never
* {@code null} or blank
* @see #selectClasspathResource(String, FilePosition)
* @see #selectClasspathResources(String...)
* @see #selectClasspathResourceByName(Set)
* @see ClasspathResourceSelector
* @see ClassLoader#getResource(String)
Expand Down Expand Up @@ -322,6 +324,7 @@ public static ClasspathResourceSelector selectClasspathResource(String classpath
* {@code null} or blank
* @param position the position inside the classpath resource; may be {@code null}
* @see #selectClasspathResource(String)
* @see #selectClasspathResources(String...)
* @see #selectClasspathResourceByName(Set)
* @see ClasspathResourceSelector
* @see ClassLoader#getResource(String)
Expand Down Expand Up @@ -413,6 +416,45 @@ public static ClasspathResourceSelector selectClasspathResourceByName(Set<? exte
return new ClasspathResourceSelector(classpathResources);
}

/**
* Create a {@code ClasspathResourceSelector} for each supplied classpath
* resource name.
*
* @param classpathResourceNames the names of the classpath resource; never
* {@code null} and never containing {@code null} or blank references.
* @since 6.1
* @see #selectClasspathResource(String)
* @see #selectClasspathResources(String...)
* @see ClasspathResourceSelector
*/
@API(status = EXPERIMENTAL, since = "6.1")
public static List<ClasspathResourceSelector> selectClasspathResources(String... classpathResourceNames) {
Preconditions.notNull(classpathResourceNames, "classpathResourceNames must not be null");
return selectClasspathResources(Arrays.asList(classpathResourceNames));
}

/**
* Create a {@code ClasspathResourceSelector} for each supplied classpath
* resource name.
*
* @param classpathResourceNames the names of the classpath resource; never
* {@code null} and never containing {@code null} or blank references.
* @since 6.1
* @see #selectClasspathResource(String)
* @see #selectClasspathResources(String...)
* @see ClasspathResourceSelector
*/
@API(status = EXPERIMENTAL, since = "6.1")
public static List<ClasspathResourceSelector> selectClasspathResources(List<String> classpathResourceNames) {
Preconditions.notNull(classpathResourceNames, "classpathResourceNames must not be null");
Preconditions.containsNoBlankElements(classpathResourceNames,
"Individual classpathResourceNames must not be null or blank");
return classpathResourceNames.stream() //
.distinct() //
.map(DiscoverySelectors::selectClasspathResource) //
.toList();
}

/**
* Create a {@code ModuleSelector} for the supplied module name.
*
Expand Down Expand Up @@ -540,7 +582,8 @@ public static ClassSelector selectClass(@Nullable ClassLoader classLoader, Strin
*/
@API(status = EXPERIMENTAL, since = "6.0")
public static List<ClassSelector> selectClasses(Class<?>... classes) {
return selectClasses(List.of(classes));
Preconditions.notNull(classes, "classes must not be null");
return selectClasses(Arrays.asList(classes));
}

/**
Expand Down Expand Up @@ -579,7 +622,8 @@ public static List<ClassSelector> selectClasses(List<Class<?>> classes) {
*/
@API(status = EXPERIMENTAL, since = "6.0")
public static List<ClassSelector> selectClassesByName(String... classNames) {
return selectClassesByName(List.of(classNames));
Preconditions.notNull(classNames, "classNames must not be null");
return selectClassesByName(Arrays.asList(classNames));
}

/**
Expand All @@ -595,6 +639,7 @@ public static List<ClassSelector> selectClassesByName(String... classNames) {
*/
@API(status = EXPERIMENTAL, since = "6.0")
public static List<ClassSelector> selectClassesByName(List<String> classNames) {
Preconditions.notNull(classNames, "classNames must not be null");
return selectClassesByName(null, classNames);
}

Expand All @@ -613,7 +658,8 @@ public static List<ClassSelector> selectClassesByName(List<String> classNames) {
*/
@API(status = EXPERIMENTAL, since = "6.0")
public static List<ClassSelector> selectClassesByName(@Nullable ClassLoader classLoader, String... classNames) {
return selectClassesByName(classLoader, List.of(classNames));
Preconditions.notNull(classNames, "classNames must not be null");
return selectClassesByName(classLoader, Arrays.asList(classNames));
}

/**
Expand All @@ -631,7 +677,7 @@ public static List<ClassSelector> selectClassesByName(@Nullable ClassLoader clas
@API(status = EXPERIMENTAL, since = "6.0")
public static List<ClassSelector> selectClassesByName(@Nullable ClassLoader classLoader, List<String> classNames) {
Preconditions.notNull(classNames, "classNames must not be null");
Preconditions.containsNoNullElements(classNames, "Individual class names must not be null");
Preconditions.containsNoBlankElements(classNames, "Individual class names must not be null or blank");

// @formatter:off
return classNames.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
import static org.junit.platform.commons.util.Preconditions.condition;
import static org.junit.platform.commons.util.Preconditions.containsNoBlankElements;
import static org.junit.platform.commons.util.Preconditions.containsNoNullElements;
import static org.junit.platform.commons.util.Preconditions.notBlank;
import static org.junit.platform.commons.util.Preconditions.notEmpty;
Expand Down Expand Up @@ -106,6 +107,32 @@ void notEmptyThrowsForEmptyCollection() {
assertPreconditionViolationFor(() -> notEmpty(List.of(), message)).withMessage(message);
}

@Test
void containsNoBlankElementsPassesForCollectionThatIsNullOrEmpty() {
containsNoBlankElements((List<String>) null, "collection is null");
containsNoBlankElements(List.of(), "collection is empty");

containsNoBlankElements((List<String>) null, () -> "collection is null");
containsNoBlankElements(List.of(), () -> "collection is empty");
}

@Test
void containsNoBlankElementsPassesForCollectionContainingNonBlankElements() {
var input = List.of("a", "b", "c");
var output = containsNoBlankElements(input, "message");
assertSame(input, output);
}

@Test
void containsNoBlankElementsThrowsForCollectionContainingNullElements() {
var message = "collection contains blank elements";

assertPreconditionViolationFor(() -> containsNoBlankElements(singletonList(""), message)) //
.withMessage(message);
assertPreconditionViolationFor(() -> containsNoBlankElements(singletonList((String) null), message)) //
.withMessage(message);
}

@Test
void containsNoNullElementsPassesForArrayThatIsNullOrEmpty() {
containsNoNullElements((Object[]) null, "array is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.junit.platform.engine.discovery;

import static java.lang.String.join;
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.type;
Expand All @@ -25,6 +26,7 @@
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClassesByName;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasspathResource;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasspathResourceByName;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasspathResources;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasspathRoots;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectDirectory;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectFile;
Expand Down Expand Up @@ -298,7 +300,7 @@ class SelectClasspathResourceTests {

@SuppressWarnings("DataFlowIssue")
@Test
void selectClasspathResourcesPreconditions() {
void selectClasspathResourcePreconditions() {
// @formatter:off
assertPreconditionViolationFor(() -> selectClasspathResource((String) null));
assertPreconditionViolationFor(() -> selectClasspathResource(""));
Expand All @@ -317,7 +319,7 @@ void selectClasspathResourcesPreconditions() {
}

@Test
void selectIndividualClasspathResources() {
void selectIndividualClasspathResource() {
// with unnecessary "/" prefix
var selector = selectClasspathResource("/foo/bar/spec.xml");
assertEquals("foo/bar/spec.xml", selector.getClasspathResourceName());
Expand All @@ -328,7 +330,7 @@ void selectIndividualClasspathResources() {
}

@Test
void getSelectedClasspathResources() {
void getSelectedClasspathResource() {
var selector = selectClasspathResource("org/junit/platform/commons/example.resource");
var classpathResources = selector.getResources();
assertAll(() -> assertThat(classpathResources).hasSize(1), //
Expand All @@ -339,7 +341,7 @@ void getSelectedClasspathResources() {
}

@Test
void getMissingClasspathResources() {
void getMissingClasspathResource() {
var selector = selectClasspathResource("org/junit/platform/commons/no-such-example.resource");
assertPreconditionViolationFor(selector::getResources);
}
Expand Down Expand Up @@ -417,6 +419,52 @@ public URI getUri() {
}
}

/**
* @since 6.1
*/
@Nested
class SelectClasspathResourcesTests {

@SuppressWarnings("DataFlowIssue")
@Test
void selectClasspathResourcesPreconditions() {
assertPreconditionViolationFor(() -> selectClasspathResources((String) null));
assertPreconditionViolationFor(() -> selectClasspathResources((String[]) null));
assertPreconditionViolationFor(() -> selectClasspathResources(""));
assertPreconditionViolationFor(() -> selectClasspathResources(singletonList(null)));
assertPreconditionViolationFor(() -> selectClasspathResources(singletonList("")));
}

@Test
void selectMultipleClasspathResources() {
var selectors = selectClasspathResources( //
"org/junit/platform/example-a.resource", //
"org/junit/platform/example-b.resource" //
);
assertThat(selectors).extracting(ClasspathResourceSelector::getClasspathResourceName) //
.containsExactly( //
"org/junit/platform/example-a.resource", //
"org/junit/platform/example-b.resource" //
);
}

@Test
void selectDistinctClasspathResources() {
var selectors = selectClasspathResources( //
"org/junit/platform/example-a.resource", //
"org/junit/platform/example-b.resource", //
"org/junit/platform/example-a.resource", //
"org/junit/platform/example-c.resource" //
);
assertThat(selectors).extracting(ClasspathResourceSelector::getClasspathResourceName) //
.containsExactly( //
"org/junit/platform/example-a.resource", //
"org/junit/platform/example-b.resource", //
"org/junit/platform/example-c.resource" //
);
}
}

@Nested
class SelectModuleTests {

Expand Down
Loading