Skip to content

Commit 2ac1470

Browse files
committed
Improve runTCK
1 parent cca8f47 commit 2ac1470

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

coverage/src/main/java/fr/insee/vtl/coverage/TCK.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import fr.insee.vtl.coverage.utils.JSONStructureLoader;
77

88
import java.io.File;
9+
import java.io.FileInputStream;
910
import java.io.IOException;
11+
import java.io.InputStream;
1012
import java.nio.charset.StandardCharsets;
1113
import java.nio.file.Files;
1214
import java.nio.file.Path;
@@ -18,26 +20,38 @@
1820
public class TCK {
1921
private static final ObjectMapper objectMapper = new ObjectMapper();
2022

21-
public static List<Folder> runTCK(String zipPath) {
22-
File extractedFolder = null;
23+
public static List<Folder> runTCK(InputStream zipInputStream) {
24+
File extractedFolder;
2325
try {
24-
extractedFolder = init(new File(zipPath));
26+
extractedFolder = init(zipInputStream);
2527
} catch (IOException e) {
26-
throw new RuntimeException(e);
28+
throw new RuntimeException("Error unzipping input stream", e);
2729
}
28-
List<Folder> folders;
30+
2931
try {
30-
folders = loadInput(extractedFolder);
32+
return loadInput(extractedFolder);
3133
} catch (Exception e) {
32-
throw new RuntimeException(e);
34+
throw new RuntimeException("Error loading input from extracted folder", e);
35+
} finally {
36+
deleteDirectory(extractedFolder);
3337
}
34-
deleteDirectory(extractedFolder);
35-
return folders;
3638
}
3739

38-
private static File init(File zipFile) throws IOException {
40+
public static List<Folder> runTCK(File zipFile) {
41+
try (InputStream in = Files.newInputStream(zipFile.toPath())) {
42+
return runTCK(in);
43+
} catch (IOException e) {
44+
throw new RuntimeException("Error reading zip file: " + zipFile, e);
45+
}
46+
}
47+
48+
public static List<Folder> runTCK(String zipPath) {
49+
return runTCK(new File(zipPath));
50+
}
51+
52+
private static File init(InputStream zipInputStream) throws IOException {
3953
Path tempDir = Files.createTempDirectory("tck-unzip-");
40-
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(zipFile.toPath()))) {
54+
try (ZipInputStream zis = new ZipInputStream(zipInputStream)) {
4155
ZipEntry entry;
4256
while ((entry = zis.getNextEntry()) != null) {
4357
Path newPath = zipSlipProtect(entry, tempDir);

coverage/src/test/java/fr/insee/vtl/coverage/TCKTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.jupiter.api.*;
99

1010
import javax.script.*;
11+
import java.io.InputStream;
1112
import java.util.ArrayList;
1213
import java.util.List;
1314
import java.util.Map;
@@ -35,12 +36,11 @@ public void setUp() {
3536

3637
@TestFactory
3738
Stream<DynamicNode> generateTests() {
38-
String zipPath = "v2.1.zip";
39-
boolean resourceExists = getClass().getClassLoader().getResource(zipPath) != null;
39+
InputStream in = getClass().getClassLoader().getResourceAsStream("v2.1.zip");
4040
// Skip the test factory entirely if file is not present
41-
Assumptions.assumeTrue(resourceExists, "Skipping TCK tests: resource file not found");
41+
Assumptions.assumeTrue(in != null, "Skipping TCK tests: resource file not found");
4242

43-
List<Folder> tests = TCK.runTCK(zipPath);
43+
List<Folder> tests = TCK.runTCK(in);
4444
Folder root = new Folder();
4545
root.setName("root");
4646
root.setFolders(tests);

0 commit comments

Comments
 (0)