This project has retired. For details please refer to its Attic page.
MimeHelperTest xref

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   * Contributors:
20   *     Florent Guillaume
21   */
22  package org.apache.chemistry.opencmis.commons.impl.misc;
23  
24  import static org.apache.chemistry.opencmis.commons.impl.MimeHelper.decodeContentDisposition;
25  import static org.apache.chemistry.opencmis.commons.impl.MimeHelper.decodeContentDispositionFilename;
26  import static org.apache.chemistry.opencmis.commons.impl.MimeHelper.encodeContentDisposition;
27  
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import junit.framework.TestCase;
33  
34  public class MimeHelperTest extends TestCase {
35  
36      public void testEncodeContentDisposition() {
37          assertEquals("inline; filename=foo.bar",
38                  encodeContentDisposition("inline", "foo.bar"));
39          assertEquals("attachment; filename=foo.bar",
40                  encodeContentDisposition(null, "foo.bar"));
41          assertEquals("attachment; filename*=UTF-8''caf%C3%A9.pdf",
42                  encodeContentDisposition(null, "caf\u00e9.pdf"));
43          assertEquals(
44                  "attachment; filename*=UTF-8''%20%27%2A%25%20abc%20%C2%81%C2%82%0D%0A%09",
45                  encodeContentDisposition(null, " '*% abc \u0081\u0082\r\n\t"));
46      }
47  
48      public void testDecodeContentDisposition() {
49          Map<String, String> params = new HashMap<String, String>();
50          Map<String, String> expected = new HashMap<String, String>();
51  
52          assertEquals("attachment",
53                  decodeContentDisposition("attachment; a=b; c=d", params));
54          expected.put("a", "b");
55          expected.put("c", "d");
56          assertEquals(expected, params);
57          params.clear();
58          expected.clear();
59  
60          assertEquals(
61                  "inline",
62                  decodeContentDisposition(
63                          "  inline ; a = \"b b\" (this is a comment) ; c =d;",
64                          params));
65          expected.put("a", "b b");
66          expected.put("c", "d");
67          assertEquals(expected, params);
68          params.clear();
69          expected.clear();
70  
71          assertEquals(
72                  "inline",
73                  decodeContentDisposition(
74                          "inline; modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"",
75                          params));
76          assertEquals(Collections.singletonMap("modification-date",
77                  "Wed, 12 Feb 1997 16:29:51 -0500"), params);
78          params.clear();
79      }
80  
81      public void testDecodeContentDispositionFilename() {
82          assertNull(decodeContentDispositionFilename("attachment; a=b; c=d;"));
83          assertNull(decodeContentDispositionFilename("inline"));
84          assertNull(decodeContentDispositionFilename("inline; modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\""));
85          assertEquals(
86                  "foo.bar",
87                  decodeContentDispositionFilename("attachment; filename=foo.bar"));
88          assertEquals(
89                  "foo.bar",
90                  decodeContentDispositionFilename("attachment; filename = \"foo.bar\""));
91          assertEquals(
92                  "foo.bar",
93                  decodeContentDispositionFilename(" guess ; filename = (this is rfc822 a comment) \"foo.bar\""));
94          assertEquals(
95                  "caf\u00e9.pdf",
96                  decodeContentDispositionFilename("foo; filename*=UTF-8''caf%C3%A9.pdf"));
97          assertEquals(
98                  "caf\u00e9.pdf",
99                  decodeContentDispositionFilename("bar; filename*=ISO-8859-1''caf%E9.pdf"));
100     }
101 
102 }