Commits

Michael Ludwig committed 45fc68b

Reduce number of duplicated tests between unit and integration tests

Comments (0)

Files changed (8)

entreri-apt-tests/src/main/java/com.lhkbob.entreri.components/AbstractComponent.java

-/*
- * Entreri, an entity-component framework in Java
- *
- * Copyright (c) 2012, Michael Ludwig
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- *     Redistributions of source code must retain the above copyright notice,
- *         this list of conditions and the following disclaimer.
- *     Redistributions in binary form must reproduce the above copyright notice,
- *         this list of conditions and the following disclaimer in the
- *         documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.lhkbob.entreri.components;
-
-import com.lhkbob.entreri.Component;
-
-/**
- * A test component that is abstract to test component validation, e.g. this type is
- * invalid.
- *
- * @author Michael Ludwig
- */
-public abstract class AbstractComponent implements Component {
-
-    public abstract Object getUserData();
-
-    public abstract void setUserData(Object value);
-}

entreri-apt-tests/src/main/java/com.lhkbob.entreri.components/BadConstructorComponent.java

-/*
- * Entreri, an entity-component framework in Java
- *
- * Copyright (c) 2012, Michael Ludwig
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- *     Redistributions of source code must retain the above copyright notice,
- *         this list of conditions and the following disclaimer.
- *     Redistributions in binary form must reproduce the above copyright notice,
- *         this list of conditions and the following disclaimer in the
- *         documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.lhkbob.entreri.components;
-
-import com.lhkbob.entreri.*;
-
-/**
- * A test component that is a class to test component validation, e.g. this type is
- * invalid.
- *
- * @author Michael Ludwig
- */
-public class BadConstructorComponent implements Component {
-
-    @Override
-    public EntitySystem getEntitySystem() {
-        return null;
-    }
-
-    @Override
-    public Entity getEntity() {
-        return null;
-    }
-
-    @Override
-    public boolean isAlive() {
-        return false;
-    }
-
-    @Override
-    public boolean isFlyweight() {
-        return false;
-    }
-
-    @Override
-    public int getIndex() {
-        return 0;
-    }
-
-    @Override
-    public int getVersion() {
-        return 0;
-    }
-
-    @Override
-    public void updateVersion() {
-    }
-
-    @Override
-    public Class<? extends Component> getType() {
-        return null;
-    }
-
-    @Override
-    public void setOwner(Owner owner) {
-    }
-
-    @Override
-    public Owner getOwner() {
-        return null;
-    }
-
-    @Override
-    public Owner notifyOwnershipGranted(Ownable obj) {
-        return null;
-    }
-
-    @Override
-    public void notifyOwnershipRevoked(Ownable obj) {
-    }
-}

entreri-apt-tests/src/test/java/com/lhkbob/entreri/ComponentIteratorTest.java

