~ubuntu-branches/ubuntu/saucy/apache-mime4j/saucy

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/james/mime4j/message/SingleBodyCopyTest.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-07-13 09:28:28 UTC
  • Revision ID: james.westby@ubuntu.com-20100713092828-g6wafdtidgmtx7su
Tags: upstream-0.6
ImportĀ upstreamĀ versionĀ 0.6

Show diffs side-by-side

added added

removed removed

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