Skip to content

Commit

Permalink
BoskTestUtils.boskName
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Jul 21, 2024
1 parent c46890a commit 445b01e
Show file tree
Hide file tree
Showing 23 changed files with 109 additions and 69 deletions.
17 changes: 9 additions & 8 deletions bosk-core/src/test/java/works/bosk/BoskConstructorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static works.bosk.BoskTestUtils.boskName;
import static works.bosk.TypeValidationTest.SimpleTypes.MyEnum.LEFT;

/**
Expand All @@ -28,7 +29,7 @@ public class BoskConstructorTest {

@Test
void basicProperties_correctValues() {
String name = "Name";
String name = boskName();
Type rootType = SimpleTypes.class;
StateTreeNode root = newEntity();

Expand Down Expand Up @@ -63,7 +64,7 @@ void basicProperties_correctValues() {
void invalidRootType_throws() {
assertThrows(IllegalArgumentException.class, ()->
new Bosk<MutableField>(
"Invalid root type",
boskName("Invalid root type"),
MutableField.class,
bosk -> new MutableField(),
Bosk::simpleDriver));
Expand All @@ -89,7 +90,7 @@ void badDefaultRootFunction_throws() {
void mismatchedRootType_throws() {
assertThrows(ClassCastException.class, ()->
new Bosk<Entity> (
"Mismatched root",
boskName("Mismatched root"),
BoxedPrimitives.class, // Valid but wrong
bosk -> newEntity(),
Bosk::simpleDriver
Expand All @@ -101,7 +102,7 @@ void mismatchedRootType_throws() {
void driverInitialRoot_matches() {
SimpleTypes root = newEntity();
Bosk<StateTreeNode> bosk = new Bosk<StateTreeNode>(
"By value",
boskName(),
SimpleTypes.class,
__ -> {throw new AssertionError("Shouldn't be called");},
initialRootDriver(()->root));
Expand All @@ -114,14 +115,14 @@ void driverInitialRoot_matches() {
void defaultRoot_matches() {
SimpleTypes root = newEntity();
{
Bosk<StateTreeNode> valueBosk = new Bosk<>("By value", SimpleTypes.class, root, Bosk::simpleDriver);
Bosk<StateTreeNode> valueBosk = new Bosk<>(boskName(), SimpleTypes.class, root, Bosk::simpleDriver);
try (val __ = valueBosk.readContext()) {
assertSame(root, valueBosk.rootReference().value());
}
}

{
Bosk<StateTreeNode> functionBosk = new Bosk<StateTreeNode>("By value", SimpleTypes.class, __ -> root, Bosk::simpleDriver);
Bosk<StateTreeNode> functionBosk = new Bosk<StateTreeNode>(boskName(), SimpleTypes.class, __ -> root, Bosk::simpleDriver);
try (val __ = functionBosk.readContext()) {
assertSame(root, functionBosk.rootReference().value());
}
Expand All @@ -135,7 +136,7 @@ void defaultRoot_matches() {

private static void assertInitialRootThrows(Class<? extends Throwable> expectedType, InitialRootFunction initialRootFunction) {
assertThrows(expectedType, () -> new Bosk<>(
"Throw test",
boskName(),
SimpleTypes.class,
newEntity(),
initialRootDriver(initialRootFunction)
Expand All @@ -144,7 +145,7 @@ private static void assertInitialRootThrows(Class<? extends Throwable> expectedT

private static void assertDefaultRootThrows(Class<? extends Throwable> expectedType, DefaultRootFunction<StateTreeNode> defaultRootFunction) {
assertThrows(expectedType, () -> new Bosk<>(
"Throw test",
boskName(),
SimpleTypes.class,
defaultRootFunction,
Bosk::simpleDriver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static works.bosk.BoskTestUtils.boskName;

/**
* Note that context propagation for driver operations is tested by {@link DriverConformanceTest}.
Expand All @@ -27,7 +28,7 @@ public interface Refs {
@BeforeEach
void setupBosk() throws InvalidTypeException {
bosk = new Bosk<TestEntity>(
BoskDiagnosticContextTest.class.getSimpleName(),
boskName(),
TestEntity.class,
AbstractDriverTest::initialRoot,
Bosk::simpleDriver
Expand Down
15 changes: 8 additions & 7 deletions bosk-core/src/test/java/works/bosk/BoskLocalReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static works.bosk.BoskTestUtils.boskName;
import static works.bosk.ListingEntry.LISTING_ENTRY;
import static works.bosk.ReferenceUtils.rawClass;

Expand All @@ -55,6 +56,7 @@
*
*/
class BoskLocalReferenceTest {
String boskName;
Bosk<Root> bosk;
Root root;
Refs refs;
Expand All @@ -66,8 +68,9 @@ public interface Refs {

@BeforeEach
void initializeBosk() throws InvalidTypeException {
boskName = boskName();
Root initialRoot = new Root(1, Catalog.empty());
bosk = new Bosk<>(BOSK_NAME, Root.class, initialRoot, Bosk::simpleDriver);
bosk = new Bosk<>(boskName, Root.class, initialRoot, Bosk::simpleDriver);
refs = bosk.rootReference().buildReferences(Refs.class);
Identifier ernieID = Identifier.from("ernie");
Identifier bertID = Identifier.from("bert");
Expand Down Expand Up @@ -255,7 +258,7 @@ void testBogusReferenceReference() {

@Test
void testName() {
assertEquals(BOSK_NAME, bosk.name());
assertEquals(boskName, bosk.name());
}

@Test
Expand All @@ -270,15 +273,15 @@ public InvalidRoot(Identifier id, Catalog<TestEntity> entities, String str) {
this.mutableString = str;
}
}
assertThrows(IllegalArgumentException.class, () -> new Bosk<>("invalid", InvalidRoot.class, new InvalidRoot(Identifier.unique("yucky"), Catalog.empty(), "hello"), Bosk::simpleDriver));
assertThrows(IllegalArgumentException.class, () -> new Bosk<>("invalid", String.class, new InvalidRoot(Identifier.unique("yucky"), Catalog.empty(), "hello"), Bosk::simpleDriver));
assertThrows(IllegalArgumentException.class, () -> new Bosk<>(boskName(), InvalidRoot.class, new InvalidRoot(Identifier.unique("yucky"), Catalog.empty(), "hello"), Bosk::simpleDriver));
assertThrows(IllegalArgumentException.class, () -> new Bosk<>(boskName(), String.class, new InvalidRoot(Identifier.unique("yucky"), Catalog.empty(), "hello"), Bosk::simpleDriver));
}

@Test
void testDriver() {
// This doesn't test the operation of the driver; merely that the right driver is returned
AtomicReference<BoskDriver<Root>> driver = new AtomicReference<>();
Bosk<Root> myBosk = new Bosk<>("My bosk", Root.class, new Root(123, Catalog.empty()), (b,d) -> {
Bosk<Root> myBosk = new Bosk<>(boskName(), Root.class, new Root(123, Catalog.empty()), (b,d) -> {
BoskDriver<Root> bd = new ProxyDriver(d);
driver.set(bd);
return bd;
Expand Down Expand Up @@ -446,6 +449,4 @@ private Reference<TestEntity> refUpdater(Reference<TestEntity> ref) {
throw new AssertionError("Unexpected!", e);
}
}

private static final String BOSK_NAME = "bosk name";
}
3 changes: 2 additions & 1 deletion bosk-core/src/test/java/works/bosk/BoskUpdateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import works.bosk.exceptions.InvalidTypeException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static works.bosk.BoskTestUtils.boskName;

/**
* To get complete coverage of Bosk.java, include these:
Expand Down Expand Up @@ -40,7 +41,7 @@ public interface Refs {
@BeforeEach
void createBosk() throws InvalidTypeException {
bosk = new Bosk<TestRoot>(
BoskUpdateTest.class.getSimpleName(),
boskName(),
TestRoot.class,
AbstractBoskTest::initialRoot,
Bosk::simpleDriver
Expand Down
3 changes: 2 additions & 1 deletion bosk-core/src/test/java/works/bosk/CatalogBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import works.bosk.exceptions.InvalidTypeException;

import static org.openjdk.jmh.annotations.Mode.Throughput;
import static works.bosk.BoskTestUtils.boskName;

@Fork(0)
@Warmup(iterations = 5, time = 1)
Expand All @@ -29,7 +30,7 @@ public static class BenchmarkState {
@Setup(Level.Trial)
public void setup() throws InvalidTypeException {
Bosk<AbstractBoskTest.TestRoot> bosk = new Bosk<AbstractBoskTest.TestRoot>(
"CatalogBenchmarkBosk",
boskName(),
AbstractBoskTest.TestRoot.class,
AbstractBoskTest::initialRoot,
Bosk::simpleDriver
Expand Down
7 changes: 4 additions & 3 deletions bosk-core/src/test/java/works/bosk/ListingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static works.bosk.BoskTestUtils.boskName;

/*
* TODO: This test is written in a mighty weird style. Change it to set up
Expand All @@ -54,7 +55,7 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return childrenStream
.map(children -> {
TestEntity root = new TestEntity(Identifier.unique("parent"), Catalog.of(children));
Bosk<TestEntity> bosk = new Bosk<>("Test Bosk", TestEntity.class, root, Bosk::simpleDriver);
Bosk<TestEntity> bosk = new Bosk<>(boskName(), TestEntity.class, root, Bosk::simpleDriver);
CatalogReference<TestEntity> catalog;
try {
catalog = bosk.rootReference().thenCatalog(TestEntity.class, Path.just(TestEntity.Fields.children));
Expand All @@ -73,7 +74,7 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) th
TestEntity child = new TestEntity(Identifier.unique("child"), Catalog.empty());
List<TestEntity> children = singletonList(child);
TestEntity root = new TestEntity(Identifier.unique("parent"), Catalog.of(children));
Bosk<TestEntity> bosk = new Bosk<>("Test Bosk", TestEntity.class, root, Bosk::simpleDriver);
Bosk<TestEntity> bosk = new Bosk<>(boskName(), TestEntity.class, root, Bosk::simpleDriver);
CatalogReference<TestEntity> childrenRef = bosk.rootReference().thenCatalog(TestEntity.class, Path.just(TestEntity.Fields.children));
return idStreams().map(list -> Arguments.of(list.map(Identifier::from).collect(toList()), childrenRef, bosk));
}
Expand Down Expand Up @@ -248,7 +249,7 @@ void testEmpty() throws InvalidTypeException {
TestEntity child = new TestEntity(Identifier.unique("child"), Catalog.empty());
List<TestEntity> children = singletonList(child);
TestEntity root = new TestEntity(Identifier.unique("parent"), Catalog.of(children));
Bosk<TestEntity> bosk = new Bosk<>("Test Bosk", TestEntity.class, root, Bosk::simpleDriver);
Bosk<TestEntity> bosk = new Bosk<>(boskName(), TestEntity.class, root, Bosk::simpleDriver);
CatalogReference<TestEntity> childrenRef = bosk.rootReference().thenCatalog(TestEntity.class, Path.just(TestEntity.Fields.children));

Listing<TestEntity> actual = Listing.empty(childrenRef);
Expand Down
5 changes: 3 additions & 2 deletions bosk-core/src/test/java/works/bosk/OptionalRefsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static works.bosk.BoskTestUtils.boskName;

class OptionalRefsTest extends AbstractRoundTripTest {
private static final Identifier ID = Identifier.from("dummy");

@Test
void testReferenceOptionalNotAllowed() {
Bosk<OptionalString> bosk = new Bosk<>("optionalNotAllowed", OptionalString.class, new OptionalString(ID, Optional.empty()), Bosk::simpleDriver);
Bosk<OptionalString> bosk = new Bosk<>(boskName(), OptionalString.class, new OptionalString(ID, Optional.empty()), Bosk::simpleDriver);
InvalidTypeException e = assertThrows(InvalidTypeException.class, () -> bosk.rootReference().then(Optional.class, Path.just("field")));
assertThat(e.getMessage(), containsString("not supported"));
}
Expand Down Expand Up @@ -117,7 +118,7 @@ private interface ValueFactory<R extends Entity, V> {
}

private <E extends Entity, V> void doTest(E initialRoot, ValueFactory<E, V> valueFactory, DriverFactory<E> driverFactory) throws InvalidTypeException {
Bosk<E> bosk = new Bosk<>("bosk", initialRoot.getClass(), initialRoot, driverFactory);
Bosk<E> bosk = new Bosk<>(boskName(), initialRoot.getClass(), initialRoot, driverFactory);
V value = valueFactory.createFrom(bosk);
@SuppressWarnings("unchecked")
Reference<V> optionalRef = bosk.rootReference().then((Class<V>)value.getClass(), "field");
Expand Down
3 changes: 2 additions & 1 deletion bosk-core/src/test/java/works/bosk/ReferenceErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import works.bosk.exceptions.InvalidTypeException;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static works.bosk.BoskTestUtils.boskName;

public class ReferenceErrorTest {
Bosk<?> bosk;

@BeforeEach
void setupBosk() {
bosk = new Bosk<>(
"Test",
boskName(),
BadGetters.class,
new BadGetters(Identifier.from("test"), new NestedObject(Optional.of("stringValue"))),
Bosk::simpleDriver);
Expand Down
3 changes: 2 additions & 1 deletion bosk-core/src/test/java/works/bosk/VariantTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static works.bosk.BoskTestUtils.boskName;

class VariantTest extends AbstractBoskTest {

Expand Down Expand Up @@ -46,7 +47,7 @@ public interface Refs {
@Test
void test() throws InvalidTypeException, IOException, InterruptedException {
String stringValue = "test";
var bosk = new Bosk<>(VariantTest.class.getSimpleName(), BoskState.class, new BoskState(new StringCase(stringValue)), Bosk::simpleDriver);
var bosk = new Bosk<>(boskName(), BoskState.class, new BoskState(new StringCase(stringValue)), Bosk::simpleDriver);
var refs = bosk.rootReference().buildReferences(Refs.class);
try (var __ = bosk.readContext()) {
assertEquals(stringValue, refs.stringValue().value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
import static works.bosk.BoskTestUtils.boskName;
import static works.bosk.ListingEntry.LISTING_ENTRY;

public class PathCompilerTest extends AbstractBoskTest {
Expand Down Expand Up @@ -268,7 +269,7 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
.getConstructor(Identifier.class)
.newInstance(rootID);
Bosk<StateTreeNode> differentBosk = new Bosk<>(
"Different",
boskName("Different"),
differentRootClass,
initialRoot,
Bosk::simpleDriver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import works.bosk.drivers.state.TestEntity;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static works.bosk.BoskTestUtils.boskName;

class ReplicaSetConformanceTest extends DriverConformanceTest {
Bosk<TestEntity> replicaBosk;
Expand All @@ -14,7 +15,7 @@ class ReplicaSetConformanceTest extends DriverConformanceTest {
void setupDriverFactory() {
ReplicaSet<TestEntity> replicaSet = new ReplicaSet<>();
replicaBosk = new Bosk<TestEntity>(
"Replica bosk",
boskName("Replica"),
TestEntity.class,
AbstractDriverTest::initialRoot,
replicaSet.driverFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import works.bosk.exceptions.InvalidTypeException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static works.bosk.BoskTestUtils.boskName;

class BsonPluginTest {

@Test
void sideTableOfSideTables() {
BsonPlugin bp = new BsonPlugin();
Bosk<Root> bosk = new Bosk<Root>("Test bosk", Root.class, this::defaultRoot, Bosk::simpleDriver);
Bosk<Root> bosk = new Bosk<Root>(boskName(), Root.class, this::defaultRoot, Bosk::simpleDriver);
CodecRegistry registry = CodecRegistries.fromProviders(bp.codecProviderFor(bosk), new ValueCodecProvider());
Codec<Root> codec = registry.get(Root.class);
try (var __ = bosk.readContext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import works.bosk.exceptions.InvalidTypeException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static works.bosk.BoskTestUtils.boskName;

class MongoDriverDottedFieldNameTest extends AbstractDriverTest {
private Bosk<TestEntity> bosk;

@BeforeEach
void setUpStuff() {
bosk = new Bosk<TestEntity>("Test bosk", TestEntity.class, AbstractDriverTest::initialRoot, Bosk::simpleDriver);
bosk = new Bosk<TestEntity>(boskName(), TestEntity.class, AbstractDriverTest::initialRoot, Bosk::simpleDriver);
}

private CatalogReference<TestEntity> rootCatalogRef(Bosk<TestEntity> bosk) throws InvalidTypeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static ch.qos.logback.classic.Level.ERROR;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static works.bosk.BoskTestUtils.boskName;

/**
* Tests the functionality of {@link MongoDriverSettings.InitialDatabaseUnavailableMode#FAIL FAIL} mode.
Expand All @@ -27,7 +28,7 @@ void initialOutage_throws() {
mongoService.proxy().setConnectionCut(true);
tearDownActions.add(()->mongoService.proxy().setConnectionCut(false));
assertThrows(InitialRootFailureException.class, ()->{
new Bosk<TestEntity>("Fail", TestEntity.class, this::initialRoot, super.createDriverFactory(logController));
new Bosk<TestEntity>(boskName("Fail"), TestEntity.class, this::initialRoot, super.createDriverFactory(logController));
});
}

Expand Down
Loading

0 comments on commit 445b01e

Please sign in to comment.