Skip to content

Commit e74c14c

Browse files
Make CannedFormattedString a record and add null checks. Add CannedFormattedString.CannedArgument interface to make CannedFormattedString instances provide system-independent test descriptions.
1 parent 2ede257 commit e74c14c

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/CannedFormattedString.java

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,60 @@
2222
*/
2323
package jdk.jpackage.test;
2424

25+
import java.nio.file.Path;
2526
import java.util.List;
27+
import java.util.Objects;
2628
import java.util.function.BiFunction;
29+
import java.util.function.Supplier;
30+
import java.util.stream.Stream;
2731

28-
public final class CannedFormattedString {
32+
public record CannedFormattedString(BiFunction<String, Object[], String> formatter, String key, Object[] args) {
2933

30-
CannedFormattedString(BiFunction<String, Object[], String> formatter,
31-
String key, Object[] args) {
32-
this.formatter = formatter;
33-
this.key = key;
34-
this.args = args;
34+
@FunctionalInterface
35+
public interface CannedArgument {
36+
public String value();
37+
}
38+
39+
public static Object cannedArgument(Supplier<Object> supplier, String label) {
40+
Objects.requireNonNull(supplier);
41+
Objects.requireNonNull(label);
42+
return new CannedArgument() {
43+
44+
@Override
45+
public String value() {
46+
return supplier.get().toString();
47+
}
48+
49+
@Override
50+
public String toString( ) {
51+
return label;
52+
}
53+
};
54+
}
55+
56+
public static Object cannedAbsolutePath(Path v) {
57+
return cannedArgument(() -> v.toAbsolutePath(), String.format("AbsolutePath(%s)", v));
58+
}
59+
60+
public static Object cannedAbsolutePath(String v) {
61+
return cannedAbsolutePath(Path.of(v));
62+
}
63+
64+
public CannedFormattedString {
65+
Objects.requireNonNull(formatter);
66+
Objects.requireNonNull(key);
67+
Objects.requireNonNull(args);
68+
List.of(args).forEach(Objects::requireNonNull);
3569
}
3670

3771
public String getValue() {
38-
return formatter.apply(key, args);
72+
return formatter.apply(key, Stream.of(args).map(arg -> {
73+
if (arg instanceof CannedArgument cannedArg) {
74+
return cannedArg.value();
75+
} else {
76+
return arg;
77+
}
78+
}).toArray());
3979
}
4080

4181
@Override
@@ -46,8 +86,4 @@ public String toString() {
4686
return String.format("%s+%s", key, List.of(args));
4787
}
4888
}
49-
50-
private final BiFunction<String, Object[], String> formatter;
51-
private final String key;
52-
private final Object[] args;
5389
}

0 commit comments

Comments
 (0)