-/*
- * Entreri, an entity-component framework in Java
- *
- * Copyright (c) 2012, Michael Ludwig
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- *     Redistributions of source code must retain the above copyright notice,
- *         this list of conditions and the following disclaimer.
- *     Redistributions in binary form must reproduce the above copyright notice,
- *         this list of conditions and the following disclaimer in the
- *         documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.lhkbob.entreri;
-
-import com.lhkbob.entreri.components.FloatComponent;
-import com.lhkbob.entreri.components.IntComponent;
-import com.lhkbob.entreri.components.ObjectComponent;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-public class ComponentIteratorTest {
-    private static final int ENTITY_COUNT = 5;
-
-    private EntitySystem system;
-    private List<Integer> entityIds;
-    private List<ObjectComponent.FooBlah> entityObjValues;
-    private List<Float> entityFloatValues;
-
-    private List<ObjectComponent.FooBlah> entityCombinedObjValues;
-    private List<Float> entityCombinedFloatValues;
-
-    private int countWithObj;
-    private int countWithFloat;
-    private int countWithBoth;
-
-    private ObjectComponent objData;
-    private FloatComponent floatData;
-
-    @Before
-    public void setup() {
-        entityIds = new ArrayList<Integer>();
-        entityObjValues = new ArrayList<ObjectComponent.FooBlah>();
-        entityFloatValues = new ArrayList<Float>();
-        entityCombinedObjValues = new ArrayList<ObjectComponent.FooBlah>();
-        entityCombinedFloatValues = new ArrayList<Float>();
-
-        system = EntitySystem.create();
-
-        for (int i = 0; i < ENTITY_COUNT; i++) {
-            Entity e = system.addEntity();
-
-            entityIds.add(e.getId());
-
-            double c = Math.random();
-            if (c > .8) {
-                // both components to add
-                objData = e.add(ObjectComponent.class);
-                ObjectComponent.FooBlah v = new ObjectComponent.FooBlah();
-                entityObjValues.add(v);
-                entityCombinedObjValues.add(v);
-                objData.setObject(v);
-
-                floatData = e.add(FloatComponent.class);
-                float fv = (float) (Math.random() * 1000);
-                entityFloatValues.add(fv);
-                entityCombinedFloatValues.add(fv);
-                floatData.setFloat(fv);
-
-                countWithBoth++;
-                countWithObj++;
-                countWithFloat++;
-            } else if (c > .4) {
-                // just float component
-                floatData = e.add(FloatComponent.class);
-                float fv = (float) (Math.random() * 1000);
-                entityFloatValues.add(fv);
-                floatData.setFloat(fv);
-
-                countWithFloat++;
-            } else {
-                // just object component
-                objData = e.add(ObjectComponent.class);
-                ObjectComponent.FooBlah v = new ObjectComponent.FooBlah();
-                entityObjValues.add(v);
-                objData.setObject(v);
-
-                countWithObj++;
-            }
-        }
-    }
-
-    private void doTestIterator(Iterator<Entity> it) {
-        int i = 0;
-        while (it.hasNext()) {
-            Assert.assertEquals(entityIds.get(i), Integer.valueOf(it.next().getId()));
-            i++;
-        }
-
-        Assert.assertEquals(entityIds.size(), i);
-    }
-
-    // assumes it has objData in it
-    private void doTestObjectComponentIterator(ComponentIterator it) {
-        int i = 0;
-        while (it.next()) {
-            Assert.assertEquals(entityObjValues.get(i), objData.getObject());
-            i++;
-        }
-
-        Assert.assertEquals(countWithObj, i);
-    }
-
-    // assumes it has floatData in it
-    private void doTestFloatComponentIterator(ComponentIterator it) {
-        int i = 0;
-        while (it.next()) {
-            Assert.assertEquals(entityFloatValues.get(i), floatData.getFloat(), .0001f);
-            i++;
-        }
-
-        Assert.assertEquals(countWithFloat, i);
-    }
-
-    // assumes it has floatData and objData as required
-    private void doTestBulkComponentIterator(ComponentIterator it) {
-        int i = 0;
-        while (it.next()) {
-            Assert.assertEquals(entityCombinedObjValues.get(i), objData.getObject());
-            Assert.assertEquals(entityCombinedFloatValues.get(i), floatData.getFloat(),
-                                .0001f);
-            i++;
-        }
-
-        Assert.assertEquals(countWithBoth, i);
-    }
-
-    private void doIteratorRemove(Iterator<Entity> it) {
-        int i = 0;
-        Iterator<Integer> ids = entityIds.iterator();
-        while (it.hasNext()) {
-            it.next();
-            ids.next();
-            if (i > ENTITY_COUNT / 2) {
-                it.remove();
-                ids.remove();
-            }
-
-            i++;
-        }
-
-        // this invalidates all of the value lists, but that is okay
-    }
-
-    @Test
-    public void testEntityIterator() {
-        doTestIterator(system.iterator());
-    }
-
-    @Test
-    public void testComponentIterator() {
-        ComponentIterator ft = system.fastIterator();
-        floatData = ft.addRequired(FloatComponent.class);
-        ComponentIterator it = system.fastIterator();
-        objData = it.addRequired(ObjectComponent.class);
-
-        doTestObjectComponentIterator(it);
-        it.reset();
-        doTestObjectComponentIterator(it);
-
-        doTestFloatComponentIterator(ft);
-        ft.reset();
-        doTestFloatComponentIterator(ft);
-    }
-
-    @Test
-    public void testBulkComponentIterator() {
-        ComponentIterator it = system.fastIterator();
-        floatData = it.addRequired(FloatComponent.class);
-        objData = it.addRequired(ObjectComponent.class);
-
-        doTestBulkComponentIterator(it);
-        it.reset();
-        doTestBulkComponentIterator(it);
-    }
-
-    @Test
-    public void testEntityIteratorRemove() {
-        doIteratorRemove(system.iterator());
-        doTestIterator(system.iterator());
-    }
-
-    @Test
-    public void testFlyweightComponent() {
-        ComponentIterator it = system.fastIterator();
-        IntComponent c = it.addRequired(IntComponent.class);
-
-        Assert.assertTrue(c.isFlyweight());
-        Assert.assertFalse(c.isAlive());
-
-        int count = 0;
-        system.addEntity().add(IntComponent.class);
-        while (it.next()) {
-            Assert.assertTrue(c.isFlyweight());
-            Assert.assertTrue(c.isAlive());
-            count++;
-        }
-
-        Assert.assertEquals(1, count);
-    }
-}

entreri-apt-tests/src/test/java/com/lhkbob/entreri/EntitySystemTest.java

-/*
- * Entreri, an entity-component framework in Java
- *
- * Copyright (c) 2012, Michael Ludwig
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- *     Redistributions of source code must retain the above copyright notice,
- *         this list of conditions and the following disclaimer.
- *     Redistributions in binary form must reproduce the above copyright notice,
- *         this list of conditions and the following disclaimer in the
- *         documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.lhkbob.entreri;
-
-import com.lhkbob.entreri.components.ComplexComponent;
-import com.lhkbob.entreri.components.FloatComponent;
-import com.lhkbob.entreri.components.IntComponent;
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-public class EntitySystemTest {
-    @Test
-    public void testAddEntity() {
-        // There really isn't much to test with this one, everything else
-        // is validated by other tests in this package
-        EntitySystem system = EntitySystem.create();
-        Entity e = system.addEntity();
-
-        int componentCount = 0;
-        for (@SuppressWarnings("unused") Component c : e) {
-            componentCount++;
-        }
-
-        Assert.assertEquals(0, componentCount);
-        Assert.assertEquals(system, e.getEntitySystem());
-        Assert.assertTrue(e.isAlive());
-
-        int entityCount = 0;
-        for (Entity entity : system) {
-            entityCount++;
-            Assert.assertSame(e, entity);
-        }
-        Assert.assertEquals(1, entityCount);
-    }
-
-    @Test
-    public void testAddEntityFromTemplate() {
-        EntitySystem system = EntitySystem.create();
-        Entity template = system.addEntity();
-
-        IntComponent tc1 = template.add(IntComponent.class);
-        tc1.setInt(2);
-        FloatComponent tc2 = template.add(FloatComponent.class);
-        tc2.setFloat(3f);
-
-        Entity fromTemplate = system.addEntity(template);
-        IntComponent c1 = fromTemplate.get(IntComponent.class);
-        FloatComponent c2 = fromTemplate.get(FloatComponent.class);
-
-        Assert.assertEquals(2, c1.getInt());
-        Assert.assertEquals(3f, c2.getFloat(), .0001f);
-        Assert.assertFalse(c1.equals(tc1));
-        Assert.assertFalse(c2.equals(tc2));
-        Assert.assertNotSame(template, fromTemplate);
-    }
-
-    @Test
-    public void testAddEntityFromTemplateInAnotherSystem() {
-        EntitySystem system1 = EntitySystem.create();
-        Entity template = system1.addEntity();
-
-        IntComponent tc1 = template.add(IntComponent.class);
-        tc1.setInt(2);
-        FloatComponent tc2 = template.add(FloatComponent.class);
-        tc2.setFloat(3f);
-
-        EntitySystem system2 = EntitySystem.create();
-        Entity fromTemplate = system2.addEntity(template);
-        IntComponent c1 = fromTemplate.get(IntComponent.class);
-        FloatComponent c2 = fromTemplate.get(FloatComponent.class);
-
-        Assert.assertEquals(2, c1.getInt());
-        Assert.assertEquals(3f, c2.getFloat(), .0001f);
-        Assert.assertFalse(c1.equals(tc1));
-        Assert.assertFalse(c2.equals(tc2));
-        Assert.assertNotSame(template, fromTemplate);
-    }
-
-    @Test
-    public void testRemoveEntity() {
-        EntitySystem system = EntitySystem.create();
-        Entity e = system.addEntity();
-        IntComponent c = e.add(IntComponent.class);
-
-        system.removeEntity(e);
-        Assert.assertFalse(e.isAlive());
-        Assert.assertFalse(c.isAlive());
-        Assert.assertEquals(1, e.getId()); // it's id should remain unchanged
-
-        Assert.assertFalse(system.iterator().hasNext());
-    }
-
-    @Test
-    public void testIteratorRemoveEntity() {
-        EntitySystem system = EntitySystem.create();
-        List<Entity> original = new ArrayList<Entity>();
-
-        List<Entity> removed = new ArrayList<Entity>();
-
-        for (int i = 0; i < 10; i++) {
-            original.add(system.addEntity());
-        }
-
-        Iterator<Entity> it = system.iterator();
-        while (it.hasNext()) {
-            removed.add(it.next());
-            it.remove();
-        }
-
-        Assert.assertEquals(original, removed);
-    }
-
-    @Test
-    public void testIteratorExternalRemoveEntity() {
-        EntitySystem system = EntitySystem.create();
-        List<Entity> original = new ArrayList<Entity>();
-
-        List<Entity> removed = new ArrayList<Entity>();
-
-        for (int i = 0; i < 10; i++) {
-            original.add(system.addEntity());
-        }
-
-        Iterator<Entity> it = system.iterator();
-        while (it.hasNext()) {
-            Entity e = it.next();
-            removed.add(e);
-            system.removeEntity(e);
-        }
-
-        Assert.assertEquals(original, removed);
-    }
-
-    @Test
-    public void testCompactNoOp() {
-        EntitySystem system = EntitySystem.create();
-        for (int i = 0; i < 5; i++) {
-            system.addEntity().add(ComplexComponent.class);
-        }
-
-        system.compact();
-
-        int count = 0;
-        Iterator<Entity> it = system.iterator();
-        while (it.hasNext()) {
-            Entity e = it.next();
-            Assert.assertNotNull(e.get(ComplexComponent.class));
-            count++;
-        }
-
-        Assert.assertEquals(5, count);
-    }
-
-    @Test
-    public void testCompactRepairRemoves() {
-        EntitySystem system = EntitySystem.create();
-        List<Entity> es = new ArrayList<Entity>();
-        List<Float> cs = new ArrayList<Float>();
-        for (int i = 0; i < 100; i++) {
-            es.add(system.addEntity());
-            ComplexComponent c = es.get(es.size() - 1).add(ComplexComponent.class);
-            float f = (float) Math.random();
-            float f2 = (float) Math.random();
-            c.setFloat(f);
-            c.setFactoryFloat(f2);
-
-            cs.add(f);
-            cs.add(f2);
-        }
-
-        int i = 0;
-        Iterator<Entity> it = es.iterator();
-        Iterator<Float> ft = cs.iterator();
-        while (it.hasNext()) {
-            Entity e = it.next();
-            ft.next(); // always advance once
-            if (i % 2 == 0) {
-                it.remove();
-                system.removeEntity(e);
-
-                ft.remove();
-                ft.next();
-                ft.remove(); // remove 2nd element
-            } else {
-                ft.next(); // advance past 2nd element
-            }
-            i++;
-        }
-
-        system.compact();
-
-        it = es.iterator();
-        Iterator<Entity> si = system.iterator();
-        ft = cs.iterator();
-        while (it.hasNext() && si.hasNext()) {
-            Entity e = si.next();
-            Assert.assertEquals(it.next(), e);
-            Assert.assertEquals(ft.next(), e.get(ComplexComponent.class).getFloat(),
-                                .0001f);
-            Assert.assertEquals(ft.next(),
-                                e.get(ComplexComponent.class).getFactoryFloat(), .0001f);
-        }
-        Assert.assertFalse(it.hasNext());
-        Assert.assertFalse(si.hasNext());
-    }
-
-    @Test
-    public void testCompactAddRemoveRepair() {
-        EntitySystem system = EntitySystem.create();
-        List<Entity> es = new ArrayList<Entity>();
-        List<Float> cs = new ArrayList<Float>();
-        for (int i = 0; i < 100; i++) {
-            es.add(system.addEntity());
-            ComplexComponent c = es.get(es.size() - 1).add(ComplexComponent.class);
-            float f = (float) Math.random();
-            float f2 = (float) Math.random();
-            c.setFloat(f);
-            c.setFactoryFloat(f2);
-
-            cs.add(f);
-            cs.add(f2);
-        }
-
-        // remove a bunch of components from the entities
-        int i = 0;
-        Iterator<Entity> it = es.iterator();
-        while (it.hasNext()) {
-            Entity e = it.next();
-            if (i % 2 == 0) {
-                e.remove(ComplexComponent.class);
-            }
-            i++;
-        }
-
-        // now add back in component values for the previously removed entities
-        i = 0;
-        it = es.iterator();
-        Iterator<Float> ft = cs.iterator();
-        while (it.hasNext()) {
-            Entity e = it.next();
-            ComplexComponent c = e.get(ComplexComponent.class);
-
-            float f = ft.next();
-            float f2 = ft.next();
-
-            if (c == null) {
-                c = e.add(ComplexComponent.class);
-                c.setFloat(f);
-                c.setFactoryFloat(f2);
-            }
-            i++;
-        }
-
-        system.compact();
-
-        it = es.iterator();
-        Iterator<Entity> si = system.iterator();
-        ft = cs.iterator();
-        while (it.hasNext() && si.hasNext()) {
-            Entity e = si.next();
-            Assert.assertEquals(it.next(), e);
-            Assert.assertEquals(ft.next(), e.get(ComplexComponent.class).getFloat(),
-                                .0001f);
-            Assert.assertEquals(ft.next(),
-                                e.get(ComplexComponent.class).getFactoryFloat(), .0001f);
-        }
-        Assert.assertFalse(it.hasNext());
-        Assert.assertFalse(si.hasNext());
-    }
-}

entreri-apt-tests/src/test/java/com/lhkbob/entreri/OwnerTest.java

-/*
- * Entreri, an entity-component framework in Java
- *
- * Copyright (c) 2012, Michael Ludwig
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- *     Redistributions of source code must retain the above copyright notice,
- *         this list of conditions and the following disclaimer.
- *     Redistributions in binary form must reproduce the above copyright notice,
- *         this list of conditions and the following disclaimer in the
- *         documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.lhkbob.entreri;
-
-import com.lhkbob.entreri.components.FloatComponent;
-import com.lhkbob.entreri.components.IntComponent;
-import junit.framework.Assert;
-import org.junit.Test;
-
-public class OwnerTest {
-
-    @Test
-    public void testEntitySetOwner() {
-        EntitySystem system = EntitySystem.create();
-
-        Entity e1 = system.addEntity();
-        Entity e2 = system.addEntity();
-
-        Assert.assertNull(e1.getOwner());
-        Assert.assertNull(e2.getOwner());
-
-        e1.setOwner(e2);
-
-        Assert.assertSame(e2, e1.getOwner());
-        Assert.assertNull(e2.getOwner());
-
-        IntComponent c2 = e2.add(IntComponent.class);
-        e1.setOwner(c2);
-
-        Assert.assertSame(c2, e1.getOwner());
-    }
-
-    @Test
-    public void testComponentSetOwner() {
-        EntitySystem system = EntitySystem.create();
-
-        Entity e1 = system.addEntity();
-        Entity e2 = system.addEntity();
-
-        IntComponent c1a = e1.add(IntComponent.class);
-        FloatComponent c1b = e1.add(FloatComponent.class);
-
-        Assert.assertNull(c1a.getOwner());
-        Assert.assertNull(c1b.getOwner());
-
-        c1a.setOwner(c1b);
-
-        Assert.assertSame(c1b, c1a.getOwner());
-        Assert.assertNull(c1b.getOwner());
-
-        c1a.setOwner(e2);
-
-        Assert.assertSame(e2, c1a.getOwner());
-    }
-
-    @Test
-    public void testOwnedEntityRemoval() {
-        EntitySystem system = EntitySystem.create();
-        Entity e1 = system.addEntity();
-
-        final boolean[] revoked = new boolean[1];
-
-        e1.setOwner(new Owner() {
-            @Override
-            public Owner notifyOwnershipGranted(Ownable obj) {
-                return this;
-            }
-
-            @Override
-            public void notifyOwnershipRevoked(Ownable obj) {
-                revoked[0] = true;
-            }
-        });
-
-        system.removeEntity(e1);
-        Assert.assertTrue(revoked[0]);
-    }
-
-    @Test
-    public void testOwnedComponentRemoval() {
-        EntitySystem system = EntitySystem.create();
-        Entity e1 = system.addEntity();
-        IntComponent c1 = e1.add(IntComponent.class);
-
-        final boolean[] revoked = new boolean[1];
-
-        c1.setOwner(new Owner() {
-            @Override
-            public Owner notifyOwnershipGranted(Ownable obj) {
-                return this;
-            }
-
-            @Override
-            public void notifyOwnershipRevoked(Ownable obj) {
-                revoked[0] = true;
-            }
-        });
-
-        e1.remove(IntComponent.class);
-        Assert.assertTrue(revoked[0]);
-    }
-
-    @Test
-    public void testOwnedComponentAdd() {
-        EntitySystem system = EntitySystem.create();
-        Entity e1 = system.addEntity();
-
-        IntComponent c1 = e1.add(IntComponent.class);
-
-        final boolean[] revoked = new boolean[1];
-
-        c1.setOwner(new Owner() {
-            @Override
-            public Owner notifyOwnershipGranted(Ownable obj) {
-                return this;
-            }
-
-            @Override
-            public void notifyOwnershipRevoked(Ownable obj) {
-                revoked[0] = true;
-            }
-        });
-
-        IntComponent c2 = e1.add(IntComponent.class);
-        Assert.assertTrue(revoked[0]);
-        Assert.assertNull(c2.getOwner());
-    }
-
-    @Test
-    public void testEntityRemovalCleanup() {
-        EntitySystem system = EntitySystem.create();
-
-        Entity owner = system.addEntity();
-
-        Entity spare = system.addEntity();
-        IntComponent ownedC = spare.add(IntComponent.class);
-        ownedC.setOwner(owner);
-
-        Entity ownedE = system.addEntity();
-        ownedE.setOwner(owner);
-
-        system.removeEntity(owner);
-        Assert.assertFalse(ownedC.isAlive());
-        Assert.assertFalse(ownedE.isAlive());
-        Assert.assertTrue(spare.isAlive());
-    }
-
-    @Test
-    public void testComponentRemovalCleanup() {
-        EntitySystem system = EntitySystem.create();
-
-        Entity e1 = system.addEntity();
-
-        IntComponent owner = e1.add(IntComponent.class);
-        FloatComponent ownedC = e1.add(FloatComponent.class);
-        ownedC.setOwner(owner);
-
-        Entity ownedE = system.addEntity();
-        ownedE.setOwner(owner);
-
-        e1.remove(IntComponent.class);
-        Assert.assertFalse(ownedC.isAlive());
-        Assert.assertFalse(ownedE.isAlive());
-    }
-
-    @Test
-    public void testComplexOwnershipHierarchyCleanup() {
-        EntitySystem system = EntitySystem.create();
-
-        Entity e1 = system.addEntity();
-        IntComponent c1 = e1.add(IntComponent.class);
-
-        Entity e2 = system.addEntity();
-        IntComponent c2 = e2.add(IntComponent.class);
-
-        Entity e3 = system.addEntity();
-        IntComponent c3 = e3.add(IntComponent.class);
-
-        e1.setOwner(e2);
-        e2.setOwner(e3);
-        e3.setOwner(c1);
-        c1.setOwner(c2);
-        c2.setOwner(c3);
-
-        e3.remove(IntComponent.class);
-
-        Assert.assertFalse(e1.isAlive());
-        Assert.assertFalse(e2.isAlive());
-        Assert.assertFalse(e3.isAlive());
-        Assert.assertFalse(c1.isAlive());
-        Assert.assertFalse(c2.isAlive());
-        Assert.assertFalse(c3.isAlive());
-    }
-
-    @Test
-    public void testComponentOwningParentEntityRemoval() {
-        EntitySystem system = EntitySystem.create();
-        Entity e = system.addEntity();
-        IntComponent c = e.add(IntComponent.class);
-
-        e.setOwner(c);
-
-        system.removeEntity(e);
-        Assert.assertFalse(e.isAlive());
-        Assert.assertFalse(c.isAlive());
-    }
-
-    @Test
-    public void testFlyweightComponentOwnership() {
-        EntitySystem system = EntitySystem.create();
-        Entity e = system.addEntity();
-        IntComponent c = e.add(IntComponent.class);
-
-        Entity e2 = system.addEntity();
-
-        ComponentIterator it = system.fastIterator();
-        IntComponent flyweight = it.addRequired(IntComponent.class);
-        it.next();
-
-        flyweight.setOwner(e2);
-        Assert.assertSame(e2, flyweight.getOwner());
-        Assert.assertSame(e2, c.getOwner());
-
-        e.setOwner(flyweight);
-        Assert.assertSame(c, e.getOwner());
-    }
-}

entreri-apt-tests/src/test/java/com/lhkbob/entreri/PropertyFactoryTest.java

-/*
- * Entreri, an entity-component framework in Java
- *
- * Copyright (c) 2012, Michael Ludwig
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- *     Redistributions of source code must retain the above copyright notice,
- *         this list of conditions and the following disclaimer.
- *     Redistributions in binary form must reproduce the above copyright notice,
- *         this list of conditions and the following disclaimer in the
- *         documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.lhkbob.entreri;
-
-import com.lhkbob.entreri.property.Attributes;
-import com.lhkbob.entreri.property.Clone;
-import com.lhkbob.entreri.property.Clone.Policy;
-import com.lhkbob.entreri.property.DoubleProperty;
-import com.lhkbob.entreri.property.DoubleProperty.DefaultDouble;
-import com.lhkbob.entreri.property.ObjectProperty;
-import junit.framework.Assert;
-import org.junit.Test;
-
-@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
-public class PropertyFactoryTest {
-    /*
-     * ObjectProperty fields for Attributes creation
-     */
-    private ObjectProperty<Object> objectPropertyNoPolicy;
-
-    @Clone(Policy.DISABLE)
-    private ObjectProperty<Object> objectPropertyDisabled;
-
-    @Clone(Policy.JAVA_DEFAULT)
-    private ObjectProperty<Object> objectPropertyDefault;
-
-    @Clone(Policy.INVOKE_CLONE)
-    private ObjectProperty<Object> objectPropertyInvoke;
-
-    /*
-     * DoubleProperty fields for Attributes creation
-     */
-    @DefaultDouble(1.0)
-    private DoubleProperty doublePropertyNoPolicy;
-
-    @DefaultDouble(2.0)
-    @Clone(Policy.DISABLE)
-    private DoubleProperty doublePropertyDisabled;
-
-    @Clone(Policy.JAVA_DEFAULT)
-    private DoubleProperty doublePropertyDefault;
-
-    @Clone(Policy.INVOKE_CLONE)
-    private DoubleProperty doublePropertyInvoke;
-
-    private Attributes createAttributes(String fieldName) throws Exception {
-        return new Attributes(getClass().getDeclaredField(fieldName).getAnnotations());
-    }
-
-    @Test
-    public void testObjectPropertyCloneNoPolicy() throws Exception {
-        ObjectProperty.Factory factory = new ObjectProperty.Factory(
-                createAttributes("objectPropertyNoPolicy"));
-
-        ObjectProperty p1 = factory.create();
-        ObjectProperty p2 = factory.create();
-
-        factory.setDefaultValue(p1, 0);
-        factory.setDefaultValue(p2, 0);
-
-        Object val = new Object();
-        p1.set(0, val);
-
-        Assert.assertSame(val, p1.get(0));
-        Assert.assertNull(p2.get(0));
-
-        factory.clone(p1, 0, p2, 0);
-
-        Assert.assertSame(val, p1.get(0));
-        Assert.assertSame(val, p2.get(0));
-    }
-
-    @Test
-    public void testObjectPropertyCloneDisabled() throws Exception {
-        ObjectProperty.Factory factory = new ObjectProperty.Factory(
-                createAttributes("objectPropertyDisabled"));
-
-        ObjectProperty p1 = factory.create();
-        ObjectProperty p2 = factory.create();
-
-        factory.setDefaultValue(p1, 0);
-        factory.setDefaultValue(p2, 0);
-
-        Object val = new Object();
-        p1.set(0, val);
-
-        Assert.assertSame(val, p1.get(0));
-        Assert.assertNull(p2.get(0));
-
-        factory.clone(p1, 0, p2, 0);
-
-        Assert.assertSame(val, p1.get(0));
-        Assert.assertNull(p2.get(0));
-    }
-
-    @Test
-    public void testObjectPropertyCloneJavaDefault() throws Exception {
-        ObjectProperty.Factory factory = new ObjectProperty.Factory(
-                createAttributes("objectPropertyDefault"));
-
-        ObjectProperty p1 = factory.create();
-        ObjectProperty p2 = factory.create();
-
-        factory.setDefaultValue(p1, 0);
-        factory.setDefaultValue(p2, 0);
-
-        Object val = new Object();
-        p1.set(0, val);
-
-        Assert.assertSame(val, p1.get(0));
-        Assert.assertNull(p2.get(0));
-
-        factory.clone(p1, 0, p2, 0);
-
-        Assert.assertSame(val, p1.get(0));
-        Assert.assertSame(val, p2.get(0));
-    }
-
-    @Test
-    public void testObjectPropertyCloneInvoke() throws Exception {
-        ObjectProperty.Factory factory = new ObjectProperty.Factory(
-                createAttributes("objectPropertyInvoke"));
-
-        ObjectProperty p1 = factory.create();
-        ObjectProperty p2 = factory.create();
-
-        factory.setDefaultValue(p1, 0);
-        factory.setDefaultValue(p2, 0);
-
-        CloneObject val = new CloneObject(5);
-        p1.set(0, val);
-
-        Assert.assertSame(val, p1.get(0));
-        Assert.assertNull(p2.get(0));
-
-        factory.clone(p1, 0, p2, 0);
-
-        Assert.assertSame(val, p1.get(0));
-        Assert.assertNotSame(val, p2.get(0));
-        Assert.assertNotNull(p2.get(0));
-        Assert.assertEquals(5, ((CloneObject) p2.get(0)).foo);
-    }
-
-    @Test
-    public void testPrimitivePropertyCloneNoPolicy() throws Exception {
-        DoubleProperty.Factory factory = new DoubleProperty.Factory(
-                createAttributes("doublePropertyNoPolicy"));
-
-        DoubleProperty p1 = factory.create();
-        DoubleProperty p2 = factory.create();
-
-        factory.setDefaultValue(p1, 0);
-        factory.setDefaultValue(p2, 0);
-
-        p1.set(0, 4.0);
-
-        Assert.assertEquals(4.0, p1.get(0), 0.0001);
-        Assert.assertEquals(1.0, p2.get(0), 0.0001);
-
-        factory.clone(p1, 0, p2, 0);
-
-        Assert.assertEquals(4.0, p1.get(0), 0.0001);
-        Assert.assertEquals(4.0, p2.get(0), 0.0001);
-    }
-
-    @Test
-    public void testPrimitivePropertyCloneDisabled() throws Exception {
-        DoubleProperty.Factory factory = new DoubleProperty.Factory(
-                createAttributes("doublePropertyDisabled"));
-
-        DoubleProperty p1 = factory.create();
-        DoubleProperty p2 = factory.create();
-
-        factory.setDefaultValue(p1, 0);
-        factory.setDefaultValue(p2, 0);
-
-        p1.set(0, 4.0);
-
-        Assert.assertEquals(4.0, p1.get(0), 0.0001);
-        Assert.assertEquals(2.0, p2.get(0), 0.0001);
-
-        factory.clone(p1, 0, p2, 0);
-
-        Assert.assertEquals(4.0, p1.get(0), 0.0001);
-        Assert.assertEquals(2.0, p2.get(0), 0.0001);
-    }
-
-    @Test
-    public void testPrimitivePropertyCloneJavaDefault() throws Exception {
-        DoubleProperty.Factory factory = new DoubleProperty.Factory(
-                createAttributes("doublePropertyDefault"));
-
-        DoubleProperty p1 = factory.create();
-        DoubleProperty p2 = factory.create();
-
-        factory.setDefaultValue(p1, 0);
-        factory.setDefaultValue(p2, 0);
-
-        p1.set(0, 4.0);
-
-        Assert.assertEquals(4.0, p1.get(0), 0.0001);
-        Assert.assertEquals(0.0, p2.get(0), 0.0001);
-
-        factory.clone(p1, 0, p2, 0);
-
-        Assert.assertEquals(4.0, p1.get(0), 0.0001);
-        Assert.assertEquals(4.0, p2.get(0), 0.0001);
-    }
-
-    @Test
-    public void testPrimitivePropertyCloneInvoke() throws Exception {
-        DoubleProperty.Factory factory = new DoubleProperty.Factory(
-                createAttributes("doublePropertyInvoke"));
-
-        DoubleProperty p1 = factory.create();
-        DoubleProperty p2 = factory.create();
-
-        factory.setDefaultValue(p1, 0);
-        factory.setDefaultValue(p2, 0);
-
-        p1.set(0, 4.0);
-
-        Assert.assertEquals(4.0, p1.get(0), 0.0001);
-        Assert.assertEquals(0.0, p2.get(0), 0.0001);
-
-        factory.clone(p1, 0, p2, 0);
-
-        Assert.assertEquals(4.0, p1.get(0), 0.0001);
-        Assert.assertEquals(4.0, p2.get(0), 0.0001);
-    }
-
-    public static class CloneObject implements Cloneable {
-        public final int foo;
-
-        public CloneObject(int foo) {
-            this.foo = foo;
-        }
-
-        @Override
-        public CloneObject clone() {
-            return new CloneObject(foo);
-        }
-    }
-}

