|
Java example source code file (PunycodeTest.java)
The PunycodeTest.java Java example source code
/*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary Unit test for sun.net.idn.Punycode
* @bug 4737170
* @compile -XDignore.symbol.file PunycodeTest.java
* @run main/othervm -ea PunycodeTest
* @author Edward Wang
*/
import java.util.Scanner;
import java.text.ParseException;
import sun.net.idn.Punycode;
/**
* unit test for Punycode that is also originated from the sample code
* provided in rfc3492.txt
*/
public class PunycodeTest {
/* For testing, we'll just set some compile-time limits rather than */
/* use malloc(), and set a compile-time option rather than using a */
/* command-line option. */
static final int unicode_max_length = 256;
static final int ace_max_length = 256;
static final String too_big =
"input or output is too large, recompile with larger limits\n";
static final String invalid_input = "invalid input\n";
static final String overflow = "arithmetic overflow\n";
static final String io_error = "I/O error\n";
/* The following string is used to convert printable */
/* characters between ASCII and the native charset: */
static void fail(String msg, String input) {
System.out.println(msg+" input: "+input);
throw new RuntimeException(msg+" input: "+input);
}
public int testCount = 0;
private int input_length, j;
private int output_length[] = new int[1];
private boolean case_flags[] = new boolean[unicode_max_length];
public String testEncoding(String inputS) {
char input[] = new char[unicode_max_length];
int codept = 0;
char uplus[] = new char[2];
StringBuffer output;
int c;
/* Read the input code points: */
input_length = 0;
Scanner sc = new Scanner(inputS);
while (sc.hasNext()) { // need to stop at end of line
try {
String next = sc.next();
uplus[0] = next.charAt(0);
uplus[1] = next.charAt(1);
codept = Integer.parseInt(next.substring(2), 16);
} catch (Exception ex) {
fail(invalid_input, inputS);
}
if (uplus[1] != '+' || codept > Integer.MAX_VALUE) {
fail(invalid_input, inputS);
}
if (input_length == unicode_max_length) fail(too_big, inputS);
if (uplus[0] == 'u') case_flags[input_length] = false;
else if (uplus[0] == 'U') case_flags[input_length] = true;
else fail(invalid_input, inputS);
input[input_length++] = (char)codept;
}
/* Encode: */
output_length[0] = ace_max_length;
try {
output = Punycode.encode((new StringBuffer()).append(input, 0, input_length), case_flags);
} catch (Exception e) {
fail(invalid_input, inputS);
// never reach here, just to make compiler happy
return null;
}
testCount++;
return output.toString();
}
public String testDecoding(String inputS) {
char input[] = new char[0];
int pp;
StringBuffer output;
/* Read the Punycode input string and convert to ASCII: */
if (inputS.length() <= ace_max_length+2) {
input = inputS.toCharArray();
} else {
fail(invalid_input, inputS);
}
input_length = input.length;
/* Decode: */
output_length[0] = unicode_max_length;
try {
output = Punycode.decode((new StringBuffer()).append(input, 0, input_length), case_flags);
} catch (Exception e) {
fail(invalid_input, inputS);
// never reach here, just to make compiler happy
return null;
}
/* Output the result: */
StringBuffer result = new StringBuffer();
for (j = 0; j < output.length(); ++j) {
result.append(String.format("%s+%04X ",
case_flags[j] ? "U" : "u",
(int)output.charAt(j) ));
}
testCount++;
return result.substring(0, result.length() - 1);
}
// test data from rfc3492
static String[][] testdata = {
{"(A) Arabic (Egyptian):",
"u+0644 u+064A u+0647 u+0645 u+0627 u+0628 u+062A u+0643 u+0644 "+
"u+0645 u+0648 u+0634 u+0639 u+0631 u+0628 u+064A u+061F",
"egbpdaj6bu4bxfgehfvwxn"},
{"(B) Chinese (simplified):",
"u+4ED6 u+4EEC u+4E3A u+4EC0 u+4E48 u+4E0D u+8BF4 u+4E2D u+6587",
"ihqwcrb4cv8a8dqg056pqjye"},
{"(C) Chinese (traditional):",
"u+4ED6 u+5011 u+7232 u+4EC0 u+9EBD u+4E0D u+8AAA u+4E2D u+6587",
"ihqwctvzc91f659drss3x8bo0yb"},
{"(D) Czech: Pro<ccaron>prost
Other Java examples (source code examples)Here is a short list of links related to this Java PunycodeTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.