Skip to content

Commit

Permalink
address comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
hatyo committed Feb 6, 2025
1 parent 86ef7ae commit 9c750e7
Showing 1 changed file with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.apple.foundationdb.relational.util.SpotBugsSuppressWarnings;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multiset;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;
Expand Down Expand Up @@ -64,7 +63,7 @@ public static List<?> arrayList(@Nonnull final Object obj, @Nonnull final String
if (obj instanceof List) {
return (List<?>) obj;
}
fail(String.format("Expecting '%s' to be of type '%s'", desc, List.class.getSimpleName()));
fail(String.format(Locale.ROOT, "Expecting '%s' to be of type '%s'", desc, List.class.getSimpleName()));
return null;
}

Expand All @@ -80,15 +79,15 @@ public static List<?> arrayList(@Nonnull final Object obj, @Nonnull final String
if (obj instanceof Map<?, ?>) {
return (Map<?, ?>) obj;
}
fail(String.format("Expecting %s to be of type %s", desc, Map.class.getSimpleName()));
fail(String.format(Locale.ROOT, "Expecting %s to be of type %s", desc, Map.class.getSimpleName()));
return null;
}

public static Map.Entry<?, ?> mapEntry(@Nonnull final Object obj, @Nonnull final String desc) {
if (obj instanceof Map.Entry<?, ?>) {
return (Map.Entry<?, ?>) obj;
}
fail(String.format("Expecting %s to be of type %s", desc, Map.Entry.class.getSimpleName()));
fail(String.format(Locale.ROOT, "Expecting %s to be of type %s", desc, Map.Entry.class.getSimpleName()));
return null;
}

Expand All @@ -98,21 +97,21 @@ public static Object first(@Nonnull final List<?> obj) {

public static Object first(@Nonnull final List<?> obj, @Nonnull final String desc) {
if (obj.isEmpty()) {
fail(String.format("Expecting %s to contain at least one element, however it is empty", desc));
fail(String.format(Locale.ROOT, "Expecting %s to contain at least one element, however it is empty", desc));
}
return obj.get(0);
}

public static Object second(@Nonnull final List<?> obj) {
if (obj.size() <= 1) {
fail(String.format("Expecting %s to contain at least two elements, however it contains %s element", obj, obj.size()));
fail(String.format(Locale.ROOT, "Expecting %s to contain at least two elements, however it contains %s element", obj, obj.size()));
}
return obj.get(1);
}

public static Object third(@Nonnull final List<?> obj) {
if (obj.size() <= 2) {
fail(String.format("Expecting %s to contain at least three elements, however it contains %s element", obj, obj.size()));
fail(String.format(Locale.ROOT, "Expecting %s to contain at least three elements, however it contains %s element", obj, obj.size()));
}
return obj.get(2);
}
Expand All @@ -122,7 +121,7 @@ public static String string(@Nonnull final Object obj, @Nonnull final String des
// <NULL> should return null maybe?
return (String) obj;
}
fail(String.format("Expecting %s to be of type %s, however it is of type %s", desc, String.class.getSimpleName(), obj.getClass().getSimpleName()));
fail(String.format(Locale.ROOT, "Expecting %s to be of type %s, however it is of type %s", desc, String.class.getSimpleName(), obj.getClass().getSimpleName()));
return null;
}

Expand All @@ -147,7 +146,7 @@ public static boolean bool(@Nonnull final Object obj) {
// <NULL> should return null maybe?
return (Boolean) obj;
}
fail(String.format("Expecting %s to be of type %s, however it is of type %s", obj, Boolean.class.getSimpleName(), obj.getClass().getSimpleName()));
fail(String.format(Locale.ROOT, "Expecting %s to be of type %s, however it is of type %s", obj, Boolean.class.getSimpleName(), obj.getClass().getSimpleName()));
return false; // never reached.
}

Expand All @@ -162,7 +161,7 @@ public static long longValue(@Nonnull final Object obj) {
if (obj instanceof Integer) {
return Long.valueOf((Integer) obj);
}
fail(String.format("Expecting %s to be of type %s, however it is of type %s", obj, Long.class.getSimpleName(), obj.getClass().getSimpleName()));
fail(String.format(Locale.ROOT, "Expecting %s to be of type %s, however it is of type %s", obj, Long.class.getSimpleName(), obj.getClass().getSimpleName()));
return -1; // never reached.
}

