Package org.qi4j.api.unitofwork

Examples of org.qi4j.api.unitofwork.UnitOfWork


class TestData
{
    static void populate( Module module )
        throws UnitOfWorkCompletionException
    {
        UnitOfWork unitOfWork = module.newUnitOfWork();
        try
        {
            ValueBuilderFactory valueBuilderFactory = module;

            NameableAssert.clear();
            Domain gaming;
            {
                EntityBuilder<Domain> domainBuilder = unitOfWork.newEntityBuilder( Domain.class, "Gaming" );
                gaming = domainBuilder.instance();
                gaming.name().set( "Gaming" );
                gaming.description().set( "Gaming domain" );
                gaming = domainBuilder.newInstance();
                NameableAssert.trace( gaming );
            }

            Domain programming;
            {
                EntityBuilder<Domain> domainBuilder = unitOfWork.newEntityBuilder( Domain.class );
                programming = domainBuilder.instance();
                programming.name().set( "Programming" );
                programming.description().set( "Programing domain" );
                programming = domainBuilder.newInstance();
                NameableAssert.trace( programming );
            }

            Domain cooking;
            {
                EntityBuilder<Domain> domainBuilder = unitOfWork.newEntityBuilder( Domain.class );
                cooking = domainBuilder.instance();
                cooking.name().set( "Cooking" );
                cooking.description().set( "Cooking domain" );
                cooking = domainBuilder.newInstance();
                NameableAssert.trace( cooking );
            }

            Domain cars;
            {
                EntityBuilder<Domain> domainBuilder = unitOfWork.newEntityBuilder( Domain.class );
                cars = domainBuilder.instance();
                cars.name().set( "Cars" );
                cars.description().set( "Cars" );
                cars = domainBuilder.newInstance();
                NameableAssert.trace( cars );
            }

            City kualaLumpur;
            {
                EntityBuilder<City> cityBuilder = unitOfWork.newEntityBuilder( City.class );
                kualaLumpur = cityBuilder.instance();
                kualaLumpur.name().set( "Kuala Lumpur" );
                kualaLumpur.country().set( "Malaysia" );
                kualaLumpur.county().set( "Some Jaya" );
                kualaLumpur = cityBuilder.newInstance();
                NameableAssert.trace( kualaLumpur );
            }

            City penang;
            {
                EntityBuilder<City> cityBuilder = unitOfWork.newEntityBuilder( City.class );
                penang = cityBuilder.instance();
                penang.name().set( "Penang" );
                penang.country().set( "Malaysia" );
                penang.county().set( "Some Other Jaya" );
                penang = cityBuilder.newInstance();
                NameableAssert.trace( penang );
            }

            Account annsAccount;
            {
                EntityBuilder<Account> accountBuilder = unitOfWork.newEntityBuilder( Account.class );
                annsAccount = accountBuilder.instance();
                annsAccount.number().set( "accountOfAnnDoe" );
                annsAccount = accountBuilder.newInstance();
            }

            Account jacksAccount;
            {
                EntityBuilder<Account> accountBuilder = unitOfWork.newEntityBuilder( Account.class );
                jacksAccount = accountBuilder.instance();
                jacksAccount.number().set( "accountOfJackDoe" );
                jacksAccount = accountBuilder.newInstance();
            }

            ValueBuilder<Address> addressBuilder = module.newValueBuilder( Address.class );
            Address address = addressBuilder.prototype();
            address.line1().set( "Qi Street 4j" );
            address.line2().set( "Off main Java Street" );
            address.zipcode().set( "12345" );

            Female annDoe;
            {
                EntityBuilder<FemaleEntity> femaleBuilder = unitOfWork.newEntityBuilder( FemaleEntity.class, "anndoe" );
                annDoe = femaleBuilder.instance();
                annDoe.name().set( "Ann Doe" );
                annDoe.title().set( Person.Title.MRS );
                annDoe.placeOfBirth().set( kualaLumpur );
                annDoe.yearOfBirth().set( 1975 );
                annDoe.interests().add( 0, cooking );
                annDoe.password().set( "passwordOfAnnDoe" );
                annDoe.mainAccount().set( annsAccount );
                annDoe.accounts().add( 0, annsAccount );
                annDoe.accounts().add( 0, jacksAccount );
                annDoe.address().set( addressBuilder.newInstance() );
                annDoe = femaleBuilder.newInstance();
                NameableAssert.trace( annDoe );
            }

            {

                EntityBuilder<MaleEntity> maleBuilder = unitOfWork.newEntityBuilder( MaleEntity.class );
                Male joeDoe = maleBuilder.instance();
                joeDoe.name().set( "Joe Doe" );
                joeDoe.title().set( Person.Title.MR );
                joeDoe.placeOfBirth().set( kualaLumpur );
                joeDoe.yearOfBirth().set( 1990 );
                joeDoe.mother().set( annDoe );
                joeDoe.interests().add( 0, programming );
                joeDoe.interests().add( 0, gaming );
                joeDoe.email().set( "joe@thedoes.net" );
                joeDoe.password().set( "passwordOfJoeDoe" );
                joeDoe = maleBuilder.newInstance();
                address = module.newValueBuilderWithPrototype( address ).prototype();
                address.line1().set( "Qi Alley 4j" );
                joeDoe.address().set( address );
                NameableAssert.trace( joeDoe );
            }

            {
                EntityBuilder<MaleEntity> maleBuilder = unitOfWork.newEntityBuilder( MaleEntity.class );
                Male jackDoe = maleBuilder.instance();
                jackDoe.name().set( "Jack Doe" );
                jackDoe.title().set( Person.Title.DR );
                jackDoe.placeOfBirth().set( penang );
                jackDoe.yearOfBirth().set( 1970 );
                jackDoe.interests().add( 0, cars );
                jackDoe.wife().set( annDoe );
                jackDoe.password().set( "passwordOfJohnDoe" );
                jackDoe.mainAccount().set( jacksAccount );
                jackDoe.accounts().add( 0, annsAccount );
                jackDoe.accounts().add( 0, jacksAccount );
                address = module.newValueBuilderWithPrototype( address ).prototype();
                address.line1().set( "Qi Avenue 4j" );
                jackDoe.address().set( address );

                ValueBuilder<URL> urlBuilder = module.newValueBuilder( URL.class );
                ValueBuilder<Protocol> protocolBuilder = module.newValueBuilder( Protocol.class );
                ValueBuilder<QueryParam> queryParamBuilder = module.newValueBuilder( QueryParam.class );

                Protocol protocol = protocolBuilder.prototype();
                protocol.value().set( "http" );

                List<QueryParam> queryParams = new ArrayList<QueryParam>();
                QueryParam param = queryParamBuilder.prototype();
                param.name().set( "user" );
                param.value().set( "jackdoe" );
                queryParams.add( queryParamBuilder.newInstance() );
                queryParamBuilder = module.newValueBuilder( QueryParam.class );
                param = queryParamBuilder.prototype();
                param.name().set( "password" );
                param.value().set( "somepassword" );
                queryParams.add( queryParamBuilder.newInstance() );

                URL url = urlBuilder.prototype();
                url.protocol().set( protocolBuilder.newInstance() );
                url.queryParams().set( queryParams );

                jackDoe.personalWebsite().set( urlBuilder.newInstance() );

                jackDoe = maleBuilder.newInstance();
                NameableAssert.trace( jackDoe );
            }

            {
                EntityBuilder<CatEntity> catBuilder = unitOfWork.newEntityBuilder( CatEntity.class );
                Cat felix = catBuilder.instance();
                felix.name().set( "Felix" );
                felix = catBuilder.newInstance();
                // NameableAssert.trace( felix );
                unitOfWork.complete();
            }
        }
        finally
        {
            unitOfWork.discard();
        }
    }
View Full Code Here


