View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.io;
19  
20  import java.io.IOException;
21  import java.io.StringReader;
22  
23  import junit.framework.TestCase;
24  
25  public class DotTerminatedMessageReaderTest extends TestCase {
26  
27      private DotTerminatedMessageReader reader;
28      private final StringBuilder str = new StringBuilder();
29      private final char[] buf = new char[64];
30      private static final String CRLF = "\r\n";
31      private static final String DOT = ".";
32      private static final String EOM = CRLF+DOT+CRLF;
33  
34      public void testReadSimpleStringCrLfLineEnding() throws IOException {
35          final String test = "Hello World!"+EOM;
36          reader = new DotTerminatedMessageReader(new StringReader(test));
37  
38          int read = 0;
39          while ((read = reader.read(buf)) != -1) {
40              str.append(buf, 0, read);
41          }
42  
43          assertEquals("Hello World!"+CRLF, str.toString());
44      }
45  
46      public void testReadSimpleStringLfLineEnding() throws IOException {
47          final String test = "Hello World!"+EOM;
48          reader = new DotTerminatedMessageReader(new StringReader(test));
49  
50          int read = 0;
51          while ((read = reader.read(buf)) != -1) {
52              str.append(buf, 0, read);
53          }
54  
55          assertEquals("Hello World!"+CRLF, str.toString());
56      }
57  
58      public void testEmbeddedNewlines() throws IOException {
59          final String test = "Hello"+CRLF+"World\nA\rB"+EOM;
60          reader = new DotTerminatedMessageReader(new StringReader(test));
61  
62          int read = 0;
63          while ((read = reader.read(buf)) != -1) {
64              str.append(buf, 0, read);
65          }
66  
67          assertEquals(str.toString(), "Hello" + CRLF  +"World\nA\rB" + CRLF);
68      }
69  
70      public void testDoubleCrBeforeDot() throws IOException {
71          final String test = "Hello World!\r"+EOM;
72          reader = new DotTerminatedMessageReader(new StringReader(test));
73  
74          int read = 0;
75          while ((read = reader.read(buf)) != -1) {
76              str.append(buf, 0, read);
77          }
78  
79          assertEquals("Hello World!\r" + CRLF,str.toString());
80      }
81  
82      public void testLeadingDot() throws IOException {
83          final String test = "Hello World!"+CRLF+"..text"+EOM;
84          reader = new DotTerminatedMessageReader(new StringReader(test));
85  
86          int read = 0;
87          while ((read = reader.read(buf)) != -1) {
88              str.append(buf, 0, read);
89          }
90  
91          assertEquals("Hello World!" + CRLF+".text"+CRLF,str.toString());
92      }
93  
94      public void testEmbeddedDot1() throws IOException {
95          final String test = "Hello . World!"+EOM;
96          reader = new DotTerminatedMessageReader(new StringReader(test));
97  
98          int read = 0;
99          while ((read = reader.read(buf)) != -1) {
100             str.append(buf, 0, read);
101         }
102 
103         assertEquals("Hello . World!" + CRLF,str.toString());
104     }
105 
106     public void testEmbeddedDot2() throws IOException {
107         final String test = "Hello .. World!"+EOM;
108         reader = new DotTerminatedMessageReader(new StringReader(test));
109 
110         int read = 0;
111         while ((read = reader.read(buf)) != -1) {
112             str.append(buf, 0, read);
113         }
114 
115         assertEquals("Hello .. World!" + CRLF,str.toString());
116     }
117 
118     public void testEmbeddedDot3() throws IOException {
119         final String test = "Hello World."+CRLF+"more"+EOM;
120         reader = new DotTerminatedMessageReader(new StringReader(test));
121 
122         int read = 0;
123         while ((read = reader.read(buf)) != -1) {
124             str.append(buf, 0, read);
125         }
126 
127         assertEquals("Hello World." + CRLF+"more"+CRLF,str.toString());
128     }
129 
130     public void testEmbeddedDot4() throws IOException {
131         final String test = "Hello World\r.\nmore"+EOM;
132         reader = new DotTerminatedMessageReader(new StringReader(test));
133 
134         int read = 0;
135         while ((read = reader.read(buf)) != -1) {
136             str.append(buf, 0, read);
137         }
138 
139         assertEquals("Hello World\r.\nmore" + CRLF,str.toString());
140     }
141 
142     public void testReadLine1() throws Exception {
143         final String test = "Hello World"+CRLF+"more"+EOM;
144         reader = new DotTerminatedMessageReader(new StringReader(test));
145 
146         String line;
147         while ((line = reader.readLine()) != null) {
148             str.append(line);
149             str.append("#");
150         }
151 
152         assertEquals("Hello World#more#",str.toString());
153 
154     }
155 
156     public void testReadLine2() throws Exception {
157         final String test = "Hello World\r.\nmore"+EOM;
158         reader = new DotTerminatedMessageReader(new StringReader(test));
159 
160         String line;
161         while ((line = reader.readLine()) != null) {
162             str.append(line);
163             str.append("#");
164         }
165 
166         assertEquals("Hello World\r.\nmore#",str.toString());
167 
168     }
169 
170     public void testSingleDotWithTrailingText() throws IOException {
171         final String test = "Hello World!"+CRLF+".text"+EOM;
172         reader = new DotTerminatedMessageReader(new StringReader(test));
173 
174         int read = 0;
175         while ((read = reader.read(buf)) != -1) {
176             str.append(buf, 0, read);
177         }
178 
179         assertEquals("Hello World!"+CRLF+".text"+CRLF,str.toString());
180     }
181 
182 }