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;
Variable 'playerBuilder' must be private and have accessor methods.

Since Checkstyle 3.0

Checks visibility of class members. Only static final, immutable or annotated by specified annotation members may be public; other class members must be private unless the property protectedAllowed or packageAllowed is set.

Public members are not flagged if the name matches the public member regular expression (contains "^serialVersionUID$" by default).

Note that Checkstyle 2 used to include "^f[A-Z][a-zA-Z0-9]*$" in the default pattern to allow names used in container-managed persistence for Enterprise JavaBeans (EJB) 1.1 with the default settings. With EJB 2.0 it is no longer necessary to have public access for persistent fields, so the default has been changed.

Rationale: Enforce encapsulation.

Check also has options making it less strict:

ignoreAnnotationCanonicalNames - the list of annotations which ignore variables in consideration. If user will provide short annotation name that type will match to any named the same type without consideration of package

allowPublicFinalFields - which allows public final fields. Default value is false

allowPublicImmutableFields - which allows immutable fields to be declared as public if defined in final class. Default value is false

Field is known to be immutable if: - It's declared as final - Has either a primitive type or instance of class user defined to be immutable (such as String, ImmutableCollection from Guava and etc)

Classes known to be immutable are listed in immutableClassCanonicalNames by their canonical names.

Rationale: Forcing all fields of class to have private modified by default is good in most cases, but in some cases it drawbacks in too much boilerplate get/set code. One of such cases are immutable classes.

Restriction: Check doesn't check if class is immutable, there's no checking if accessory methods are missing and all fields are immutable, we only check if current field is immutable or final. Under the flag allowPublicImmutableFields, the enclosing class must also be final, to encourage immutability. Under the flag allowPublicFinalFields, the final modifier on the enclosing class is optional.

Star imports are out of scope of this Check. So if one of type imported via star import collides with user specified one by its short name - there won't be Check's violation.

/** * 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; } }