Skip to content

Commit

Permalink
Fix Box button not working after Star Pass (#78)
Browse files Browse the repository at this point in the history
* Fix Box button not working after Star Pass

When the skater changes positiosn from "Pivot" to "Jammer", the state is not correctly sync-ed between the PositionModel and the SkaterModel.  At Jam end, the Jammer position's PenaltyBox status is cleared, but then not re-set based on the SkaterModel.

This is now "fixed", but this PositionModel/SkaterModel nonsense is a mess, and in an ideal world I would just maintain the state on the SkaterModel and fire Position updates if the skater's status changes.  I would be more comfortable making those changes once unit tests are in place though (#72).

Closes #5

* add "unit" tests for DefaultPositionModel

they aren't very unit-y, they depend rather heavily on the Team and Skater Models to work correctly, but I think this accomplishes good coverage of the Position interface.  Added Mockito to avoid pulling in too much of the universe to do these tests.  Near term plan (sometime in the few weeks) is to cook up a nice fixture to test the event system, add Tests for Skater and Team models, then start teasing apart the interweaving of the Team, Skater and Position models.

* fix some issues with ant build with new tests
  • Loading branch information
official-sounding authored and brian-brazil committed Jun 21, 2018
1 parent e183594 commit 262b06f
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 1 deletion.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
<classpathentry kind="lib" path="lib/deps/json-20140107.jar"/>
<classpathentry kind="lib" path="test-libs/junit-4.11.jar"/>
<classpathentry kind="lib" path="test-libs/hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="test-libs/mockito-all-1.9.5.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 2 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<path id="classpath.test">
<pathelement location="test-libs/junit-4.11.jar"/>
<pathelement location="test-libs/hamcrest-core-1.3.jar"/>
<pathelement location="test-libs/mockito-all-1.9.5.jar"/>
<pathelement location="${dest.dir}"/>
</path>

Expand Down Expand Up @@ -132,7 +133,7 @@
</classpath>
<formatter type="brief" usefile="false" />
<batchtest>
<fileset dir="${test.src.dir}" includes="**/*Test.java" />
<fileset dir="${test.src.dir}" includes="**/*Test*.java" />
</batchtest>
</junit>
</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void _setSkaterModel(String skaterId) throws SkaterNotFoundException {
clear();
SkaterModel last = skaterModel;
skaterModel = newSkaterModel;
_setPenaltyBox(newSkaterModel.isPenaltyBox());
scoreBoardChange(new ScoreBoardEvent(getPosition(), EVENT_SKATER, skaterModel, last));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void setPosition(String p) throws PositionNotFoundException {
}

public boolean isPenaltyBox() { return penaltyBox; }

public void setPenaltyBox(boolean box) {
synchronized (positionLock) {
if (box == penaltyBox)
Expand All @@ -108,6 +109,7 @@ public void setPenaltyBox(boolean box) {
}

public String getFlags() { return flags; }

public void setFlags(String f) {
synchronized (flagsLock) {
String last = flags;
Expand All @@ -119,6 +121,7 @@ public void setFlags(String f) {

public List<Penalty> getPenalties() { return Collections.unmodifiableList(new ArrayList<Penalty>(penalties)); }
public Penalty getFOEXPPenalty() { return foexp_penalty; }

public void AddPenaltyModel(String id, boolean foulout_explusion, int period, int jam, String code) {
synchronized (penaltiesLock) {
if (foulout_explusion && code != null) {
Expand Down
Binary file added test-libs/mockito-all-1.9.5.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package com.carolinarollergirls.scoreboard;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import com.carolinarollergirls.scoreboard.model.*;
import com.carolinarollergirls.scoreboard.defaults.*;

public class DefaultPositionModelTests {
private final String firstId = "662caf51-17da-4ef2-8f01-a6d7e1c30d56";
private final String secondId = "91a6c3f5-8258-46c7-a845-62c21b6e37ca";
private final String thirdId = "5df0a35b-aaa6-4c30-93ef-07ba4f174cdc";

private ScoreBoardModel sbModelMock;
private Ruleset ruleMock;
private TeamModel teamModel;
private SkaterModel first;
private SkaterModel second;
private SkaterModel third;

@Before
public void setup() {
sbModelMock = Mockito.mock(DefaultScoreBoardModel.class);

ruleMock = Mockito.mock(Ruleset.class);

Mockito
.when(sbModelMock.getScoreBoard())
.thenReturn(sbModelMock);

Mockito
.when(sbModelMock._getRuleset())
.thenReturn(ruleMock);

teamModel = new DefaultTeamModel(sbModelMock, "A");

first = new DefaultSkaterModel(teamModel, firstId, "First","123", "");
second = new DefaultSkaterModel(teamModel, secondId, "Second","456", "");
third = new DefaultSkaterModel(teamModel, thirdId, "Third","789","");

teamModel.addSkaterModel(first);
teamModel.addSkaterModel(second);
teamModel.addSkaterModel(third);
}

@Test
public void key_values_populated() {
PositionModel blocker = teamModel.getPositionModel(Position.ID_BLOCKER1);

assertSame(blocker.getId(),Position.ID_BLOCKER1);
assertSame(blocker.getProviderName(), "Position");
assertSame(blocker.getProviderId(), Position.ID_BLOCKER1);
assertSame(blocker.getProviderClass(), Position.class);
assertSame(blocker.getTeam(), teamModel);
}

@Test
public void make_skater_jammer_via_position() {
PositionModel jammer = teamModel.getPositionModel(Position.ID_JAMMER);
jammer.setSkaterModel(firstId);

assertSame(jammer.getSkaterModel(), first);
assertSame(first.getPosition(),Position.ID_JAMMER);
}

@Test
public void make_skater_jammer_via_skater() {
first.setPosition(Position.ID_JAMMER);
PositionModel jammer = teamModel.getPositionModel(Position.ID_JAMMER);

assertSame(jammer.getSkaterModel(), first);
assertSame(first.getPosition(), Position.ID_JAMMER);
}

@Test
public void position_knows_skater_penalty() {
PositionModel jammer = teamModel.getPositionModel(Position.ID_JAMMER);
jammer.setSkaterModel(firstId);
first.setPenaltyBox(true);

assertTrue(jammer.getPenaltyBox());
}

@Test
public void skater_knows_position_penalty() {
PositionModel jammer = teamModel.getPositionModel(Position.ID_JAMMER);
jammer.setSkaterModel(firstId);
jammer.setPenaltyBox(true);

assertTrue(first.isPenaltyBox());
}

@Test
public void doesnt_set_penalty_with_no_skater() {
PositionModel jammer = teamModel.getPositionModel(Position.ID_JAMMER);
jammer.setPenaltyBox(true);

assertFalse(jammer.getPenaltyBox());
}

@Test
public void sp_works() {
PositionModel jammer = teamModel.getPositionModel(Position.ID_JAMMER);
PositionModel pivot = teamModel.getPositionModel(Position.ID_PIVOT);
jammer.setSkaterModel(firstId);
pivot.setSkaterModel(firstId);

assertSame(pivot.getSkaterModel(), first);
assertNull(jammer.getSkaterModel());
}

@Test
public void clears_with_empty_model_id() {
PositionModel jammer = teamModel.getPositionModel(Position.ID_JAMMER);
jammer.setSkaterModel(firstId);
jammer.setSkaterModel("");

assertNull(jammer.getSkaterModel());
}

@Test
public void clears_with_null_model_id() {
PositionModel jammer = teamModel.getPositionModel(Position.ID_JAMMER);
jammer.setSkaterModel(firstId);
jammer.setSkaterModel(null);

assertNull(jammer.getSkaterModel());
}


@Test(expected = SkaterNotFoundException.class)
public void throws_with_bogus_skater_id() {
PositionModel jammer = teamModel.getPositionModel(Position.ID_JAMMER);
jammer.setSkaterModel("bogus");
}

@Test
public void position_knows_penalty_after_sp() {
PositionModel jammer = teamModel.getPositionModel(Position.ID_JAMMER);
PositionModel pivot = teamModel.getPositionModel(Position.ID_PIVOT);
pivot.setSkaterModel(firstId);
pivot.setPenaltyBox(true);
first.setPosition(Position.ID_JAMMER);

assertTrue(jammer.getPenaltyBox());
assertFalse(pivot.getPenaltyBox());
assertTrue(first.isPenaltyBox());
}

@Test
public void position_knows_penalty_after_sp_position() {
PositionModel jammer = teamModel.getPositionModel(Position.ID_JAMMER);
PositionModel pivot = teamModel.getPositionModel(Position.ID_PIVOT);
pivot.setSkaterModel(firstId);
pivot.setPenaltyBox(true);
jammer.setSkaterModel(firstId);

assertTrue(jammer.getPenaltyBox());
assertFalse(pivot.getPenaltyBox());
assertTrue(first.isPenaltyBox());
}




}

0 comments on commit 262b06f

Please sign in to comment.