1
2
3
4
5 package net.sf.atmodem4j.core;
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
155
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
167 fail("The test case is a prototype.");
168 }
169
170
171
172
173 @Test
174 @Ignore
175 public void testSetSpeaker() {
176 System.out.println("setSpeaker");
177 Modem instance = new ItuV250Modem();
178 instance.setSpeaker();
179
180 fail("The test case is a prototype.");
181 }
182
183
184
185
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
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
205
206 @Test
207 @Ignore
208 public void testReenterDataMode() {
209 System.out.println("reenterDataMode");
210 Modem instance = new ItuV250Modem();
211 instance.reenterDataMode();
212
213 fail("The test case is a prototype.");
214 }
215
216
217
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
226 fail("The test case is a prototype.");
227 }
228 }