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

Revision 6785, 4.9 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.storage;
21
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24
25import junit.framework.TestCase;
26
27import org.apache.james.mime4j.dom.SingleBody;
28import org.apache.james.mime4j.message.MessageImpl;
29import org.apache.james.mime4j.util.CharsetUtil;
30
31public class SingleBodyCopyTest extends TestCase {
32
33    public void testCopyStorageBinaryBody() throws Exception {
34        Storage storage = new MemoryStorageProvider()
35                .store(new ByteArrayInputStream("test".getBytes()));
36        MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage(
37                storage);
38        SingleBody body = new StorageBinaryBody(multiReferenceStorage);
39        copyTest(body);
40    }
41
42    public void testCopyStorageTextBody() throws Exception {
43        Storage storage = new MemoryStorageProvider()
44                .store(new ByteArrayInputStream("test".getBytes()));
45        MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage(
46                storage);
47        SingleBody body = new StorageTextBody(multiReferenceStorage,
48                CharsetUtil.US_ASCII);
49        copyTest(body);
50    }
51
52    public void testCopyStringTextBody() throws Exception {
53        SingleBody body = new StringTextBody("test", CharsetUtil.US_ASCII);
54        copyTest(body);
55    }
56
57    public void testDisposeStorageBinaryBody() throws Exception {
58        Storage storage = new MemoryStorageProvider()
59                .store(new ByteArrayInputStream("test".getBytes()));
60        MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage(
61                storage);
62        SingleBody body = new StorageBinaryBody(multiReferenceStorage);
63        disposeTest(body, storage);
64    }
65
66    public void testDisposeStorageTextBody() throws Exception {
67        Storage storage = new MemoryStorageProvider()
68                .store(new ByteArrayInputStream("test".getBytes()));
69        MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage(
70                storage);
71        SingleBody body = new StorageTextBody(multiReferenceStorage,
72                CharsetUtil.US_ASCII);
73        disposeTest(body, storage);
74    }
75
76    private void copyTest(SingleBody body) throws Exception {
77        MessageImpl parent = new MessageImpl();
78        parent.setBody(body);
79
80        SingleBody copy = body.copy();
81        assertNotNull(copy);
82        assertNotSame(body, copy);
83
84        assertSame(parent, body.getParent());
85        assertNull(copy.getParent());
86
87        sameContentTest(body, copy);
88    }
89
90    private void sameContentTest(SingleBody expectedBody, SingleBody actualBody)
91            throws Exception {
92        ByteArrayOutputStream expBaos = new ByteArrayOutputStream();
93        expectedBody.writeTo(expBaos);
94        byte[] expected = expBaos.toByteArray();
95
96        ByteArrayOutputStream actBaos = new ByteArrayOutputStream();
97        actualBody.writeTo(actBaos);
98        byte[] actual = actBaos.toByteArray();
99
100        assertEquals(expected.length, actual.length);
101        for (int i = 0; i < expected.length; i++) {
102            assertEquals(expected[i], actual[i]);
103        }
104    }
105
106    private void disposeTest(SingleBody body, Storage storage) throws Exception {
107        assertTrue(storageIsReadable(storage));
108
109        SingleBody copy = body.copy();
110        assertTrue(storageIsReadable(storage));
111
112        body.dispose();
113        assertTrue(storageIsReadable(storage));
114
115        copy.dispose();
116        assertFalse(storageIsReadable(storage));
117    }
118
119    private boolean storageIsReadable(Storage storage) throws Exception {
120        try {
121            storage.getInputStream().close();
122            return true;
123        } catch (IllegalStateException e) {
124            return false;
125        }
126    }
127
128}
Note: See TracBrowser for help on using the repository browser.