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 PayloadStateTest extends TestCase {
26  	
27  	private Charset charset = Charset.forName("US-ASCII");
28  	
29  	public void testProcess() throws Exception {
30  		MockControl control = MockControl.createControl(ParseStateContext.class);
31  		ParseStateContext context = (ParseStateContext) control.getMock();
32  		
33  		context.handlePayload(charset.encode("123456789"));
34  		control.replay();
35  		
36  		Charset charset = Charset.forName("US-ASCII");
37  		ByteBuffer buffer = charset.encode("1234567890");
38  		
39  		ParseState state = new PayloadState(9);
40  		assertTrue(state.process(buffer, context));
41  		
42  		assertEquals(9, buffer.position());
43  		
44  		control.verify();
45  	}
46  	
47  	public void testProcessTwice() throws Exception {
48  		MockControl control = MockControl.createControl(ParseStateContext.class);
49  		ParseStateContext context = (ParseStateContext) control.getMock();
50  		
51  		context.handlePayload(charset.encode("123456789"));
52  		control.replay();
53  		
54  		Charset charset = Charset.forName("US-ASCII");
55  		
56  		ParseState state = new PayloadState(9);
57  		
58  		ByteBuffer buffer = charset.encode("12345");
59  		assertFalse(state.process(buffer, context));
60  		assertEquals(5, buffer.position());
61  		
62  		buffer = charset.encode("67890");
63  		assertTrue(state.process(buffer, context));
64  		assertEquals(4, buffer.position());
65  		
66  		control.verify();
67  	}
68  	
69  }