Package org.qi4j.api.common

Source Code of org.qi4j.api.common.RemovalTest$PidParameters

/*
* Copyright 2009 Niclas Hedhman.
*
* Licensed  under the  Apache License,  Version 2.0  (the "License");
* you may not use  this file  except in  compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed  under the  License is distributed on an "AS IS" BASIS,
* WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.qi4j.api.common;

import org.junit.Test;
import org.qi4j.api.entity.EntityBuilder;
import org.qi4j.api.entity.EntityComposite;
import org.qi4j.api.property.Property;
import org.qi4j.api.unitofwork.UnitOfWork;
import org.qi4j.api.unitofwork.UnitOfWorkCompletionException;
import org.qi4j.bootstrap.AssemblyException;
import org.qi4j.bootstrap.ModuleAssembly;
import org.qi4j.entitystore.memory.MemoryEntityStoreService;
import org.qi4j.spi.uuid.UuidIdentityGeneratorService;
import org.qi4j.test.AbstractQi4jTest;

public class RemovalTest
    extends AbstractQi4jTest
{
    public void assemble( ModuleAssembly module )
        throws AssemblyException
    {
        module.entities( TestEntity.class );

        module.entities( PidRegulator.class );
        module.services( MemoryEntityStoreService.class );
        module.services( UuidIdentityGeneratorService.class );
    }

    @Test
    public void givenEntityIsCreatedAndUnitOfWorkIsNotCompletedWhenEntityIsRemoveThenSuccessfulRemoval()
        throws Exception
    {
        UnitOfWork uow = module.newUnitOfWork();
        EntityBuilder<TestEntity> builder = uow.newEntityBuilder( TestEntity.class, "123" );
        builder.instance().test().set( "habba" );
        TestEntity test = builder.newInstance();
        uow.remove( test );
        uow.complete();
    }

    @Test
    public void givenStandardPidRegulatorWhenNoChangeInInputExpectOutputToGoTowardsMinimum()
        throws Exception
    {
        UnitOfWork uow = module.newUnitOfWork();
        PidRegulator regulator = null;
        try
        {
            regulator = createPidRegulator( uow );
        }
        finally
        {
            if( regulator != null )
            {
                uow.remove( regulator );
            }
            // TODO: This problem is related to that uow.remove() has a bug.
            // If the Entity is both created and removed in the same session, then the remove() should simply remove
            // the entity from the internal UoW holding area, and not set the REMOVED status.

            // Probably that UnitOfWorkInstance.remove() should also call instanceCache.remove(), but the question is
            // then what is an InstanceKey vs EntityReference
            uow.complete();
        }
    }

    public interface TestEntity
        extends EntityComposite
    {
        @Optional
        Property<String> test();
    }

    private PidRegulator createPidRegulator( UnitOfWork uow )
        throws UnitOfWorkCompletionException
    {
        EntityBuilder<PidRegulator> builder = uow.newEntityBuilder( PidRegulator.class );
        PidRegulator prototype = builder.instance();
        prototype.p().set( 1.0f );
        prototype.i().set( 10f );
        prototype.d().set( 0.1f );
        prototype.maxD().set( 10f );
        prototype.maximum().set( 100f );
        prototype.minimum().set( 0f );
        PidRegulator regulator = builder.newInstance();

        return regulator;
    }

    //    @Mixins( { PidRegulatorAlgorithmMixin.class } )
    public interface PidRegulator
        extends PidParameters, EntityComposite
    {
    }

    public interface PidParameters
    {
        Property<Float> p();

        Property<Float> i();

        Property<Float> d();

        Property<Float> maxD();

        Property<Float> minimum();

        Property<Float> maximum();
    }
}
TOP

Related Classes of org.qi4j.api.common.RemovalTest$PidParameters

TOP
Copyright © 2018 www.massapi.com. 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.
unction(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-20639858-1', 'auto'); ga('send', 'pageview');