01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/package org.apache.james.util;
19:
20: import java.io.ByteArrayOutputStream;
21: import java.io.IOException;
22: import java.io.OutputStream;
23:
24: import junit.framework.TestCase;
25:
26: /**
27: * Tests for the ExtraDotOutputStream
28: */
29: public class ExtraDotOutputStreamTest extends TestCase {
30:
31: public void testMain() throws IOException {
32: 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";
33: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
34: OutputStream os = new ExtraDotOutputStream(bOut);
35: os.write(data.getBytes());
36: os.flush();
37: 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";
38: assertEquals(expected, bOut.toString());
39: }
40:
41: /*
42: * Test method for 'org.apache.james.util.ExtraDotOutputStream.checkCRLFTerminator()'
43: */
44: public void testCheckCRLFTerminator() throws IOException {
45: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
46: ExtraDotOutputStream os = new ExtraDotOutputStream(bOut);
47: // if the stream is empty then we should not add the CRLF.
48: os.checkCRLFTerminator();
49: os.flush();
50: assertEquals("", bOut.toString());
51: os.write("Test".getBytes());
52: os.flush();
53: assertEquals("Test", bOut.toString());
54: os.checkCRLFTerminator();
55: os.flush();
56: assertEquals("Test\r\n", bOut.toString());
57: // if the stream ends with \r we should simply add the \n
58: os.write("A line with incomplete ending\r".getBytes());
59: os.flush();
60: assertEquals("Test\r\nA line with incomplete ending\r", bOut
61: .toString());
62: os.checkCRLFTerminator();
63: os.flush();
64: assertEquals("Test\r\nA line with incomplete ending\r\n", bOut
65: .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
70: .toString());
71: }
72:
73: }
|