    @Test
    public void givenEntityWithFieldPropertiesWhenUpdatedThenReturnCorrect()
        throws Exception
    {
        UnitOfWork unitOfWork = module.newUnitOfWork();
        try
        {
            PersonEntity charles = unitOfWork.newEntity( PersonEntity.class );
            charles.changeName( "Charles" );
            Assert.assertEquals( charles.getName(), "Charles" );

            PersonEntity daniel = unitOfWork.newEntity( PersonEntity.class );
            daniel.changeName( "Daniel" );
            Assert.assertEquals( daniel.getName(), "Daniel" );

            PersonEntity lisa = unitOfWork.newEntity( PersonEntity.class );
            lisa.changeName( "Lisa" );
            Assert.assertEquals( lisa.getName(), "Lisa" );

            charles.befriend( daniel );
            charles.befriend( lisa );
            charles.marry( lisa );

            unitOfWork.complete();

            unitOfWork = module.newUnitOfWork(  );

            charles = unitOfWork.get( charles );
            daniel = unitOfWork.get( daniel );
            Assert.assertTrue( charles.isFriend( daniel ) );

            unitOfWork.complete();
        }
        finally
        {
            unitOfWork.discard();
        }
    }
View Full Code Here

        app.activate();

        Module module = app.findModule( "Layer 1", "Module 1" );
        module.injectTo( this );

        UnitOfWork unitOfWork = uowf.newUnitOfWork();

        try
        {
            unitOfWork.newEntity( ProductEntity.class );
            fail( "Should not be able to create product here" );
        }
        catch (EntityTypeNotFoundException e)
        {
            // Ok
            ProductCatalog catalog = unitOfWork.newEntity( ProductCatalog.class, "1" );
            unitOfWork.complete();
        }
        unitOfWork = uowf.newUnitOfWork();

