Skip to content

Commit de24032

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. It replaces the earlier fix in the example tests, the unused QuickStart.ensureOutputDir() and the copies in PropertyValueChecks and SaveAndReloadChecks. QuickStart, which is example code, creates the directory itself before writing migration.kdbx too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1f8ca26 commit de24032

7 files changed

Lines changed: 39 additions & 44 deletions

File tree

‎example/src/main/java/org/linguafranca/pwdb/kdbx/QuickStart.java‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import org.linguafranca.pwdb.security.Encryption;
2424

2525
import java.io.*;
26-
import java.nio.file.Files;
27-
import java.nio.file.Paths;
2826
import java.util.List;
2927

3028
import static org.linguafranca.test.util.TestUtil.getTestPrintStream;
@@ -43,10 +41,6 @@ public abstract class QuickStart<D extends Database<D, G, E, I>, G extends Group
4341
public abstract D getDatabase();
4442
public abstract D loadDatabase(Credentials creds, InputStream inputStream);
4543

46-
public static void ensureOutputDir() throws IOException {
47-
Files.createDirectories(Paths.get("testOutput"));
48-
}
49-
5044
/**
5145
* Load KDBX
5246
*/
@@ -133,6 +127,7 @@ public void loadKdb() throws IOException {
133127
// deep copy from group (not including source group, KDB database has simulated root)
134128
kdbxDatabase.getRootGroup().copy(database.getRootGroup());
135129
// save it
130+
new File("testOutput").mkdirs();
136131
try (FileOutputStream f = new FileOutputStream("testOutput/migration.kdbx")) {
137132
kdbxDatabase.save(new KdbxCreds("123".getBytes()), f);
138133
}

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

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

1717
package org.linguafranca.pwdb.kdbx;
1818

19-
import org.junit.BeforeClass;
2019
import org.junit.Test;
2120
import org.linguafranca.pwdb.Credentials;
2221
import org.linguafranca.pwdb.kdbx.dom.DomDatabaseWrapper;
@@ -29,9 +28,9 @@
2928
import java.io.InputStream;
3029
import java.nio.file.Files;
3130
import java.nio.file.Path;
32-
import java.nio.file.Paths;
3331

3432
import static org.junit.Assert.assertEquals;
33+
import static org.linguafranca.test.util.TestUtil.testOutputPath;
3534

3635
/**
3736
* Simple illustration of hooking a SAX parser up to process a KDBX file
@@ -40,11 +39,6 @@
4039
*/
4140
public class SimpleQuickStartTest extends QuickStart<DomDatabaseWrapper, DomGroupWrapper, DomEntryWrapper, DomIconWrapper> {
4241

43-
@BeforeClass
44-
public static void createOutputDir() throws IOException {
45-
ensureOutputDir();
46-
}
47-
4842

4943
@Override
5044
public DomDatabaseWrapper getDatabase() {
@@ -77,7 +71,7 @@ public void loadKdbSaveVernacular() throws IOException {
7771

7872
@Test
7973
public void loadSave() throws IOException {
80-
Path path = Paths.get("testOutput/CHACHA-AES-CHACHA.kdbx");
74+
Path path = testOutputPath("CHACHA-AES-CHACHA.kdbx");
8175

8276
loadKdbx3SaveKdbx4("test123.kdbx","123".getBytes(), Files.newOutputStream(path));
8377

@@ -91,7 +85,7 @@ public void loadSave() throws IOException {
9185
}
9286
@Test
9387
public void loadSave2() throws IOException {
94-
Path path = Paths.get("testOutput/CHACHA-AES-CHACHA.kdbx");
88+
Path path = testOutputPath("CHACHA-AES-CHACHA.kdbx");
9589

9690
loadKdbx4SaveKdbx3("V4-ChaCha20-Argon2-Attachment.kdbx","123".getBytes(), Files.newOutputStream(path));
9791

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
import java.io.OutputStream;
1414
import java.nio.file.Files;
1515
import java.nio.file.Path;
16-
import java.nio.file.Paths;
1716
import java.util.List;
1817
import java.util.UUID;
1918

2019
import static org.junit.Assert.*;
2120
import static org.linguafranca.pwdb.kdbx.Util.streamToString;
21+
import static org.linguafranca.test.util.TestUtil.testOutputPath;
2222

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

2929
public static final String TEST_RESOURCE1 = "issue-87/customIcon-123.kdbx";
3030
public static final KdbxCreds CREDENTIALS1 = new KdbxCreds("123".getBytes());
31-
public static final String TEST_OUTPUT_ISSUE_87_KDBX = "testOutput/Issue87.kdbx";
3231

3332

3433
@Test
@@ -46,8 +45,7 @@ public void testCustomIcon() throws IOException {
4645
System.out.println("Custom icon id: " + customIcon);
4746
KeePassFile.Icon icon = entries.get(0).getCustomIcon();
4847

49-
Path path = Paths.get(TEST_OUTPUT_ISSUE_87_KDBX);
50-
Files.createDirectories(path.getParent());
48+
Path path = testOutputPath("Issue87.kdbx");
5149
try (OutputStream outputStream = Files.newOutputStream(path)) {
5250
database.save(CREDENTIALS1, outputStream);
5351
}

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

Lines changed: 2 additions & 4 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.Util.streamToString;
16+
import static org.linguafranca.test.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 KdbxCreds CREDENTIALS1 = new KdbxCreds("123".getBytes());
26-
public static final String TEST_OUTPUT_ISSUE_88_KDBX = "testOutput/Issue88.kdbx";
2726

2827

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

36-
Path path = Paths.get(TEST_OUTPUT_ISSUE_88_KDBX);
37-
Files.createDirectories(path.getParent());
35+
Path path = testOutputPath("Issue88.kdbx");
3836
try (OutputStream outputStream = Files.newOutputStream(path)) {
3937
database.save(CREDENTIALS1, outputStream);
4038
}

‎test/src/test/java/org/linguafranca/pwdb/checks/PropertyValueChecks.java‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616

1717
package org.linguafranca.pwdb.checks;
1818

19-
import org.junit.BeforeClass;
2019
import org.junit.Test;
2120
import org.linguafranca.pwdb.*;
2221

2322
import java.io.*;
24-
import java.nio.file.Files;
25-
import java.nio.file.Paths;
2623
import java.util.ArrayList;
2724
import java.util.List;
2825
import java.util.function.Consumer;
2926

3027
import static org.junit.Assert.*;
28+
import static org.linguafranca.test.util.TestUtil.testOutputPath;
3129

3230
/**
3331
* Testing the operation of PropertyValue mechanisms
@@ -42,10 +40,6 @@ public abstract class PropertyValueChecks<D extends Database<D, G, E, I>, G exte
4240
private final boolean propertyValueSupported;
4341
protected D database;
4442

45-
@BeforeClass
46-
public static void ensureOutputDir() throws IOException {
47-
Files.createDirectories(Paths.get("testOutput"));
48-
}
4943

5044
public PropertyValueChecks(boolean propertyValueSupported) throws IOException {
5145
this.database = createDatabase();
@@ -156,13 +150,13 @@ public PropertyValue.Factory<? extends PropertyValue> newUnprotected() {
156150

157151
// Save database
158152

159-
FileOutputStream fos = new FileOutputStream("testOutput/test9.kdbx");
153+
FileOutputStream fos = new FileOutputStream(testOutputPath("test9.kdbx").toFile());
160154
saveDatabase(database, getCreds("123".getBytes()), fos);
161155
fos.flush();
162156
fos.close();
163157

164158
// reload database, "random" is still protected even though it's not protected by default
165-
FileInputStream fis = new FileInputStream("testOutput/test9.kdbx");
159+
FileInputStream fis = new FileInputStream(testOutputPath("test9.kdbx").toFile());
166160
D input = loadDatabase(getCreds("123".getBytes()), fis);
167161

168162
List<? extends E> entries = input.findEntries("random");

‎test/src/test/java/org/linguafranca/pwdb/checks/SaveAndReloadChecks.java‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@
1717
package org.linguafranca.pwdb.checks;
1818

1919
import org.junit.Assert;
20-
import org.junit.BeforeClass;
2120
import org.junit.Ignore;
2221
import org.junit.Test;
2322
import org.linguafranca.pwdb.*;
2423

2524
import java.io.*;
2625
import java.nio.file.Files;
27-
import java.nio.file.Paths;
2826
import java.util.UUID;
2927

3028
import static org.junit.Assert.*;
3129
import static org.linguafranca.test.util.TestUtil.getTestPrintStream;
30+
import static org.linguafranca.test.util.TestUtil.testOutputPath;
3231

3332
/**
3433
* @author jo
@@ -45,10 +44,6 @@ public abstract class SaveAndReloadChecks <D extends Database<D, G, E, I>, G ext
4544
public abstract Credentials getCreds(byte[] creds);
4645
public abstract boolean verifyStreamFormat (StreamFormat<?> s1, StreamFormat<?> s2);
4746

48-
@BeforeClass
49-
public static void ensureOutputDir() throws IOException {
50-
Files.createDirectories(Paths.get("testOutput"));
51-
}
5247

5348
/**
5449
* Test verifies that entries contain the same content on reload as they did on save,
@@ -64,7 +59,7 @@ public void saveAndReloadTest() throws IOException {
6459
verifyContents(output);
6560
//output.save(new StreamFormat.None(), new Credentials.None(), printStream);
6661

67-
FileOutputStream fos = new FileOutputStream("testOutput/test1.kdbx");
62+
FileOutputStream fos = new FileOutputStream(testOutputPath("test1.kdbx").toFile());
6863
saveDatabase(output, getCreds("123".getBytes()), fos);
6964
Assert.assertFalse(output.isDirty());
7065
fos.flush();
@@ -74,7 +69,7 @@ public void saveAndReloadTest() throws IOException {
7469
//output.save(new StreamFormat.None(), new Credentials.None(), printStream);
7570

7671

77-
FileInputStream fis = new FileInputStream("testOutput/test1.kdbx");
72+
FileInputStream fis = new FileInputStream(testOutputPath("test1.kdbx").toFile());
7873
D input = loadDatabase(getCreds("123".getBytes()), fis);
7974
verifyContents(input);
8075
//input.save(new StreamFormat.None(), new Credentials.None(), printStream);
@@ -98,12 +93,12 @@ public void saveAndReloadTest2() throws IOException {
9893
entry.setBinaryProperty("letter L.jpeg", content);
9994
assertArrayEquals(new String[] {"letter J.jpeg", "letter L.jpeg"}, entry.getBinaryPropertyNames().toArray());
10095

101-
FileOutputStream fos = new FileOutputStream("testOutput/test2.kdbx");
96+
FileOutputStream fos = new FileOutputStream(testOutputPath("test2.kdbx").toFile());
10297
saveDatabase(attachment, getCreds("123".getBytes()), fos);
10398
fos.flush();
10499
fos.close();
105100

106-
FileInputStream fis = new FileInputStream("testOutput/test2.kdbx");
101+
FileInputStream fis = new FileInputStream(testOutputPath("test2.kdbx").toFile());
107102
D input = loadDatabase(getCreds("123".getBytes()), fis);
108103

109104
entry = input.findEntries("Test attachment").get(0);
@@ -126,9 +121,9 @@ public void saveAndReloadTest3() throws IOException {
126121
D database = getDatabase(resource, this.getCreds("123".getBytes()));
127122
StreamFormat<?> format1 = database.getStreamFormat();
128123

129-
database.save(getCreds("123".getBytes()), Files.newOutputStream(Paths.get("testOutput/test3.kdbx")));
124+
database.save(getCreds("123".getBytes()), Files.newOutputStream(testOutputPath("test3.kdbx")));
130125

131-
FileInputStream fis = new FileInputStream("testOutput/test3.kdbx");
126+
FileInputStream fis = new FileInputStream(testOutputPath("test3.kdbx").toFile());
132127
D input = loadDatabase(getCreds("123".getBytes()), fis);
133128
StreamFormat<?> format2 = input.getStreamFormat();
134129
assertTrue(verifyStreamFormat(format1, format2));

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,29 @@
33
import java.io.IOException;
44
import java.io.OutputStream;
55
import java.io.PrintStream;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
69

710
public class TestUtil {
11+
/**
12+
* The directory tests write their output files to
13+
*/
14+
public static final String TEST_OUTPUT_DIR = "testOutput";
15+
16+
/**
17+
* Returns the path of a file in the test output directory, creating the directory if need be,
18+
* so that a test doesn't depend on another test having created it
19+
* @param name the file name
20+
* @return the path
21+
* @throws IOException if the directory can't be created
22+
*/
23+
public static Path testOutputPath(String name) throws IOException {
24+
Path dir = Paths.get(TEST_OUTPUT_DIR);
25+
Files.createDirectories(dir);
26+
return dir.resolve(name);
27+
}
28+
829
/**
930
* Do nothing output stream
1031
*/

0 commit comments

Comments
 (0)