View Javadoc
1   package com.atlassian.dbexporter.node.stax;
2   
3   import org.junit.Test;
4   
5   import static org.junit.Assert.assertEquals;
6   import static org.junit.Assert.assertNull;
7   
8   public class StaxUtilsTest {
9       private final String legal = "legal: \u0020 \uFFFC";
10      private final String decoded = "Illegal: \u0000 \u0008 \u0010 \u000B \u001F \uFFFE \\";
11      private final String encoded = "Illegal: \\u0000 \\u0008 \\u0010 \\u000B \\u001F \\uFFFE \\\\";
12  
13      @Test
14      public void testUnicodeDecoding() {
15          assertEquals(legal, StaxUtils.unicodeDecode(legal));
16          assertEquals(decoded, StaxUtils.unicodeDecode(encoded));
17          assertNull(StaxUtils.unicodeDecode(null));
18      }
19  
20      @Test
21      public void testUnicodeEncoding() {
22          assertEquals(legal, StaxUtils.unicodeEncode(legal));
23          assertEquals(encoded, StaxUtils.unicodeEncode(decoded));
24          assertNull(StaxUtils.unicodeEncode(null));
25      }
26  
27      /**
28       * This test pushes a string that contains every valid 16-bit Java char
29       * through the unicode encoder and decoder and verifies that the exact string
30       * is properly restored.
31       */
32      @Test
33      public void testCodec() {
34          final String encoded = StaxUtils.unicodeEncode(createTestString());
35          final String decoded = StaxUtils.unicodeDecode(encoded);
36  
37          assertEquals(createTestString(), decoded);
38      }
39  
40      /**
41       * Returns a unicode string that contains every character from U+0000 to
42       * U+FFFF (AKA the Basic Multilingual Plane (BMP)).
43       */
44      private String createTestString() {
45          StringBuilder builder = new StringBuilder();
46          for (char c = 0x0; c < 0xFFFF; c++) {
47              if (c >= 0xd801 && c <= 0xdbff) {
48                  continue;   // exclude the high surrogate range
49              } else {
50                  builder.append(c);
51              }
52          }
53          return builder.append((char) 0xFFFF).toString();
54      }
55  }