View Javadoc
1   /*
2    * Copyright © 2016 Greg Chabala
3    *
4    * This file is part of brick-control-lab.
5    *
6    * brick-control-lab is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   *
11   * brick-control-lab is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with brick-control-lab.  If not, see http://www.gnu.org/licenses/.
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   * Testing {@link Output}.
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 }