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.storage; |
---|
21 | |
---|
22 | import java.io.ByteArrayInputStream; |
---|
23 | import java.io.IOException; |
---|
24 | import java.io.InputStream; |
---|
25 | |
---|
26 | import junit.framework.TestCase; |
---|
27 | |
---|
28 | public class MultiReferenceStorageTest extends TestCase { |
---|
29 | |
---|
30 | public void testForwardsGetInputStream() throws Exception { |
---|
31 | DummyStorage storage = new DummyStorage(); |
---|
32 | MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage( |
---|
33 | storage); |
---|
34 | |
---|
35 | assertEquals(ByteArrayInputStream.class, multiReferenceStorage |
---|
36 | .getInputStream().getClass()); |
---|
37 | } |
---|
38 | |
---|
39 | public void testSingleReference() throws Exception { |
---|
40 | DummyStorage storage = new DummyStorage(); |
---|
41 | MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage( |
---|
42 | storage); |
---|
43 | |
---|
44 | assertFalse(storage.deleted); |
---|
45 | |
---|
46 | multiReferenceStorage.delete(); |
---|
47 | assertTrue(storage.deleted); |
---|
48 | } |
---|
49 | |
---|
50 | public void testMultiReference() throws Exception { |
---|
51 | DummyStorage storage = new DummyStorage(); |
---|
52 | MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage( |
---|
53 | storage); |
---|
54 | |
---|
55 | multiReferenceStorage.addReference(); |
---|
56 | |
---|
57 | multiReferenceStorage.delete(); |
---|
58 | assertFalse(storage.deleted); |
---|
59 | |
---|
60 | multiReferenceStorage.delete(); |
---|
61 | assertTrue(storage.deleted); |
---|
62 | } |
---|
63 | |
---|
64 | public void testGetInputStreamOnDeleted() throws Exception { |
---|
65 | DummyStorage storage = new DummyStorage(); |
---|
66 | MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage( |
---|
67 | storage); |
---|
68 | |
---|
69 | multiReferenceStorage.delete(); |
---|
70 | |
---|
71 | try { |
---|
72 | multiReferenceStorage.getInputStream(); |
---|
73 | fail(); |
---|
74 | } catch (IllegalStateException expected) { |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | public void testAddReferenceOnDeleted() throws Exception { |
---|
79 | DummyStorage storage = new DummyStorage(); |
---|
80 | MultiReferenceStorage multiReferenceStorage = new MultiReferenceStorage( |
---|
81 | storage); |
---|
82 | |
---|
83 | multiReferenceStorage.delete(); |
---|
84 | |
---|
85 | try { |
---|
86 | multiReferenceStorage.addReference(); |
---|
87 | fail(); |
---|
88 | } catch (IllegalStateException expected) { |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | private static final class DummyStorage implements Storage { |
---|
93 | public boolean deleted = false; |
---|
94 | |
---|
95 | public InputStream getInputStream() throws IOException { |
---|
96 | if (deleted) |
---|
97 | throw new IllegalStateException("deleted"); |
---|
98 | |
---|
99 | return new ByteArrayInputStream("dummy".getBytes()); |
---|
100 | } |
---|
101 | |
---|
102 | public void delete() { |
---|
103 | deleted = true; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | } |
---|