Skip to content

Commit

Permalink
Update EntityPool-related tests and make them pass
Browse files Browse the repository at this point in the history
Update tests for moveToPool() and setScope() to be more comprehensive,
and add some tests for PojoEntityPool.

Make fixes in PojoEntityManager and PojoEntityPool to esure all tests
pass, by properly assigning the entity to the new pool when
moveToPool() is called.
  • Loading branch information
Vizaxo committed Jul 16, 2017
1 parent 12c0f5e commit aa99eb0
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;

/**
Expand Down Expand Up @@ -76,16 +77,19 @@ public void setup() {
public void testSetScope() {
assertEquals(ref.getScope(), EntityData.Entity.Scope.GLOBAL);
assertTrue(entityManager.getGlobalPool().contains(ref.getId()));
assertFalse(entityManager.getSectorManager().contains(ref.getId()));

//Move into sector scope
ref.setScope(EntityData.Entity.Scope.SECTOR);
assertEquals(ref.getScope(), EntityData.Entity.Scope.SECTOR);
assertTrue(entityManager.getSectorManager().contains(ref.getId()));
assertFalse(entityManager.getGlobalPool().contains(ref.getId()));

//And move back to global scope
ref.setScope(EntityData.Entity.Scope.GLOBAL);
assertEquals(ref.getScope(), EntityData.Entity.Scope.GLOBAL);
assertTrue(entityManager.getGlobalPool().contains(ref.getId()));
assertFalse(entityManager.getSectorManager().contains(ref.getId()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,18 @@ public void testMoveToPool() {
EntityRef entity = entityManager.create();
long id = entity.getId();

PojoEntityPool pool = new PojoEntityPool(entityManager);
PojoEntityPool pool1 = new PojoEntityPool(entityManager);
PojoEntityPool pool2 = new PojoEntityPool(entityManager);

entityManager.moveToPool(id, pool);
assertFalse(pool1.contains(id));
assertFalse(pool2.contains(id));

assertTrue(pool.contains(id));
assertTrue(entityManager.moveToPool(id, pool1));
assertTrue(pool1.contains(id));
assertFalse(pool2.contains(id));

assertTrue(entityManager.moveToPool(id, pool2));
assertTrue(pool2.contains(id));
assertFalse(pool1.contains(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2017 MovingBlocks
*
* 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.terasology.entitySystem;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.terasology.assets.AssetFactory;
import org.terasology.assets.management.AssetManager;
import org.terasology.assets.module.ModuleAwareAssetTypeManager;
import org.terasology.context.Context;
import org.terasology.context.internal.ContextImpl;
import org.terasology.engine.bootstrap.EntitySystemSetupUtil;
import org.terasology.engine.module.ModuleManager;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.entity.internal.PojoEntityManager;
import org.terasology.entitySystem.entity.internal.PojoEntityPool;
import org.terasology.entitySystem.prefab.Prefab;
import org.terasology.entitySystem.prefab.PrefabData;
import org.terasology.entitySystem.prefab.internal.PojoPrefab;
import org.terasology.network.NetworkSystem;
import org.terasology.registry.CoreRegistry;
import org.terasology.testUtil.ModuleManagerFactory;

import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

/**
*/
public class PojoEntityPoolTest {

private PojoEntityPool pool;
private static Context context;
private PojoEntityManager entityManager;

@BeforeClass
public static void setupClass() throws Exception {
context = new ContextImpl();
ModuleManager moduleManager = ModuleManagerFactory.create();
context.put(ModuleManager.class, moduleManager);
ModuleAwareAssetTypeManager assetTypeManager = new ModuleAwareAssetTypeManager();
assetTypeManager.registerCoreAssetType(Prefab.class,
(AssetFactory<Prefab, PrefabData>) PojoPrefab::new, "prefabs");
assetTypeManager.switchEnvironment(moduleManager.getEnvironment());
context.put(AssetManager.class, assetTypeManager.getAssetManager());
CoreRegistry.setContext(context);
}

@Before
public void setup() {
context.put(NetworkSystem.class, mock(NetworkSystem.class));
EntitySystemSetupUtil.addReflectionBasedLibraries(context);
EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
entityManager = (PojoEntityManager) context.get(EntityManager.class);

pool = new PojoEntityPool(entityManager);
}


@Test
public void testContains() {
assertFalse(pool.contains(PojoEntityManager.NULL_ID));
assertFalse(pool.contains(1000000));
EntityRef ref = pool.create();
assertTrue(pool.contains(ref.getId()));
}

@Test
public void testRemove() {
EntityRef ref = pool.create();
assertTrue(pool.contains(ref.getId()));

pool.remove(ref.getId());
assertFalse(pool.contains(ref.getId()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public interface EntityPool {
/**
* Does this pool contain the given entity?
*
* @param id the id to search for
* @param id the id of the entity to search for
* @return true if this pool contains the entity; false otherwise
*/
boolean contains(long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
public abstract class BaseEntityRef extends EntityRef {

protected LowLevelEntityManager entityManager;
private Logger logger = LoggerFactory.getLogger(BaseEntityRef.class);
private static final Logger logger = LoggerFactory.getLogger(BaseEntityRef.class);

public BaseEntityRef(LowLevelEntityManager entityManager) {
this.entityManager = entityManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,20 @@ protected void assignToPool(long entityId, EngineEntityPool pool) {
}
}

/**
* Remove the assignment of an entity to a pool.
*
* This does not affect anything else related to the entity, but may lead to the entity or its components being
* unable to be found.
*
* When using this method, be sure to properly re-assign the entity to its correct pool afterwards.
*
* @param id the id of the entity to remove the assignment for
*/
protected void unassignPool(long id) {
poolMap.remove(id);
}

public boolean moveToPool(long id, EngineEntityPool pool) {

if (getPool(id).isPresent() && getPool(id).get().equals(pool)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,15 @@ public boolean hasComponent(long entityId, Class<? extends Component> componentC
@Override
public Optional<BaseEntityRef> remove(long id) {
componentStore.remove(id);
return Optional.of(entityStore.get(id));
entityManager.unassignPool(id);
return Optional.of(entityStore.remove(id));
}

@Override
public void insertRef(BaseEntityRef ref, Iterable<Component> components) {
entityStore.put(ref.getId(), ref);
components.forEach(comp -> componentStore.put(ref.getId(), comp));
entityManager.assignToPool(ref.getId(), this);
}

@Override
Expand Down

0 comments on commit aa99eb0

Please sign in to comment.