use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.
the class ObjectVisibilityTest method givenFromEntityWhenAccessingModuleLayerVisibleExpectSuccess.
@Test
public void givenFromEntityWhenAccessingModuleLayerVisibleExpectSuccess() {
UnitOfWork unitOfWork = module.newUnitOfWork();
try {
FromEntity entity = unitOfWork.newEntity(FromEntity.class, "123");
entity.moduleLayerVisible();
} finally {
if (unitOfWork.isOpen()) {
unitOfWork.discard();
}
}
}
use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.
the class AbstractEntityStoreTest method whenNewEntityThenCanFindEntityAndCorrectValues.
@Test
public void whenNewEntityThenCanFindEntityAndCorrectValues() throws Exception {
UnitOfWork unitOfWork = module.newUnitOfWork();
try {
TestEntity instance = createEntity(unitOfWork);
unitOfWork.complete();
// Find entity
unitOfWork = module.newUnitOfWork();
instance = unitOfWork.get(instance);
// Check state
assertThat("property 'intValue' has correct value", instance.intValue().get(), equalTo(42));
assertThat("property 'longValue' has correct value", instance.longValue().get(), equalTo(42L));
assertThat("property 'doubleValue' has correct value", instance.doubleValue().get(), equalTo(42D));
assertThat("property 'floatValue' has correct value", instance.floatValue().get(), equalTo(42F));
assertThat("property 'booleanValue' has correct value", instance.booleanValue().get(), equalTo(Boolean.TRUE));
assertThat("property 'bigInteger' has correct value", instance.bigIntegerValue().get(), equalTo(new BigInteger("42")));
assertThat("property 'bigDecimal' has correct value", instance.bigDecimalValue().get(), equalTo(new BigDecimal("42")));
assertThat("property 'dateValue' has correct value", instance.dateValue().get(), equalTo(new DateTime("2020-03-04T13:24:35", UTC).toDate()));
assertThat("property 'dateTimeValue' has correct value", instance.dateTimeValue().get(), equalTo(new DateTime("2020-03-04T13:24:35", UTC)));
assertThat("property 'localDateTimeValue' has correct value", instance.localDateTimeValue().get(), equalTo(new LocalDateTime("2020-03-04T13:23:00", UTC)));
assertThat("property 'localDateValue' has correct value", instance.localDateValue().get(), equalTo(new LocalDate("2020-03-04")));
assertThat("property 'name' has correct value", instance.name().get(), equalTo("Test"));
assertThat("property 'unsetName' has correct value", instance.unsetName().get(), equalTo(null));
assertThat("property 'emptyName' has correct value", instance.emptyName().get(), equalTo(""));
assertThat("property 'valueProperty.stringValue' has correct value", instance.valueProperty().get().valueProperty().get().stringValue().get(), equalTo("Bar"));
assertThat("property 'valueProperty.listProperty' has correct value", instance.valueProperty().get().listProperty().get().get(0), equalTo("Foo"));
assertThat("property 'valueProperty.enumProperty' has correct value", instance.valueProperty().get().enumProperty().get(), equalTo(TestEnum.VALUE3));
assertThat("property 'valueProperty.anotherValue.bling' has correct value", instance.valueProperty().get().valueProperty().get().anotherValue().get().bling().get(), equalTo("BlinkLjus"));
assertThat("property 'valueProperty.tjabbaProperty.bling' has correct value", instance.valueProperty().get().tjabbaProperty().get().bling().get(), equalTo("Brakfis"));
Map<String, String> mapValue = new HashMap<>();
mapValue.put("foo", "bar");
assertThat("property 'valueProperty.mapStringStringProperty' has correct value", instance.valueProperty().get().mapStringStringProperty().get(), equalTo(mapValue));
assertThat("association has correct value", instance.association().get(), equalTo(instance));
assertThat("manyAssociation has correct value", instance.manyAssociation().iterator().next(), equalTo(instance));
assertThat("namedAssociation has correct 'foo' value", instance.namedAssociation().get("foo"), equalTo(instance));
assertThat("namedAssociation has correct 'bar' value", instance.namedAssociation().get("bar"), equalTo(instance));
unitOfWork.discard();
} finally {
unitOfWork.discard();
}
}
use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.
the class AbstractEntityStoreTest method givenConcurrentUnitOfWorksWhenUoWCompletesThenCheckConcurrentModification.
@Test
public void givenConcurrentUnitOfWorksWhenUoWCompletesThenCheckConcurrentModification() throws UnitOfWorkCompletionException {
TestEntity testEntity;
{
UnitOfWork unitOfWork = module.newUnitOfWork();
EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder(TestEntity.class);
testEntity = builder.newInstance();
unitOfWork.complete();
}
UnitOfWork unitOfWork1;
TestEntity testEntity1;
String version;
{
// Start working with Entity in one UoW
unitOfWork1 = module.newUnitOfWork();
testEntity1 = unitOfWork1.get(testEntity);
version = spi.entityStateOf(testEntity1).version();
if (version.isEmpty()) {
unitOfWork1.discard();
// Store doesn't track versions - no point in testing it
return;
}
testEntity1.name().set("A");
testEntity1.unsetName().set("A");
}
{
// Start working with same Entity in another UoW, and complete it
UnitOfWork unitOfWork = module.newUnitOfWork();
TestEntity testEntity2 = unitOfWork.get(testEntity);
assertThat("version is correct", spi.entityStateOf(testEntity1).version(), equalTo(version));
testEntity2.name().set("B");
unitOfWork.complete();
}
{
// Try to complete first UnitOfWork
try {
unitOfWork1.complete();
fail("Should have thrown concurrent modification exception");
} catch (ConcurrentEntityModificationException e) {
unitOfWork1.discard();
}
}
{
// Check values
unitOfWork1 = module.newUnitOfWork();
testEntity1 = unitOfWork1.get(testEntity);
assertThat("property name has not been set", testEntity1.name().get(), equalTo("B"));
assertThat("version is incorrect", spi.entityStateOf(testEntity1).version(), not(equalTo(version)));
unitOfWork1.discard();
}
}
use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.
the class AbstractEntityStoreTest method whenRemovedEntityThenCannotFindEntity.
@Test
public void whenRemovedEntityThenCannotFindEntity() throws Exception {
UnitOfWork unitOfWork = module.newUnitOfWork();
TestEntity newInstance = createEntity(unitOfWork);
String identity = newInstance.identity().get();
unitOfWork.complete();
// Remove entity
unitOfWork = module.newUnitOfWork();
TestEntity instance = unitOfWork.get(newInstance);
unitOfWork.remove(instance);
unitOfWork.complete();
// Find entity
unitOfWork = module.newUnitOfWork();
try {
unitOfWork.get(TestEntity.class, identity);
fail("Should not be able to find entity");
} catch (NoSuchEntityException e) {
// Ok!
} finally {
unitOfWork.discard();
}
}
use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.
the class UnitOfWorkFactoryTest method testUnitOfWork.
@Test
public void testUnitOfWork() throws Exception {
UnitOfWork unitOfWork = module.newUnitOfWork();
// Create product
EntityBuilder<ProductEntity> cb = unitOfWork.newEntityBuilder(ProductEntity.class);
cb.instance().name().set("Chair");
cb.instance().price().set(57);
Product chair = cb.newInstance();
String actual = chair.name().get();
org.junit.Assert.assertThat("Chair.name()", actual, org.hamcrest.CoreMatchers.equalTo("Chair"));
org.junit.Assert.assertThat("Chair.price()", chair.price().get(), org.hamcrest.CoreMatchers.equalTo(57));
unitOfWork.complete();
}
Aggregations