001: /*
002: *******************************************************************************
003: * Copyright (C) 2005-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006:
007: *******************************************************************************
008: */
009:
010: package com.ibm.icu.dev.test.stringprep;
011:
012: import java.io.BufferedReader;
013: import java.io.FileNotFoundException;
014: import java.io.IOException;
015: import java.io.UnsupportedEncodingException;
016: import java.util.HashMap;
017: import java.util.Iterator;
018: import java.util.Set;
019: import java.util.TreeMap;
020:
021: import com.ibm.icu.dev.test.TestFmwk;
022: import com.ibm.icu.dev.test.TestUtil;
023: import com.ibm.icu.text.IDNA;
024: import com.ibm.icu.text.StringPrepParseException;
025: import com.ibm.icu.text.UTF16;
026:
027: /**
028: * @author limaoyu
029: *
030: */
031: public class IDNAConformanceTest extends TestFmwk {
032:
033: public static void main(String[] args) throws Exception {
034: new IDNAConformanceTest().run(args);
035: }
036:
037: public void TestConformance() {
038:
039: TreeMap inputData = null;
040:
041: try {
042: inputData = ReadInput.getInputData();
043: } catch (UnsupportedEncodingException e) {
044: errln(e.getMessage());
045: } catch (IOException e) {
046: errln(e.getMessage());
047: }
048:
049: Set keyMap = inputData.keySet();
050: for (Iterator iter = keyMap.iterator(); iter.hasNext();) {
051: Long element = (Long) iter.next();
052: HashMap tempHash = (HashMap) inputData.get(element);
053:
054: //get all attributes from input data
055: String passfail = (String) tempHash.get("passfail");
056: String desc = (String) tempHash.get("desc");
057: String type = (String) tempHash.get("type");
058: String namebase = (String) tempHash.get("namebase");
059: String nameutf8 = (String) tempHash.get("nameutf8");
060: String namezone = (String) tempHash.get("namezone");
061: String failzone1 = (String) tempHash.get("failzone1");
062: String failzone2 = (String) tempHash.get("failzone2");
063:
064: //they maybe includes <*> style unicode
065: namebase = stringReplace(namebase);
066: namezone = stringReplace(namezone);
067:
068: String result = null;
069: boolean failed = false;
070:
071: if ("toascii".equals(tempHash.get("type"))) {
072:
073: //get the result
074: try {
075: //by default STD3 rules are not used, but if the description
076: //includes UseSTD3ASCIIRules, we will set it.
077: if (desc.toLowerCase().indexOf(
078: "UseSTD3ASCIIRules".toLowerCase()) == -1) {
079: result = IDNA.convertIDNToASCII(namebase,
080: IDNA.ALLOW_UNASSIGNED).toString();
081: } else {
082: result = IDNA.convertIDNToASCII(namebase,
083: IDNA.USE_STD3_RULES).toString();
084: }
085: } catch (StringPrepParseException e2) {
086: //errln(e2.getMessage());
087: failed = true;
088: }
089:
090: if ("pass".equals(passfail)) {
091: if (!namezone.equals(result)) {
092: printInfo(desc, namebase, nameutf8, namezone,
093: failzone1, failzone2, result, type,
094: passfail);
095: errln("\t pass fail standard is pass, but failed");
096: } else {
097: printInfo(desc, namebase, nameutf8, namezone,
098: failzone1, failzone2, result, type,
099: passfail);
100: logln("\tpassed");
101: }
102: }
103:
104: if ("fail".equals(passfail)) {
105: if (failed) {
106: printInfo(desc, namebase, nameutf8, namezone,
107: failzone1, failzone2, result, type,
108: passfail);
109: logln("passed");
110: } else {
111: printInfo(desc, namebase, nameutf8, namezone,
112: failzone1, failzone2, result, type,
113: passfail);
114: errln("\t pass fail standard is fail, but no exception thrown out");
115: }
116: }
117: } else if ("tounicode".equals(tempHash.get("type"))) {
118: try {
119: //by default STD3 rules are not used, but if the description
120: //includes UseSTD3ASCIIRules, we will set it.
121: if (desc.toLowerCase().indexOf(
122: "UseSTD3ASCIIRules".toLowerCase()) == -1) {
123: result = IDNA.convertIDNToUnicode(namebase,
124: IDNA.ALLOW_UNASSIGNED).toString();
125: } else {
126: result = IDNA.convertIDNToUnicode(namebase,
127: IDNA.USE_STD3_RULES).toString();
128: }
129: } catch (StringPrepParseException e2) {
130: //errln(e2.getMessage());
131: failed = true;
132: }
133: if ("pass".equals(passfail)) {
134: if (!namezone.equals(result)) {
135: printInfo(desc, namebase, nameutf8, namezone,
136: failzone1, failzone2, result, type,
137: passfail);
138:
139: errln("\t Did not get the expected result. Expected: "
140: + prettify(namezone)
141: + " Got: "
142: + prettify(result));
143: } else {
144: printInfo(desc, namebase, nameutf8, namezone,
145: failzone1, failzone2, result, type,
146: passfail);
147: logln("\tpassed");
148: }
149: }
150:
151: if ("fail".equals(passfail)) {
152: if (failed || namebase.equals(result)) {
153: printInfo(desc, namebase, nameutf8, namezone,
154: failzone1, failzone2, result, type,
155: passfail);
156:
157: logln("\tpassed");
158: } else {
159: printInfo(desc, namebase, nameutf8, namezone,
160: failzone1, failzone2, result, type,
161: passfail);
162:
163: errln("\t pass fail standard is fail, but no exception thrown out");
164: }
165: }
166: } else {
167: continue;
168: }
169: }
170: }
171:
172: /**
173: * Print log message.
174: * @param desc
175: * @param namebase
176: * @param nameutf8
177: * @param namezone
178: * @param failzone1
179: * @param failzone2
180: * @param result
181: */
182: private void printInfo(String desc, String namebase,
183: String nameutf8, String namezone, String failzone1,
184: String failzone2, String result, String type,
185: String passfail) {
186: logln("desc:\t" + desc);
187: log("\t");
188: logln("type:\t" + type);
189: log("\t");
190: logln("pass fail standard:\t" + passfail);
191: log("\t");
192: logln("namebase:\t" + namebase);
193: log("\t");
194: logln("nameutf8:\t" + nameutf8);
195: log("\t");
196: logln("namezone:\t" + namezone);
197: log("\t");
198: logln("failzone1:\t" + failzone1);
199: log("\t");
200: logln("failzone2:\t" + failzone2);
201: log("\t");
202: logln("result:\t" + result);
203: }
204:
205: /**
206: * Change unicode string from <00AD> to \u00AD, for the later is accepted
207: * by Java
208: * @param str String including <*> style unicode
209: * @return \\u String
210: */
211: private static String stringReplace(String str) {
212:
213: StringBuffer result = new StringBuffer();
214: char[] chars = str.toCharArray();
215: StringBuffer sbTemp = new StringBuffer();
216: for (int i = 0; i < chars.length; i++) {
217: if ('<' == chars[i]) {
218: sbTemp = new StringBuffer();
219: while ('>' != chars[i + 1]) {
220: sbTemp.append(chars[++i]);
221: }
222: /*
223: * The unicode sometimes is larger then \uFFFF, so have to use
224: * UTF16.
225: */
226: int toBeInserted = Integer.parseInt(sbTemp.toString(),
227: 16);
228: if ((toBeInserted >> 16) == 0) {
229: result.append((char) toBeInserted);
230: } else {
231: String utf16String = UTF16.valueOf(toBeInserted);
232: char[] charsTemp = utf16String.toCharArray();
233: for (int j = 0; j < charsTemp.length; j++) {
234: result.append((char) charsTemp[j]);
235: }
236: }
237: } else if ('>' == chars[i]) {//end when met with '>'
238: continue;
239: } else {
240: result.append(chars[i]);
241: }
242:
243: }
244: return result.toString();
245: }
246:
247: /**
248: * This class is used to read test data from TestInput file.
249: *
250: * @author limaoyu
251: *
252: */
253: public static class ReadInput {
254:
255: public static TreeMap getInputData() throws IOException,
256: UnsupportedEncodingException {
257:
258: TreeMap result = new TreeMap();
259: try {
260: BufferedReader in = TestUtil.getDataReader(
261: "IDNATestInput.txt", "utf-8");
262:
263: String tempStr = null;
264: int records = 0;
265: boolean firstLine = true;
266: HashMap hashItem = new HashMap();
267:
268: while ((tempStr = in.readLine()) != null) {
269: //ignore the first line if it's "====="
270: if (firstLine) {
271: if ("=====".equals(tempStr))
272: continue;
273: firstLine = false;
274: }
275:
276: //Ignore empty line
277: if ("".equals(tempStr)) {
278: continue;
279: }
280:
281: String attr = "";//attribute
282: String body = "";//value
283:
284: //get attr and body from line input, and then set them into each hash item.
285: int postion = tempStr.indexOf(":");
286: if (postion > -1) {
287: attr = tempStr.substring(0, postion).trim();
288: body = tempStr.substring(postion + 1).trim();
289:
290: //deal with combination lines when end with '\'
291: while (null != body
292: && body.length() > 0
293: && '\\' == body
294: .charAt(body.length() - 1)) {
295: body = body.substring(0, body.length() - 1);
296: body += "\n";
297: tempStr = in.readLine();
298: body += tempStr;
299: }
300: }
301: //push them to hash item
302: hashItem.put(attr, body);
303:
304: //if met "=====", it means this item is finished
305: if ("=====".equals(tempStr)) {
306: //set them into result, using records number as key
307: result.put(new Long(records), hashItem);
308: //create another hash item and continue
309: hashItem = new HashMap();
310: records++;
311: continue;
312: }
313: }
314: } catch (UnsupportedEncodingException e) {
315: throw e;
316: } catch (FileNotFoundException e) {
317: throw e;
318: } catch (IOException e) {
319: throw e;
320: }
321: return result;
322: }
323: }
324: }
|