Expand All @@ -174,7 +173,7 @@ public static int intValue(@Nonnull final Object obj) {
if (obj instanceof Integer) {
return (Integer) obj;
}
fail(String.format("Expecting %s to be of type %s, however it is of type %s", obj, Integer.class.getSimpleName(), obj.getClass().getSimpleName()));
fail(String.format(Locale.ROOT, "Expecting %s to be of type %s, however it is of type %s", obj, Integer.class.getSimpleName(), obj.getClass().getSimpleName()));
return -1; // never reached.
}

Expand All @@ -186,7 +185,7 @@ public static double doubleValue(@Nonnull final Object obj) {
if (obj instanceof Double) {
return (Double) obj;
}
fail(String.format("Expecting %s to be of type %s, however it is of type %s", obj, Double.class.getSimpleName(), obj.getClass().getSimpleName()));
fail(String.format(Locale.ROOT, "Expecting %s to be of type %s, however it is of type %s", obj, Double.class.getSimpleName(), obj.getClass().getSimpleName()));
return -1; // never reached.
}

Expand All @@ -208,15 +207,15 @@ public static Message message(@Nonnull final Object obj) {
if (obj instanceof Message) {
return (Message) obj;
}
fail(String.format("Expecting %s to be of type %s, however it is of type %s.", obj, Message.class.getSimpleName(), obj.getClass().getSimpleName()));
fail(String.format(Locale.ROOT, "Expecting %s to be of type %s, however it is of type %s.", obj, Message.class.getSimpleName(), obj.getClass().getSimpleName()));
return null;
}

@SpotBugsSuppressWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "should never happen, fail throws")
@Nonnull
public static <T> T notNull(@Nullable final T object, @Nonnull final String desc) {
if (object == null) {
fail(String.format("unexpected %s to be null", desc));
fail(String.format(Locale.ROOT, "unexpected %s to be null", desc));
}
return object;
}
Expand All @@ -227,14 +226,14 @@ public static <T> T notNull(@Nullable final T object, @Nonnull final String desc
if (obj instanceof Map) {
return ((Map<?, ?>) obj).entrySet().iterator().next();
}
fail(String.format("Expecting %s to be of type %s, however it is of type %s.", desc, Map.class.getSimpleName(), obj.getClass().getSimpleName()));
fail(String.format(Locale.ROOT, "Expecting %s to be of type %s, however it is of type %s.", desc, Map.class.getSimpleName(), obj.getClass().getSimpleName()));
return null;
}

