Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import org.terasology.gestalt.entitysystem.component.Component;

public class DummyComponent implements Component<DummyComponent> {
public boolean dummy = false;
public boolean eventReceived = false;
public String name;

@Override
public void copyFrom(DummyComponent other) {
this.dummy = other.dummy;
eventReceived = other.eventReceived;
name = other.name;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
/*
* 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.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.moduletestingenvironment.fixtures;

import org.terasology.engine.entitySystem.event.Event;

public class DummyEvent implements Event {
public boolean dummy = false;
}
public class DummyEvent implements Event { }
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* 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.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.moduletestingenvironment.fixtures;

import org.terasology.engine.entitySystem.entity.EntityRef;
Expand All @@ -27,7 +14,7 @@
public class DummySystem extends BaseComponentSystem {
@ReceiveEvent
public void onDummyEvent(DummyEvent event, EntityRef entity, DummyComponent component) {
component.dummy = true;
component.eventReceived = true;
entity.saveComponent(component);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.terasology.engine.entitySystem.entity.EntityManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.registry.In;
import org.terasology.moduletestingenvironment.extension.Dependencies;
import org.terasology.moduletestingenvironment.fixtures.DummyComponent;
import org.terasology.moduletestingenvironment.fixtures.DummyEvent;
import org.terasology.engine.registry.In;

@Tag("MteTest")
@ExtendWith(MTEExtension.class)
Expand All @@ -24,6 +24,6 @@ public class ComponentSystemTest {
public void simpleEventTest() {
EntityRef entity = entityManager.create(new DummyComponent());
entity.send(new DummyEvent());
Assertions.assertTrue(entity.getComponent(DummyComponent.class).dummy);
Assertions.assertTrue(entity.getComponent(DummyComponent.class).eventReceived);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@

import com.google.common.collect.Sets;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.terasology.engine.entitySystem.entity.EntityManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.registry.In;
import org.terasology.moduletestingenvironment.extension.Dependencies;
import org.terasology.moduletestingenvironment.fixtures.DummyComponent;
import org.terasology.moduletestingenvironment.fixtures.DummyEvent;
import org.terasology.engine.registry.In;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Tag("MteTest")
@ExtendWith(IsolatedMTEExtension.class)
@Dependencies({"engine", "ModuleTestingEnvironment"})
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class IsolatedEngineTest {
private final Set<EntityManager> entityManagerSet = Sets.newHashSet();
private EntityRef entity;
Expand All @@ -34,21 +40,23 @@ public void prepareEntityForTest() {
}

@Test
@Order(1)
public void someTest() {
// make sure we don't reuse the EntityManager
assertFalse(entityManagerSet.contains(entityManager));
entityManagerSet.add(entityManager);

entity.send(new DummyEvent());
assertTrue(entity.getComponent(DummyComponent.class).dummy);
assertTrue(entity.getComponent(DummyComponent.class).eventReceived);
}

@Test
@Order(2)
public void someOtherTest() {
// make sure we don't reuse the EntityManager
assertFalse(entityManagerSet.contains(entityManager));
entityManagerSet.add(entityManager);

assertFalse(entity.getComponent(DummyComponent.class).dummy);
assertFalse(entity.getComponent(DummyComponent.class).eventReceived);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.moduletestingenvironment;

import com.google.common.collect.Lists;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.terasology.engine.entitySystem.entity.EntityManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.registry.In;
import org.terasology.moduletestingenvironment.fixtures.DummyComponent;
import org.terasology.moduletestingenvironment.fixtures.DummyEvent;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Ensure a test class with a per-method Jupiter lifecycle can share an engine between tests.
*/
@Tag("MteTest")
@ExtendWith(MTEExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_METHOD) // The default, but here for explicitness.
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MTEExtensionTestWithPerMethodLifecycle {
// java 8 doesn't have ConcurrentSet
@SuppressWarnings("checkstyle:constantname")
private static final ConcurrentMap<String, Integer> seenNames = new ConcurrentHashMap<>();

@In
public EntityManager entityManager;

@BeforeAll
public static void createEntity(EntityManager entityManager, TestInfo testInfo) {
// Create some entity to be shared by all the tests.
EntityRef entity = entityManager.create(new DummyComponent());

// Do some stuff to configure it.
entity.send(new DummyEvent());

entity.updateComponent(DummyComponent.class, component -> {
// Mark with something unique (and not reliant on the entity id system)
component.name = testInfo.getDisplayName() + "#" + UUID.randomUUID();
return component;
});
}

@Test
@Order(1)
public void firstTestFindsThings() {
List<EntityRef> entities = Lists.newArrayList(entityManager.getEntitiesWith(DummyComponent.class));
// There should be one entity, created by the @BeforeAll method
assertEquals(1, entities.size());

DummyComponent component = entities.get(0).getComponent(DummyComponent.class);
assertTrue(component.eventReceived);

// Remember that a test has seen this one.
assertNotNull(component.name);
assertFalse(seenNames.containsKey(component.name));
seenNames.put(component.name, 1);
}

@Test
@Order(2)
public void thingsStillExistForSecondTest() {
List<EntityRef> entities = Lists.newArrayList(entityManager.getEntitiesWith(DummyComponent.class));
// There should be one entity, created by the @BeforeAll method
assertEquals(1, entities.size());

// Make sure that this is the same one that the first test saw.
DummyComponent component = entities.get(0).getComponent(DummyComponent.class);
assertTrue(component.eventReceived);
assertNotNull(component.name);
assertTrue(seenNames.containsKey(component.name), () ->
String.format("This is not the same entity as seen in the first test!%n"
+ "Current entity: %s%n"
+ "Previously seen: %s",
component.name, seenNames.keySet()));
}
}

This file was deleted.