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