Skip to content

Commit

Permalink
Disallow ReadContext inside DriverFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Nov 17, 2023
1 parent 1e42dba commit a2003d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion bosk-core/src/main/java/io/vena/bosk/Bosk.java
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,11 @@ public final class ReadContext implements AutoCloseable {
private ReadContext() {
originalRoot = rootSnapshot.get();
if (originalRoot == null) {
snapshot = currentRoot;
try {
snapshot = requireNonNull(currentRoot);
} catch (NullPointerException e) {
throw new IllegalStateException("Bosk constructor has not yet finished; cannot create a ReadContext", e);
}
rootSnapshot.set(snapshot);
LOGGER.trace("New {}", this);
} else {
Expand Down
10 changes: 10 additions & 0 deletions bosk-core/src/test/java/io/vena/bosk/BoskConstructorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ void defaultRoot_matches() {
}
}

@Test
void readContextDuringDriverFactory_throws() {
assertThrows(IllegalStateException.class, ()->{
new Bosk<>("readContext", SimpleTypes.class, newEntity(), (b,d) -> {
try (val __ = b.readContext()) {}
return d;
});
});
}

////////////////
//
// Helpers
Expand Down

0 comments on commit a2003d8

Please sign in to comment.