Content of file MemoryStateTest.java
package de.fhdw.gaming.ipspiel23.memory; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.Optional; import org.junit.jupiter.api.Test; import de.fhdw.gaming.ipspiel23.memory.dummy.DummyMemoryState; import de.fhdw.gaming.ipspiel23.memory.dummy.DummyPlayer; import de.fhdw.gaming.ipspiel23.memory.dummy.DummyRoundData; import de.fhdw.gaming.ipspiel23.memory.dummy.DummyStrategy; /** * tests the class {@link MemoryState}. * */ class MemoryStateTest { /** * the Memory Provider should be the same for all MemoryStates. */ @Test void testProviderStaysTheSame() { final DummyMemoryState firstState = new DummyMemoryState(); final DummyMemoryState secondState = new DummyMemoryState(); assertEquals(firstState.getMemoryProvider(), secondState.getMemoryProvider()); } /** * MemoryProvider returns an empty optional when no memory can be found using tryRequestMemoryForStrategy(). */ @Test void testTryRequestMemory() { final DummyMemoryState state = new DummyMemoryState(); final IGameMemoryIdentifier identifier = GameMemoryIdentifier.of( new DummyPlayer("Player"), new DummyStrategy(), new DummyStrategy()); final Optional<IGameMemory<Object>> retVal = state.getMemoryProvider().tryRequestMemoryForStrategy(identifier); assertEquals(Optional.empty(), retVal); } /** * Test {@link GameMemoryProvider#requestMemoryForStrategy(IGameMemoryIdentifier, IGameMemoryCapacity)}. */ @Test void testRequestMemory() { final DummyMemoryState state = new DummyMemoryState(); final IGameMemoryIdentifier identifier = GameMemoryIdentifier.of( new DummyPlayer("test"), new DummyStrategy(), new DummyStrategy());
final IGameMemory<Object> retVal = state.getMemoryProvider().requestMemoryForStrategy(identifier, GameMemoryCapacity.of(2));
Since Checkstyle 3.0
Checks for long lines.
Rationale: Long lines are hard to read in printouts or if developers have limited screen space for the source code, e.g. if the IDE displays additional information like project tree, class hierarchy, etc.
assertNotNull(retVal); assertEquals(0, retVal.size()); assertEquals(2, retVal.capacity()); // test that the memory is stored in the provider. final Optional<IGameMemory<Object>> retVal2 = state.getMemoryProvider().tryRequestMemoryForStrategy(identifier); assertNotNull(retVal2.get()); assertEquals(0, retVal2.get().size()); assertEquals(2, retVal2.get().capacity()); assertEquals(retVal, retVal2.get()); // test that the memory is not replaced when requested again. final IGameMemory<Object> retVal3 = state.getMemoryProvider().requestMemoryForStrategy(identifier, GameMemoryCapacity.of(3)); assertNotNull(retVal3); assertEquals(0, retVal3.size()); assertEquals(2, retVal3.capacity()); assertEquals(retVal, retVal3); } /** * Test for storing a round in a memory. */ @Test void testStoreRound() { final DummyMemoryState state = new DummyMemoryState(); final IGameMemoryIdentifier identifier = GameMemoryIdentifier.of( new DummyPlayer("test"), new DummyStrategy(), new DummyStrategy()); state.getMemoryProvider().requestMemoryForStrategy(identifier, GameMemoryCapacity.of(2)); state.storeRoundData(identifier, new DummyRoundData()); final Optional<IGameMemory<Object>> retVal = state.getMemoryProvider().tryRequestMemoryForStrategy(identifier); assertNotNull(retVal.get()); assertEquals(1, retVal.get().size()); assertEquals(2, retVal.get().capacity()); } }