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.field; |
---|
21 | |
---|
22 | import java.util.Date; |
---|
23 | |
---|
24 | import junit.framework.TestCase; |
---|
25 | |
---|
26 | import org.apache.james.mime4j.dom.field.ContentDispositionField; |
---|
27 | import org.apache.james.mime4j.field.DefaultFieldParser; |
---|
28 | |
---|
29 | public class ContentDispositionFieldTest extends TestCase { |
---|
30 | |
---|
31 | public void testDispositionTypeWithSemiColonNoParams() throws Exception { |
---|
32 | ContentDispositionField f = null; |
---|
33 | |
---|
34 | f = (ContentDispositionField) DefaultFieldParser |
---|
35 | .parse("Content-Disposition: inline;"); |
---|
36 | assertEquals("inline", f.getDispositionType()); |
---|
37 | } |
---|
38 | |
---|
39 | public void testGetDispositionType() throws Exception { |
---|
40 | ContentDispositionField f = null; |
---|
41 | |
---|
42 | f = (ContentDispositionField) DefaultFieldParser |
---|
43 | .parse("Content-Disposition: attachment"); |
---|
44 | assertEquals("attachment", f.getDispositionType()); |
---|
45 | |
---|
46 | f = (ContentDispositionField) DefaultFieldParser |
---|
47 | .parse("content-disposition: InLiNe "); |
---|
48 | assertEquals("inline", f.getDispositionType()); |
---|
49 | |
---|
50 | f = (ContentDispositionField) DefaultFieldParser |
---|
51 | .parse("CONTENT-DISPOSITION: x-yada ;" + " param = yada"); |
---|
52 | assertEquals("x-yada", f.getDispositionType()); |
---|
53 | |
---|
54 | f = (ContentDispositionField) DefaultFieldParser.parse("CONTENT-DISPOSITION: "); |
---|
55 | assertEquals("", f.getDispositionType()); |
---|
56 | } |
---|
57 | |
---|
58 | public void testGetParameter() throws Exception { |
---|
59 | ContentDispositionField f = null; |
---|
60 | |
---|
61 | f = (ContentDispositionField) DefaultFieldParser |
---|
62 | .parse("CONTENT-DISPOSITION: inline ;" |
---|
63 | + " filename=yada yada"); |
---|
64 | assertEquals("yada", f.getParameter("filename")); |
---|
65 | |
---|
66 | f = (ContentDispositionField) DefaultFieldParser |
---|
67 | .parse("Content-Disposition: x-yada;" |
---|
68 | + " fileNAme= \"ya:\\\"*da\"; " + "\tSIZE\t = 1234"); |
---|
69 | assertEquals("ya:\"*da", f.getParameter("filename")); |
---|
70 | assertEquals("1234", f.getParameter("size")); |
---|
71 | |
---|
72 | f = (ContentDispositionField) DefaultFieldParser |
---|
73 | .parse("Content-Disposition: x-yada; " |
---|
74 | + "fileNAme= \"ya \\\"\\\"\tda \\\"\"; " |
---|
75 | + "\tx-Yada\t = \"\\\"hepp\\\" =us\t-ascii\""); |
---|
76 | assertEquals("ya \"\"\tda \"", f.getParameter("filename")); |
---|
77 | assertEquals("\"hepp\" =us\t-ascii", f.getParameter("x-yada")); |
---|
78 | } |
---|
79 | |
---|
80 | public void testIsDispositionType() throws Exception { |
---|
81 | ContentDispositionField f = null; |
---|
82 | |
---|
83 | f = (ContentDispositionField) DefaultFieldParser.parse("Content-Disposition:INline"); |
---|
84 | assertTrue(f.isDispositionType("InLiNe")); |
---|
85 | assertFalse(f.isDispositionType("NiLiNe")); |
---|
86 | assertTrue(f.isInline()); |
---|
87 | assertFalse(f.isAttachment()); |
---|
88 | |
---|
89 | f = (ContentDispositionField) DefaultFieldParser |
---|
90 | .parse("Content-Disposition: attachment"); |
---|
91 | assertTrue(f.isDispositionType("ATTACHMENT")); |
---|
92 | assertFalse(f.isInline()); |
---|
93 | assertTrue(f.isAttachment()); |
---|
94 | |
---|
95 | f = (ContentDispositionField) DefaultFieldParser |
---|
96 | .parse("Content-Disposition: x-something"); |
---|
97 | assertTrue(f.isDispositionType("x-SomeThing")); |
---|
98 | assertFalse(f.isInline()); |
---|
99 | assertFalse(f.isAttachment()); |
---|
100 | } |
---|
101 | |
---|
102 | public void testGetFilename() throws Exception { |
---|
103 | ContentDispositionField f = null; |
---|
104 | |
---|
105 | f = (ContentDispositionField) DefaultFieldParser |
---|
106 | .parse("Content-Disposition: inline; filename=yada.txt"); |
---|
107 | assertEquals("yada.txt", f.getFilename()); |
---|
108 | |
---|
109 | f = (ContentDispositionField) DefaultFieldParser |
---|
110 | .parse("Content-Disposition: inline; filename=yada yada.txt"); |
---|
111 | assertEquals("yada", f.getFilename()); |
---|
112 | |
---|
113 | f = (ContentDispositionField) DefaultFieldParser |
---|
114 | .parse("Content-Disposition: inline; filename=\"yada yada.txt\""); |
---|
115 | assertEquals("yada yada.txt", f.getFilename()); |
---|
116 | |
---|
117 | f = (ContentDispositionField) DefaultFieldParser |
---|
118 | .parse("Content-Disposition: inline"); |
---|
119 | assertNull(f.getFilename()); |
---|
120 | } |
---|
121 | |
---|
122 | public void testGetCreationDate() throws Exception { |
---|
123 | ContentDispositionField f = null; |
---|
124 | |
---|
125 | f = (ContentDispositionField) DefaultFieldParser |
---|
126 | .parse("Content-Disposition: inline; " |
---|
127 | + "creation-date=\"Tue, 01 Jan 1970 00:00:00 +0000\""); |
---|
128 | assertEquals(new Date(0), f.getCreationDate()); |
---|
129 | |
---|
130 | f = (ContentDispositionField) DefaultFieldParser |
---|
131 | .parse("Content-Disposition: inline; " |
---|
132 | + "creation-date=Tue, 01 Jan 1970 00:00:00 +0000"); |
---|
133 | assertNull(f.getCreationDate()); |
---|
134 | |
---|
135 | f = (ContentDispositionField) DefaultFieldParser |
---|
136 | .parse("Content-Disposition: attachment"); |
---|
137 | assertNull(f.getCreationDate()); |
---|
138 | } |
---|
139 | |
---|
140 | public void testGetModificationDate() throws Exception { |
---|
141 | ContentDispositionField f = null; |
---|
142 | |
---|
143 | f = (ContentDispositionField) DefaultFieldParser |
---|
144 | .parse("Content-Disposition: inline; " |
---|
145 | + "modification-date=\"Tue, 01 Jan 1970 00:00:00 +0000\""); |
---|
146 | assertEquals(new Date(0), f.getModificationDate()); |
---|
147 | |
---|
148 | f = (ContentDispositionField) DefaultFieldParser |
---|
149 | .parse("Content-Disposition: inline; " |
---|
150 | + "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\""); |
---|
151 | assertEquals(new Date(855782991000l), f.getModificationDate()); |
---|
152 | |
---|
153 | f = (ContentDispositionField) DefaultFieldParser |
---|
154 | .parse("Content-Disposition: inline; " |
---|
155 | + "modification-date=yesterday"); |
---|
156 | assertNull(f.getModificationDate()); |
---|
157 | |
---|
158 | f = (ContentDispositionField) DefaultFieldParser |
---|
159 | .parse("Content-Disposition: attachment"); |
---|
160 | assertNull(f.getModificationDate()); |
---|
161 | } |
---|
162 | |
---|
163 | public void testGetReadDate() throws Exception { |
---|
164 | ContentDispositionField f = null; |
---|
165 | |
---|
166 | f = (ContentDispositionField) DefaultFieldParser |
---|
167 | .parse("Content-Disposition: inline; " |
---|
168 | + "read-date=\"Tue, 01 Jan 1970 00:00:00 +0000\""); |
---|
169 | assertEquals(new Date(0), f.getReadDate()); |
---|
170 | |
---|
171 | f = (ContentDispositionField) DefaultFieldParser |
---|
172 | .parse("Content-Disposition: inline; read-date="); |
---|
173 | assertNull(f.getReadDate()); |
---|
174 | |
---|
175 | f = (ContentDispositionField) DefaultFieldParser |
---|
176 | .parse("Content-Disposition: attachment"); |
---|
177 | assertNull(f.getReadDate()); |
---|
178 | } |
---|
179 | |
---|
180 | public void testGetSize() throws Exception { |
---|
181 | ContentDispositionField f = null; |
---|
182 | |
---|
183 | f = (ContentDispositionField) DefaultFieldParser |
---|
184 | .parse("Content-Disposition: attachment; size=0"); |
---|
185 | assertEquals(0, f.getSize()); |
---|
186 | |
---|
187 | f = (ContentDispositionField) DefaultFieldParser |
---|
188 | .parse("Content-Disposition: attachment; size=matters"); |
---|
189 | assertEquals(-1, f.getSize()); |
---|
190 | |
---|
191 | f = (ContentDispositionField) DefaultFieldParser |
---|
192 | .parse("Content-Disposition: attachment"); |
---|
193 | assertEquals(-1, f.getSize()); |
---|
194 | |
---|
195 | f = (ContentDispositionField) DefaultFieldParser |
---|
196 | .parse("Content-Disposition: attachment; size=-12"); |
---|
197 | assertEquals(-1, f.getSize()); |
---|
198 | |
---|
199 | f = (ContentDispositionField) DefaultFieldParser |
---|
200 | .parse("Content-Disposition: attachment; size=12"); |
---|
201 | assertEquals(12, f.getSize()); |
---|
202 | } |
---|
203 | |
---|
204 | } |
---|