Skip to content

Content of file SSPPlayerBuilderImplTest.java

package de.schereSteinPapier.domain.impl;

import static de.schereSteinPapier.SSPConstants.AuswahlConstants.PAPIER;
import static de.schereSteinPapier.SSPConstants.AuswahlConstants.SCHERE;
import static de.schereSteinPapier.SSPConstants.AuswahlConstants.STEIN;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import de.fhdw.gaming.core.domain.GameException;
import de.schereSteinPapier.domain.SSPPlayer;
import de.schereSteinPapier.domain.SSPPlayerBuilder;


/**
 * Tests {@link class SSPPlayerBuilderImpl}.
 */
class SSPPlayerBuilderImplTest {

    /**
     * Tries to create a SSP player.
     */
    @Test
    void testCreatePlayerWithDefaults() throws GameException {
        final SSPPlayerBuilder builder = new SSPPlayerBuilderImpl();
        final SSPPlayerBuilder builder2 = new SSPPlayerBuilderImpl();

        final Map<String, Map<String, Double>> possibleOutcomes = new LinkedHashMap<>();
        final Map<String, Double> possibleOutcomesPapier = new LinkedHashMap<>();
        final Map<String, Double> possibleOutcomesStein = new LinkedHashMap<>();
        final Map<String, Double> possibleOutcomesSchere = new LinkedHashMap<>();

        possibleOutcomesSchere.put(SCHERE, 0.0);
        possibleOutcomesSchere.put(STEIN, -1.0);
        possibleOutcomesSchere.put(PAPIER, 1.0);

        possibleOutcomesStein.put(SCHERE, 1.0);
        possibleOutcomesStein.put(STEIN, 0.0);
        possibleOutcomesStein.put(PAPIER, -1.0);

        possibleOutcomesPapier.put(SCHERE, -1.0);
        possibleOutcomesPapier.put(STEIN, 1.0);
        possibleOutcomesPapier.put(PAPIER, 0.0);

        possibleOutcomes.put(SCHERE, possibleOutcomesSchere);
        possibleOutcomes.put(STEIN, possibleOutcomesStein);
        possibleOutcomes.put(PAPIER, possibleOutcomesPapier);

        final Map<String, Map<String, Double>> possibleOutcomesSecond = new LinkedHashMap<>();
        final Map<String, Double> possibleOutcomesPapierSecond = new LinkedHashMap<>();
        final Map<String, Double> possibleOutcomesSteinSecond = new LinkedHashMap<>();
        final Map<String, Double> possibleOutcomesSchereSecond = new LinkedHashMap<>();

        possibleOutcomesSchereSecond.put(SCHERE, 0.0);
        possibleOutcomesSchereSecond.put(STEIN, 1.0);
        possibleOutcomesSchereSecond.put(PAPIER, -1.0);

        possibleOutcomesSteinSecond.put(SCHERE, -1.0);
        possibleOutcomesSteinSecond.put(STEIN, 0.0);
        possibleOutcomesSteinSecond.put(PAPIER, 1.0);

        possibleOutcomesPapierSecond.put(SCHERE, 1.0);
        possibleOutcomesPapierSecond.put(STEIN, -1.0);
        possibleOutcomesPapierSecond.put(PAPIER, 0.0);

        possibleOutcomesSecond.put(SCHERE, possibleOutcomesSchereSecond);
        possibleOutcomesSecond.put(STEIN, possibleOutcomesSteinSecond);
        possibleOutcomesSecond.put(PAPIER, possibleOutcomesPapierSecond);

        builder.changeName("A").changePossibleOutcomes(possibleOutcomes);
        builder2.changeName("B").changePossibleOutcomes(possibleOutcomesSecond);

        final SSPPlayer player = builder.build();
        final SSPPlayer player2 = builder2.build();

        assertThat(player.getName(), is(equalTo("A")));
        assertThat(player2.getName(), is(equalTo("B")));
    }

    SSPPlayerBuilder playerBuilder;
Instance variable definition in wrong order.

Since Checkstyle 3.2

According to Code Conventions for the Java Programming Language , the parts of a class or interface declaration should appear in the following order:

  1. Class (static) variables. First the public class variables, then protected, then package level (no access modifier), and then private.
  2. Instance variables. First the public class variables, then protected, then package level (no access modifier), and then private.
  3. Constructors
  4. Methods

Purpose of ignore* option is to ignore related violations, however it still impacts on other class members.

ATTENTION: the check skips class fields which have forward references from validation due to the fact that we have Checkstyle's limitations to clearly detect user intention of fields location and grouping. For example,


public class A {
  private double x = 1.0;
  private double y = 2.0;
  public double slope = x / y; // will be skipped from validation due to forward reference
}
          
/** * Setup */ @BeforeEach void setUp() { playerBuilder = new SSPPlayerBuilderImpl(); } /** * Test for method changeName. */ @Test void changeName() throws GameException { SSPPlayer sspPlayer = playerBuilder.changeName("A").changePossibleOutcomes(createPossibleOutcomes()).build(); assertEquals("A", sspPlayer.getName()); } /** * Test for method changePossibleOutcomes. */ @Test void changePossibleOutcomes() throws GameException { SSPPlayer player = playerBuilder.changeName("A").changePossibleOutcomes(createPossibleOutcomes()).build(); assertEquals(createPossibleOutcomes(), player.getPossibleOutcomes()); } /** * Test for method build. */ @Test void build() throws GameException { SSPPlayer player = playerBuilder.changeName("A").changePossibleOutcomes(createPossibleOutcomes()).build(); assertEquals(player, playerBuilder.changeName("A").changePossibleOutcomes(createPossibleOutcomes()).build()); } Map<String, Map<String, Double>> createPossibleOutcomes() { final Map<String, Map<String, Double>> possibleOutcomes = new LinkedHashMap<>(); Map<String, Double> possibleOutcomesSchere = new LinkedHashMap<>(); Map<String, Double> possibleOutcomesStein = new LinkedHashMap<>(); Map<String, Double> possibleOutcomesPapier = new LinkedHashMap<>(); possibleOutcomesSchere.put(SCHERE, 0.0); possibleOutcomesSchere.put(STEIN, 1.0); possibleOutcomesSchere.put(PAPIER, -1.0); possibleOutcomesStein.put(STEIN, 0.0); possibleOutcomesStein.put(SCHERE, -1.0); possibleOutcomesStein.put(PAPIER, 1.0); possibleOutcomesPapier.put(PAPIER, 0.0); possibleOutcomesPapier.put(SCHERE, 1.0); possibleOutcomesPapier.put(STEIN, -1.0); possibleOutcomes.put(SCHERE, possibleOutcomesSchere); possibleOutcomes.put(STEIN, possibleOutcomesStein); possibleOutcomes.put(PAPIER, possibleOutcomesPapier); return possibleOutcomes; } }