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 static final String CRLF = "\r\n";
28      private static final String DOT = ".";
29      private static final String EOM = CRLF + DOT + CRLF;
30      private DotTerminatedMessageReader reader;
31      private final StringBuilder str = new StringBuilder();
32      private final char[] buf = new char[64];
33  
34      public void testDoubleCrBeforeDot() throws IOException {
35          final String test = "Hello World!\r" + 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!\r" + CRLF, str.toString());
44      }
45  
46      public void testEmbeddedDot1() 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 testEmbeddedDot2() throws IOException {
59          final String test = "Hello .. World!" + 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("Hello .. World!" + CRLF, str.toString());
68      }
69  
70      public void testEmbeddedDot3() throws IOException {
71          final String test = "Hello World." + CRLF + "more" + 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." + CRLF + "more" + CRLF, str.toString());
80      }
81  
82      public void testEmbeddedDot4() throws IOException {
83          final String test = "Hello World\r.\nmore" + 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\r.\nmore" + CRLF, str.toString());
92      }
93  
94      public void testEmbeddedNewlines() throws IOException {
95          final String test = "Hello" + CRLF + "World\nA\rB" + 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(str.toString(), "Hello" + CRLF + "World\nA\rB" + CRLF);
104     }
105 
106     public void testLeadingDot() throws IOException {
107         final String test = "Hello World!" + CRLF + "..text" + 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 + ".text" + CRLF, str.toString());
116     }
117 
118     public void testReadLine1() throws Exception {
119         final String test = "Hello World" + CRLF + "more" + EOM;
120         reader = new DotTerminatedMessageReader(new StringReader(test));
121 
122         String line;
123         while ((line = reader.readLine()) != null) {
124             str.append(line);
125             str.append("#");
126         }
127 
128         assertEquals("Hello World#more#", str.toString());
129 
130     }
131 
132     public void testReadLine2() throws Exception {
133         final String test = "Hello World\r.\nmore" + EOM;
134         reader = new DotTerminatedMessageReader(new StringReader(test));
135 
136         String line;
137         while ((line = reader.readLine()) != null) {
138             str.append(line);
139             str.append("#");
140         }
141 
142         assertEquals("Hello World\r.\nmore#", str.toString());
143 
144     }
145 
146     public void testReadSimpleStringCrLfLineEnding() throws IOException {
147         final String test = "Hello World!" + EOM;
148         reader = new DotTerminatedMessageReader(new StringReader(test));
149 
150         int read = 0;
151         while ((read = reader.read(buf)) != -1) {
152             str.append(buf, 0, read);
153         }
154 
155         assertEquals("Hello World!" + CRLF, str.toString());
156     }
157 
158     public void testReadSimpleStringLfLineEnding() throws IOException {
159         final String test = "Hello World!" + EOM;
160         reader = new DotTerminatedMessageReader(new StringReader(test));
161 
162         int read = 0;
163         while ((read = reader.read(buf)) != -1) {
164             str.append(buf, 0, read);
165         }
166 
167         assertEquals("Hello World!" + CRLF, str.toString());
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 }