Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jbduncan authored and Andrei94 committed Jun 23, 2018
1 parent 1cbc61d commit ca38f09
Show file tree
Hide file tree
Showing 22 changed files with 78 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.junit.jupiter.api;

import static java.lang.String.format;
import static java.lang.String.join;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.platform.commons.util.Preconditions.condition;
import static org.junit.platform.commons.util.Preconditions.notNull;
Expand All @@ -19,6 +20,7 @@
import java.util.Deque;
import java.util.List;
import java.util.regex.PatternSyntaxException;
import java.util.stream.IntStream;

/**
* {@code AssertLinesMatch} is a collection of utility methods that support asserting
Expand Down Expand Up @@ -55,15 +57,7 @@ static void assertLinesMatch(List<String> expectedLines, List<String> actualLine

// simple case: both list are equally sized, compare them line-by-line
if (expectedSize == actualSize) {
boolean allOk = true;
for (int i = 0; i < expectedSize; i++) {
if (matches(expectedLines.get(i), actualLines.get(i))) {
continue;
}
allOk = false;
break;
}
if (allOk) {
if (IntStream.range(0, expectedSize).allMatch(i -> matches(expectedLines.get(i), actualLines.get(i)))) {
return;
}
}
Expand Down Expand Up @@ -148,8 +142,8 @@ private static void fail(List<String> expectedLines, List<String> actualLines, S
actualLines.subList(0, MAX_SNIPPET_LENGTH);
}
// use standard assertEquals(Object, Object, message) to let IDEs present the textual difference
String expected = String.join(System.lineSeparator(), expectedLines);
String actual = String.join(System.lineSeparator(), actualLines);
String expected = join(System.lineSeparator(), expectedLines);
String actual = join(System.lineSeparator(), actualLines);
assertEquals(expected, actual, format(format, args));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ static String formatValues(Object expected, Object actual) {
return String.format("expected: %s but was: %s", formatClassAndValue(expected, expectedString),
formatClassAndValue(actual, actualString));
}
else {
return String.format("expected: <%s> but was: <%s>", expectedString, actualString);
}
return String.format("expected: <%s> but was: <%s>", expectedString, actualString);
}

private static String formatClassAndValue(Object value, String valueString) {
Expand Down Expand Up @@ -147,9 +145,7 @@ static boolean objectsAreEqual(Object obj1, Object obj2) {
if (obj1 == null) {
return (obj2 == null);
}
else {
return obj1.equals(obj2);
}
return obj1.equals(obj2);
}

private static void failIllegalDelta(String delta) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ public <E extends Extension> Stream<E> stream(Class<E> extensionType) {
if (this.parent == null) {
return streamLocal(extensionType);
}
else {
return concat(this.parent.stream(extensionType), streamLocal(extensionType));
}
return concat(this.parent.stream(extensionType), streamLocal(extensionType));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package org.junit.jupiter.engine;

import static java.util.stream.Collectors.joining;
import static java.lang.String.join;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -27,7 +27,6 @@
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -617,7 +616,7 @@ private static String concat(Class<?> c1, Class<?> c2) {
}

private static String concat(String... args) {
return Arrays.stream(args).collect(joining("."));
return join(".", args);
}

// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.junit.jupiter.engine.kotlin

import org.junit.jupiter.api.*
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS

class InstancePerMethodKotlinTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static java.util.stream.Collectors.joining;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.stream.IntStream;

import org.junit.platform.commons.JUnitException;
Expand Down Expand Up @@ -51,10 +52,8 @@ private String prepareMessageFormatPattern(int invocationIndex, Object[] argumen
private Object[] makeReadable(Object[] arguments) {
// Note: humanReadableArguments must be an Object[] in order to
// avoid varargs issues with non-Eclipse compilers.
Object[] humanReadableArguments = new String[arguments.length];
for (int i = 0; i < arguments.length; i++) {
humanReadableArguments[i] = StringUtils.nullSafeToString(arguments[i]);
}
Object[] humanReadableArguments = //
Arrays.stream(arguments).map(StringUtils::nullSafeToString).toArray(String[]::new);
return humanReadableArguments;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,7 @@ private static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElemen
}

// Meta-present on indirectly present annotations?
Optional<A> indirectMetaAnnotation = findMetaAnnotation(annotationType, element.getAnnotations(), key, visited);
if (indirectMetaAnnotation.isPresent()) {
return indirectMetaAnnotation;
}

return Optional.empty();
return findMetaAnnotation(annotationType, element.getAnnotations(), key, visited);
}

private static <A extends Annotation> Optional<A> findMetaAnnotation(Class<A> annotationType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ public enum HierarchyTraversalMode {
*
* @see Class#getName()
*/
private static final Pattern VM_INTERNAL_PRIMITIVE_ARRAY_PATTERN = //
Pattern.compile("^(\\[+)(\\[[Z,B,C,D,F,I,J,S])$");
private static final Pattern VM_INTERNAL_PRIMITIVE_ARRAY_PATTERN = Pattern.compile("^(\\[+)(\\[[ZBCDFIJS])$");

