Skip to content

Commit 4364b0f

Browse files
committed
son-java21-transforms
1 parent d058788 commit 4364b0f

File tree

88 files changed

+1076
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1076
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package json.java21.transforms;
2+
3+
import jdk.sandbox.java.util.json.Json;
4+
import jdk.sandbox.java.util.json.JsonValue;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
import java.io.IOException;
10+
import java.nio.charset.StandardCharsets;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.util.Comparator;
14+
import java.util.Objects;
15+
import java.util.logging.Logger;
16+
import java.util.stream.Stream;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
/// Executes the Microsoft json-document-transforms JSON fixtures against this implementation.
21+
///
22+
/// Fixtures are vendored into:
23+
/// `json-java21-transforms/src/test/resources/microsoft-json-document-transforms/Inputs/`
24+
class MicrosoftJsonDocumentTransformsExamplesTest extends JsonTransformsLoggingConfig {
25+
26+
private static final Logger LOG = Logger.getLogger(MicrosoftJsonDocumentTransformsExamplesTest.class.getName());
27+
28+
static Stream<Arguments> microsoftFixtures() throws IOException {
29+
final Path base = Path.of(System.getProperty("transforms.test.resources"))
30+
.resolve("microsoft-json-document-transforms")
31+
.resolve("Inputs");
32+
33+
if (!Files.isDirectory(base)) {
34+
throw new IllegalStateException("Missing fixture directory: " + base);
35+
}
36+
37+
try (Stream<Path> paths = Files.walk(base)) {
38+
final var files = paths
39+
.filter(Files::isRegularFile)
40+
.filter(p -> p.toString().endsWith(".json"))
41+
.filter(p -> p.getFileName().toString().contains(".Transform"))
42+
.filter(p -> !p.toString().contains("/Skipped/"))
43+
.sorted(Comparator.comparing(Path::toString))
44+
.toList();
45+
46+
return files.stream()
47+
.map(p -> Arguments.of(base.relativize(p).toString(), p));
48+
}
49+
}
50+
51+
@ParameterizedTest(name = "{0}")
52+
@MethodSource("microsoftFixtures")
53+
void microsoftFixture(String fixtureName, Path transformFile) throws Exception {
54+
LOG.info(() -> "TEST: microsoftFixture - " + fixtureName);
55+
56+
final Path dir = transformFile.getParent();
57+
final String transformFileName = transformFile.getFileName().toString();
58+
59+
final int transformIdx = transformFileName.indexOf(".Transform");
60+
assertThat(transformIdx)
61+
.as("transform file name must contain '.Transform': %s", transformFileName)
62+
.isGreaterThan(0);
63+
64+
final String testName = transformFileName.substring(0, transformIdx);
65+
final int dot = testName.indexOf('.');
66+
final String category = dot >= 0 ? testName.substring(0, dot) : testName;
67+
68+
final Path sourceFile = dir.resolve(category + ".Source.json");
69+
final Path expectedFile = dir.resolve(testName + ".Expected.json");
70+
71+
assertThat(sourceFile)
72+
.as("missing source fixture for %s", fixtureName)
73+
.exists();
74+
assertThat(expectedFile)
75+
.as("missing expected fixture for %s", fixtureName)
76+
.exists();
77+
78+
final JsonValue source = Json.parse(readUtf8(sourceFile));
79+
final JsonValue transform = Json.parse(readUtf8(transformFile));
80+
final JsonValue expected = Json.parse(readUtf8(expectedFile));
81+
82+
final JsonTransforms compiled = JsonTransforms.parse(transform);
83+
final JsonValue actual = compiled.apply(source);
84+
85+
assertThat(actual)
86+
.as(() -> "Expected:\n" + Json.toDisplayString(expected, 2) + "\n\nActual:\n" + Json.toDisplayString(actual, 2))
87+
.isEqualTo(expected);
88+
}
89+
90+
private static String readUtf8(Path file) throws IOException {
91+
Objects.requireNonNull(file, "file must not be null");
92+
final String text = Files.readString(file, StandardCharsets.UTF_8);
93+
// Some upstream fixtures are saved with a UTF-8 BOM; Json.parse does not accept it.
94+
return text.startsWith("\ufeff") ? text.substring(1) : text;
95+
}
96+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"A": [
3+
{ "Added": true }
4+
],
5+
"B": [
6+
{
7+
"Name": "B1",
8+
"Value": 1
9+
},
10+
{
11+
"Name": "B2",
12+
"Value": 2
13+
},
14+
{
15+
"Name": "B2",
16+
"Value": 3
17+
}
18+
],
19+
"C": [
20+
{},
21+
{ "Empty": false },
22+
{ "Array": [ { "Inception": true } ] },
23+
{ "Empty": false }
24+
],
25+
"D": {
26+
"D1": [
27+
{ "Value": 1 },
28+
{ "Value": 0 }
29+
]
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"A": [
3+
{ "Added": true }
4+
],
5+
"B": [
6+
{
7+
"Name": "B2",
8+
"Value": 2
9+
},
10+
{
11+
"Name": "B2",
12+
"Value": 3
13+
}
14+
],
15+
"C": [
16+
{ "Empty": false }
17+
],
18+
"D": {
19+
"D1": [ { "Value": 0 } ]
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"A": {
3+
"Replaced": true
4+
},
5+
"B": [
6+
{
7+
"Name": "B1",
8+
"Value": 1
9+
}
10+
],
11+
"C": "Replaced",
12+
"D": {
13+
"D1": null
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"A": {
3+
"Replaced": true
4+
},
5+
"C": "Replaced",
6+
"D": {
7+
"D1": null
8+
}
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"A": [],
3+
"B": [
4+
{
5+
"Name": "B1",
6+
"Value": 1
7+
}
8+
],
9+
"C": [
10+
{},
11+
{ "Empty": false },
12+
{ "Array": [ { "Inception": true } ] }
13+
],
14+
"D": {
15+
"D1": [ { "Value": 1 } ]
16+
}
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"A": {
3+
"A1": "New"
4+
},
5+
"B": {
6+
"B1": 10,
7+
"B2": 2,
8+
"B3": 30
9+
},
10+
"C": {
11+
"C1": {
12+
"C11": true,
13+
"C12": false,
14+
"C13": [
15+
{ "Name": "C131" },
16+
{ "Name": "C132" }
17+
]
18+
},
19+
"C2": 2,
20+
"C3": {
21+
"Added": true
22+
}
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"A": {
3+
"A1": "New"
4+
},
5+
"B": {
6+
"B1": 10,
7+
"B3": 30
8+
},
9+
"C": {
10+
"C1": {
11+
"C12": false,
12+
"C13": [
13+
{ "Name": "C131" },
14+
{ "Name": "C132" }
15+
]
16+
},
17+
"C3": {
18+
"Added": true
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"A": null,
3+
"B": "Replaced",
4+
"C": {
5+
"C1": [ { "C11": true } ],
6+
"C2": 2
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"A": null,
3+
"B": "Replaced",
4+
"C": {
5+
"C1": [ {"C11": true} ]
6+
}
7+
}

0 commit comments

Comments
 (0)