View Javadoc
1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package net.sf.atmodem4j.core;
6   
7   /*
8    * #%L
9    * ATModem4J Core
10   * %%
11   * Copyright (C) 2009 - 2014 atmodem4j
12   * %%
13   * atmodem4j - Drivers for the AT modem - http://atmodem4j.sourceforge.net/
14   * Copyright (C) 2009-2014, atmodem4j.sf.net, and individual contributors as indicated
15   * by the @authors tag. See the copyright.txt in the distribution for a
16   * full listing of individual contributors.
17   * 
18   * This is free software; you can redistribute it and/or modify it
19   * under the terms of the GNU General Public License as
20   * published by the Free Software Foundation; either version 3 of
21   * the License, or (at your option) any later version.
22   * 
23   * This software is distributed in the hope that it will be useful,
24   * but WITHOUT ANY WARRANTY; without even the implied warranty of
25   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26   * Lesser General Public License for more details.
27   * 
28   * You should have received a copy of the GNU Lesser General Public
29   * License along with this software; if not, write to the Free
30   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
31   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
32   * #L%
33   */
34  
35  import java.io.IOException;
36  import java.io.InputStream;
37  import java.io.OutputStream;
38  import org.junit.After;
39  import org.junit.AfterClass;
40  import org.junit.Before;
41  import org.junit.BeforeClass;
42  import org.junit.Ignore;
43  import org.junit.Test;
44  import static org.junit.Assert.*;
45  
46  /**
47   *
48   * @author aploese
49   */
50  public class ItuV250ModemTest {
51  
52      public class TestModem {
53  
54          class CompareOutputStream extends OutputStream implements Runnable {
55  
56              byte[] request;
57              int pos;
58              long delay;
59  
60              private CompareOutputStream(String request, long delay) {
61                  this.request = request.getBytes();
62                  this.delay = delay;
63              }
64  
65              @Override
66              public void write(int b) throws IOException {
67                  if (request[pos] == b) {
68                      pos++;
69                      if (pos == request.length) {
70                          new Thread(this).start();
71                      }
72                  } else {
73                      throw new RuntimeException(String.format("request mismatch! at pos: \"%d\" \"%s\" \"%s\"", pos, (char)request[pos], (char)b));
74                  }
75              }
76  
77              public void run() {
78                  try {
79                      Thread.sleep(delay);
80                  } catch (InterruptedException ex) {
81                      throw new RuntimeException(ex);
82                  }
83                  synchronized (this) {
84                      notifyAll();
85                  }
86              }
87          }
88  
89          class DelayedInputStream extends InputStream {
90  
91              byte[] replay;
92              int pos;
93              final Object lock;
94  
95              private DelayedInputStream(String response, Object lock) {
96                  this.lock = lock;
97                  replay = response.getBytes();
98              }
99  
100             @Override
101             public int read() throws IOException {
102                 try {
103                     if (pos == 0) {
104                         synchronized (lock) {
105                             lock.wait(1000);
106                         }
107                     }
108                 } catch (InterruptedException ex) {
109                     throw new RuntimeException(ex);
110                 }
111                 if (pos >= replay.length) {
112                     return -1;
113                 }
114                 return replay[pos++];
115             }
116         }
117         InputStream is;
118         OutputStream os;
119 
120         private TestModem(String request, String response, long delay) {
121             os = new CompareOutputStream(request, delay);
122             is = new DelayedInputStream(response, os);
123         }
124     }
125 
126     public ItuV250ModemTest() {
127     }
128 
129     @BeforeClass
130     public static void setUpClass() throws Exception {
131     }
132 
133     @AfterClass
134     public static void tearDownClass() throws Exception {
135     }
136 
137     @Before
138     public void setUp() {
139     }
140 
141     @After
142     public void tearDown() {
143     }
144 
145     Modem initModem(String request, String response, long delay) {
146         TestModem testModem = new TestModem(request, response, delay);
147         Modem atModem = new ItuV250Modem();
148         atModem.setModemInputStream(testModem.is);
149         atModem.setModemOutputStream(testModem.os);
150         return atModem;
151     }
152 
153     /**
154      * Test of dial method, of class ATModem.
155      * @throws Exception 
156      */
157     @Test
158     @Ignore
159     public void testDial() throws Exception {
160         System.out.println("dial");
161         String number = "";
162         Modem instance = new ItuV250Modem();
163         Connection expResult = null;
164         Connection result = instance.dial(number);
165         assertEquals(expResult, result);
166         // TODO review the generated test code and remove the default call to fail.
167         fail("The test case is a prototype.");
168     }
169 
170     /**
171      * Test of setSpeaker method, of class ATModem.
172      */
173     @Test
174     @Ignore
175     public void testSetSpeaker() {
176         System.out.println("setSpeaker");
177         Modem instance = new ItuV250Modem();
178         instance.setSpeaker();
179         // TODO review the generated test code and remove the default call to fail.
180         fail("The test case is a prototype.");
181     }
182 
183     /**
184      * Test of reset method, of class ATModem.
185      * @throws Exception 
186      */
187     @Test
188     public void testReset() throws Exception {
189         System.out.println("reset");
190         Modem instance = initModem("ATZ\r", "ATZ\r\r\nOK\r\n", 100);
191         assertTrue(instance.reset());
192     }
193 
194     // Cant test because OK ist faster then send.... setup of testmodem....
195     @Ignore
196     @Test
197     public void testPrompt() throws Exception {
198         System.out.println("prompt");
199         Modem instance = initModem("AT+CMGS=8\r00" + Modem.EOF, "AT+CMGS=8\r>00" + Modem.EOF + "\r\nOK\r\n", 100);
200         assertTrue(instance.sendCommandLineWithPrompt("AT+CMGS=8", "00"));
201     }
202 
203     /**
204      * Test of reenterDataMode method, of class ATModem.
205      */
206     @Test
207     @Ignore
208     public void testReenterDataMode() {
209         System.out.println("reenterDataMode");
210         Modem instance = new ItuV250Modem();
211         instance.reenterDataMode();
212         // TODO review the generated test code and remove the default call to fail.
213         fail("The test case is a prototype.");
214     }
215 
216     /**
217      * Test of sendHangUp method, of class ATModem.
218      */
219     @Test
220     @Ignore
221     public void testSendHangUp() throws Exception {
222         System.out.println("sendHangUp");
223         Modem instance = new ItuV250Modem();
224         instance.hangUp();
225         // TODO review the generated test code and remove the default call to fail.
226         fail("The test case is a prototype.");
227     }
228 }