View Javadoc
1   package net.sf.atmodem4j.spsw.tests;
2   
3   /*
4    * #%L
5    * SPSW Tests
6    * %%
7    * Copyright (C) 2009 - 2014 atmodem4j
8    * %%
9    * atmodem4j - A serial port socket wrapper- http://atmodem4j.sourceforge.net/
10   * Copyright (C) 2009-2014, atmodem4j.sf.net, and individual contributors as indicated
11   * by the @authors tag. See the copyright.txt in the distribution for a
12   * full listing of individual contributors.
13   * 
14   * This is free software; you can redistribute it and/or modify it
15   * under the terms of the GNU General Public License as
16   * published by the Free Software Foundation; either version 3 of
17   * the License, or (at your option) any later version.
18   * 
19   * This software is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22   * Lesser General Public License for more details.
23   * 
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this software; if not, write to the Free
26   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
28   * #L%
29   */
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.text.MessageFormat;
33  import java.util.Arrays;
34  import java.util.Properties;
35  import java.util.logging.Level;
36  import java.util.logging.Logger;
37  import net.sf.atmodem4j.spsw.Baudrate;
38  import net.sf.atmodem4j.spsw.DataBits;
39  import net.sf.atmodem4j.spsw.FlowControl;
40  import net.sf.atmodem4j.spsw.GenericTermiosSerialPortSocket;
41  import net.sf.atmodem4j.spsw.Parity;
42  import net.sf.atmodem4j.spsw.SerialPortSocket;
43  import net.sf.atmodem4j.spsw.StopBits;
44  import org.junit.After;
45  import org.junit.Assert;
46  import org.junit.Assume;
47  import org.junit.Before;
48  import org.junit.BeforeClass;
49  import org.junit.Ignore;
50  import org.junit.Test;
51  
52  /**
53   * Unit test for simple App.
54   */
55  public class TwoPortSingleByteTest {
56  
57      private static final Logger LOG = Logger.getLogger("SerialTests");
58      private final static int DEFAULT_TEST_BUFFER_SIZE = 1024 * 2;
59  
60      public class ReceiverThread extends Thread {
61  
62          boolean done;
63          final Object LOCK = new Object();
64          byte[] dataIn;
65          byte[] dataOut;
66          long startTimeStamp;
67          Exception ex;
68          Error err;
69  
70          @Override
71          public void run() {
72              try {
73                  int offset = 0;
74                  while (true) {
75                      try {
76                          final int data = spc[1].getInputStream().read();
77                          if (data >= 0) {
78                              dataIn[offset] = (byte) data;
79                              Assert.assertEquals("Error @Offset: " + offset, dataOut[offset], dataIn[offset]);
80                              offset++;
81                              if (offset == dataOut.length) {
82                                  break;
83                              }
84                          } else {
85                              if (offset < dataIn.length) {
86                                  LOG.log(Level.SEVERE, "Bytes missing: {0}", dataIn.length - offset);
87                                  printArrays("Too short");
88                              }
89                              break;
90                          }
91                      } catch (IOException e) {
92                          throw new RuntimeException(e);
93                      }
94                  }
95                  LOG.log(Level.INFO, "Byte total read: {0}", offset);
96                  LOG.log(Level.INFO, "Receive time in millis:: {0}", (System.currentTimeMillis() - startTimeStamp));
97                  synchronized (LOCK) {
98                      done = true;
99                      LOCK.notifyAll();
100                     LOG.log(Level.INFO, "Send Thread finished");
101                 }
102             } catch (Exception ex) {
103                 synchronized (LOCK) {
104                     done = true;
105                     this.ex = ex;
106                     LOG.log(Level.SEVERE, "Send Thread Exception", ex);
107                     LOCK.notifyAll();
108                 }
109             } catch (Error err) {
110                 synchronized (LOCK) {
111                     done = true;
112                     this.err = err;
113                     printArrays("Error");
114                     LOG.log(Level.SEVERE, "Send Thread Error", err);
115                     LOCK.notifyAll();
116                 }
117             }
118         }
119 
120         private void initBuffers(final int size) {
121             dataOut = new byte[size];
122             for (int i = 0; i < size; i++) {
123 //                  dataOut[i] = (byte) (i & 0xFF);
124                 dataOut[i] = (byte) (Math.round(Math.random() * 0xFF) & 0xFF);
125             }
126             dataIn = new byte[size];
127         }
128 
129         private void printArrays(String title) {
130             System.err.println(title);
131             for (int i = 0; i < dataOut.length; i++) {
132                 System.err.println(i + "\t: " + dataOut[i] + "\t" + dataIn[i]);
133             }
134         }
135 
136     }
137 
138     private static final String[] serialPortName = new String[2];
139     private final SerialPortSocket[] spc = new SerialPortSocket[2];
140     private ReceiverThread receiverThread;
141 
142     @BeforeClass
143     public static void setUpClass() throws Exception {
144         try (InputStream is = TwoPortSingleByteTest.class.getClassLoader().getResourceAsStream("junit-spsw-config.properties")) {
145             if (is == null) {
146                 serialPortName[0] = null;
147                 serialPortName[1] = null;
148             } else {
149                 Properties p = new Properties();
150                 p.load(is);
151                 serialPortName[0] = p.getProperty("port0", null);
152                 serialPortName[1] = p.getProperty("port1", null);
153             }
154         }
155     }
156 
157     @Before
158     public void setUp() throws Exception {
159         for (int i = 0; i < serialPortName.length; i++) {
160             if (serialPortName[i] != null) {
161                 spc[i] = SerialPortSocket.FACTORY.createSerialPortSocket(serialPortName[i]);
162                 receiverThread = new ReceiverThread();
163             } else {
164                 spc[i] = null;
165             }
166         }
167     }
168 
169     @After
170     public void tearDown() throws Exception {
171         for (int i = 0; i < spc.length; i++) {
172             if (spc[i] != null) {
173                 if (spc[i].isOpen()) {
174                     spc[i].close();
175                 }
176             }
177             spc[i] = null;
178         }
179         receiverThread = null;
180     }
181 
182     private void runTest(Baudrate baudrate, int buffersize) throws Exception {
183         LOG.info(MessageFormat.format("do run test @baudrate: {0}, buffer size: {1}", baudrate.value, buffersize));
184 
185         receiverThread.initBuffers(buffersize);
186         receiverThread.startTimeStamp = System.currentTimeMillis();
187 
188         Assume.assumeNotNull((Object[]) spc);
189         spc[0].openRaw(baudrate, DataBits.DB_8, StopBits.SB_1, Parity.NONE, FlowControl.getFC_RTS_CTS());
190         spc[1].openRaw(spc[0].getBaudrate(), spc[0].getDatatBits(), spc[0].getStopBits(), spc[0].getParity(), spc[0].getFlowControl());
191         printPort(spc[0]);
192 
193         //Make sure buffers are empty
194         Assert.assertEquals(0, spc[0].getOutBufferBytesCount());
195         Assert.assertEquals(0, spc[1].getInBufferBytesCount());
196 
197         receiverThread.start();
198 
199         spc[0].getOutputStream().write(receiverThread.dataOut);
200 //        for (int i = 0; i < receiverThread.dataOut.length; i++) spc[0].getOutputStream().write(receiverThread.dataOut[i]);
201 
202         LOG.log(Level.INFO, "Send time in millis:: {0}", (System.currentTimeMillis() - receiverThread.startTimeStamp));
203         synchronized (receiverThread.LOCK) {
204             while (!receiverThread.done) {
205                 receiverThread.LOCK.wait();
206                 if (receiverThread.ex != null) {
207                     throw receiverThread.ex;
208                 }
209                 if (receiverThread.err != null) {
210                     throw receiverThread.err;
211                 }
212             }
213             LOG.log(Level.INFO, "Thread finished received");
214         }
215         try {
216             Assert.assertArrayEquals(receiverThread.dataOut, receiverThread.dataIn);
217         } catch (Error err) {
218             if (spc[0] instanceof GenericTermiosSerialPortSocket) {
219                 ((GenericTermiosSerialPortSocket) spc[0]).printTermios();
220                 ((GenericTermiosSerialPortSocket) spc[1]).printTermios();
221             }
222             throw err;
223         }
224     }
225 
226     private void printPort(SerialPortSocket sPort) throws IOException {
227         System.err.println("Baudrate: " + sPort.getBaudrate().value);
228         System.err.println("DataBits: " + sPort.getDatatBits().value);
229         System.err.println("StopBits: " + sPort.getStopBits().value);
230         System.err.println("Parity: " + sPort.getParity());
231         System.err.println("Flowcontrol: " + sPort.getFlowControl());
232 
233     }
234 
235     @Ignore
236     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 300)
237     public void test_0000300() throws Exception {
238         runTest(Baudrate.B300, DEFAULT_TEST_BUFFER_SIZE);
239     }
240 
241     @Ignore
242     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 2400)
243     public void test_0002400() throws Exception {
244         runTest(Baudrate.B2400, DEFAULT_TEST_BUFFER_SIZE);
245     }
246 
247     @Ignore
248     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 9600)
249     public void test_0009600() throws Exception {
250         runTest(Baudrate.B9600, DEFAULT_TEST_BUFFER_SIZE);
251     }
252 
253     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 19200)
254     public void test_0019200() throws Exception {
255         runTest(Baudrate.B19200, DEFAULT_TEST_BUFFER_SIZE);
256     }
257 
258     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 57600)
259     public void test_0057600() throws Exception {
260         runTest(Baudrate.B57600, DEFAULT_TEST_BUFFER_SIZE);
261     }
262 
263     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 115200)
264     public void test_0115200() throws Exception {
265         runTest(Baudrate.B115200, DEFAULT_TEST_BUFFER_SIZE);
266     }
267 
268     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 230400)
269     public void test_0230400() throws Exception {
270         runTest(Baudrate.B230400, DEFAULT_TEST_BUFFER_SIZE);
271     }
272 
273     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 460800)
274     public void test_0460800() throws Exception {
275         runTest(Baudrate.B460800, DEFAULT_TEST_BUFFER_SIZE);
276     }
277 
278     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 500000)
279     public void test_0500000() throws Exception {
280         runTest(Baudrate.B500000, DEFAULT_TEST_BUFFER_SIZE);
281     }
282 
283     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 576000)
284     public void test_0576000() throws Exception {
285         runTest(Baudrate.B576000, DEFAULT_TEST_BUFFER_SIZE);
286     }
287 
288     @Ignore
289     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 1000000)
290     public void test_1000000() throws Exception {
291         runTest(Baudrate.B1000000, DEFAULT_TEST_BUFFER_SIZE);
292     }
293 
294     @Ignore
295     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 1152000)
296     public void test_1152000() throws Exception {
297         runTest(Baudrate.B1152000, DEFAULT_TEST_BUFFER_SIZE);
298     }
299 
300     @Ignore
301     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 2000000)
302     public void test_2000000() throws Exception {
303         runTest(Baudrate.B2000000, DEFAULT_TEST_BUFFER_SIZE);
304     }
305 
306     @Ignore
307     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 3000000)
308     public void test_3000000() throws Exception {
309         runTest(Baudrate.B3000000, DEFAULT_TEST_BUFFER_SIZE);
310     }
311 
312     @Ignore
313     @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 4000000)
314     public void test_4000000() throws Exception {
315         runTest(Baudrate.B4000000, DEFAULT_TEST_BUFFER_SIZE);
316     }
317 
318 }