source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/core/src/test/java/org/apache/james/mime4j/io/EOLConvertingInputStreamTest.java @ 6785

Revision 6785, 4.7 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
20package org.apache.james.mime4j.io;
21
22import java.io.ByteArrayInputStream;
23import java.io.IOException;
24import java.io.UnsupportedEncodingException;
25
26import org.apache.james.mime4j.io.EOLConvertingInputStream;
27
28
29import junit.framework.TestCase;
30
31public class EOLConvertingInputStreamTest extends TestCase {
32
33    public void testRead() throws IOException {
34        testConvertBoth("Line 1\r\nLine 2\r\n", "Line 1\r\nLine 2\r\n");
35        testConvertCR("Line 1\r\nLine 2\r\n", "Line 1\r\nLine 2\r\n");
36        testConvertLF("Line 1\r\nLine 2\r\n", "Line 1\r\nLine 2\r\n");
37       
38        testConvertBoth("Line 1\n\rLine 2\n\r", "Line 1\r\n\r\nLine 2\r\n\r\n");
39        testConvertCR("Line 1\n\rLine 2\n\r", "Line 1\n\r\nLine 2\n\r\n");
40        testConvertLF("Line 1\n\rLine 2\n\r", "Line 1\r\n\rLine 2\r\n\r");
41       
42        testConvertBoth("Line 1\nLine 2\n", "Line 1\r\nLine 2\r\n");
43        testConvertCR("Line 1\nLine 2\n", "Line 1\nLine 2\n");
44        testConvertLF("Line 1\nLine 2\n", "Line 1\r\nLine 2\r\n");
45       
46        testConvertBoth("Line 1\rLine 2\r", "Line 1\r\nLine 2\r\n");
47        testConvertCR("Line 1\rLine 2\r", "Line 1\r\nLine 2\r\n");
48        testConvertLF("Line 1\rLine 2\r", "Line 1\rLine 2\r");
49       
50        testConvertBoth("\r\n", "\r\n");
51        testConvertCR("\r\n", "\r\n");
52        testConvertLF("\r\n", "\r\n");
53       
54        testConvertBoth("\n", "\r\n");
55        testConvertCR("\n", "\n");
56        testConvertLF("\n", "\r\n");
57       
58        testConvertBoth("\r", "\r\n");
59        testConvertCR("\r", "\r\n");
60        testConvertLF("\r", "\r");
61       
62        testConvertBoth("", "");
63        testConvertCR("", "");
64        testConvertLF("", "");
65    }
66
67    private void testConvertBoth(String s1, String s2) throws IOException {
68        byte[] bytes = new byte[1024];
69       
70        ByteArrayInputStream bais = new ByteArrayInputStream(fromString(s1));
71        EOLConvertingInputStream in =
72            new EOLConvertingInputStream(bais,
73                        EOLConvertingInputStream.CONVERT_BOTH);
74        int n = in.read(bytes);
75        assertEquals(s2, toString(bytes, n));
76    }
77   
78    private void testConvertCR(String s1, String s2) throws IOException {
79        byte[] bytes = new byte[1024];
80       
81        ByteArrayInputStream bais = new ByteArrayInputStream(fromString(s1));
82        EOLConvertingInputStream in =
83            new EOLConvertingInputStream(bais,
84                        EOLConvertingInputStream.CONVERT_CR);
85        int n = in.read(bytes);
86        assertEquals(s2, toString(bytes, n));
87    }
88   
89    private void testConvertLF(String s1, String s2) throws IOException {
90        byte[] bytes = new byte[1024];
91       
92        ByteArrayInputStream bais = new ByteArrayInputStream(fromString(s1));
93        EOLConvertingInputStream in =
94            new EOLConvertingInputStream(bais,
95                        EOLConvertingInputStream.CONVERT_LF);
96        int n = in.read(bytes);
97        assertEquals(s2, toString(bytes, n));
98    }
99   
100    private String toString(byte[] b, int len) {
101        try {
102            if (len == -1) {
103                return "";
104            }
105            return new String(b, 0, len, "US-ASCII");
106        } catch (UnsupportedEncodingException e) {
107            fail(e.getMessage());
108            return null;
109        }
110    }   
111   
112    private byte[] fromString(String s) {
113        try {
114            return s.getBytes("US-ASCII");
115        } catch (UnsupportedEncodingException e) {
116            fail(e.getMessage());
117            return null;
118        }
119    }   
120}
Note: See TracBrowser for help on using the repository browser.