1
2
3
4
5 package net.sf.atmodem4j.core.parser;
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.ByteArrayInputStream;
36 import net.sf.atmodem4j.core.Modem;
37 import org.easymock.EasyMock;
38 import org.easymock.IMocksControl;
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Ignore;
44 import org.junit.Test;
45 import static org.junit.Assert.*;
46 import static net.sf.atmodem4j.core.parser.ParserMatcher.eqResultCodeToken;
47 import static net.sf.atmodem4j.core.parser.ParserMatcher.eqParser;
48
49
50
51
52
53 public class ParserTest {
54
55 private Modem modem;
56
57 public ParserTest() {
58 }
59
60 @BeforeClass
61 public static void setUpClass() throws Exception {
62 }
63
64 @AfterClass
65 public static void tearDownClass() throws Exception {
66 }
67
68 @Before
69 public void setUp() {
70 }
71
72 @After
73 public void tearDown() {
74 }
75
76 @Test
77 public void testRING() throws Exception {
78 StringBuilder sb = new StringBuilder();
79 sb.append("RING");
80 sb.append("\r\n");
81 ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().
82 getBytes());
83
84 Parser parser = new Parser();
85 parser.setLineChars('\r', '\n');
86
87 IMocksControl ctl = EasyMock.createStrictControl();
88 modem = ctl.createMock("modem", Modem.class);
89 parser.setModem(modem);
90 modem.parsedRing(parser);
91 EasyMock.replay(modem);
92 parser.setInputStream(is);
93
94
95 Thread.sleep(200);
96 EasyMock.verify(modem);
97 }
98
99 @Test
100 public void testAT() throws Exception {
101 doTestCommand("AT", "OK");
102 }
103
104 @Test
105 public void testS0() throws Exception {
106 doTestCommand("ATS0?", "OK", "000");
107 }
108
109 @Test
110 public void testATD() throws Exception {
111 doTestCommand("ATD **", "CONNECT");
112 }
113
114 @Test
115 public void testATDText() throws Exception {
116
117 }
118
119 @Test
120 public void testATGMR() throws Exception {
121 doTestCommand("AT+GMR", "OK", "+GMR: \"Version 1.87 / 31.07.2000\"");
122 }
123
124 @Test
125 public void testPrompt() throws Exception {
126 StringBuilder sb = new StringBuilder();
127 sb.append("AT+CMGS=6\r\r\n>0000"+ Modem.EOF + "\r\nOK\r\n");
128 ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().
129 getBytes());
130
131 Parser parser = new Parser();
132 parser.setLineChars('\r', '\n');
133
134 IMocksControl ctl = EasyMock.createStrictControl();
135 modem = ctl.createMock("modem", Modem.class);
136 parser.setModem(modem);
137 modem.parsedPrompt(parser);
138 modem.parsedResultCode(eqParser(parser), eqResultCodeToken(new ResultCodeToken("OK", new String[] {"0000"+ Modem.EOF}, "AT+CMGS=6")));
139 EasyMock.replay(modem);
140 parser.setInputStream(is);
141
142
143 Thread.sleep(200);
144 EasyMock.verify(modem);
145 }
146
147
148 private void doTestCommand(String command, String resultCode, String... data)
149 throws Exception {
150 StringBuilder sb = new StringBuilder();
151 sb.append(command).append("\r\r\n");
152 for (String s : data) {
153 sb.append(s).append("\r\n");
154 }
155 sb.append(resultCode).append("\r\n");
156 ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().
157 getBytes());
158
159 Parser parser = new Parser();
160 parser.setLineChars('\r', '\n');
161
162 IMocksControl ctl = EasyMock.createStrictControl();
163 modem = ctl.createMock("modem", Modem.class);
164 parser.setModem(modem);
165 modem.parsedResultCode(eqParser(parser), eqResultCodeToken(
166 new ResultCodeToken(resultCode, data, command)));
167 EasyMock.replay(modem);
168 parser.setInputStream(is);
169
170
171 Thread.sleep(200);
172 EasyMock.verify(modem);
173 }
174 }