entreri-apt-tests/src/test/java/com/lhkbob/entreri/SchedulerTest.java

-/*
- * Entreri, an entity-component framework in Java
- *
- * Copyright (c) 2012, Michael Ludwig
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- *     Redistributions of source code must retain the above copyright notice,
- *         this list of conditions and the following disclaimer.
- *     Redistributions in binary form must reproduce the above copyright notice,
- *         this list of conditions and the following disclaimer in the
- *         documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.lhkbob.entreri;
-
-import com.lhkbob.entreri.task.Job;
-import com.lhkbob.entreri.task.Result;
-import com.lhkbob.entreri.task.Task;
-import org.junit.Assert;
-import org.junit.Test;
-
-// NOTE: this does not test the thread-safety aspects of a job, because
-// it's a little too difficult to right a unit test for that
-public class SchedulerTest {
-    @Test
-    public void testResultsReportedToFutureTasksOnly() {
-        EntitySystem system = EntitySystem.create();
-
-        ResultReportingTask t1 = new ResultReportingTask(null, new ResultA(),
-                                                         new ResultA(), new ResultB());
-        ResultAListeningTask t2 = new ResultAListeningTask(null);
-        ResultBListeningTask t3 = new ResultBListeningTask(null);
-        AllResultListeningTask t4 = new AllResultListeningTask(null);
-
-        // note the reordering of the tasks, t1 doesn't report results until after
-        // t2 and t3 are already run
-        Job j = system.getScheduler().createJob("test", t2, t3, t1, t4);
-
-        j.run();
-        Assert.assertEquals(0, t2.aReceiveCount);
-        Assert.assertEquals(0, t3.bReceiveCount);
-        Assert.assertEquals(2, t4.aReceiveCount);
-        Assert.assertEquals(1, t4.bReceiveCount);
-        Assert.assertEquals(3, t4.receiveCount);
-
-        // run the job a second time to make sure resubmitting results doesn't
-        // screw anything up after a reset
-        j.run();
-        Assert.assertEquals(0, t2.aReceiveCount);
-        Assert.assertEquals(0, t3.bReceiveCount);
-        Assert.assertEquals(2, t4.aReceiveCount);
-        Assert.assertEquals(1, t4.bReceiveCount);
-        Assert.assertEquals(3, t4.receiveCount);
-    }
-
-    @Test
-    public void testPostProcessTasksInvoked() {
-        EntitySystem system = EntitySystem.create();
-
-        BasicTask t1 = new BasicTask(null);
-        BasicTask t2 = new BasicTask(t1);
-        BasicTask t3 = new BasicTask(t2);
-
-        Job j = system.getScheduler().createJob("test", t3);
-        j.run();
-
-        Assert.assertTrue(t1.invoked);
-        Assert.assertTrue(t2.invoked);
-        Assert.assertTrue(t3.invoked);
-    }
-
-    @Test(expected = IllegalStateException.class)
-    public void testMultipleSingletonResultsReported() {
-        EntitySystem system = EntitySystem.create();
-
-        ResultReportingTask t1 = new ResultReportingTask(null, new ResultA(),
-                                                         new ResultB(), new ResultB());
-
-        Job j = system.getScheduler().createJob("test", t1);
-        j.run();
-    }
-
-    @Test
-    public void testResultsReported() {
-        EntitySystem system = EntitySystem.create();
-
-        ResultReportingTask t1 = new ResultReportingTask(null, new ResultA(),
-                                                         new ResultA(), new ResultB());
-        ResultAListeningTask t2 = new ResultAListeningTask(null);
-        ResultBListeningTask t3 = new ResultBListeningTask(null);
-        AllResultListeningTask t4 = new AllResultListeningTask(null);
-
-        Job j = system.getScheduler().createJob("test", t1, t2, t3, t4);
-
-        j.run();
-        Assert.assertEquals(2, t2.aReceiveCount);
-        Assert.assertEquals(1, t3.bReceiveCount);
-        Assert.assertEquals(2, t4.aReceiveCount);
-        Assert.assertEquals(1, t4.bReceiveCount);
-        Assert.assertEquals(3, t4.receiveCount);
-
-        // run the job a second time to make sure resubmitting results doesn't
-        // screw anything up after a reset
-        j.run();
-        Assert.assertEquals(2, t2.aReceiveCount);
-        Assert.assertEquals(1, t3.bReceiveCount);
-        Assert.assertEquals(2, t4.aReceiveCount);
-        Assert.assertEquals(1, t4.bReceiveCount);
-        Assert.assertEquals(3, t4.receiveCount);
-    }
-
-    @Test
-    public void testResetInvoked() {
-        EntitySystem system = EntitySystem.create();
-
-        BasicTask t1 = new BasicTask(null);
-        Job j = system.getScheduler().createJob("test", t1);
-        j.run();
-
-        Assert.assertTrue(t1.invoked);
-        Assert.assertTrue(t1.reset);
-    }
-
-    private static class ResultA extends Result {
-        @Override
-        public boolean isSingleton() {
-            return false;
-        }
-    }
-
-    private static class ResultB extends Result {
-        @Override
-        public boolean isSingleton() {
-            return true;
-        }
-    }
-
-    private static class BasicTask implements Task {
-        boolean invoked;
-        boolean reset;
-
-        final BasicTask postProcess;
-
-        public BasicTask(BasicTask postProcess) {
-            this.postProcess = postProcess;
-        }
-
-        @Override
-        public Task process(EntitySystem system, Job job) {
-            invoked = true;
-            return postProcess;
-        }
-
-        @Override
-        public void reset(EntitySystem system) {
-            reset = true;
-        }
-    }
-
-    private static class ResultReportingTask extends BasicTask {
-        private final Result[] toReport;
-
-        public ResultReportingTask(BasicTask postProcess, Result... results) {
-            super(postProcess);
-            toReport = results;
-        }
-
-        @Override
-        public Task process(EntitySystem system, Job job) {
-            for (Result r : toReport) {
-                job.report(r);
-            }
-            return super.process(system, job);
-        }
-    }
-
-    private static class ResultAListeningTask extends BasicTask {
-        int aReceiveCount;
-
-        public ResultAListeningTask(BasicTask parent) {
-            super(parent);
-            aReceiveCount = 0;
-        }
-
-        @Override
-        public void reset(EntitySystem system) {
-            aReceiveCount = 0;
-            super.reset(system);
-        }
-
-        @SuppressWarnings("unused")
-        public void report(ResultA r) {
-            aReceiveCount++;
-        }
-    }
-
-    private static class ResultBListeningTask extends BasicTask {
-        int bReceiveCount;
-
-        public ResultBListeningTask(BasicTask parent) {
-            super(parent);
-            bReceiveCount = 0;
-        }
-
-        @Override
-        public void reset(EntitySystem system) {
-            bReceiveCount = 0;
-            super.reset(system);
-        }
-
-        @SuppressWarnings("unused")
-        public void report(ResultB r) {
-            bReceiveCount++;
-        }
-    }
-
-    private static class AllResultListeningTask extends BasicTask {
-        int aReceiveCount;
-        int bReceiveCount;
-        int receiveCount;
-
-        public AllResultListeningTask(BasicTask parent) {
-            super(parent);
-        }
-
-        @Override
-        public void reset(EntitySystem system) {
-            aReceiveCount = 0;
-            bReceiveCount = 0;
-            receiveCount = 0;
-            super.reset(system);
-        }
-
-        @SuppressWarnings("unused")
-        public void report(Result r) {
-            receiveCount++;
-        }
-
-        @SuppressWarnings("unused")
-        public void report(ResultA r) {
-            aReceiveCount++;
-        }
-
-        @SuppressWarnings("unused")
-        public void report(ResultB r) {
-            bReceiveCount++;
-        }
-    }
-}

entreri-apt-tests/src/test/java/com/lhkbob/entreri/impl/ComponentRepositoryTest.java

-/*
- * Entreri, an entity-component framework in Java
- *
- * Copyright (c) 2012, Michael Ludwig
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- *     Redistributions of source code must retain the above copyright notice,
- *         this list of conditions and the following disclaimer.
- *     Redistributions in binary form must reproduce the above copyright notice,
- *         this list of conditions and the following disclaimer in the
- *         documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.lhkbob.entreri.impl;
-
-import com.lhkbob.entreri.Entity;
-import com.lhkbob.entreri.EntitySystem;
-import com.lhkbob.entreri.components.ComplexComponent;
-import com.lhkbob.entreri.components.FloatPropertyFactory;
-import com.lhkbob.entreri.components.IntComponent;
-import com.lhkbob.entreri.property.FloatProperty;
-import com.lhkbob.entreri.property.Property;
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.lang.ref.WeakReference;
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.List;
-
-public class ComponentRepositoryTest {
-    @Test
-    public void testFactorySetValue() {
-        EntitySystem system = EntitySystem.create();
-        ComplexComponent c = system.addEntity().add(ComplexComponent.class);
-        Assert.assertEquals(FloatPropertyFactory.DEFAULT, c.getFactoryFloat(), .0001f);
-    }
-
-    @Test
-    public void testDecorateProperty() {
-        EntitySystem system = EntitySystem.create();
-        Entity e = system.addEntity();
-        IntComponent c = e.add(IntComponent.class);
-
-        FloatProperty decorated = system
-                .decorate(IntComponent.class, new FloatPropertyFactory());
-        decorated.getIndexedData()[c.getIndex()] = 1f;
-
-        int count = 0;
-        for (Entity entity : system) {
-            Assert.assertNotNull(entity.get(IntComponent.class));
-            count++;
-
-            Assert.assertEquals(1f, decorated.getIndexedData()[c.getIndex()], .0001f);
-        }
-        Assert.assertEquals(1, count);
-    }
-
-    @Test
-    public void testDecoratePropertyAddComponent() {
-        EntitySystem system = EntitySystem.create();
-        Entity e = system.addEntity();
-        IntComponent c = e.add(IntComponent.class);
-
-        FloatProperty decorated = system
-                .decorate(IntComponent.class, new FloatPropertyFactory());
-        decorated.getIndexedData()[c.getIndex()] = 1f;
-
-        Entity e2 = system.addEntity();
-        IntComponent c2 = e2.add(IntComponent.class);
-        decorated.getIndexedData()[c2.getIndex()] = 2f;
-
-        int count = 0;
-        for (Entity entity : system) {
-            IntComponent c3 = entity.get(IntComponent.class);
-            count++;
-
-            if (c3.getIndex() == c.getIndex()) {
-                Assert.assertEquals(1f, decorated.getIndexedData()[c3.getIndex()],
-                                    .0001f);
-            } else {
-                Assert.assertEquals(2f, decorated.getIndexedData()[c3.getIndex()],
-                                    .0001f);
-            }
-        }
-        Assert.assertEquals(2, count);
-    }
-
-    @Test
-    @SuppressWarnings("unused")
-    public void testUndecorateValidProperty() throws Exception {
-        // This is an ugly ugly test case since it has to verify that the
-        // property gets garbage collected. The only way it can get at that
-        // is to use reflection to inspect the component repository
-        EntitySystemImpl system = (EntitySystemImpl) EntitySystem.create();
-        ComponentRepository<IntComponent> cr = system.getRepository(IntComponent.class);
-        int count = getDecoratedProperties(cr).size();
-
-        FloatProperty decorated = system
-                .decorate(IntComponent.class, new FloatPropertyFactory());
-
-        Assert.assertEquals(count + 1, getDecoratedProperties(cr).size());
-
-        decorated = null;
-        System.gc();
-        Thread.sleep(100);
-
-        system.compact();
-
-        Assert.assertEquals(count, getDecoratedProperties(cr).size());
-    }
-
-    @SuppressWarnings("unchecked")
-    private static List<Property> getDecoratedProperties(ComponentRepository<?> cr)
-            throws Exception {
-        Field decorated = ComponentRepository.class
-                .getDeclaredField("decoratedProperties");
-        decorated.setAccessible(true);
-        List<?> value = (List<?>) decorated.get(cr);
-
-        List<Property> converted = new ArrayList<Property>();
-        for (Object o : value) {
-            Field ref = o.getClass().getDeclaredField("property");
-            ref.setAccessible(true);
-            converted.add(((WeakReference<? extends Property>) ref.get(o)).get());
-        }
-        return converted;
-    }
-}