// Pattern: "java.lang.String[]", "int[]", "int[][][][]", etc.
private static final Pattern SOURCE_CODE_SYNTAX_ARRAY_PATTERN = Pattern.compile("^([^\\[\\]]+)((\\[\\])+)+$");
Expand Down Expand Up @@ -915,26 +914,21 @@ private static List<Method> getDeclaredMethods(Class<?> clazz, HierarchyTraversa
* in the Java Language Specification
*/
private static List<Method> getDefaultMethods(Class<?> clazz) {
// @formatter:off
// Visible default methods are interface default methods that have not
// been overridden.
List<Method> visibleDefaultMethods = new ArrayList<>();
for (Method candidate : clazz.getMethods()) {
if (candidate.isDefault()) {
visibleDefaultMethods.add(candidate);
}
}
List<Method> visibleDefaultMethods = Arrays.stream(clazz.getMethods())
.filter(Method::isDefault)
.collect(toCollection(ArrayList::new));
if (visibleDefaultMethods.isEmpty()) {
return visibleDefaultMethods;
}
List<Method> defaultMethods = new ArrayList<>();
for (Class<?> ifc : clazz.getInterfaces()) {
for (Method method : getMethods(ifc)) {
if (visibleDefaultMethods.contains(method)) {
defaultMethods.add(method);
}
}
}
return defaultMethods;
return Arrays.stream(clazz.getInterfaces())
.map(ReflectionUtils::getMethods)
.flatMap(List::stream)
.filter(visibleDefaultMethods::contains)
.collect(toCollection(ArrayList::new));
// @formatter:on
}

private static List<Method> toSortedMutableList(Method[] methods) {
Expand Down Expand Up @@ -1038,15 +1032,8 @@ private static boolean hasCompatibleSignature(Method candidate, String methodNam
}

static boolean isGeneric(Method method) {
if (isGeneric(method.getGenericReturnType())) {
return true;
}
for (Type type : method.getGenericParameterTypes()) {
if (isGeneric(type)) {
return true;
}
}
return false;
return isGeneric(method.getGenericReturnType())
|| Arrays.stream(method.getGenericParameterTypes()).anyMatch(ReflectionUtils::isGeneric);
}

private static boolean isGeneric(Type type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,7 @@ public static boolean isNotBlank(String str) {
* @see Character#isWhitespace(int)
*/
public static boolean containsWhitespace(String str) {
if (str == null) {
return false;
}

for (int i = 0; i < str.length(); i++) {
if (Character.isWhitespace(str.codePointAt(i))) {
return true;
}
}

return false;
return str != null && str.codePoints().anyMatch(Character::isWhitespace);
}

/**
Expand All @@ -106,17 +96,7 @@ public static boolean doesNotContainWhitespace(String str) {
* @see Character#isISOControl(int)
*/
public static boolean containsIsoControlCharacter(String str) {
if (str == null) {
return false;
}

for (int i = 0; i < str.length(); i++) {
if (Character.isISOControl(str.codePointAt(i))) {
return true;
}
}

return false;
return str != null && str.codePoints().anyMatch(Character::isISOControl);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package org.junit.platform.commons.util;

import static java.lang.String.join;
import static org.junit.platform.commons.meta.API.Usage.Internal;

import java.util.ArrayList;
Expand Down Expand Up @@ -51,15 +52,12 @@ public ToStringBuilder append(String name, Object value) {
}

private String toString(Object obj) {
if (obj instanceof CharSequence) {
return "'" + obj + "'";
}
return StringUtils.nullSafeToString(obj);
return (obj instanceof CharSequence) ? ("'" + obj + "'") : StringUtils.nullSafeToString(obj);
}

@Override
public String toString() {
return this.type.getSimpleName() + " [" + String.join(", ", this.values) + "]";
return this.type.getSimpleName() + " [" + join(", ", this.values) + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,43 +79,43 @@ public static Theme valueOf(Charset charset) {
this.blank = new String(new char[vertical().length()]).replace('\0', ' ');
}

public String root() {
public final String root() {
return tiles[0];
}

public String vertical() {
public final String vertical() {
return tiles[1];
}

public String blank() {
public final String blank() {
return blank;
}

public String entry() {
public final String entry() {
return tiles[2];
}

public String end() {
public final String end() {
return tiles[3];
}

public String successful() {
public final String successful() {
return tiles[4];
}

public String aborted() {
public final String aborted() {
return tiles[5];
}

public String failed() {
public final String failed() {
return tiles[6];
}

public String skipped() {
public final String skipped() {
return tiles[7];
}

public String status(TestExecutionResult result) {
public final String status(TestExecutionResult result) {
switch (result.getStatus()) {
case SUCCESSFUL:
return successful();
Expand All @@ -133,7 +133,7 @@ public String status(TestExecutionResult result) {
* available options.
*/
@Override
public String toString() {
public final String toString() {
return name().toLowerCase();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ private Optional<TestExecutionListener> createDetailsPrintingListener(PrintWrite
}

private Optional<TestExecutionListener> createXmlWritingListener(PrintWriter out) {
if (options.getReportsDir().isPresent()) {
return Optional.of(new XmlReportsWritingListener(options.getReportsDir().get(), out));
}
return Optional.empty();
return options.getReportsDir().map(reportsDir -> new XmlReportsWritingListener(reportsDir, out));
}

private void printSummary(TestExecutionSummary summary, PrintWriter out) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,7 @@ public static MethodSelector selectMethod(String fullyQualifiedMethodName) throw
if (StringUtils.isNotBlank(methodParameters)) {
return selectMethod(className, methodName, methodParameters);
}
else {
return selectMethod(className, methodName);
}
return selectMethod(className, methodName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static Condition<Throwable> suppressed(int index, Condition<Throwable> ch
}

public static Condition<TestExecutionResult> cause(Condition<? super Throwable> condition) {
return new Condition<TestExecutionResult>(where(TestExecutionResult::getThrowable, throwable -> {
return new Condition<>(where(TestExecutionResult::getThrowable, throwable -> {
return throwable.isPresent() && condition.matches(throwable.get());
}), "cause where %s", condition);
}
Expand Down
Loading

0 comments on commit ca38f09

Please sign in to comment.