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.dom; |
---|
21 | |
---|
22 | import org.apache.james.mime4j.dom.Body; |
---|
23 | import org.apache.james.mime4j.dom.Entity; |
---|
24 | import org.apache.james.mime4j.dom.Header; |
---|
25 | import org.apache.james.mime4j.field.DefaultFieldParser; |
---|
26 | import org.apache.james.mime4j.message.BasicBodyFactory; |
---|
27 | import org.apache.james.mime4j.message.BodyPart; |
---|
28 | import org.apache.james.mime4j.message.HeaderImpl; |
---|
29 | |
---|
30 | import junit.framework.TestCase; |
---|
31 | |
---|
32 | public class EntityTest extends TestCase { |
---|
33 | |
---|
34 | public void testSetBody() throws Exception { |
---|
35 | Entity entity = new BodyPart(); |
---|
36 | assertNull(entity.getBody()); |
---|
37 | |
---|
38 | Body body = new BasicBodyFactory().textBody("test"); |
---|
39 | assertNull(body.getParent()); |
---|
40 | |
---|
41 | entity.setBody(body); |
---|
42 | assertSame(body, entity.getBody()); |
---|
43 | assertSame(entity, body.getParent()); |
---|
44 | } |
---|
45 | |
---|
46 | public void testSetBodyTwice() throws Exception { |
---|
47 | Entity entity = new BodyPart(); |
---|
48 | |
---|
49 | Body b1 = new BasicBodyFactory().textBody("foo"); |
---|
50 | Body b2 = new BasicBodyFactory().textBody("bar"); |
---|
51 | |
---|
52 | entity.setBody(b1); |
---|
53 | try { |
---|
54 | entity.setBody(b2); |
---|
55 | fail(); |
---|
56 | } catch (IllegalStateException expected) { |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | public void testRemoveBody() throws Exception { |
---|
61 | Entity entity = new BodyPart(); |
---|
62 | Body body = new BasicBodyFactory().textBody("test"); |
---|
63 | entity.setBody(body); |
---|
64 | |
---|
65 | Body removed = entity.removeBody(); |
---|
66 | assertSame(body, removed); |
---|
67 | |
---|
68 | assertNull(entity.getBody()); |
---|
69 | assertNull(removed.getParent()); |
---|
70 | } |
---|
71 | |
---|
72 | public void testGetDispositionType() throws Exception { |
---|
73 | BodyPart entity = new BodyPart(); |
---|
74 | |
---|
75 | assertNull(entity.getDispositionType()); |
---|
76 | |
---|
77 | Header header = new HeaderImpl(); |
---|
78 | header.setField(DefaultFieldParser.parse("Content-Disposition: inline")); |
---|
79 | entity.setHeader(header); |
---|
80 | |
---|
81 | assertEquals("inline", entity.getDispositionType()); |
---|
82 | } |
---|
83 | |
---|
84 | public void testSetContentDispositionType() throws Exception { |
---|
85 | BodyPart entity = new BodyPart(); |
---|
86 | |
---|
87 | entity.setContentDisposition("attachment"); |
---|
88 | |
---|
89 | assertEquals("attachment", entity.getHeader().getField( |
---|
90 | "Content-Disposition").getBody()); |
---|
91 | } |
---|
92 | |
---|
93 | public void testSetContentDispositionTypeFilename() throws Exception { |
---|
94 | BodyPart entity = new BodyPart(); |
---|
95 | |
---|
96 | entity.setContentDisposition("attachment", "some file.dat"); |
---|
97 | |
---|
98 | assertEquals("attachment; filename=\"some file.dat\"", entity |
---|
99 | .getHeader().getField("Content-Disposition").getBody()); |
---|
100 | } |
---|
101 | |
---|
102 | public void testGetFilename() throws Exception { |
---|
103 | BodyPart entity = new BodyPart(); |
---|
104 | |
---|
105 | assertNull(entity.getFilename()); |
---|
106 | |
---|
107 | Header header = new HeaderImpl(); |
---|
108 | header.setField(DefaultFieldParser.parse("Content-Disposition: attachment; " |
---|
109 | + "filename=\"some file.dat\"")); |
---|
110 | entity.setHeader(header); |
---|
111 | |
---|
112 | assertEquals("some file.dat", entity.getFilename()); |
---|
113 | } |
---|
114 | |
---|
115 | public void testSetFilename() throws Exception { |
---|
116 | BodyPart entity = new BodyPart(); |
---|
117 | |
---|
118 | entity.setFilename("file name.ext"); |
---|
119 | |
---|
120 | assertEquals("attachment; filename=\"file name.ext\"", entity |
---|
121 | .getHeader().getField("Content-Disposition").getBody()); |
---|
122 | |
---|
123 | entity.setFilename(null); |
---|
124 | |
---|
125 | assertEquals("attachment", entity.getHeader().getField( |
---|
126 | "Content-Disposition").getBody()); |
---|
127 | } |
---|
128 | |
---|
129 | } |
---|