        String id;
        try
        {
            ProductCatalog catalog = unitOfWork.get( ProductCatalog.class, "1" );
            id = ( (Identity) catalog.newProduct() ).identity().get();
            unitOfWork.complete();
        }
        finally
        {
            unitOfWork.discard();
        }

        unitOfWork = module.newUnitOfWork();
        try
        {
            ProductCatalog catalog = unitOfWork.get( ProductCatalog.class, "1" );
            Product product = catalog.findProduct( id );
            product.price().set( 100 );
            unitOfWork.complete();
        }
        finally
        {
            unitOfWork.discard();
        }
    }
View Full Code Here

                ValueBuilder<ProductInfo> vb = vbf.newValueBuilder( ProductInfo.class );
                vb.prototype().description().set( "Some mundane description" );
                vb.prototype().weight().set( 1.0f );
                ProductInfo info = vb.newInstance();

                UnitOfWork uow = uowf.currentUnitOfWork();
                EntityBuilder<Product> eb = uow.newEntityBuilder( Product.class );
                eb.instance().name().set( "Product Name" );
                eb.instance().price().set( 100 );
                eb.instance().productInfo().set( info );
                return eb.newInstance();
            }
View Full Code Here

                return eb.newInstance();
            }

            public Product findProduct( String id )
            {
                UnitOfWork uow = uowf.currentUnitOfWork();
                return uow.get( Product.class, id );
            }
View Full Code Here

    @Test
    public void givenUnitOfWorkHasBeenCreateWhenCreatingNewEntityThenFindNewEntityWithGet()
        throws Exception
    {
        UnitOfWork uow = module.newUnitOfWork();
        try
        {
            EntityBuilder<Abc> builder = uow.newEntityBuilder( Abc.class, "123" );
            builder.instance().name().set( "Niclas" );
            builder.newInstance();
            uow.complete();
            uow = module.newUnitOfWork();
            Abc abc = uow.get( Abc.class, "123" );
            assertEquals( "Niclas", abc.name().get() );
        }
        finally
        {
            uow.discard();
        }
    }
View Full Code Here

    @Test
    public void givenEntityCreatedWhenRemovingEntityThenFindNewEntityShouldNotExist()
        throws Exception
    {
        UnitOfWork uow = module.newUnitOfWork();
        try
        {
            EntityBuilder<Abc> builder = uow.newEntityBuilder( Abc.class, "123" );
            builder.instance().name().set( "Niclas" );
            builder.newInstance();
            uow.complete();
            uow = module.newUnitOfWork();
            Abc abc = uow.get( Abc.class, "123" );
            assertEquals( "Niclas", abc.name().get() );
            uow.remove( abc );
            uow.complete();
            uow = module.newUnitOfWork();
            uow.get( Abc.class, "123" );
            fail( "This '123' entity should not exist." );
        }
        catch( NoSuchEntityException e )
        {
            // Expected.
        }
        finally
        {
            uow.discard();
        }
    }
View Full Code Here

    @Test
    public void givenDetachedEntityWhenRemovingEntityThenFindNewEntityShouldNotExist()
        throws Exception
    {
        UnitOfWork uow = module.newUnitOfWork();
        try
        {
            EntityBuilder<Abc> builder = uow.newEntityBuilder( Abc.class, "123" );
            builder.instance().name().set( "Niclas" );
            Abc abc = builder.newInstance();
            uow.complete();
            uow = module.newUnitOfWork();
            abc = uow.get( abc )// Attach the detached entity to 'uow' session.
            uow.remove( abc );
            uow.complete();
            uow = module.newUnitOfWork();
            uow.get( Abc.class, "123" );
            fail( "This '123' entity should not exist." );
        }
        catch( NoSuchEntityException e )
        {
            // Expected.
        }
        finally
        {
            uow.discard();
        }
    }
View Full Code Here

    @Test
    public void givenEntityCreatedWhenRemovingEntityBeforeCompletingUowThenFindNewEntityShouldNotExist()
        throws Exception
    {
        UnitOfWork uow = module.newUnitOfWork();
        try
        {
            EntityBuilder<Abc> builder = uow.newEntityBuilder( Abc.class, "123" );
            builder.instance().name().set( "Niclas" );
            Abc abc = builder.newInstance();
            uow.remove( abc );
            uow.complete();
            uow = module.newUnitOfWork();
            uow.get( Abc.class, "123" );
            fail( "This '123' entity should not exist." );
        }
        catch( NoSuchEntityException e )
        {
            // Expected.
        }
        finally
        {
            uow.discard();
        }
    }
View Full Code Here

    @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();
    }
View Full Code Here

TOP

Related Classes of org.qi4j.api.unitofwork.UnitOfWork

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.