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.mime4j.codec; |
---|
21 | |
---|
22 | import java.io.ByteArrayOutputStream; |
---|
23 | import java.io.IOException; |
---|
24 | import java.io.UnsupportedEncodingException; |
---|
25 | |
---|
26 | import junit.framework.TestCase; |
---|
27 | |
---|
28 | public class QuotedPrintableOutputStreamTest extends TestCase { |
---|
29 | |
---|
30 | public void testEncode() throws IOException { |
---|
31 | ByteArrayOutputStream bos = null; |
---|
32 | QuotedPrintableOutputStream encoder = null; |
---|
33 | |
---|
34 | /* |
---|
35 | * Simple initial test. |
---|
36 | */ |
---|
37 | bos = new ByteArrayOutputStream(); |
---|
38 | encoder = new QuotedPrintableOutputStream(bos, false); |
---|
39 | encoder.write(fromString("This is the plain text message containing a few euros: 100 \u20ac!")); |
---|
40 | encoder.close(); |
---|
41 | assertEquals("This is the plain text message containing a few euros: 100 =E2=82=AC!", |
---|
42 | toString(bos.toByteArray())); |
---|
43 | } |
---|
44 | |
---|
45 | public void testEncodeUnderlyingStreamStaysOpen() throws IOException { |
---|
46 | ByteArrayOutputStream bos = null; |
---|
47 | QuotedPrintableOutputStream encoder = null; |
---|
48 | |
---|
49 | bos = new ByteArrayOutputStream(); |
---|
50 | encoder = new QuotedPrintableOutputStream(bos, false); |
---|
51 | encoder.write(fromString("This is the plain text message containing a few euros: 100 \u20ac!")); |
---|
52 | encoder.close(); |
---|
53 | |
---|
54 | try { |
---|
55 | encoder.write('b'); |
---|
56 | fail(); |
---|
57 | } catch (IOException expected) { |
---|
58 | } |
---|
59 | |
---|
60 | bos.write('y'); |
---|
61 | bos.write('a'); |
---|
62 | bos.write('d'); |
---|
63 | bos.write('a'); |
---|
64 | assertEquals("This is the plain text message containing a few euros: 100 =E2=82=AC!yada", |
---|
65 | toString(bos.toByteArray())); |
---|
66 | } |
---|
67 | |
---|
68 | private byte[] fromString(String s) { |
---|
69 | try { |
---|
70 | return s.getBytes("UTF-8"); |
---|
71 | } catch (UnsupportedEncodingException e) { |
---|
72 | fail(e.getMessage()); |
---|
73 | return null; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | private String toString(byte[] b) { |
---|
78 | try { |
---|
79 | return new String(b, "US-ASCII"); |
---|
80 | } catch (UnsupportedEncodingException e) { |
---|
81 | fail(e.getMessage()); |
---|
82 | return null; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|