]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/stringprep/IDNAConformanceTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / stringprep / IDNAConformanceTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2005-2007, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6 \r
7  *******************************************************************************\r
8  */\r
9 \r
10 package com.ibm.icu.dev.test.stringprep;\r
11 \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
20 \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
26 \r
27 /**\r
28  * @author limaoyu\r
29  * \r
30  */\r
31 public class IDNAConformanceTest extends TestFmwk {\r
32 \r
33     public static void main(String[] args) throws Exception {\r
34         new IDNAConformanceTest().run(args);\r
35     }\r
36 \r
37     public void TestConformance() {\r
38 \r
39         TreeMap inputData = null;\r
40         \r
41         try {\r
42             inputData = ReadInput.getInputData();\r
43         } catch (UnsupportedEncodingException e) {\r
44             errln(e.getMessage());\r
45             return;\r
46         } catch (IOException e) {\r
47             errln(e.getMessage());\r
48             return;\r
49         }\r
50         \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
55 \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
65 \r
66             //they maybe includes <*> style unicode\r
67             namebase = stringReplace(namebase);\r
68             namezone = stringReplace(namezone);\r
69 \r
70             String result = null;\r
71             boolean failed = false;\r
72 \r
73             if ("toascii".equals(tempHash.get("type"))) {\r
74                 \r
75                 //get the result\r
76                 try {\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
83                     } else {\r
84                         result = IDNA.convertIDNToASCII(namebase,\r
85                                 IDNA.USE_STD3_RULES).toString();\r
86                     }\r
87                 } catch (StringPrepParseException e2) {\r
88                     //errln(e2.getMessage());\r
89                     failed = true;\r
90                 }\r
91                 \r
92                 \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
98                     } else {\r
99                         printInfo(desc, namebase, nameutf8, namezone,\r
100                                 failzone1, failzone2, result, type, passfail);\r
101                         logln("\tpassed");\r
102                     }\r
103                 }\r
104 \r
105                 if ("fail".equals(passfail)) {\r
106                     if (failed) {\r
107                         printInfo(desc, namebase, nameutf8, namezone,\r
108                                 failzone1, failzone2, result, type, passfail);\r
109                         logln("passed");\r
110                     } else {\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
114                     }\r
115                 }\r
116             } else if ("tounicode".equals(tempHash.get("type"))) {\r
117                 try {\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
124                     } else {\r
125                         result = IDNA.convertIDNToUnicode(namebase,\r
126                                 IDNA.USE_STD3_RULES).toString();\r
127                     }\r
128                 } catch (StringPrepParseException e2) {\r
129                     //errln(e2.getMessage());\r
130                     failed = true;\r
131                 }\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
136                         \r
137                         errln("\t Did not get the expected result. Expected: " + prettify(namezone) + " Got: " + prettify(result));\r
138                     } else {\r
139                         printInfo(desc, namebase, nameutf8, namezone,\r
140                                 failzone1, failzone2, result, type, passfail);                        \r
141                         logln("\tpassed");\r
142                     }\r
143                 }\r
144 \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
149                         \r
150                         logln("\tpassed");\r
151                     } else {\r
152                         printInfo(desc, namebase, nameutf8, namezone,\r
153                                 failzone1, failzone2, result, type, passfail);\r
154                         \r
155                         errln("\t pass fail standard is fail, but no exception thrown out");\r
156                     }\r
157                 }\r
158             } else {\r
159                 continue;\r
160             }\r
161         }\r
162     }\r
163 \r
164     /**\r
165      * Print log message.\r
166      * @param desc\r
167      * @param namebase\r
168      * @param nameutf8\r
169      * @param namezone\r
170      * @param failzone1\r
171      * @param failzone2\r
172      * @param result\r
173      */\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
178         log("\t");\r
179         logln("type:\t" + type);\r
180         log("\t");\r
181         logln("pass fail standard:\t" + passfail);\r
182         log("\t");\r
183         logln("namebase:\t" + namebase);\r
184         log("\t");\r
185         logln("nameutf8:\t" + nameutf8);\r
186         log("\t");\r
187         logln("namezone:\t" + namezone);\r
188         log("\t");\r
189         logln("failzone1:\t" + failzone1);\r
190         log("\t");\r
191         logln("failzone2:\t" + failzone2);\r
192         log("\t");\r
193         logln("result:\t" + result);\r
194     }\r
195 \r
196     /**\r
197      * Change unicode string from <00AD> to \u00AD, for the later is accepted\r
198      * by Java\r
199      * @param str String including <*> style unicode\r
200      * @return \\u String\r
201      */\r
202     private static String stringReplace(String str) {\r
203 \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
212                 }\r
213                 /*\r
214                  * The unicode sometimes is larger then \uFFFF, so have to use\r
215                  * UTF16.\r
216                  */\r
217                 int toBeInserted = Integer.parseInt(sbTemp.toString(), 16);\r
218                 if ((toBeInserted >> 16) == 0) {\r
219                     result.append((char) toBeInserted);\r
220                 } else {\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
225                     }\r
226                 }\r
227             } else if ('>' == chars[i]) {//end when met with '>'\r
228                 continue;\r
229             } else {\r
230                 result.append(chars[i]);\r
231             }\r
232 \r
233         }\r
234         return result.toString();\r
235     }\r
236 \r
237     /**\r
238      * This class is used to read test data from TestInput file.\r
239      * \r
240      * @author limaoyu\r
241      *  \r
242      */\r
243     public static class ReadInput {\r
244 \r
245         public static TreeMap getInputData() throws IOException,\r
246                 UnsupportedEncodingException {\r
247 \r
248             TreeMap result = new TreeMap();\r
249             try {\r
250                 BufferedReader in = TestUtil.getDataReader("IDNATestInput.txt", "utf-8");\r
251 \r
252                 String tempStr = null;\r
253                 int records = 0;\r
254                 boolean firstLine = true;\r
255                 HashMap hashItem = new HashMap();\r
256 \r
257                 while ((tempStr = in.readLine()) != null) {\r
258                     //ignore the first line if it's "====="\r
259                     if (firstLine) {\r
260                         if ("=====".equals(tempStr))\r
261                             continue;\r
262                         firstLine = false;\r
263                     }\r
264 \r
265                     //Ignore empty line\r
266                     if ("".equals(tempStr)) {\r
267                         continue;\r
268                     }\r
269 \r
270                     String attr = "";//attribute\r
271                     String body = "";//value\r
272 \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
278 \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
283                             body += "\n";\r
284                             tempStr = in.readLine();\r
285                             body += tempStr;\r
286                         }\r
287                     }\r
288                     //push them to hash item\r
289                     hashItem.put(attr, body);\r
290 \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
297                         records++;\r
298                         continue;\r
299                     }\r
300                 }\r
301             } catch (UnsupportedEncodingException e) {\r
302                 throw e;\r
303             } catch (FileNotFoundException e) {\r
304                 throw e;\r
305             } catch (IOException e) {\r
306                 throw e;\r
307             }\r
308             return result;\r
309         }\r
310     }\r
311 }