Search in sources :

Example 81 with UnitOfWork

use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.

the class PostgreSQLDBIntegrityTest method createAndRemoveEntityAndVerifyNoExtraDataLeftInDB.

@Test
public void createAndRemoveEntityAndVerifyNoExtraDataLeftInDB() throws Exception {
    UnitOfWork uow = this.module.newUnitOfWork();
    TestEntity entity = uow.newEntity(TestEntity.class);
    uow.complete();
    uow = this.module.newUnitOfWork();
    entity = uow.get(entity);
    SQLConfiguration config = uow.get(SQLConfiguration.class, PostgreSQLIndexQueryAssembler.DEFAULT_IDENTITY);
    String schemaName = config.schemaName().get();
    if (schemaName == null) {
        schemaName = PostgreSQLAppStartup.DEFAULT_SCHEMA_NAME;
    }
    uow.remove(entity);
    uow.complete();
    Connection connection = this.module.findService(DataSource.class).get().getConnection();
    try {
        GenericDatabaseExplorer.visitDatabaseTables(connection, null, schemaName, null, new DatabaseProcessorAdapter() {

            @Override
            public void beginProcessRowInfo(String schemaNamee, String tableName, Object[] rowContents) {
                if ((tableName.startsWith(DBNames.QNAME_TABLE_NAME_PREFIX) && (tableName.equals(DBNames.QNAME_TABLE_NAME_PREFIX + 0) || tableName.equals(DBNames.QNAME_TABLE_NAME_PREFIX + 1))) || tableName.equals(DBNames.ALL_QNAMES_TABLE_NAME) || tableName.equals(DBNames.ENTITY_TABLE_NAME)) {
                    throw new RuntimeException("Table: " + schemaNamee + "." + tableName);
                }
            }
        }, SQLVendorProvider.createVendor(PostgreSQLVendor.class));
    } finally {
        SQLUtil.closeQuietly(connection);
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) DatabaseProcessorAdapter(org.qi4j.index.sql.support.common.GenericDatabaseExplorer.DatabaseProcessorAdapter) Connection(java.sql.Connection) SQLConfiguration(org.qi4j.library.sql.common.SQLConfiguration) PostgreSQLVendor(org.sql.generation.api.vendor.PostgreSQLVendor) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 82 with UnitOfWork

use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.

the class SolrQueryServiceTest method index.

@Before
public void index() throws UnitOfWorkCompletionException, InterruptedException {
    // Create and index an entity
    UnitOfWork uow = module.newUnitOfWork();
    TestEntity test = uow.newEntity(TestEntity.class);
    test.name().set("Hello World");
    uow.complete();
    Thread.sleep(40);
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Before(org.junit.Before)

Example 83 with UnitOfWork

use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.

the class NoMetricsInstalledTest method createEntity.

private void createEntity(int id) throws UnitOfWorkCompletionException {
    UnitOfWork uow = module.newUnitOfWork();
    try {
        uow.newEntity(Person.class, "" + id);
        uow.complete();
    } finally {
        if (uow.isOpen())
            uow.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork)

Example 84 with UnitOfWork

use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.

the class YammerTest method createEntity.

private void createEntity(int id) throws UnitOfWorkCompletionException {
    UnitOfWork uow = module.newUnitOfWork();
    try {
        uow.newEntity(Person.class, "" + id);
        uow.complete();
    } finally {
        if (uow.isOpen())
            uow.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork)

Example 85 with UnitOfWork

use of org.qi4j.api.unitofwork.UnitOfWork 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)

Aggregations

UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)332 Test (org.junit.Test)232 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)108 Before (org.junit.Before)21 AssemblyException (org.qi4j.bootstrap.AssemblyException)21 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)18 Delivery (org.qi4j.sample.dcicargo.sample_a.data.shipping.delivery.Delivery)17 HandlingEventsEntity (org.qi4j.sample.dcicargo.sample_a.data.entity.HandlingEventsEntity)15 PersonEntity (org.qi4j.library.conversion.values.TestModel.PersonEntity)13 CargoAggregateRoot (org.qi4j.sample.dcicargo.sample_b.data.aggregateroot.CargoAggregateRoot)13 IOException (java.io.IOException)12 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)12 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)12 Cargos (org.qi4j.sample.dcicargo.sample_a.data.shipping.cargo.Cargos)12 HandlingEvent (org.qi4j.sample.dcicargo.sample_a.data.shipping.handling.HandlingEvent)12 Location (org.qi4j.sample.dcicargo.sample_a.data.shipping.location.Location)11 HandlingEventAggregateRoot (org.qi4j.sample.dcicargo.sample_b.data.aggregateroot.HandlingEventAggregateRoot)11 Date (java.util.Date)10 BalanceData (org.qi4j.dci.moneytransfer.domain.data.BalanceData)10 File (java.io.File)8