Skip to content

Commit

Permalink
Fix test so as not to depend on the class path.
Browse files Browse the repository at this point in the history
  • Loading branch information
broneill committed Sep 18, 2024
1 parent a48c008 commit 9515de1
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions src/test/java/org/cojen/tupl/core/OpenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
package org.cojen.tupl.core;

import java.io.File;
import java.io.FileWriter;

import org.junit.*;
import static org.junit.Assert.*;

import org.cojen.tupl.*;

import org.cojen.tupl.io.Utils;

import static org.cojen.tupl.TestUtils.*;

/**
Expand All @@ -33,27 +36,51 @@
*/
public class OpenTest {
public static void main(String[] args) throws Exception {
if (args.length == 0) {
org.junit.runner.JUnitCore.main(OpenTest.class.getName());
return;
}
org.junit.runner.JUnitCore.main(OpenTest.class.getName());
}

boolean readOnly = false;
private static String sourceFile;

if (args.length > 1 && args[1].equals("r")) {
readOnly = true;
}
@BeforeClass
public static void createSourceFile() throws Exception {
String src = """
import org.cojen.tupl.*;
public class Test {
public static void main(String[] args) throws Exception {
boolean readOnly = false;
if (args.length > 1 && args[1].equals("r")) {
readOnly = true;
}
var config = new DatabaseConfig().baseFilePath(args[0]).readOnly(readOnly);
var config = new DatabaseConfig().baseFilePath(args[0]).readOnly(readOnly);
Database db = Database.open(config);
System.out.println(readOnly ? "r" : "w");
Database db = Database.open(config);
System.out.println(readOnly ? "r" : "w");
for (int i=0; i<Integer.MAX_VALUE; i++) {
Thread.sleep(9999999999999L);
}
for (int i=0; i<Integer.MAX_VALUE; i++) {
Thread.sleep(9999999999999L);
System.out.println(db);
}
}
""";

File file = new File(newTempBaseFile(OpenTest.class) + ".java");
sourceFile = file.getPath();

try (var writer = new FileWriter(file)) {
writer.write(src);
}
}

System.out.println(db);
@AfterClass
public static void deleteSourceFile() throws Exception {
if (sourceFile != null) {
Utils.delete(new File(sourceFile));
}
}

@After
Expand Down Expand Up @@ -90,7 +117,7 @@ public void openTwiceDifferentProcess() throws Exception {
File baseFile = baseFileForTempDatabase(getClass(), db);
db.close();

Process p = new ProcessBuilder("java", getClass().getName(), baseFile.getPath()).start();
Process p = new ProcessBuilder("java", sourceFile, baseFile.getPath()).start();

int c = p.getInputStream().read();
assertEquals('w', c);
Expand Down Expand Up @@ -148,8 +175,7 @@ public void openReadOnlyDifferentProcess() throws Exception {
db.checkpoint();
db.close();

Process p = new ProcessBuilder
("java", getClass().getName(), baseFile.getPath(), "r").start();
Process p = new ProcessBuilder("java", sourceFile, baseFile.getPath(), "r").start();

try {
int c = p.getInputStream().read();
Expand Down

0 comments on commit 9515de1

Please sign in to comment.