Generated.
[libidn.git] / java / misc / TestIDNA.java
blobf174f2a22dc3aba8abd99cb872d85d16a9f0fe14
1 /**
2 * Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4 * Author: Oliver Hitz
6 * This file is part of GNU Libidn.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 import gnu.inet.encoding.IDNA;
25 import gnu.inet.encoding.IDNAException;
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.FileReader;
30 import java.io.InputStreamReader;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import java.util.StringTokenizer;
35 public class TestIDNA
37 final static int STATE_SCAN = 0;
38 final static int STATE_INPUT = 1;
40 public static void usage()
42 System.err.println("Usage: "+TestIDNA.class.toString()+" [-a|-u string] [-t]");
43 System.err.println(" -a string: apply toASCII(string)");
44 System.err.println(" -u string: apply toUnicode(string)");
45 System.err.println(" -t: automatic test using draft-josefsson-idn-test-vectors.html");
46 System.exit(1);
49 public static void main(String[] args)
50 throws Exception
52 String[] tests = new String[] {
53 "domain\u3002invalid",
54 "domain\uFF0Einvalid",
55 "domain\uFF61invalid",
57 for ( int i = 0; i < tests.length ; i++ ) {
58 assert IDNA.toASCII( tests[i] ).equals( "domain.invalid" );
61 if (args.length == 2) {
62 if (args[0].equals("-u")) {
63 try {
64 System.out.println("Input: "+args[1]);
65 System.out.println("Output: "+IDNA.toASCII(args[1]));
66 } catch (IDNAException e) {
67 System.out.println(e);
69 } else if (args[0].equals("-a")) {
70 System.out.println("Input: "+args[1]);
71 System.out.println("Output: "+IDNA.toUnicode(args[1]));
72 } else {
73 usage();
75 } else if (args.length == 1 && args[0].equals("-t")) {
76 File f = new File("draft-josefsson-idn-test-vectors.html");
77 if (!f.exists()) {
78 System.err.println("Unable to find draft-josefsson-idn-test-vectors.html.");
79 System.err.println("Please download the latest version of this file from:");
80 System.err.println("http://www.gnu.org/software/libidn/");
81 System.exit(1);
84 BufferedReader r = new BufferedReader(new FileReader(f));
85 int state = STATE_SCAN;
87 StringBuffer input = new StringBuffer();
88 String out;
90 while (true) {
91 String l = r.readLine();
92 if (null == l) {
93 break;
96 switch (state) {
97 case STATE_SCAN:
98 if (l.startsWith("input (length ")) {
99 state = STATE_INPUT;
100 input = new StringBuffer();
102 break;
103 case STATE_INPUT:
104 if (l.equals("")) {
105 // Empty line (before "out:")
106 } else if (l.startsWith("out: ")) {
107 out = l.substring(5).trim();
109 try {
110 String ascii = IDNA.toASCII(input.toString());
111 if (ascii.equals(out)) {
112 // Ok
113 } else {
114 System.err.println("Error detected:");
115 System.err.println(" Input: "+input);
116 System.err.println(" toASCII returned: "+ascii);
117 System.err.println(" expected result: "+out);
118 System.exit(1);
120 } catch (IDNAException e) {
121 System.out.println(" exception thrown ("+e+")");
124 state = STATE_SCAN;
125 } else {
126 StringTokenizer tok = new StringTokenizer(l.trim(), " ");
127 while (tok.hasMoreTokens()) {
128 String t = tok.nextToken();
129 if (t.startsWith("U+")) {
130 char u = (char) Integer.parseInt(t.substring(2, 6), 16);
131 input.append(u);
132 } else {
133 System.err.println("Unknown token: "+t);
137 break;
141 System.out.println("No errors detected!");
142 } else {
143 usage();