Exemplo n.º 1
0
  public void testCustomStringEncoding() throws Exception {
    final String ENCODING = System.getProperty("file.encoding");
    // Keep stuff within the extended ASCII range so we work with more
    // limited native encodings
    String UNICODE = "Un \u00e9l\u00e9ment gr\u00e2ce \u00e0 l'index";

    if (!UNICODE.equals(new String(UNICODE.getBytes()))) {
      // If the extended characters aren't encodable in the default
      // encoding, punt and use straight ASCII
      UNICODE = "";
      for (char ch = 1; ch < 128; ch++) {
        UNICODE += ch;
      }
    }
    final String UNICODEZ = UNICODE + "\0more stuff";

    byte[] customEncoded = Native.getBytes(UNICODE, ENCODING);
    byte[] expected = UNICODE.getBytes(ENCODING);
    for (int i = 0; i < Math.min(customEncoded.length, expected.length); i++) {
      assertEquals(
          "Improperly encoded (" + ENCODING + ") from Java at " + i, expected[i], customEncoded[i]);
    }
    assertEquals(
        "Wrong number of encoded characters (" + ENCODING + ")",
        expected.length,
        customEncoded.length);
    String result = Native.toString(customEncoded, ENCODING);
    assertEquals("Improperly decoded from native bytes (" + ENCODING + ")", UNICODE, result);

    assertEquals(
        "Should truncate bytes at NUL terminator",
        UNICODE,
        Native.toString(UNICODEZ.getBytes(ENCODING), ENCODING));
  }
Exemplo n.º 2
0
 public void testGetBytesBadEncoding() throws Exception {
   byte[] buf = Native.getBytes(getName(), "unsupported");
   assertEquals(
       "Incorrect fallback bytes with bad encoding",
       getName(),
       new String(buf, System.getProperty("file.encoding")));
 }
Exemplo n.º 3
0
  public void testDefaultStringEncoding() throws Exception {
    final String UNICODE = "\u0444\u043b\u0441\u0432\u0443";
    final String UNICODEZ = UNICODE + "\0more stuff";
    byte[] utf8 = Native.getBytes(UNICODE);
    byte[] expected = UNICODE.getBytes(Native.DEFAULT_ENCODING);
    for (int i = 0; i < Math.min(utf8.length, expected.length); i++) {
      assertEquals("Improperly encoded at " + i, expected[i], utf8[i]);
    }
    assertEquals("Wrong number of encoded characters", expected.length, utf8.length);
    String result = Native.toString(utf8);
    assertEquals("Improperly decoded", UNICODE, result);

    assertEquals(
        "Should truncate bytes at NUL terminator",
        UNICODE,
        Native.toString(UNICODEZ.getBytes(Native.DEFAULT_ENCODING)));
  }
Exemplo n.º 4
0
 public void testGetBytes() throws Exception {
   byte[] buf = Native.getBytes(getName() + UNICODE, "utf8");
   assertEquals(
       "Incorrect native bytes from Java String", getName() + UNICODE, new String(buf, "utf8"));
 }