View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.codec.textline;
21  
22  import static org.junit.Assert.*;
23  
24  import java.nio.ByteBuffer;
25  
26  import org.apache.mina.codec.textline.TextLineDecoder.Context;
27  import org.junit.Test;
28  
29  /**
30   * A {@link TextLineDecoder} test.
31   * 
32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
33   */
34  public class WindowsTextLineDecoderTest {
35  
36      @Test
37      public void testThatEmptyBufferReturnsEmptyResult() {
38          TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
39          Context context = decoder.createDecoderState();
40          String results = decoder.decode(ByteBuffer.allocate(0), context);
41          assertNull(results);
42      }
43  
44      @Test
45      public void testThatNonLineTerminatedStringReturnsEmptyResult() {
46          TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
47          Context context = decoder.createDecoderState();
48          String results = decoder.decode(ByteBuffer.wrap("a string".getBytes()), context);
49          assertNull(results);
50          assertEquals(8, context.getBuffer().position());
51      }
52  
53      @Test
54      public void testThatUnixLineTerminatedStringReturnsEmptyResult() {
55          TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
56          Context context = decoder.createDecoderState();
57          String results = decoder.decode(ByteBuffer.wrap("a string\n".getBytes()), context);
58          assertNull(results);
59          assertEquals(9, context.getBuffer().position());
60      }
61  
62      @Test
63      public void testThatWindowsLineTerminatedStringReturnsNonEmptyResult() {
64          TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
65          Context context = decoder.createDecoderState();
66          String results = decoder.decode(ByteBuffer.wrap("a string\r\n".getBytes()), context);
67          assertNotNull(results);
68          assertEquals("a string", results);
69          assertEquals(0, context.getBuffer().position());
70      }
71  
72      @Test
73      public void testThatContextIsMaintainedBetweenMessages() {
74          TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
75          Context context = decoder.createDecoderState();
76          String results = decoder.decode(ByteBuffer.wrap("a string\r\na".getBytes()), context);
77          assertNotNull(results);
78          assertEquals("a string", results);
79          assertEquals(1, context.getBuffer().position());
80          results = decoder.decode(ByteBuffer.wrap(" string\r\n".getBytes()), context);
81          assertNotNull(results);
82          assertEquals("a string", results);
83          assertEquals(0, context.getBuffer().position());
84      }
85  
86      @Test
87      public void testThatUnixLineTerminatedLongStringReturnsEmptyResult() {
88          TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
89          Context context = decoder.createDecoderState();
90          StringBuffer sb = new StringBuffer();
91          for (int i = 0; i < 100; ++i) {
92              sb.append("a string");
93          }
94          String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\n").getBytes()), context);
95          assertNull(results);
96          assertEquals(801, context.getBuffer().position());
97      }
98  
99      @Test
100     public void testThatWindowsLineTerminatedLongStringReturnsNonEmptyResult() {
101         TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
102         Context context = decoder.createDecoderState();
103         StringBuffer sb = new StringBuffer();
104         for (int i = 0; i < 100; ++i) {
105             sb.append("a string");
106         }
107         String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\r\n").getBytes()), context);
108         assertNotNull(results);
109         assertEquals(sb.toString(), results);
110         assertEquals(0, context.getBuffer().position());
111     }
112 }