View Javadoc
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.commons.compress.archivers.zip;
21  
22  import static java.nio.charset.StandardCharsets.ISO_8859_1;
23  import static java.nio.charset.StandardCharsets.US_ASCII;
24  import static java.nio.charset.StandardCharsets.UTF_16BE;
25  import static java.nio.charset.StandardCharsets.UTF_8;
26  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  
29  import java.nio.ByteBuffer;
30  import java.util.Arrays;
31  
32  import org.junit.jupiter.api.Test;
33  
34  public class NioZipEncodingTest {
35  
36      private static final String UMLAUTS = "\u00e4\u00f6\u00fc";
37  
38      private static final String RAINBOW_EMOJI = "\ud83c\udf08";
39  
40      @Test
41      public void testPartialSurrogatePair() {
42          final NioZipEncoding e = new NioZipEncoding(US_ASCII, false);
43          final ByteBuffer bb = e.encode("\ud83c");
44          final int off = bb.arrayOffset();
45          final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
46          assertEquals(0, result.length);
47      }
48  
49      @Test
50      public void testRainbowEmojiToSurrogatePairUTF16() {
51          final NioZipEncoding e = new NioZipEncoding(UTF_16BE, false);
52          final ByteBuffer bb = e.encode(RAINBOW_EMOJI);
53          final int off = bb.arrayOffset();
54          final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
55          assertArrayEquals(RAINBOW_EMOJI.getBytes(UTF_16BE), result);
56      }
57  
58      @Test
59      public void testUmlautToISO88591() {
60          final NioZipEncoding e = new NioZipEncoding(ISO_8859_1, true);
61          final ByteBuffer bb = e.encode("\u00e4\u00f6\u00fc");
62          final int off = bb.arrayOffset();
63          final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
64          assertArrayEquals(UMLAUTS.getBytes(ISO_8859_1), result);
65      }
66  
67      @Test
68      public void testUmlautToUTF16BE() {
69          final NioZipEncoding e = new NioZipEncoding(UTF_16BE, false);
70          final ByteBuffer bb = e.encode(UMLAUTS);
71          final int off = bb.arrayOffset();
72          final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
73          assertArrayEquals(UMLAUTS.getBytes(UTF_16BE), result);
74      }
75  
76      @Test
77      public void testUmlautToUTF8() {
78          final NioZipEncoding e = new NioZipEncoding(UTF_8, true);
79          final ByteBuffer bb = e.encode("\u00e4\u00f6\u00fc");
80          final int off = bb.arrayOffset();
81          final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
82          assertArrayEquals(UMLAUTS.getBytes(UTF_8), result);
83      }
84  
85      @Test
86      public void testUnmappableRainbowEmoji() {
87          final NioZipEncoding e = new NioZipEncoding(US_ASCII, false);
88          final ByteBuffer bb = e.encode(RAINBOW_EMOJI);
89          final int off = bb.arrayOffset();
90          final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
91          assertEquals("%UD83C%UDF08", new String(result, US_ASCII));
92      }
93  
94      @Test
95      public void testUnmappableUmlauts() {
96          final NioZipEncoding e = new NioZipEncoding(US_ASCII, false);
97          final ByteBuffer bb = e.encode("\u00e4\u00f6\u00fc");
98          final int off = bb.arrayOffset();
99          final byte[] result = Arrays.copyOfRange(bb.array(), off, off + bb.limit() - bb.position());
100         assertEquals("%U00E4%U00F6%U00FC", new String(result, US_ASCII));
101     }
102 }