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 jssc.SerialPortException;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnit;
28 import org.mockito.junit.MockitoRule;
29
30 import java.io.IOException;
31 import java.util.Random;
32
33 import static org.hamcrest.Matchers.is;
34 import static org.junit.Assert.assertThat;
35 import static org.mockito.Mockito.*;
36
37
38
39
40 public class JsscSerialPortTest {
41
42 @Rule
43 public MockitoRule rule = MockitoJUnit.rule();
44
45 @Rule
46 public ExpectedException thrown = ExpectedException.none();
47
48 @Mock
49 private jssc.SerialPort innerSerialPort;
50
51 @Mock
52 private SerialPortException serialPortException;
53
54 @Mock
55 private SerialPortEventListener serialPortEventListener;
56
57 private Random random = new Random();
58 private SerialPort serialPort;
59
60 @Before
61 public void setUp() throws Exception {
62 serialPort = new JsscSerialPort(innerSerialPort);
63 }
64
65 @Test
66 public void testGetPortNameDelegatesToJssc() throws Exception {
67 final String portname = "one";
68 when(innerSerialPort.getPortName()).thenReturn(portname);
69 assertThat(serialPort.getPortName(), is(portname));
70 }
71
72 @Test
73 public void testOpenPortDelegatesToJssc() throws Exception {
74 serialPort.openPort();
75 verify(innerSerialPort, times(1)).openPort();
76 verify(innerSerialPort, times(1)).setParams(
77 jssc.SerialPort.BAUDRATE_9600,
78 jssc.SerialPort.DATABITS_8,
79 jssc.SerialPort.STOPBITS_1,
80 jssc.SerialPort.PARITY_NONE,
81 true,
82 true);
83 verify(innerSerialPort, times(1)).setEventsMask(
84 jssc.SerialPort.MASK_RXCHAR |
85 jssc.SerialPort.MASK_CTS |
86 jssc.SerialPort.MASK_DSR);
87 }
88
89 @Test
90 public void testOpenPortOnlyThrowsIOExceptions() throws Exception {
91 thrown.expect(IOException.class);
92 thrown.expectCause(is(serialPortException));
93 when(innerSerialPort.openPort()).thenThrow(serialPortException);
94 serialPort.openPort();
95 }
96
97 @Test
98 public void testIsOpenDelegatesToJssc() throws Exception {
99 boolean opened = random.nextBoolean();
100 when(innerSerialPort.isOpened()).thenReturn(opened);
101 assertThat(serialPort.isOpen(), is(opened));
102 }
103
104 @Test
105 public void testWriteByteDelegatesToJssc() throws Exception {
106 byte b = 10;
107 final boolean result = true;
108 when(innerSerialPort.writeByte(b)).thenReturn(result);
109 assertThat(serialPort.write(b), is(result));
110 }
111
112 @Test
113 public void testWriteByteOnlyThrowsIOExceptions() throws Exception {
114 byte b = 10;
115 thrown.expect(IOException.class);
116 thrown.expectCause(is(serialPortException));
117 when(innerSerialPort.writeByte(b)).thenThrow(serialPortException);
118 serialPort.write(b);
119 }
120
121 @Test
122 public void testWriteByteArrayDelegatesToJssc() throws Exception {
123 byte[] bytes = new byte[2];
124 random.nextBytes(bytes);
125 final boolean result = true;
126 when(innerSerialPort.writeBytes(bytes)).thenReturn(result);
127 assertThat(serialPort.write(bytes), is(result));
128 }
129
130 @Test
131 public void testWriteByteArrayOnlyThrowsIOExceptions() throws Exception {
132 byte[] bytes = new byte[2];
133 random.nextBytes(bytes);
134 thrown.expect(IOException.class);
135 thrown.expectCause(is(serialPortException));
136 when(innerSerialPort.writeBytes(bytes)).thenThrow(serialPortException);
137 serialPort.write(bytes);
138 }
139
140 @Test
141 public void testReadBytesDelegatesToJssc() throws Exception {
142 int byteCount = 5;
143 byte[] bytes = new byte[byteCount];
144 random.nextBytes(bytes);
145 when(innerSerialPort.readBytes(byteCount)).thenReturn(bytes);
146 byte[] readBytes = serialPort.readBytes(byteCount);
147 assertThat(readBytes.length, is(byteCount));
148 assertThat(readBytes, is(bytes));
149 }
150
151 @Test
152 public void testReadBytesOnlyThrowsIOExceptions() throws Exception {
153 int byteCount = 5;
154 thrown.expect(IOException.class);
155 thrown.expectCause(is(serialPortException));
156 when(innerSerialPort.readBytes(byteCount)).thenThrow(serialPortException);
157 serialPort.readBytes(byteCount);
158 }
159
160 @Test
161 public void testCloseWhenClosed() throws Exception {
162 when(innerSerialPort.isOpened()).thenReturn(false);
163 serialPort.close();
164 verify(innerSerialPort, never()).closePort();
165 }
166
167 @Test
168 public void testCloseWhenOpen() throws Exception {
169 when(innerSerialPort.isOpened()).thenReturn(true);
170 serialPort.close();
171 verify(innerSerialPort, times(1)).closePort();
172 }
173
174 @Test
175 public void testCloseOnlyThrowsIOExceptions() throws Exception {
176 thrown.expect(IOException.class);
177 thrown.expectCause(is(serialPortException));
178 when(innerSerialPort.isOpened()).thenReturn(true);
179 when(innerSerialPort.closePort()).thenThrow(serialPortException);
180 serialPort.close();
181 }
182
183 @Test
184 public void testAddEventListenerDelegatesToJssc() throws Exception {
185 serialPort.addEventListener(serialPortEventListener);
186 verify(innerSerialPort, times(1)).addEventListener(serialPortEventListener);
187 }
188
189 @Test
190 public void testAddEventListenerOnlyThrowsIOExceptions() throws Exception {
191 thrown.expect(IOException.class);
192 thrown.expectCause(is(serialPortException));
193 doThrow(serialPortException).when(innerSerialPort).addEventListener(serialPortEventListener);
194 serialPort.addEventListener(serialPortEventListener);
195 }
196 }