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  package org.apache.james.util;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.OutputStream;
24  
25  import junit.framework.TestCase;
26  
27  /***
28   * Tests for the ExtraDotOutputStream
29   */
30  public class ExtraDotOutputStreamTest extends TestCase {
31  
32      public void testMain() throws IOException {
33          String data = ".This is a test\r\nof the thing.\r\nWe should not have much trouble.\r\n.doubled?\r\nor not?\n.doubled\nor not?\r\n\r\n\n\n\r\r\r\n";
34          ByteArrayOutputStream bOut = new ByteArrayOutputStream();
35          OutputStream os = new ExtraDotOutputStream(bOut);
36          os.write(data.getBytes());
37          os.flush();
38          String expected = "..This is a test\r\nof the thing.\r\nWe should not have much trouble.\r\n..doubled?\r\nor not?\r\n..doubled\r\nor not?\r\n\r\n\r\n\r\n\r\n\r\n\r\n";
39          assertEquals(expected,bOut.toString());
40      }
41  
42      
43  
44  
45      public void testCheckCRLFTerminator() throws IOException {
46          ByteArrayOutputStream bOut = new ByteArrayOutputStream();
47          ExtraDotOutputStream os = new ExtraDotOutputStream(bOut);
48          
49          os.checkCRLFTerminator();
50          os.flush();
51          assertEquals("",bOut.toString());
52          os.write("Test".getBytes());
53          os.flush();
54          assertEquals("Test",bOut.toString());
55          os.checkCRLFTerminator();
56          os.flush();
57          assertEquals("Test\r\n",bOut.toString());
58          
59          os.write("A line with incomplete ending\r".getBytes());
60          os.flush();
61          assertEquals("Test\r\nA line with incomplete ending\r",bOut.toString());
62          os.checkCRLFTerminator();
63          os.flush();
64          assertEquals("Test\r\nA line with incomplete ending\r\n",bOut.toString());
65          
66          os.checkCRLFTerminator();
67          os.flush();
68          assertEquals("Test\r\nA line with incomplete ending\r\n",bOut.toString());
69      }
70  
71  }