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