KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > ExtraDotOutputStreamTest


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.james.util;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import junit.framework.TestCase;
27
28 /**
29  * Tests for the ExtraDotOutputStream
30  */

31 public class ExtraDotOutputStreamTest extends TestCase {
32
33     public void testMain() throws IOException JavaDoc {
34         String JavaDoc 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";
35         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
36         OutputStream JavaDoc os = new ExtraDotOutputStream(bOut);
37         os.write(data.getBytes());
38         os.flush();
39         String JavaDoc 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";
40         assertEquals(expected,bOut.toString());
41     }
42
43     /*
44      * Test method for 'org.apache.james.util.ExtraDotOutputStream.checkCRLFTerminator()'
45      */

46     public void testCheckCRLFTerminator() throws IOException JavaDoc {
47         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
48         ExtraDotOutputStream os = new ExtraDotOutputStream(bOut);
49         // if the stream is empty then we should not add the CRLF.
50
os.checkCRLFTerminator();
51         os.flush();
52         assertEquals("",bOut.toString());
53         os.write("Test".getBytes());
54         os.flush();
55         assertEquals("Test",bOut.toString());
56         os.checkCRLFTerminator();
57         os.flush();
58         assertEquals("Test\r\n",bOut.toString());
59         // if the stream ends with \r we should simply add the \n
60
os.write("A line with incomplete ending\r".getBytes());
61         os.flush();
62         assertEquals("Test\r\nA line with incomplete ending\r",bOut.toString());
63         os.checkCRLFTerminator();
64         os.flush();
65         assertEquals("Test\r\nA line with incomplete ending\r\n",bOut.toString());
66         // already correctly terminated, should leave the output untouched
67
os.checkCRLFTerminator();
68         os.flush();
69         assertEquals("Test\r\nA line with incomplete ending\r\n",bOut.toString());
70     }
71
72 }
73
Popular Tags