@Test public void testMarshall_Embedded() { RootEntity rootObject = new RootEntity(); // one entity ChildEntity childObject = new ChildEntity("Test City"); ChildEntity embeddedObject = new ChildEntity("Old Test City"); embeddedObject.setId(1L); embeddedObject.setType("EmbeddedType"); rootObject.setId("TestUser"); rootObject.setCount(25); childObject.setParent(rootObject); rootObject.setNewChildEntity(childObject); // one entity rootObject.setEmbeddedEntity(embeddedObject); // not included, @Embedded IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, rootObject); Entity rootObjectEntity = stack.get(rootObject); Entity childObjectEntity = stack.get(childObject); EmbeddedEntity ee = (EmbeddedEntity) rootObjectEntity.getProperty("embeddedEntity"); Key childKey = (Key) rootObjectEntity.getProperty("newChildEntity"); Key parentKey = childKey.getParent(); assertEquals(2, stack.size()); assertNotNull(parentKey); assertEquals(EmbeddedEntity.class, ee.getClass()); assertTrue(childKey instanceof Key); assertEquals(rootObjectEntity.getKey().getId(), parentKey.getId()); assertEquals(rootObjectEntity.getKey().getName(), parentKey.getName()); assertEquals(1L, ee.getProperties().get("id")); assertEquals("EmbeddedType", ee.getProperties().get("type")); }
@Test public void recycleEntity() { PooledEngine engine = new PooledEngine(); int numEntities = 200; Array<Entity> entities = new Array<Entity>(); for (int i = 0; i < numEntities; ++i) { Entity entity = engine.createEntity(); assertEquals(0, entity.flags); engine.addEntity(entity); entities.add(entity); entity.flags = 1; } for (Entity entity : entities) { engine.removeEntity(entity); assertEquals(0, entity.flags); } for (int i = 0; i < numEntities; ++i) { Entity entity = engine.createEntity(); assertEquals(0, entity.flags); engine.addEntity(entity); entities.add(entity); } }
@Test public void recycleComponent() { int maxEntities = 10; int maxComponents = 10; PooledEngine engine = new PooledEngine(maxEntities, maxEntities, maxComponents, maxComponents); for (int i = 0; i < maxComponents; ++i) { Entity e = engine.createEntity(); PooledComponentSpy c = engine.createComponent(PooledComponentSpy.class); assertEquals(false, c.recycled); e.add(c); engine.addEntity(e); } engine.removeAllEntities(); for (int i = 0; i < maxComponents; ++i) { Entity e = engine.createEntity(); PooledComponentSpy c = engine.createComponent(PooledComponentSpy.class); assertEquals(true, c.recycled); e.add(c); } engine.removeAllEntities(); }
@Test public void componentListener() { EntityListenerMock addedListener = new EntityListenerMock(); EntityListenerMock removedListener = new EntityListenerMock(); Entity entity = new Entity(); entity.componentAdded.add(addedListener); entity.componentRemoved.add(removedListener); assertEquals(0, addedListener.counter); assertEquals(0, removedListener.counter); entity.add(new ComponentA()); assertEquals(1, addedListener.counter); assertEquals(0, removedListener.counter); entity.remove(ComponentA.class); assertEquals(1, addedListener.counter); assertEquals(1, removedListener.counter); entity.add(new ComponentB()); assertEquals(2, addedListener.counter); entity.remove(ComponentB.class); assertEquals(2, removedListener.counter); }
@Test public void addAndReturnComponent() { Entity entity = new Entity(); ComponentA componentA = new ComponentA(); ComponentB componentB = new ComponentB(); assertEquals(componentA, entity.addAndReturn(componentA)); assertEquals(componentB, entity.addAndReturn(componentB)); assertEquals(2, entity.getComponents().size()); }
@Test public void noComponents() { Entity entity = new Entity(); assertEquals(0, entity.getComponents().size()); assertTrue(entity.getComponentBits().isEmpty()); assertNull(am.get(entity)); assertNull(bm.get(entity)); assertFalse(am.has(entity)); assertFalse(bm.has(entity)); }
@Test public void testMarshall_Child_first() { ChildEntity child = new ChildEntity(); RootEntity parent = new RootEntity(); parent.setId("ParentKey"); child.setParent(parent); IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, child); Entity childEntity = stack.get(child); assertNotNull(stack); assertEquals(2, stack.size()); assertEquals("ParentKey", childEntity.getParent().getName()); }
@Test public void createTransactionReturnsOk() { // create "cars" transaction Response response = client .path("/transactionservice/transaction/10") .request() .put(Entity.json(new Transaction("cars", new BigDecimal(5000), 0l))); assertEquals(200, response.getStatus()); assertEquals( TransactionService.OperationResult.OK, response.readEntity(TransactionService.OperationResult.class)); // create "shopping" transaction response = client .path("/transactionservice/transaction/11") .request() .put(Entity.json(new Transaction("shopping", new BigDecimal(10000), 10l))); assertEquals(200, response.getStatus()); assertEquals( TransactionService.OperationResult.OK, response.readEntity(TransactionService.OperationResult.class)); // get "cars" transactions response = client.path("/transactionservice/type/cars").request().get(); assertEquals(200, response.getStatus()); @SuppressWarnings("unchecked") List<Integer> ids = response.readEntity(List.class); assertEquals(1, ids.size()); assertEquals(10, ids.get(0).intValue()); // get "sum" for transaction 10 response = client.path("/transactionservice/sum/10").request().get(); assertEquals(200, response.getStatus()); AggregatorService.Sum sum = response.readEntity(AggregatorService.Sum.class); assertEquals(15000, sum.getSum().intValue()); // get "sum" for transaction 11 response = client.path("/transactionservice/sum/11").request().get(); assertEquals(200, response.getStatus()); sum = response.readEntity(AggregatorService.Sum.class); assertEquals(10000, sum.getSum().intValue()); }
/** @see DATAMONGO-468 */ @Test public void rendersUpdateOfDbRefPropertyWithDomainObjectCorrectly() { Entity entity = new Entity(); entity.id = "5"; Update update = new Update().set("dbRefProperty", entity); DBObject mappedObject = mapper.getMappedObject( update.getUpdateObject(), context.getPersistentEntity(DocumentWithDBRefCollection.class)); DBObject setClause = getAsDBObject(mappedObject, "$set"); assertThat(setClause.get("dbRefProperty"), is((Object) new DBRef(null, "entity", entity.id))); }
@Test public void test_Marshall_List() { Post post = new Post(); post.setTags(list("tag1", "tag2", "tag3")); IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, post); Entity e = stack.get(post); Object tags = e.getProperty("tags"); assertNotNull(e); assertTrue(tags instanceof List); assertEquals("tag1", ((List) tags).get(0)); assertEquals("tag2", ((List) tags).get(1)); assertEquals("tag3", ((List) tags).get(2)); }
@Test public void addSameComponent() { Entity entity = new Entity(); ComponentA a1 = new ComponentA(); ComponentA a2 = new ComponentA(); entity.add(a1); entity.add(a2); assertEquals(1, entity.getComponents().size()); assertTrue(am.has(entity)); assertNotEquals(a1, am.get(entity)); assertEquals(a2, am.get(entity)); }
/** @see DATAMONGO-404 */ @Test public void createsDbRefForEntityOnPulls() { Entity entity = new Entity(); entity.id = "5"; Update update = new Update().pull("dbRefAnnotatedList", entity); DBObject mappedObject = mapper.getMappedObject( update.getUpdateObject(), context.getPersistentEntity(DocumentWithDBRefCollection.class)); DBObject pullClause = getAsDBObject(mappedObject, "$pull"); assertThat( pullClause.get("dbRefAnnotatedList"), is((Object) new DBRef(null, "entity", entity.id))); }
@Override public void update(float deltaTime) { Entity entity; PooledEngine engine = (PooledEngine) getEngine(); for (int i = 0; i < 10; i++) { entity = engine.createEntity(); assertEquals(0, entity.flags); entity.flags = 1; entity.add(engine.createComponent(PositionComponent.class)); engine.addEntity(entity); } for (int i = 0; i < entities.size(); ++i) { entity = entities.get(i); engine.removeEntity(entity); engine.removeEntity(entity); } }
@Test public void testMarshall_JSONEntity() { JSONEntity json = new JSONEntity(); json.setKind("MyDocument"); json.setId("abcdef12345"); json.getFields().put("age", 28); json.getFields().put("name", "Name"); IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, json); Entity e = stack.get(json); assertNotNull(stack); assertTrue(!stack.isEmpty()); assertEquals("MyDocument", e.getKind()); }
@Test public void getComponentByClass() { ComponentA compA = new ComponentA(); ComponentB compB = new ComponentB(); Entity entity = new Entity(); entity.add(compA).add(compB); ComponentA retA = entity.getComponent(ComponentA.class); ComponentB retB = entity.getComponent(ComponentB.class); assertNotNull(retA); assertNotNull(retB); assertTrue(retA == compA); assertTrue(retB == compB); }
@Test public void addAndRemoveComponent() { Entity entity = new Entity(); entity.add(new ComponentA()); assertEquals(1, entity.getComponents().size()); Bits componentBits = entity.getComponentBits(); int componentAIndex = ComponentType.getIndexFor(ComponentA.class); for (int i = 0; i < componentBits.length(); ++i) { assertEquals(i == componentAIndex, componentBits.get(i)); } assertNotNull(am.get(entity)); assertNull(bm.get(entity)); assertTrue(am.has(entity)); assertFalse(bm.has(entity)); entity.remove(ComponentA.class); assertEquals(0, entity.getComponents().size()); for (int i = 0; i < componentBits.length(); ++i) { assertFalse(componentBits.get(i)); } assertNull(am.get(entity)); assertNull(bm.get(entity)); assertFalse(am.has(entity)); assertFalse(bm.has(entity)); }
@Test public void entityRemovalListenerOrder() { PooledEngine engine = new PooledEngine(); CombinedSystem combinedSystem = new CombinedSystem(); engine.addSystem(combinedSystem); engine.addEntityListener(Family.all(PositionComponent.class).get(), new MyPositionListener()); for (int i = 0; i < 10; i++) { Entity entity = engine.createEntity(); entity.add(engine.createComponent(PositionComponent.class)); engine.addEntity(entity); } assertEquals(10, combinedSystem.allEntities.size()); for (int i = 0; i < 10; i++) { engine.update(deltaTime); } engine.removeAllEntities(); }
@Test public void testMarshall_Child() { RootEntity rootObject = new RootEntity(); // one entity ChildEntity childObject = new ChildEntity("Test City"); rootObject.setId("TestUser"); rootObject.setCount(25); childObject.setParent(rootObject); rootObject.setNewChildEntity(childObject); // one entity IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, rootObject); Entity rootObjectEntity = stack.get(rootObject); Entity childObjectEntity = stack.get(childObject); Key childKey = (Key) rootObjectEntity.getProperty("newChildEntity"); Key parentKey = childKey.getParent(); assertEquals(2, stack.size()); assertNotNull(parentKey); assertTrue(childKey instanceof Key); assertEquals(rootObjectEntity.getKey().getId(), parentKey.getId()); assertEquals(rootObjectEntity.getKey().getName(), parentKey.getName()); }
@Test public void test() { Entity parent = new Entity("Parent"); // set parent properties... List<EmbeddedEntity> children = new ArrayList<EmbeddedEntity>(); for (int i = 0; i < 5; i++) { EmbeddedEntity child = new EmbeddedEntity(); // set child properties... children.add(child); } parent.setUnindexedProperty("children", children); parent.setUnindexedProperty("children2", list("tag1", "tag2", "tag3")); DatastoreServiceFactory.getDatastoreService().put(parent); try { Entity e = DatastoreServiceFactory.getDatastoreService().get(parent.getKey()); e.getKey(); Object children2 = e.getProperty("children2"); assertTrue(children2 instanceof List); } catch (EntityNotFoundException e1) { e1.printStackTrace(); } }
@Test public void testGetter2FieldName() { assertEquals("THIS_IS_A_FILED", Entity.getter2FieldName("getThisIsAFiled")); }