@Nonnull
public static Object valueElseKey(@Nonnull final Map.Entry<?, ?> entry) {
if (isNull(entry.getKey()) && isNull(entry.getValue())) {
fail(String.format("encountered YAML-style 'null' which is not supported, consider using '%s' instead", CustomTag.NullPlaceholder.INSTANCE));
fail(String.format(Locale.ROOT, "encountered YAML-style 'null' which is not supported, consider using '%s' instead", CustomTag.NullPlaceholder.INSTANCE));
}
return entry.getValue() == null ? entry.getKey() : entry.getValue();
}
Expand Down Expand Up @@ -430,7 +429,7 @@ private static ImmutablePair<ResultSetMatchResult, ResultSetPrettyPrinter> match
for (final var expectedRow : expectedAsList) {
if (!actual.next()) {
printCurrentAndRemaining(actual, resultSetPrettyPrinter);
return ImmutablePair.of(ResultSetMatchResult.fail(String.format("result does not contain all expected rows! expected %d rows, got %d", expectedAsList.size(), i - 1)), null);
return ImmutablePair.of(ResultSetMatchResult.fail(String.format(Locale.ROOT, "result does not contain all expected rows! expected %d rows, got %d", expectedAsList.size(), i - 1)), null);
}
if (!isMap(expectedRow)) { // I think it should be possible to expect a result set like: [[1,2,3], [4,5,6]]. But ok for now.
printCurrentAndRemaining(actual, resultSetPrettyPrinter);
Expand All @@ -444,7 +443,7 @@ private static ImmutablePair<ResultSetMatchResult, ResultSetPrettyPrinter> match
}
if (printRemaining(actual, resultSetPrettyPrinter)) {
final var leftActualRowCount = resultSetPrettyPrinter.getRowCount();
return ImmutablePair.of(ResultSetMatchResult.fail(String.format("too many rows in actual result set! expected %d rows, got %d.", expectedAsList.size(), expectedAsList.size() + leftActualRowCount)), resultSetPrettyPrinter);
return ImmutablePair.of(ResultSetMatchResult.fail(String.format(Locale.ROOT, "too many rows in actual result set! expected %d rows, got %d.", expectedAsList.size(), expectedAsList.size() + leftActualRowCount)), resultSetPrettyPrinter);
}
return ImmutablePair.of(ResultSetMatchResult.success(), null);
}
Expand All @@ -461,7 +460,7 @@ private static ImmutablePair<ResultSetMatchResult, ResultSetPrettyPrinter> match
while (actual.next()) {
actualRowsCounter++;
}
return ImmutablePair.of(ResultSetMatchResult.fail(String.format("too many rows in actual result set! expected %d row(s), got %d row(s) instead.", expectedRowCount, actualRowsCounter - 1)), resultSetPrettyPrinter);
return ImmutablePair.of(ResultSetMatchResult.fail(String.format(Locale.ROOT, "too many rows in actual result set! expected %d row(s), got %d row(s) instead.", expectedRowCount, actualRowsCounter - 1)), resultSetPrettyPrinter);
}
for (final var expectedRow : expectedAsMultiSet.elementSet()) {
if (!isMap(expectedRow)) { // I think it should be possible to expect a result set like: [[1,2,3], [4,5,6]]. But ok for now.
Expand All @@ -477,12 +476,12 @@ private static ImmutablePair<ResultSetMatchResult, ResultSetPrettyPrinter> match
}
if (!found) {
printCurrentAndRemaining(actual, resultSetPrettyPrinter);
return ImmutablePair.of(ResultSetMatchResult.fail(String.format("actual row at %d does not match any expected records", actualRowsCounter)), resultSetPrettyPrinter);
return ImmutablePair.of(ResultSetMatchResult.fail(String.format(Locale.ROOT, "actual row at %d does not match any expected records", actualRowsCounter)), resultSetPrettyPrinter);
}
actualRowsCounter++;
}
if (!expectedAsMultiSet.isEmpty()) {
return ImmutablePair.of(ResultSetMatchResult.fail(String.format("result does not contain all expected rows, expected %d row(s), got %d row(s) instead.", expectedRowCount, actualRowsCounter - 1)), null);
return ImmutablePair.of(ResultSetMatchResult.fail(String.format(Locale.ROOT, "result does not contain all expected rows, expected %d row(s), got %d row(s) instead.", expectedRowCount, actualRowsCounter - 1)), null);
}
return ImmutablePair.of(ResultSetMatchResult.success(), null);
}
Expand Down Expand Up @@ -517,7 +516,7 @@ private static ResultSetMatchResult matchRow(@Nonnull final Map<?, ?> expected,
int rowNumber) throws SQLException {
final var expectedColCount = expected.entrySet().size();
if (actualEntriesCount != expectedColCount) {
return ResultSetMatchResult.fail(String.format("row cardinality mismatch at %d! expected a row comprising %d column(s), received %d column(s) instead.", rowNumber, expectedColCount, actualEntriesCount));
return ResultSetMatchResult.fail(String.format(Locale.ROOT, "row cardinality mismatch at %d! expected a row comprising %d column(s), received %d column(s) instead.", rowNumber, expectedColCount, actualEntriesCount));
}
return matchMap(expected, actualEntriesCount, entryByNameAccessor, entryByNumberAccessor, rowNumber, "");
}
Expand All @@ -532,7 +531,7 @@ private static ResultSetMatchResult matchMap(@Nonnull final Map<?, ?> expected,
int counter = 1;
final var expectedColCount = expected.entrySet().size();
if (actualEntriesCount != expectedColCount) {
return ResultSetMatchResult.fail(String.format("! expected a row comprising %d column(s), received %d column(s) instead.", expectedColCount, actualEntriesCount));
return ResultSetMatchResult.fail(String.format(Locale.ROOT, "! expected a row comprising %d column(s), received %d column(s) instead.", expectedColCount, actualEntriesCount));
}
for (final var entry : expected.entrySet()) {
final var expectedField = valueElseKey(entry);
Expand Down Expand Up @@ -578,7 +577,7 @@ private static ResultSetMatchResult matchField(@Nullable final Object expected,
// (nested) message
if (expected instanceof Map<?, ?>) {
if (!(actual instanceof RelationalStruct)) {
return ResultSetMatchResult.fail(String.format("cell mismatch at row: %d cellRef: %s%n expected 🟒 to match a struct, got 🟑 instead.%n🟒 %s (Struct)%n🟑 %s (%s)", rowNumber, cellRef, expected, actual, actual.getClass().getSimpleName()));
return ResultSetMatchResult.fail(String.format(Locale.ROOT, "cell mismatch at row: %d cellRef: %s%n expected 🟒 to match a struct, got 🟑 instead.%n🟒 %s (Struct)%n🟑 %s (%s)", rowNumber, cellRef, expected, actual, actual.getClass().getSimpleName()));
}
final var struct = (RelationalStruct) (actual);
return matchMap(map(expected), struct.getAttributes().length, valueByName(struct), valueByIndex(struct), rowNumber, cellRef);
Expand All @@ -588,13 +587,13 @@ private static ResultSetMatchResult matchField(@Nullable final Object expected,
if (expected instanceof List<?>) {
final var expectedArray = (List<?>) (expected);
if (!(actual instanceof RelationalArray)) {
return ResultSetMatchResult.fail(String.format("cell mismatch at row: %d cellRef: %s%n expected 🟒 to match an array, got 🟑 instead.%n🟒 %s (Array)%n🟑 %s (%s)", rowNumber, cellRef, expected, actual, actual.getClass().getSimpleName()));
return ResultSetMatchResult.fail(String.format(Locale.ROOT, "cell mismatch at row: %d cellRef: %s%n expected 🟒 to match an array, got 🟑 instead.%n🟒 %s (Array)%n🟑 %s (%s)", rowNumber, cellRef, expected, actual, actual.getClass().getSimpleName()));
}
final var actualArray = (RelationalArray) (actual);
final var actualArrayContent = actualArray.getResultSet();
for (int i = 0; i < expectedArray.size(); i++) {
if (!actualArrayContent.next()) {
return ResultSetMatchResult.fail(String.format("cell mismatch at row: %d cellRef: %s%n expected 🟒 (containing %d array items) does not match 🟑 (containing %d array items).%n🟒 %s%n🟑 %s",
return ResultSetMatchResult.fail(String.format(Locale.ROOT, "cell mismatch at row: %d cellRef: %s%n expected 🟒 (containing %d array items) does not match 🟑 (containing %d array items).%n🟒 %s%n🟑 %s",
rowNumber, cellRef, expectedArray.size(), i, expected, actual));
}
if (isMap(expectedArray.get(i))) {
Expand Down Expand Up @@ -649,7 +648,7 @@ private static ResultSetMatchResult matchField(@Nullable final Object expected,
if (Objects.equals(expected, actual)) {
return ResultSetMatchResult.success();
} else {
return ResultSetMatchResult.fail(String.format("cell mismatch at row: %d cellRef: %s%n expected 🟒 does not match 🟑.%n🟒 %s (%s)%n🟑 %s (%s)", rowNumber, cellRef, expected, expected == null ? "NULL" : expected.getClass().getSimpleName(), actual, actual.getClass().getSimpleName()));
return ResultSetMatchResult.fail(String.format(Locale.ROOT, "cell mismatch at row: %d cellRef: %s%n expected 🟒 does not match 🟑.%n🟒 %s (%s)%n🟑 %s (%s)", rowNumber, cellRef, expected, expected == null ? "NULL" : expected.getClass().getSimpleName(), actual, actual.getClass().getSimpleName()));
}
}

Expand All @@ -671,6 +670,6 @@ private static ResultSetMatchResult matchIntField(@Nonnull final Integer expecte
return ResultSetMatchResult.success();
}
}
return ResultSetMatchResult.fail(String.format("cell mismatch at row: %d cellRef: %s%n expected 🟒 does not match 🟑.%n🟒 %s (Integer) %n🟑 %s (%s)", rowNumber, cellRef, expected, actual, actual.getClass().getSimpleName()));
return ResultSetMatchResult.fail(String.format(Locale.ROOT, "cell mismatch at row: %d cellRef: %s%n expected 🟒 does not match 🟑.%n🟒 %s (Integer) %n🟑 %s (%s)", rowNumber, cellRef, expected, actual, actual.getClass().getSimpleName()));
}
}

0 comments on commit 9c750e7

Please sign in to comment.