2 * Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
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
24 import gnu
.inet
.encoding
.IDNA
;
25 import gnu
.inet
.encoding
.IDNAException
;
27 import java
.io
.BufferedReader
;
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
;
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");
49 public static void main(String
[] args
)
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")) {
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]));
75 } else if (args
.length
== 1 && args
[0].equals("-t")) {
76 File f
= new File("draft-josefsson-idn-test-vectors.html");
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/");
84 BufferedReader r
= new BufferedReader(new FileReader(f
));
85 int state
= STATE_SCAN
;
87 StringBuffer input
= new StringBuffer();
91 String l
= r
.readLine();
98 if (l
.startsWith("input (length ")) {
100 input
= new StringBuffer();
105 // Empty line (before "out:")
106 } else if (l
.startsWith("out: ")) {
107 out
= l
.substring(5).trim();
110 String ascii
= IDNA
.toASCII(input
.toString());
111 if (ascii
.equals(out
)) {
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
);
120 } catch (IDNAException e
) {
121 System
.out
.println(" exception thrown ("+e
+")");
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);
133 System
.err
.println("Unknown token: "+t
);
141 System
.out
.println("No errors detected!");