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

Revision 6785, 5.1 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;
24import java.io.IOException;
25
26import org.apache.james.mime4j.codec.CodecUtil;
27
28import junit.framework.TestCase;
29
30public class StorageProviderTest extends TestCase {
31
32    public void testMemoryStorageProvider() throws Exception {
33        StorageProvider provider = new MemoryStorageProvider();
34
35        testReadWrite(provider, 0);
36        testReadWrite(provider, 1);
37        testReadWrite(provider, 1024);
38        testReadWrite(provider, 20000);
39
40        testDelete(provider);
41    }
42
43    public void testTempFileStorageProvider() throws Exception {
44        StorageProvider provider = new TempFileStorageProvider();
45
46        testReadWrite(provider, 0);
47        testReadWrite(provider, 1);
48        testReadWrite(provider, 1024);
49        testReadWrite(provider, 20000);
50
51        testDelete(provider);
52    }
53
54    public void testThresholdStorageProvider() throws Exception {
55        final int threshold = 5000;
56        StorageProvider backend = new TempFileStorageProvider();
57        StorageProvider provider = new ThresholdStorageProvider(backend,
58                threshold);
59
60        testReadWrite(provider, 0);
61        testReadWrite(provider, 1);
62        testReadWrite(provider, threshold - 1);
63        testReadWrite(provider, threshold);
64        testReadWrite(provider, threshold + 1);
65        testReadWrite(provider, 2 * threshold);
66        testReadWrite(provider, 10 * threshold);
67
68        testDelete(provider);
69    }
70
71    public void testCipherStorageProvider() throws Exception {
72        StorageProvider backend = new TempFileStorageProvider();
73        StorageProvider provider = new CipherStorageProvider(backend);
74
75        testReadWrite(provider, 0);
76        testReadWrite(provider, 1);
77        testReadWrite(provider, 1024);
78        testReadWrite(provider, 20000);
79
80        testDelete(provider);
81    }
82
83    private void testReadWrite(StorageProvider provider, int size)
84            throws IOException {
85        testStore(provider, size);
86        testCreateStorageOutputStream(provider, size);
87    }
88
89    private void testStore(StorageProvider provider, int size)
90            throws IOException {
91        byte[] data = createData(size);
92        assertEquals(size, data.length);
93
94        Storage storage = provider.store(new ByteArrayInputStream(data));
95
96        ByteArrayOutputStream baos = new ByteArrayOutputStream();
97        CodecUtil.copy(storage.getInputStream(), baos);
98        verifyData(data, baos.toByteArray());
99    }
100
101    private void testCreateStorageOutputStream(StorageProvider provider,
102            int size) throws IOException {
103        byte[] data = createData(size);
104        assertEquals(size, data.length);
105
106        StorageOutputStream out = provider.createStorageOutputStream();
107        CodecUtil.copy(new ByteArrayInputStream(data), out);
108        Storage storage = out.toStorage();
109
110        ByteArrayOutputStream baos = new ByteArrayOutputStream();
111        CodecUtil.copy(storage.getInputStream(), baos);
112        verifyData(data, baos.toByteArray());
113    }
114
115    private void verifyData(byte[] expected, byte[] actual) {
116        assertEquals(expected.length, actual.length);
117        for (int i = 0; i < expected.length; i++) {
118            assertEquals(expected[i], actual[i]);
119        }
120    }
121
122    private byte[] createData(int size) {
123        byte[] data = new byte[size];
124        for (int i = 0; i < size; i++) {
125            data[i] = (byte) i;
126        }
127        return data;
128    }
129
130    private void testDelete(StorageProvider provider) throws IOException {
131        Storage storage = provider.store(new ByteArrayInputStream(
132                createData(512)));
133
134        storage.delete();
135
136        // getInputStream has to throw an IllegalStateException
137        try {
138            storage.getInputStream();
139            fail();
140        } catch (IllegalStateException expected) {
141        }
142
143        // invoking delete a second time should not have any effect
144        storage.delete();
145    }
146
147}
Note: See TracBrowser for help on using the repository browser.