2 *******************************************************************************
3 * Copyright (C) 2005-2007, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 *******************************************************************************
10 package com.ibm.icu.dev.test.stringprep;
12 import java.io.BufferedReader;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.io.UnsupportedEncodingException;
16 import java.util.HashMap;
17 import java.util.Iterator;
19 import java.util.TreeMap;
21 import com.ibm.icu.dev.test.TestFmwk;
22 import com.ibm.icu.dev.test.TestUtil;
23 import com.ibm.icu.text.IDNA;
24 import com.ibm.icu.text.StringPrepParseException;
25 import com.ibm.icu.text.UTF16;
31 public class IDNAConformanceTest extends TestFmwk {
33 public static void main(String[] args) throws Exception {
34 new IDNAConformanceTest().run(args);
37 public void TestConformance() {
39 TreeMap inputData = null;
42 inputData = ReadInput.getInputData();
43 } catch (UnsupportedEncodingException e) {
44 errln(e.getMessage());
46 } catch (IOException e) {
47 errln(e.getMessage());
51 Set keyMap = inputData.keySet();
52 for (Iterator iter = keyMap.iterator(); iter.hasNext();) {
53 Long element = (Long) iter.next();
54 HashMap tempHash = (HashMap) inputData.get(element);
56 //get all attributes from input data
57 String passfail = (String) tempHash.get("passfail");
58 String desc = (String) tempHash.get("desc");
59 String type = (String) tempHash.get("type");
60 String namebase = (String) tempHash.get("namebase");
61 String nameutf8 = (String) tempHash.get("nameutf8");
62 String namezone = (String) tempHash.get("namezone");
63 String failzone1 = (String) tempHash.get("failzone1");
64 String failzone2 = (String) tempHash.get("failzone2");
66 //they maybe includes <*> style unicode
67 namebase = stringReplace(namebase);
68 namezone = stringReplace(namezone);
71 boolean failed = false;
73 if ("toascii".equals(tempHash.get("type"))) {
77 //by default STD3 rules are not used, but if the description
78 //includes UseSTD3ASCIIRules, we will set it.
79 if (desc.toLowerCase().indexOf(
80 "UseSTD3ASCIIRules".toLowerCase()) == -1) {
81 result = IDNA.convertIDNToASCII(namebase,
82 IDNA.ALLOW_UNASSIGNED).toString();
84 result = IDNA.convertIDNToASCII(namebase,
85 IDNA.USE_STD3_RULES).toString();
87 } catch (StringPrepParseException e2) {
88 //errln(e2.getMessage());
93 if ("pass".equals(passfail)) {
94 if (!namezone.equals(result)) {
95 printInfo(desc, namebase, nameutf8, namezone,
96 failzone1, failzone2, result, type, passfail);
97 errln("\t pass fail standard is pass, but failed");
99 printInfo(desc, namebase, nameutf8, namezone,
100 failzone1, failzone2, result, type, passfail);
105 if ("fail".equals(passfail)) {
107 printInfo(desc, namebase, nameutf8, namezone,
108 failzone1, failzone2, result, type, passfail);
111 printInfo(desc, namebase, nameutf8, namezone,
112 failzone1, failzone2, result, type, passfail);
113 errln("\t pass fail standard is fail, but no exception thrown out");
116 } else if ("tounicode".equals(tempHash.get("type"))) {
118 //by default STD3 rules are not used, but if the description
119 //includes UseSTD3ASCIIRules, we will set it.
120 if (desc.toLowerCase().indexOf(
121 "UseSTD3ASCIIRules".toLowerCase()) == -1) {
122 result = IDNA.convertIDNToUnicode(namebase,
123 IDNA.ALLOW_UNASSIGNED).toString();
125 result = IDNA.convertIDNToUnicode(namebase,
126 IDNA.USE_STD3_RULES).toString();
128 } catch (StringPrepParseException e2) {
129 //errln(e2.getMessage());
132 if ("pass".equals(passfail)) {
133 if (!namezone.equals(result)) {
134 printInfo(desc, namebase, nameutf8, namezone,
135 failzone1, failzone2, result, type, passfail);
137 errln("\t Did not get the expected result. Expected: " + prettify(namezone) + " Got: " + prettify(result));
139 printInfo(desc, namebase, nameutf8, namezone,
140 failzone1, failzone2, result, type, passfail);
145 if ("fail".equals(passfail)) {
146 if (failed || namebase.equals(result)) {
147 printInfo(desc, namebase, nameutf8, namezone,
148 failzone1, failzone2, result, type, passfail);
152 printInfo(desc, namebase, nameutf8, namezone,
153 failzone1, failzone2, result, type, passfail);
155 errln("\t pass fail standard is fail, but no exception thrown out");
174 private void printInfo(String desc, String namebase,
175 String nameutf8, String namezone, String failzone1,
176 String failzone2, String result, String type, String passfail) {
177 logln("desc:\t" + desc);
179 logln("type:\t" + type);
181 logln("pass fail standard:\t" + passfail);
183 logln("namebase:\t" + namebase);
185 logln("nameutf8:\t" + nameutf8);
187 logln("namezone:\t" + namezone);
189 logln("failzone1:\t" + failzone1);
191 logln("failzone2:\t" + failzone2);
193 logln("result:\t" + result);
197 * Change unicode string from <00AD> to \u00AD, for the later is accepted
199 * @param str String including <*> style unicode
202 private static String stringReplace(String str) {
204 StringBuffer result = new StringBuffer();
205 char[] chars = str.toCharArray();
206 StringBuffer sbTemp = new StringBuffer();
207 for (int i = 0; i < chars.length; i++) {
208 if ('<' == chars[i]) {
209 sbTemp = new StringBuffer();
210 while ('>' != chars[i + 1]) {
211 sbTemp.append(chars[++i]);
214 * The unicode sometimes is larger then \uFFFF, so have to use
217 int toBeInserted = Integer.parseInt(sbTemp.toString(), 16);
218 if ((toBeInserted >> 16) == 0) {
219 result.append((char) toBeInserted);
221 String utf16String = UTF16.valueOf(toBeInserted);
222 char[] charsTemp = utf16String.toCharArray();
223 for (int j = 0; j < charsTemp.length; j++) {
224 result.append((char) charsTemp[j]);
227 } else if ('>' == chars[i]) {//end when met with '>'
230 result.append(chars[i]);
234 return result.toString();
238 * This class is used to read test data from TestInput file.
243 public static class ReadInput {
245 public static TreeMap getInputData() throws IOException,
246 UnsupportedEncodingException {
248 TreeMap result = new TreeMap();
250 BufferedReader in = TestUtil.getDataReader("IDNATestInput.txt", "utf-8");
252 String tempStr = null;
254 boolean firstLine = true;
255 HashMap hashItem = new HashMap();
257 while ((tempStr = in.readLine()) != null) {
258 //ignore the first line if it's "====="
260 if ("=====".equals(tempStr))
266 if ("".equals(tempStr)) {
270 String attr = "";//attribute
271 String body = "";//value
273 //get attr and body from line input, and then set them into each hash item.
274 int postion = tempStr.indexOf(":");
276 attr = tempStr.substring(0, postion).trim();
277 body = tempStr.substring(postion + 1).trim();
279 //deal with combination lines when end with '\'
280 while (null != body && body.length() > 0
281 && '\\' == body.charAt(body.length() - 1)) {
282 body = body.substring(0, body.length() - 1);
284 tempStr = in.readLine();
288 //push them to hash item
289 hashItem.put(attr, body);
291 //if met "=====", it means this item is finished
292 if ("=====".equals(tempStr)) {
293 //set them into result, using records number as key
294 result.put(new Long(records), hashItem);
295 //create another hash item and continue
296 hashItem = new HashMap();
301 } catch (UnsupportedEncodingException e) {
303 } catch (FileNotFoundException e) {
305 } catch (IOException e) {