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
47 changes: 47 additions & 0 deletions src/main/java/dansapps/interakt/tests/EventFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dansapps.interakt.tests;

import dansapps.interakt.factories.EventFactory;
import dansapps.interakt.objects.Event;
import org.junit.Assert;
import org.junit.Test;

import java.time.LocalDateTime;

/**
* Characterization tests for EventFactory and Event: an event keeps its message unchanged and is
* stamped with the time it was created, and every call produces a new event.
*/
public class EventFactoryTest {
private final EventFactory eventFactory = new EventFactory();

@Test
public void testMessageIsKeptUnchanged() {
Event event = eventFactory.createEvent(" Gerald has appeared at 2, 3. ");

Assert.assertEquals(" Gerald has appeared at 2, 3. ", event.getMessage());
}

@Test
public void testTimestampIsTheCreationTime() {
LocalDateTime before = LocalDateTime.now();
Event event = eventFactory.createEvent("sentinel");
LocalDateTime after = LocalDateTime.now();

Assert.assertNotNull(event.getTimestamp());
Assert.assertFalse(event.getTimestamp().isBefore(before));
Assert.assertFalse(event.getTimestamp().isAfter(after));
}

@Test
public void testEachCallCreatesANewEvent() {
Event first = eventFactory.createEvent("sentinel");
Event second = eventFactory.createEvent("sentinel");

Assert.assertNotSame(first, second);
}

@Test
public void testNullMessageIsKept() {
Assert.assertNull(eventFactory.createEvent(null).getMessage());
}
}
116 changes: 116 additions & 0 deletions src/main/java/dansapps/interakt/tests/FoodItemFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package dansapps.interakt.tests;

import dansapps.interakt.Interakt;
import dansapps.interakt.data.PersistentData;
import dansapps.interakt.exceptions.ActorNotFoundException;
import dansapps.interakt.factories.FoodItemFactory;
import dansapps.interakt.objects.Square;
import dansapps.interakt.services.LocalStorageService;
import dansapps.interakt.utils.Logger;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.UUID;

/**
* Characterization tests for FoodItemFactory: a food item is added to the square's entities and its
* appearance is written to the event log. The food item is not an actor, yet the square counts and
* picks it as one; that is the current behavior described in issue #160 and is pinned here as-is.
* Every test points the data directory at a temporary directory of its own so the log can be read.
*/
public class FoodItemFactoryTest {
private final PersistentData persistentData = new PersistentData();
private File temporaryDirectory;
private String previousDataDirectory;
private Logger logger;
private FoodItemFactory foodItemFactory;
private Square square;

@Before
public void setUp() throws IOException {
temporaryDirectory = Files.createTempDirectory("interakt-food").toFile();
previousDataDirectory = System.getProperty(LocalStorageService.DATA_DIRECTORY_PROPERTY);
System.setProperty(LocalStorageService.DATA_DIRECTORY_PROPERTY, temporaryDirectory.getPath());
logger = new Logger(new Interakt());
foodItemFactory = new FoodItemFactory(logger);
square = new Square(2, 3, UUID.randomUUID(), logger, persistentData);
}

@After
public void tearDown() {
LocalStorageServiceTest.restoreDataDirectoryProperty(previousDataDirectory);
delete(temporaryDirectory);
}

@Test
public void testFoodItemIsAddedToTheSquare() {
foodItemFactory.createFoodItemAt("Gerald", square);

Assert.assertEquals(1, square.getEntityUUIDs().size());
}

@Test
public void testEachFoodItemIsASeparateEntity() {
foodItemFactory.createFoodItemAt("Gerald", square);
foodItemFactory.createFoodItemAt("Gerald", square);

Assert.assertEquals(2, square.getEntityUUIDs().size());
}

@Test
public void testAppearanceIsWrittenToTheEventLog() throws IOException {
foodItemFactory.createFoodItemAt("Gerald", square);

List<String> lines = Files.readAllLines(logger.getFile().toPath(), StandardCharsets.UTF_8);
Assert.assertEquals(lines.toString(), 1, lines.size());
Assert.assertTrue(lines.get(0),
lines.get(0).endsWith("] A new food item called Gerald has appeared at 2, 3."));
}

@Test
public void testFoodItemIsNotRegisteredAsAnActor() {
foodItemFactory.createFoodItemAt("Gerald", square);

UUID foodItemUUID = square.getEntityUUIDs().iterator().next();
Assert.assertTrue(persistentData.getActors().isEmpty());
try {
persistentData.getActor(foodItemUUID);
Assert.fail("A food item was found as an actor.");
} catch (ActorNotFoundException expected) {
// The food item lives only on the square.
}
}

// Current behavior, see issue #160: the food item is counted as an actor on its square.
@Test
public void testSquareCountsTheFoodItemAsAnActor() {
foodItemFactory.createFoodItemAt("Gerald", square);

Assert.assertEquals(1, square.getNumActors());
}

// Current behavior, see issue #160: picking a random actor can pick the food item and throw.
@Test(expected = ActorNotFoundException.class)
public void testRandomActorOnASquareWithOnlyFoodThrows() throws ActorNotFoundException {
foodItemFactory.createFoodItemAt("Gerald", square);

square.getRandomActor();
}

private static void delete(File file) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
delete(child);
}
}
file.delete();
}
}
45 changes: 45 additions & 0 deletions src/main/java/dansapps/interakt/tests/FoodItemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dansapps.interakt.tests;

import dansapps.interakt.misc.CONFIG;
import dansapps.interakt.objects.FoodItem;
import org.junit.Assert;
import org.junit.Test;

/**
* Characterization tests for FoodItem: it keeps the name it was given, starts with a random
* nutrition value in [0, CONFIG.MAX_NUTRITION), and that value can be overwritten.
*/
public class FoodItemTest {

@Test
public void testNameIsKept() {
Assert.assertEquals("Gerald", new FoodItem("Gerald").getName());
}

@Test
public void testFoodItemsWithTheSameNameHaveDifferentUUIDs() {
FoodItem first = new FoodItem("Gerald");
FoodItem second = new FoodItem("Gerald");

Assert.assertNotNull(first.getUUID());
Assert.assertFalse(first.getUUID().equals(second.getUUID()));
}

@Test
public void testStartingNutritionIsWithinTheConfiguredMaximum() {
for (int i = 0; i < 100; i++) {
double nutrition = new FoodItem("Gerald").getNutrition();
Assert.assertTrue(String.valueOf(nutrition), nutrition >= 0);
Assert.assertTrue(String.valueOf(nutrition), nutrition < CONFIG.MAX_NUTRITION);
}
}

@Test
public void testSetNutritionOverwritesTheValue() {
FoodItem foodItem = new FoodItem("Gerald");

foodItem.setNutrition(123.5);

Assert.assertEquals(123.5, foodItem.getNutrition(), 0.0);
}
}
Loading