Search in sources :

Example 1 with UnitOfWorkCompletionException

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
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) AssemblyException(org.qi4j.bootstrap.AssemblyException) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 2 with UnitOfWorkCompletionException

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();
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 3 with UnitOfWorkCompletionException

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.
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException)

Example 4 with UnitOfWorkCompletionException

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();
        }
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Function(org.qi4j.functional.Function) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) EntityStore(org.qi4j.spi.entitystore.EntityStore) EntityState(org.qi4j.spi.entity.EntityState) ServiceDebugRecordEntity(org.qi4j.logging.debug.records.ServiceDebugRecordEntity) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 5 with UnitOfWorkCompletionException

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.
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException)

Aggregations

UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)20 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)18 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)12 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)5 Test (org.junit.Test)5 Serializable (java.io.Serializable)4 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)3 UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)3 AssemblyException (org.qi4j.bootstrap.AssemblyException)3 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)3 File (java.io.File)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 AbstractRequestCycleListener (org.apache.wicket.request.cycle.AbstractRequestCycleListener)2 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)2 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)2 UnitOfWorkException (org.qi4j.api.unitofwork.UnitOfWorkException)2 ConcurrentUoWFileModificationException (org.qi4j.library.uowfile.internal.ConcurrentUoWFileModificationException)2 EntityInstance (org.qi4j.runtime.entity.EntityInstance)2 ConcurrentEntityStateModificationException (org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException)2