private static void buildDemoNetwork(SingletonAssembler assembler) throws Exception { UnitOfWork uow = assembler.unitOfWorkFactory().newUnitOfWork(newUsecase("Build demo network of activities")); try { // try different combinations here... ActivityEntity a = uow.newEntity(ActivityEntity.class, "A"); ActivityEntity b = uow.newEntity(ActivityEntity.class, "B"); ActivityEntity c = uow.newEntity(ActivityEntity.class, "C"); ActivityEntity d = uow.newEntity(ActivityEntity.class, "D"); a.duration().set(2); b.duration().set(7); c.duration().set(3); d.duration().set(2); a.succeededBy(c, d); // Try different variations: b.succeededBy(d); // "Original" // b.succeededBy( c ); // Variation // c.succeededBy( a ); // "C" already planned // d.succeededBy( a ); // Cyclic dependency ProjectData project = uow.newEntity(ProjectData.class, PROJECT); // project.addActivities( a, b, c, d ); project.addActivities(d, c, b, a); // More interesting execution flow // Save uow.complete(); } catch (Exception e) { uow.discard(); throw e; } }
@Override public void insertInitialData() throws Exception { UnitOfWork unitOfWork = module.newUnitOfWork(); try { unitOfWork.get(Forums.class, Forums.FORUMS_ID); } catch (NoSuchEntityException e) { unitOfWork.newEntity(Forums.class, Forums.FORUMS_ID); } try { unitOfWork.get(Users.class, Users.USERS_ID); } catch (NoSuchEntityException e) { unitOfWork.newEntity(Users.class, Users.USERS_ID); } try { unitOfWork.complete(); } catch (UnitOfWorkCompletionException e) { throw new InitializationException(e); } }
@Test public void givenEntityWithImmutableAssociationWhenBuildingThenNoException() throws Exception { UnitOfWork unitOfWork = unitOfWorkFactory.newUnitOfWork(); try { PersonEntity father = unitOfWork.newEntity(PersonEntity.class); EntityBuilder<PersonEntity> builder = unitOfWork.newEntityBuilder(PersonEntity.class); PersonEntity instance = builder.instance(); instance.father().set(father); PersonEntity child = builder.newInstance(); } finally { unitOfWork.discard(); } }
private static void bootstrapData() throws Exception { UnitOfWork uow = module.newUnitOfWork(newUsecase("Bootstrap data")); try { SavingsAccountEntity account = uow.newEntity(SavingsAccountEntity.class, SAVINGS_ACCOUNT_ID); account.increasedBalance(1000); CheckingAccountEntity checkingAccount = uow.newEntity(CheckingAccountEntity.class, CHECKING_ACCOUNT_ID); checkingAccount.increasedBalance(200); // Create some creditor debt BalanceData bakerAccount = uow.newEntity(CreditorEntity.class, CREDITOR_ID1); bakerAccount.decreasedBalance(50); BalanceData butcherAccount = uow.newEntity(CreditorEntity.class, CREDITOR_ID2); butcherAccount.decreasedBalance(90); // Save uow.complete(); } finally { uow.discard(); } }
private void loadSchedules() throws UnitOfWorkCompletionException { UnitOfWork uow = module.newUnitOfWork(); try { Schedules schedules = uow.get(Schedules.class, getSchedulesIdentity(me)); for (Schedule schedule : schedules.schedules()) { dispatchForExecution(schedule); } } catch (NoSuchEntityException e) { // Create a new Schedules entity for keeping track of them all. uow.newEntity(Schedules.class, getSchedulesIdentity(me)); uow.complete(); } finally { if (uow.isOpen()) { uow.discard(); } } }
@Test public void createAndModifyEntity() throws Exception { UnitOfWork uow = this.module.newUnitOfWork(); TestEntity entity = uow.newEntity(TestEntity.class); uow.complete(); uow = this.module.newUnitOfWork(); entity = uow.get(entity); entity.testString().set("NewTestString"); uow.complete(); uow = this.module.newUnitOfWork(); entity = uow.get(entity); Assert.assertEquals( "New value did not store in indexing.", "NewTestString", entity.testString().get()); uow.discard(); }
@Test public void testEntity() throws UnitOfWorkCompletionException, IOException { SingletonAssembler assembler = new SingletonAssembler() { @Override public void assemble(ModuleAssembly module) throws AssemblyException { module.entities(TestEntity.class).withMixins(ScalaTraitMixin.class); module.services(TestService.class).withMixins(ScalaTraitMixin.class); new EntityTestAssembler().assemble(module); new RdfMemoryStoreAssembler().assemble(module); } }; // Create and update Entity UnitOfWork uow = assembler.module().newUnitOfWork(); try { Commands entity = uow.newEntity(Commands.class); entity.updateFoo("Foo"); Data data = uow.get(Data.class, entity.toString()); Assert.assertEquals("FooFoo", data.foo().get()); } finally { uow.complete(); } assembler.module().findService(IndexExporter.class).get().exportReadableToStream(System.out); // Find it uow = assembler.module().newUnitOfWork(); try { Data data = uow.newQuery( assembler .module() .newQueryBuilder(Data.class) .where(eq(templateFor(Data.class).foo(), "FooFoo"))) .find(); Assert.assertEquals("FooFoo", data.foo().get()); } finally { uow.discard(); } }
@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); } }
@Test public void testMigration() throws UnitOfWorkCompletionException, IOException { // Set up version 1 String id; StringInputOutput data_v1 = new StringInputOutput(); { SingletonAssembler v1 = new SingletonAssembler() { public void assemble(ModuleAssembly module) throws AssemblyException { MigrationTest.this.assemble(module); module.layer().application().setVersion("1.0"); } }; UnitOfWork uow = v1.unitOfWorkFactory().newUnitOfWork(); TestEntity1_0 entity = uow.newEntity(TestEntity1_0.class); entity.foo().set("Some value"); entity.fooManyAssoc().add(entity); entity.fooAssoc().set(entity); id = entity.identity().get(); uow.complete(); BackupRestore backupRestore = (BackupRestore) v1.module().serviceFinder().findService(BackupRestore.class).get(); backupRestore.backup().transferTo(data_v1); } // Set up version 1.1 StringInputOutput data_v1_1 = new StringInputOutput(); { SingletonAssembler v1_1 = new SingletonAssembler() { public void assemble(ModuleAssembly module) throws AssemblyException { MigrationTest.this.assemble(module); module.layer().application().setVersion("1.1"); } }; BackupRestore testData = (BackupRestore) v1_1.serviceFinder().findService(BackupRestore.class).get(); data_v1.transferTo(testData.restore()); UnitOfWork uow = v1_1.unitOfWorkFactory().newUnitOfWork(); TestEntity1_1 entity = uow.get(TestEntity1_1.class, id); assertThat( "Property has been renamed", entity.newFoo().get(), CoreMatchers.equalTo("Some value")); assertThat( "ManyAssociation has been renamed", entity.newFooManyAssoc().count(), CoreMatchers.equalTo(1)); assertThat( "Association has been renamed", entity.newFooAssoc().get(), CoreMatchers.equalTo(entity)); uow.complete(); testData.backup().transferTo(data_v1_1); } // Set up version 2.0 { SingletonAssembler v2_0 = new SingletonAssembler() { public void assemble(ModuleAssembly module) throws AssemblyException { MigrationTest.this.assemble(module); module.layer().application().setVersion("2.0"); } }; BackupRestore testData = (BackupRestore) v2_0.serviceFinder().findService(BackupRestore.class).get(); // Test migration from 1.0 -> 2.0 { data_v1.transferTo(testData.restore()); UnitOfWork uow = v2_0.unitOfWorkFactory().newUnitOfWork(); TestEntity2_0 entity = uow.get(TestEntity2_0.class, id); assertThat( "Property has been created", entity.bar().get(), CoreMatchers.equalTo("Some value")); assertThat( "Custom Property has been created", entity.customBar().get(), CoreMatchers.equalTo("Hello Some value")); assertThat( "ManyAssociation has been renamed", entity.newFooManyAssoc().count(), CoreMatchers.equalTo(1)); assertThat( "Association has been renamed", entity.newFooAssoc().get(), CoreMatchers.equalTo(entity)); uow.complete(); } } // Set up version 3.0 { SingletonAssembler v3_0 = new SingletonAssembler() { public void assemble(ModuleAssembly module) throws AssemblyException { MigrationTest.this.assemble(module); module.layer().application().setVersion("3.0"); } }; BackupRestore testData = (BackupRestore) v3_0.serviceFinder().findService(BackupRestore.class).get(); data_v1_1.transferTo(testData.restore()); // Test migration from 1.0 -> 3.0 { data_v1.transferTo(testData.restore()); UnitOfWork uow = v3_0.unitOfWorkFactory().newUnitOfWork(); org.qi4j.migration.moved.TestEntity2_0 entity = uow.get(org.qi4j.migration.moved.TestEntity2_0.class, id); uow.complete(); } } }