use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class ValueCompositeTest method givenValueWithAssociationsWhenNewUoWThenCanRead.
@Test
public void givenValueWithAssociationsWhenNewUoWThenCanRead() throws UnitOfWorkCompletionException {
ValueBuilder<SomeValue> builder = module.newValueBuilder(SomeValue.class);
builder.prototype().anotherList().get().add(module.newValue(AnotherValue.class));
ValueBuilder<AnotherValue> valueBuilder = module.newValueBuilder(AnotherValue.class);
valueBuilder.prototype().val1().set("Foo");
builder.prototype().another().set(valueBuilder.newInstance());
builder.prototype().number().set(42L);
SomeValue some = builder.newInstance();
UnitOfWork unitOfWork = module.newUnitOfWork();
AssociationValue associationValue;
try {
EntityBuilder<SomeEntity> entityBuilder = unitOfWork.newEntityBuilder(SomeEntity.class);
entityBuilder.instance().someValue().set(some);
SomeEntity entity = entityBuilder.newInstance();
ValueBuilder<AssociationValue> associationBuilder = module.newValueBuilder(AssociationValue.class);
associationBuilder.prototype().some().set(entity);
associationValue = associationBuilder.newInstance();
String json = associationValue.toString();
unitOfWork.complete();
unitOfWork = module.newUnitOfWork();
AssociationValue newAssociationValue = module.newValueFromSerializedState(AssociationValue.class, json);
Assert.assertEquals(associationValue.some().get(), newAssociationValue.some().get());
} finally {
unitOfWork.discard();
}
try {
System.out.println(associationValue.toString());
fail("Should have thrown an exception");
} catch (Exception e) {
// Ok
}
}
use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class Qi173IssueTest method testPersistence.
@Test
public void testPersistence() {
UnitOfWork uow = module.newUnitOfWork();
try {
createCar("Volvo", "S80", 2007);
createCar("Volvo", "C70", 2006);
createCar("Ford", "Transit", 2007);
createCar("Ford", "Mustang", 2007);
createCar("Ford", "Mustang", 2006);
createCar("Ford", "Mustang", 2005);
uow.complete();
} catch (ConcurrentEntityModificationException e) {
// Can not happen.
e.printStackTrace();
} catch (UnitOfWorkCompletionException e) {
e.printStackTrace();
}
uow = module.newUnitOfWork();
QueryBuilder<Car> qb = module.newQueryBuilder(Car.class);
Car template = QueryExpressions.templateFor(Car.class);
qb = qb.where(QueryExpressions.eq(template.year(), 2007));
Query<Car> query = uow.newQuery(qb);
query.orderBy(orderBy(template.manufacturer()), orderBy(template.model()));
Iterator<Car> cars = query.iterator();
Assert.assertTrue(cars.hasNext());
Car car1 = cars.next();
Assert.assertEquals(car1.manufacturer().get(), "Ford");
Assert.assertEquals(car1.model().get(), "Mustang");
Assert.assertEquals((int) car1.year().get(), 2007);
Car car2 = cars.next();
Assert.assertEquals(car2.manufacturer().get(), "Ford");
Assert.assertEquals(car2.model().get(), "Transit");
Assert.assertEquals((int) car2.year().get(), 2007);
Car car3 = cars.next();
Assert.assertEquals(car3.manufacturer().get(), "Volvo");
Assert.assertEquals(car3.model().get(), "S80");
Assert.assertEquals((int) car3.year().get(), 2007);
for (Car car : query) {
System.out.println(car.manufacturer().get() + " " + car.model().get() + ", " + car.year().get());
}
uow.discard();
}
use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class TraceServiceMixin method traceSuccess.
@Override
public void traceSuccess(Class compositeType, Composite object, Method method, Object[] args, Object result, long entryTime, long durationNano) {
UnitOfWork uow = uowf.newUnitOfWork();
try {
createTraceRecord(uow, compositeType, object, method, args, entryTime, durationNano, null);
uow.complete();
} catch (ConcurrentEntityModificationException e) {
// ignore for now. Perhaps discard() and try again.
} catch (UnitOfWorkCompletionException e) {
// ignore for now. Perhaps discard() and try again.
}
}
use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class DebuggingTest method whenCallingMethodThenExpectDebugEntityCreated.
@Test
public void whenCallingMethodThenExpectDebugEntityCreated() {
UnitOfWork uow = module.newUnitOfWork();
try {
// There is no Query capability available for Libraries, since that sits in Extensions.
// Obtaining the EntityStore directly is a very ugly hack to get around this problem, and only related
// to the test sitting in qi4j-libraries source repository.
// QueryBuilder<DebugRecord> builder = module.newQueryBuilder( DebugRecord.class );
// Query<DebugRecord> query = builder.newQuery( uow );
// assertEquals( 0, query.count() );
Some service = (Some) module.findService(Some.class).get();
String message = service.doSomething("World!", 10);
assertEquals(message, "Hello!");
EntityStore es = (EntityStore) module.findService(EntityStore.class).get();
final String[] result = new String[1];
es.entityStates(module).transferTo(Transforms.map(new Function<EntityState, EntityState>() {
public EntityState map(EntityState entityState) {
if (ServiceDebugRecordEntity.class.getName().equals(first(entityState.entityDescriptor().types()).getName())) {
result[0] = entityState.identity().identity();
}
return entityState;
}
}, Outputs.<EntityState>noop()));
ServiceDebugRecordEntity debugEntry = uow.get(ServiceDebugRecordEntity.class, result[0]);
String mess = debugEntry.message().get();
System.out.println(mess);
assertEquals("some message.", mess);
uow.complete();
} catch (ConcurrentEntityModificationException e) {
e.printStackTrace();
uow.discard();
} catch (UnitOfWorkCompletionException e) {
e.printStackTrace();
uow.discard();
} finally {
if (uow.isOpen()) {
uow.discard();
}
}
}
use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class TraceServiceMixin method traceException.
@Override
public void traceException(Class compositeType, Composite object, Method method, Object[] args, Throwable t, long entryTime, long durationNano) {
UnitOfWork uow = uowf.newUnitOfWork();
try {
createTraceRecord(uow, compositeType, object, method, args, entryTime, durationNano, t);
uow.complete();
} catch (ConcurrentEntityModificationException e) {
// ignore for now. Perhaps discard() and try again.
} catch (UnitOfWorkCompletionException e) {
// ignore for now. Perhaps discard() and try again.
}
}
Aggregations