1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.chabala.brick.controllab;
20
21 import org.junit.*;
22 import org.junit.rules.ExpectedException;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnit;
25 import org.mockito.junit.MockitoRule;
26
27 import java.util.EnumSet;
28 import java.util.Set;
29
30 import static org.hamcrest.Matchers.*;
31 import static org.junit.Assert.*;
32 import static org.mockito.Mockito.*;
33
34
35
36
37 public class OutputTest {
38
39 @Rule
40 public MockitoRule rule = MockitoJUnit.rule();
41
42 @Rule
43 public ExpectedException thrown = ExpectedException.none();
44
45 @Mock
46 private ControlLab controlLab;
47
48 private OutputId outputId;
49 private Set<OutputId> outputIdSet;
50 private Output output;
51
52 private RandomEnum randomEnum = new RandomEnum();
53
54 @Before
55 public void setUp() {
56 outputId = randomEnum.get(OutputId.class);
57 outputIdSet = EnumSet.of(outputId);
58 output = new Output(controlLab, outputId);
59 }
60
61 @After
62 public void tearDown() {
63 output = null;
64 outputIdSet = null;
65 outputId = null;
66 }
67
68 @Test
69 public void testGetOutputIdSet() {
70 Set<OutputId> outputIds = output.getOutputIdSet();
71 assertThat(outputIds, contains(outputId));
72
73 thrown.expect(UnsupportedOperationException.class);
74 outputIds.add(randomEnum.get(OutputId.class));
75 }
76
77 @Test
78 public void testTurnOff() throws Exception {
79 Output newOutput = output.turnOff();
80 assertThat(newOutput, is(output));
81 verify(controlLab, times(1)).turnOutputOff(outputIdSet);
82 }
83
84 @Test
85 public void testTurnOn() throws Exception {
86 Output newOutput = output.turnOn();
87 assertThat(newOutput, is(output));
88 verify(controlLab, times(1)).turnOutputOn(outputIdSet);
89 }
90
91 @Test
92 public void testReverseDirection() throws Exception {
93 Output newOutput = output.reverseDirection();
94 assertThat(newOutput, is(output));
95 verify(controlLab, times(1)).setOutputDirection(Direction.REVERSE, outputIdSet);
96 }
97
98 @Test
99 public void testSetPowerLevel() throws Exception {
100 final PowerLevel powerLevel = randomEnum.get(PowerLevel.class);
101 Output newOutput = output.setPowerLevel(powerLevel);
102 assertThat(newOutput, is(output));
103 verify(controlLab, times(1)).setOutputPowerLevel(powerLevel, outputIdSet);
104 }
105
106 @Test
107 public void testToString() {
108 assertThat(output + "", containsString("outputIdSet=[" + outputId.name() + "]"));
109 }
110 }