1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.beep4j.internal;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.nio.ByteBuffer;
21 import java.nio.channels.FileChannel;
22 import java.nio.channels.FileChannel.MapMode;
23 import java.nio.charset.Charset;
24
25 import junit.framework.TestCase;
26
27 import org.easymock.MockControl;
28
29 public class StreamParserTest extends TestCase {
30
31 private MockControl handlerCtrl;
32
33 private FrameHandler handler;
34
35 private MockControl mappingCtrl;
36
37 private TransportMapping mapping;
38
39 private ByteBuffer getMessage(String name) throws Exception {
40 File file = new File("data/" + name);
41 FileInputStream stream = new FileInputStream(file);
42 FileChannel channel = stream.getChannel();
43 return channel.map(MapMode.READ_ONLY, 0, channel.size());
44 }
45
46 protected void setUp() {
47 handlerCtrl = MockControl.createStrictControl(FrameHandler.class);
48 handler = (FrameHandler) handlerCtrl.getMock();
49 mappingCtrl = MockControl.createStrictControl(TransportMapping.class);
50 mapping = (TransportMapping) mappingCtrl.getMock();
51 }
52
53 public void testOneFrame() throws Exception {
54
55 Charset charset = Charset.forName("UTF-8");
56 DataHeader header = new DataHeader(MessageType.RPY, 0, 0, false, 0, 52);
57 Frame frame = new Frame(
58 header,
59 charset.encode("Content-Type: application/beep+xml\r\n\r\n<greeting />\r\n"));
60
61
62 handler.handleFrame(frame);
63 mapping.checkFrame(0, 0, 52);
64 mapping.frameReceived(0, 0, 52);
65
66
67 mappingCtrl.replay();
68 handlerCtrl.replay();
69
70
71 StreamParser parser = new DefaultStreamParser(handler, mapping);
72 ByteBuffer buffer = getMessage("greeting/i_greeting.txt");
73 parser.process(buffer);
74
75
76 mappingCtrl.verify();
77 handlerCtrl.verify();
78 }
79
80 public void testTwoFrames() throws Exception {
81
82 Charset charset = Charset.forName("UTF-8");
83 DataHeader header = new DataHeader(MessageType.RPY, 0, 0, false, 0, 52);
84 Frame frame = new Frame(
85 header,
86 charset.encode("Content-Type: application/beep+xml\r\n\r\n<greeting />\r\n"));
87
88
89 handler.handleFrame(frame);
90 mapping.checkFrame(0, 0, 52);
91 mapping.frameReceived(0, 0, 52);
92 handler.handleFrame(frame);
93 mapping.checkFrame(0, 0, 52);
94 mapping.frameReceived(0, 0, 52);
95
96
97 mappingCtrl.replay();
98 handlerCtrl.replay();
99
100
101 StreamParser parser = new DefaultStreamParser(handler, mapping);
102 parser.process(getMessage("greeting/i_greeting.txt"));
103 parser.process(getMessage("greeting/i_greeting.txt"));
104
105
106 mappingCtrl.verify();
107 handlerCtrl.verify();
108 }
109
110 public void testMappingFrame() throws Exception {
111
112 mapping.processMappingFrame(new String[] { "SEQ", "0", "0", "4096" });
113 mappingCtrl.setMatcher(MockControl.ARRAY_MATCHER);
114
115
116 mappingCtrl.replay();
117 handlerCtrl.replay();
118
119
120 StreamParser parser = new DefaultStreamParser(handler, mapping);
121 Charset charset = Charset.forName("US-ASCII");
122 ByteBuffer buffer = charset.encode("SEQ 0 0 4096\r\n");
123 parser.process(buffer);
124
125
126 mappingCtrl.verify();
127 handlerCtrl.verify();
128 }
129
130 public void testMappingFollowedByDataFrame() throws Exception {
131
132 Charset charset = Charset.forName("US-ASCII");
133 DataHeader header = new DataHeader(MessageType.RPY, 0, 0, false, 0, 52);
134 Frame frame = new Frame(
135 header,
136 charset.encode("Content-Type: application/beep+xml\r\n\r\n<greeting />\r\n"));
137
138
139 mapping.processMappingFrame(new String[] { "SEQ", "0", "0", "4096" });
140 mappingCtrl.setMatcher(MockControl.ARRAY_MATCHER);
141 handler.handleFrame(frame);
142 mapping.checkFrame(0, 0, 52);
143 mapping.frameReceived(0, 0, 52);
144
145
146 mappingCtrl.replay();
147 handlerCtrl.replay();
148
149
150 StreamParser parser = new DefaultStreamParser(handler, mapping);
151 ByteBuffer buffer = charset.encode("SEQ 0 0 4096\r\n");
152 parser.process(buffer);
153 parser.process(getMessage("greeting/i_greeting.txt"));
154
155
156 mappingCtrl.verify();
157 handlerCtrl.verify();
158 }
159
160 }