Skip to content

Commit 2ceef13

Browse files
jorabinclaude
andcommitted
Add TestUtil.testOutputPath and use it wherever tests write output (#108)
testOutputPath creates the testOutput directory as well as giving the path, so a test can't depend on another having created it. Issue87Test and Issue88Test wrote there without creating it. The shared KdbxFileSaveAndReloadTest and ProtectedPropertyTest2 relied on their implementers extending DatabaseTestBase; they now create it themselves. DatabaseTestBase uses the same directory name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 576ec65 commit 2ceef13

7 files changed

Lines changed: 40 additions & 25 deletions

File tree

‎example/src/test/java/org/linguafranca/pwdb/kdbx/SimpleQuickStartTest.java‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.linguafranca.pwdb.kdbx;
1919

20-
import org.junit.jupiter.api.BeforeAll;
2120
import org.junit.jupiter.api.Test;
2221
import org.linguafranca.pwdb.format.KdbxCredentials;
2322
import org.linguafranca.pwdb.format.KdbxStreamFormat;
@@ -27,9 +26,9 @@
2726
import java.io.IOException;
2827
import java.nio.file.Files;
2928
import java.nio.file.Path;
30-
import java.nio.file.Paths;
3129

3230
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.linguafranca.util.TestUtil.testOutputPath;
3332

3433
/**
3534
* Simple illustration of hooking a SAX parser up to process a KDBX file
@@ -39,11 +38,6 @@
3938
public class SimpleQuickStartTest extends QuickStart {
4039

4140

42-
@BeforeAll
43-
public static void ensureOutputDir() throws IOException {
44-
Files.createDirectories(Paths.get(TEST_OUTPUT_DIR));
45-
}
46-
4741
@Test
4842
public void canonicalQuickstartTest() throws IOException {
4943
super.canonicalQuickStart();
@@ -71,7 +65,7 @@ public void loadKdbSaveKdbx() throws IOException {
7165

7266
@Test
7367
public void loadSave() throws IOException {
74-
Path path = Paths.get("testOutput/CHACHA-AES-CHACHA.kdbx");
68+
Path path = testOutputPath("CHACHA-AES-CHACHA.kdbx");
7569

7670
loadKdbx3SaveKdbx4("test123.kdbx","123".getBytes(), path);
7771

@@ -85,7 +79,7 @@ public void loadSave() throws IOException {
8579
}
8680
@Test
8781
public void loadSave2() throws IOException {
88-
Path path = Paths.get("testOutput/CHACHA-AES-CHACHA.kdbx");
82+
Path path = testOutputPath("CHACHA-AES-CHACHA.kdbx");
8983

9084
loadKdbx4SaveKdbx3("V4-ChaCha20-Argon2-Attachment.kdbx","123".getBytes(), path);
9185

‎kdbx-database/src/test/java/org/linguafranca/pwdb/kdbx/database/validation/Issue87Test.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
import java.io.OutputStream;
1515
import java.nio.file.Files;
1616
import java.nio.file.Path;
17-
import java.nio.file.Paths;
1817
import java.util.List;
1918
import java.util.UUID;
2019

2120
import static org.junit.jupiter.api.Assertions.*;
2221
import static org.linguafranca.pwdb.kdbx.jackson.util.Util.streamToString;
22+
import static org.linguafranca.util.TestUtil.testOutputPath;
2323

2424
/**
2525
* Review Issue-87 <a href="https://github.com/jorabin/KeePassJava2/issues/87">...</a>
@@ -29,7 +29,6 @@ public class Issue87Test {
2929

3030
public static final String TEST_RESOURCE1 = "issue-87/customIcon-123.kdbx";
3131
public static final KdbxCredentials CREDENTIALS1 = new KdbxCredentials("123".getBytes());
32-
public static final String TEST_OUTPUT_ISSUE_87_KDBX = "testOutput/Issue87.kdbx";
3332

3433

3534
@Test
@@ -48,7 +47,7 @@ public void testCustomIcon() throws IOException {
4847
System.out.println("Custom icon id: " + customIcon);
4948
KeePassFile.Icon icon = entry.getCustomIcon();
5049

51-
Path path = Paths.get(TEST_OUTPUT_ISSUE_87_KDBX);
50+
Path path = testOutputPath("Issue87.kdbx");
5251
try (OutputStream outputStream = Files.newOutputStream(path)) {
5352
database.save(CREDENTIALS1, outputStream);
5453
}

‎kdbx-database/src/test/java/org/linguafranca/pwdb/kdbx/database/validation/Issue88Test.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import java.io.OutputStream;
1212
import java.nio.file.Files;
1313
import java.nio.file.Path;
14-
import java.nio.file.Paths;
1514

1615
import static org.linguafranca.pwdb.kdbx.jackson.util.Util.streamToString;
16+
import static org.linguafranca.util.TestUtil.testOutputPath;
1717

1818
/**
1919
* Review Issue-88 <a href="https://github.com/jorabin/KeePassJava2/issues/88">...</a>
@@ -23,7 +23,6 @@ public class Issue88Test {
2323

2424
public static final String TEST_RESOURCE1 = "issue-88/newDb-123.kdbx";
2525
public static final KdbxCredentials CREDENTIALS1 = new KdbxCredentials("123".getBytes());
26-
public static final String TEST_OUTPUT_ISSUE_88_KDBX = "testOutput/Issue88.kdbx";
2726

2827

2928
@Test
@@ -33,7 +32,7 @@ public void testDefaultAutoType() throws IOException {
3332
database = KdbxDatabase.load(CREDENTIALS1, inputStream);
3433
}
3534

36-
Path path = Paths.get(TEST_OUTPUT_ISSUE_88_KDBX);
35+
Path path = testOutputPath("Issue88.kdbx");
3736
try (OutputStream outputStream = Files.newOutputStream(path)) {
3837
database.save(CREDENTIALS1, outputStream);
3938
}

‎test/src/main/java/org/linguafranca/pwdb/test/DatabaseTestBase.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.function.BiFunction;
2929
import java.util.function.Function;
3030
import java.util.function.Supplier;
31+
import org.linguafranca.util.TestUtil;
3132

3233
public class DatabaseTestBase {
3334

@@ -46,7 +47,7 @@ default TriConsumer<A, B, C> andThen(TriConsumer<? super A, ? super B, ? super C
4647
}
4748
}
4849

49-
public static String OUTPUT_DIRECTORY_PATH = "testOutput";
50+
public static String OUTPUT_DIRECTORY_PATH = TestUtil.TEST_OUTPUT_DIR;
5051

5152
@BeforeAll
5253
static void baseBeforeAll() throws Exception {

‎test/src/main/java/org/linguafranca/pwdb/test/KdbxFileSaveAndReloadTest.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838

3939
import java.io.*;
4040
import java.nio.file.Files;
41-
import java.nio.file.Paths;
4241
import java.util.UUID;
4342

4443
import static org.junit.jupiter.api.Assertions.*;
4544
import static org.linguafranca.pwdb.Entry.STANDARD_PROPERTY_NAME.*;
4645
import static org.linguafranca.util.TestUtil.getTestPrintStream;
46+
import static org.linguafranca.util.TestUtil.testOutputPath;
4747

4848

4949
/**
@@ -75,7 +75,7 @@ default void saveAndReloadTest() throws IOException {
7575
verifyContents(output);
7676
//output.save(new StreamFormat.None(), new Credentials.None(), printStream);
7777

78-
FileOutputStream fos = new FileOutputStream("testOutput/test1.kdbx");
78+
FileOutputStream fos = new FileOutputStream(testOutputPath("test1.kdbx").toFile());
7979
saveDatabase(output, getCredentials("123".getBytes()), fos);
8080
assertFalse(output.isDirty());
8181
fos.flush();
@@ -85,7 +85,7 @@ default void saveAndReloadTest() throws IOException {
8585
//output.save(new StreamFormat.None(), new Credentials.None(), printStream);
8686

8787

88-
FileInputStream fis = new FileInputStream("testOutput/test1.kdbx");
88+
FileInputStream fis = new FileInputStream(testOutputPath("test1.kdbx").toFile());
8989
Database input = loadDatabase(getCredentials("123".getBytes()), fis);
9090
verifyContents(input);
9191
}
@@ -108,12 +108,12 @@ default void saveAndReloadTest2() throws IOException {
108108
entry.setBinaryProperty("letter L.jpeg", content);
109109
assertArrayEquals(new String[] {"letter J.jpeg", "letter L.jpeg"}, entry.getBinaryPropertyNames().toArray());
110110

111-
FileOutputStream fos = new FileOutputStream("testOutput/test2.kdbx");
111+
FileOutputStream fos = new FileOutputStream(testOutputPath("test2.kdbx").toFile());
112112
saveDatabase(attachment, getCredentials("123".getBytes()), fos);
113113
fos.flush();
114114
fos.close();
115115

116-
FileInputStream fis = new FileInputStream("testOutput/test2.kdbx");
116+
FileInputStream fis = new FileInputStream(testOutputPath("test2.kdbx").toFile());
117117
Database input = loadDatabase(getCredentials("123".getBytes()), fis);
118118

119119
entry = input.findEntries("Test attachment").get(0);
@@ -137,9 +137,9 @@ default void saveAndReloadTest3() throws IOException {
137137
getClass().getClassLoader().getResourceAsStream(resource));
138138
StreamFormat<?> format1 = database.getStreamFormat();
139139

140-
database.save(getCredentials("123".getBytes()), Files.newOutputStream(Paths.get("testOutput/test3.kdbx")));
140+
database.save(getCredentials("123".getBytes()), Files.newOutputStream(testOutputPath("test3.kdbx")));
141141

142-
FileInputStream fis = new FileInputStream("testOutput/test3.kdbx");
142+
FileInputStream fis = new FileInputStream(testOutputPath("test3.kdbx").toFile());
143143
Database input = loadDatabase(getCredentials("123".getBytes()), fis);
144144
StreamFormat<?> format2 = input.getStreamFormat();
145145
assertTrue(verifyStreamFormat(format1, format2));

‎test/src/main/java/org/linguafranca/pwdb/test/ProtectedPropertyTest2.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.function.Consumer;
3030

3131
import static org.junit.jupiter.api.Assertions.*;
32+
import static org.linguafranca.util.TestUtil.testOutputPath;
3233

3334
public interface ProtectedPropertyTest2 {
3435

@@ -129,13 +130,13 @@ default void expectedStorageType() throws IOException {
129130

130131
// Save database
131132

132-
FileOutputStream fos = new FileOutputStream("testOutput/test9.kdbx");
133+
FileOutputStream fos = new FileOutputStream(testOutputPath("test9.kdbx").toFile());
133134
saveDatabase(getDatabase(), getCredentials("123".getBytes()), fos);
134135
fos.flush();
135136
fos.close();
136137

137138
// reload database, "random" is still protected even though it's not protected by default
138-
FileInputStream fis = new FileInputStream("testOutput/test9.kdbx");
139+
FileInputStream fis = new FileInputStream(testOutputPath("test9.kdbx").toFile());
139140
Database input = loadDatabase(getCredentials("123".getBytes()), fis);
140141

141142
List<? extends Entry> entries = input.findEntries("random");

‎test/src/main/java/org/linguafranca/util/TestUtil.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,29 @@
2020
import java.io.IOException;
2121
import java.io.OutputStream;
2222
import java.io.PrintStream;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
2326

2427
public class TestUtil {
28+
/**
29+
* The directory tests write their output files to
30+
*/
31+
public static final String TEST_OUTPUT_DIR = "testOutput";
32+
33+
/**
34+
* Returns the path of a file in the test output directory, creating the directory if need be,
35+
* so that a test doesn't depend on another test having created it
36+
* @param name the file name
37+
* @return the path
38+
* @throws IOException if the directory can't be created
39+
*/
40+
public static Path testOutputPath(String name) throws IOException {
41+
Path dir = Paths.get(TEST_OUTPUT_DIR);
42+
Files.createDirectories(dir);
43+
return dir.resolve(name);
44+
}
45+
2546

2647
/**
2748
* Database save closes output stream. If you want to output to console using KdbxFormat.None and <code>System.out</code>

0 commit comments

Comments
 (0)