1   /*
2    *  Copyright 2006 Simon Raess
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.beep4j.internal;
17  
18  import java.nio.ByteBuffer;
19  import java.nio.charset.Charset;
20  
21  import junit.framework.TestCase;
22  
23  import org.easymock.MockControl;
24  
25  public class TrailerStateTest extends TestCase {
26  	
27  	public void testProcess() throws Exception {
28  		MockControl control = MockControl.createControl(ParseStateContext.class);
29  		ParseStateContext context = (ParseStateContext) control.getMock();
30  		
31  		context.handleTrailer();
32  		control.replay();
33  
34  		Charset charset = Charset.forName("US-ASCII");
35  		ByteBuffer buffer = charset.encode("END\r\n");
36  		
37  		ParseState state = new TrailerState();
38  		assertFalse(state.process(buffer, context));
39  		
40  		assertEquals(5, buffer.position());
41  		
42  		control.verify();
43  	}
44  	
45  	public void testProcessMultiPass() throws Exception {
46  		MockControl control = MockControl.createControl(ParseStateContext.class);
47  		ParseStateContext context = (ParseStateContext) control.getMock();
48  		
49  		context.handleTrailer();
50  		control.replay();
51  
52  		Charset charset = Charset.forName("US-ASCII");
53  		ParseState state = new TrailerState();
54  
55  		ByteBuffer buffer = charset.encode("E");
56  		assertFalse(state.process(buffer, context));
57  		assertEquals(1, buffer.position());
58  		
59  		buffer = charset.encode("ND");
60  		assertFalse(state.process(buffer, context));
61  		assertEquals(2, buffer.position());
62  		
63  		buffer = charset.encode("\r\nMSG");
64  		assertTrue(state.process(buffer, context));
65  		assertEquals(2, buffer.position());
66  		
67  		control.verify();
68  	}
69  	
70  	public void testProcessComplex() throws Exception {
71  		MockControl control = MockControl.createControl(ParseStateContext.class);
72  		ParseStateContext context = (ParseStateContext) control.getMock();
73  		
74  		context.handleTrailer();
75  		context.handleTrailer();
76  		control.replay();
77  
78  		Charset charset = Charset.forName("US-ASCII");
79  		ParseState state = new TrailerState();
80  		
81  		ByteBuffer buffer = charset.encode("END\r\n");
82  		assertFalse(state.process(buffer, context));		
83  		assertEquals(5, buffer.position());
84  		
85  		buffer = charset.encode("END\r\nxy");
86  		assertTrue(state.process(buffer, context));		
87  		assertEquals(5, buffer.position());
88  		
89  		control.verify();
90  	}
91  	
92  	public void testProcessNonZeroPosition() throws Exception {
93  		MockControl control = MockControl.createControl(ParseStateContext.class);
94  		control.setDefaultMatcher(MockControl.ARRAY_MATCHER);
95  		ParseStateContext context = (ParseStateContext) control.getMock();
96  		
97  		context.handleTrailer();
98  		control.replay();
99  
100 		Charset charset = Charset.forName("US-ASCII");
101 		ParseState state = new TrailerState();
102 		
103 		ByteBuffer buffer = charset.encode("abcdefgEND\r\n");
104 		buffer.position(7);
105 		assertFalse(state.process(buffer, context));
106 		assertEquals(12, buffer.position());
107 		
108 		control.verify();
109 	}
110 
111 }