Skip to content

Content of file SspStrategyEqualsTest.java

package de.fhdw.gaming.ipspiel23.ssp.strategy;

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

import org.junit.jupiter.api.Test;

import de.fhdw.gaming.ipspiel23.ssp.domain.SspStrategy;
import de.fhdw.gaming.ipspiel23.ssp.domain.factory.SspStrategyFactory;
import de.fhdw.gaming.ipspiel23.ssp.moves.factory.SspMoveFactory;
import de.fhdw.gaming.ipspiel23.ssp.moves.impl.SspDefaultMoveFactory;

/**
 * Tests the equals operations of the different strategies.
 */
final class SspStrategyEqualsTest {
    
    /**
     * The factory for creating SSP moves.
     */
    private final SspMoveFactory moveFactory = new SspDefaultMoveFactory();

    /**
     * The factories for creating the SSP strategies to be tested.
     */
    private final SspStrategyFactory[] factories = new SspStrategyFactory[] {
        new SspSayPaperStrategyFactory(),
        new SspSayScissorsStrategyFactory(),
        new SspSayStoneStrategyFactory(),
        new SspMixedStrategyFactory()
    };
Array initialization can be written shorter.
When declaring and initializing array fields or variables, it is not necessary to explicitly create a new array using `new`. Instead one can simply define the initial content of the array as a expression in curly braces. E.g. `int[] x = new int[] { 1, 2, 3 };` can be written as `int[] x = { 1, 2, 3 };`.
    
        

Foo[] x = new Foo[] { ... }; // Overly verbose
Foo[] x = { ... }; //Equivalent to above line

        
    
See PMD documentation.
/** * Tests the equals operations of the different strategies. */ @Test void testEqualsSspSayPaperStrategy() { for (int i = 0; i < factories.length; i++) { final SspStrategyFactory factory = factories[i]; final SspStrategy strategy1 = factory.create(moveFactory); final SspStrategy strategy2 = factory.create(moveFactory); final SspStrategy strategy3 = factory.create(moveFactory); final SspStrategy otherStrategy1 = factories[(i + 1) % factories.length].create(moveFactory); final SspStrategy otherStrategy2 = factories[(i + 2) % factories.length].create(moveFactory); final SspStrategy nullStrategy = null; final Object otherObject = new Object(); // reflexive assertTrue(strategy1.equals(strategy1)); // symmetric assertTrue(strategy1.equals(strategy2)); assertTrue(strategy2.equals(strategy1)); // transitive assertTrue(strategy1.equals(strategy2)); assertTrue(strategy2.equals(strategy3)); assertTrue(strategy1.equals(strategy3)); // consistent assertTrue(strategy1.equals(strategy2)); assertTrue(strategy1.equals(strategy2)); // not equal assertNotEquals(strategy1, otherStrategy1); assertNotEquals(strategy1, otherStrategy2); // test null assertNotEquals(strategy1, nullStrategy); // test other object assertNotEquals(strategy1, otherObject); assertNotEquals(strategy1, "not a strategy"); } } }