source: contrib/MailArchiver/sources/vendor/mime4j/custom/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableTextEncodeTest.java @ 6785

Revision 6785, 6.5 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.codec;
21
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24import java.io.InputStream;
25import java.nio.charset.Charset;
26import java.util.Arrays;
27
28import junit.framework.TestCase;
29
30import org.apache.commons.io.IOUtils;
31import org.apache.james.mime4j.util.CharsetUtil;
32
33public class QuotedPrintableTextEncodeTest extends TestCase {
34
35    @Override
36    protected void setUp() throws Exception {
37        super.setUp();
38    }
39
40    @Override
41    protected void tearDown() throws Exception {
42        super.tearDown();
43    }
44   
45    public void testEscapedSoftBreak() throws Exception {
46        byte[] content = new byte[500];
47        Arrays.fill(content, (byte)0x18);
48        byte[] expected = new byte[1557];
49        int index = 0;
50        for (int l=0;l<20;l++) {
51            for (int i=0;i<25;i++) {
52                expected[index++] = '=';
53                expected[index++] = '1';
54                expected[index++] = '8';
55            }
56            if (l<19) {
57                expected[index++] = '=';
58                expected[index++] = '\r';
59                expected[index++] = '\n';
60            }
61        }
62        check(content, expected);
63    }
64   
65    public void testPlainAsciiSoftBreak() throws Exception {
66        byte[] content = new byte[500];
67        Arrays.fill(content, (byte)0x29);
68        byte[] expected = new byte[518];
69        Arrays.fill(expected, (byte)0x29);
70        expected[75] = '=';
71        expected[76] = '\r';
72        expected[77] = '\n';
73        expected[153] = '=';
74        expected[154] = '\r';
75        expected[155] = '\n';
76        expected[231] = '=';
77        expected[232] = '\r';
78        expected[233] = '\n';
79        expected[309] = '=';
80        expected[310] = '\r';
81        expected[311] = '\n';
82        expected[387] = '=';
83        expected[388] = '\r';
84        expected[389] = '\n';
85        expected[465] = '=';
86        expected[466] = '\r';
87        expected[467] = '\n';
88        check(content, expected);
89    }
90   
91    public void testPlainASCII() throws Exception {
92        checkRoundtrip("Thisisaverysimplemessage.Thisisaverysimplemessage.Thisisaverysimplemessage." +
93                "Thisisaverysimplemessage.Thisisaverysimplemessage.Thisisaverysimplemessage." +
94                "Thisisaverysimplemessage.Thisisaverysimplemessage.Thisisaverysimplemessage." +
95                "Thisisaverysimplemessage.Thisisaverysimplemessage.Thisisaverysimplemessage.");
96    }
97   
98    public void testEncodeSpace() throws Exception {
99        checkRoundtrip("                 A");
100    }
101   
102    public void testLetterEncoding() throws Exception {
103        for (byte b=0;b<Byte.MAX_VALUE;b++) {
104            byte[] content = {b};
105            // White space is only escaped when followed by CRLF
106            if (b != 10 && b != 13 && b != 32 && b != 9) {
107                checkRoundtrip(content);
108            }
109        }
110    }
111   
112    public void testCRLFShouldResetLineCount() throws Exception {
113        StringBuilder buffer = new StringBuilder(4096);
114        for (int i=0;i<1000;i++) {
115            buffer.append("Hugo\r\n");
116        }
117        String longLine = buffer.toString();
118        check(longLine, longLine);
119    }
120   
121    public void testDontEscapeLF() throws Exception {
122        check("Ready\nFor\n", "Ready\nFor\n");
123    }
124   
125    public void testDontEscapeCR() throws Exception {
126        check("Ready\rFor\r", "Ready\rFor\r");
127    }
128   
129    public void testEscapeSpaceAtLineEnd() throws Exception {
130        check("      \r\n", "     =20\r\n");
131    }
132   
133    public void testDontEscapeSpaceBeforeLineEnd() throws Exception {
134        check("      ", "      ");
135    }
136   
137    public void testDontEscapeTabsBeforeLineEnd() throws Exception {
138        check("\t\t\t\t", "\t\t\t\t");
139    }
140   
141    public void testDontWhiteSpaceBeforeLineEnd() throws Exception {
142        check("  \t\t  \t", "  \t\t  \t");
143    }
144   
145    private void checkRoundtrip(String content) throws Exception {
146        checkRoundtrip(content, CharsetUtil.US_ASCII);
147    }
148
149    private void checkRoundtrip(String content, Charset charset) throws Exception {
150        checkRoundtrip(charset.encode(content).array());
151    }
152   
153    private void checkRoundtrip(byte[] content) throws Exception {
154        InputStream in = new ByteArrayInputStream(content);
155        ByteArrayOutputStream out = new ByteArrayOutputStream();
156        CodecUtil.encodeQuotedPrintable(in, out);
157        // read back through decoder
158        in = new QuotedPrintableInputStream(new ByteArrayInputStream(out.toByteArray()));
159        out = new ByteArrayOutputStream();
160        IOUtils.copy(in, out);
161        assertEquals(content, out.toByteArray());
162    }
163   
164    private void check(String content, String expected) throws Exception {
165        Charset ascii = CharsetUtil.US_ASCII;
166        check(ascii.encode(content).array(), ascii.encode(expected).array());
167    }
168
169   
170    private void check(byte[] content, byte[] expected) throws Exception {
171        ByteArrayInputStream in = new ByteArrayInputStream(content);
172        ByteArrayOutputStream out = new ByteArrayOutputStream();
173        CodecUtil.encodeQuotedPrintable(in, out);
174        assertEquals(expected, out.toByteArray());
175    }
176   
177    private void assertEquals(byte[] expected, byte[] actual) {
178        assertEquals(expected.length, actual.length);
179        for (int i = 0; i < actual.length; i++) {
180            assertEquals("Mismatch@" + i, expected[i], actual[i]);
181        }
182    }
183}
Note: See TracBrowser for help on using the repository browser.