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 *
10
* http://www.apache.org/licenses/LICENSE-2.0 *
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
****************************************************************/
20
package org.apache.james.mime4j.message;
22
import java.io.ByteArrayInputStream;
23
import java.io.ByteArrayOutputStream;
25
import junit.framework.TestCase;
27
import org.apache.james.mime4j.storage.MemoryStorageProvider;
28
import org.apache.james.mime4j.storage.MultiReferenceStorage;
29
import org.apache.james.mime4j.storage.Storage;
30
import org.apache.james.mime4j.util.CharsetUtil;
32
public class SingleBodyCopyTest extends TestCase {
34
public void testCopyStorageBinaryBody() throws Exception {
35
Storage storage = new MemoryStorageProvider()
36
.store(new ByteArrayInputStream("test".getBytes()));
37
MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage(
39
SingleBody body = new StorageBinaryBody(multiReferenceStorage);
43
public void testCopyStorageTextBody() throws Exception {
44
Storage storage = new MemoryStorageProvider()
45
.store(new ByteArrayInputStream("test".getBytes()));
46
MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage(
48
SingleBody body = new StorageTextBody(multiReferenceStorage,
49
CharsetUtil.US_ASCII);
53
public void testCopyStringTextBody() throws Exception {
54
SingleBody body = new StringTextBody("test", CharsetUtil.US_ASCII);
58
public void testDisposeStorageBinaryBody() throws Exception {
59
Storage storage = new MemoryStorageProvider()
60
.store(new ByteArrayInputStream("test".getBytes()));
61
MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage(
63
SingleBody body = new StorageBinaryBody(multiReferenceStorage);
64
disposeTest(body, storage);
67
public void testDisposeStorageTextBody() throws Exception {
68
Storage storage = new MemoryStorageProvider()
69
.store(new ByteArrayInputStream("test".getBytes()));
70
MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage(
72
SingleBody body = new StorageTextBody(multiReferenceStorage,
73
CharsetUtil.US_ASCII);
74
disposeTest(body, storage);
77
private void copyTest(SingleBody body) throws Exception {
78
Message parent = new Message();
81
SingleBody copy = body.copy();
83
assertNotSame(body, copy);
85
assertSame(parent, body.getParent());
86
assertNull(copy.getParent());
88
sameContentTest(body, copy);
91
private void sameContentTest(SingleBody expectedBody, SingleBody actualBody)
93
ByteArrayOutputStream expBaos = new ByteArrayOutputStream();
94
expectedBody.writeTo(expBaos);
95
byte[] expected = expBaos.toByteArray();
97
ByteArrayOutputStream actBaos = new ByteArrayOutputStream();
98
actualBody.writeTo(actBaos);
99
byte[] actual = actBaos.toByteArray();
101
assertEquals(expected.length, actual.length);
102
for (int i = 0; i < expected.length; i++) {
103
assertEquals(expected[i], actual[i]);
107
private void disposeTest(SingleBody body, Storage storage) throws Exception {
108
assertTrue(storageIsReadable(storage));
110
SingleBody copy = body.copy();
111
assertTrue(storageIsReadable(storage));
114
assertTrue(storageIsReadable(storage));
117
assertFalse(storageIsReadable(storage));
120
private boolean storageIsReadable(Storage storage) throws Exception {
122
storage.getInputStream().close();
124
} catch (IllegalStateException e) {