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.parser;
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.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   * @author aploese
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          //TODO wait on is empty??
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 //        doTestCommand("D **", "CONNECT 33000");
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         //TODO wait on is empty??
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         //TODO wait on is empty??
171         Thread.sleep(200);
172         EasyMock.verify(modem);
173     }
174 }