]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/stringprep/TestIDNARef.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / stringprep / TestIDNARef.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2003-2005, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6 */\r
7 package com.ibm.icu.dev.test.stringprep;\r
8 \r
9 import com.ibm.icu.dev.test.TestFmwk;\r
10 import com.ibm.icu.text.StringPrepParseException;\r
11 import com.ibm.icu.text.UCharacterIterator;\r
12 \r
13 /**\r
14  * @author ram\r
15  *\r
16  * To change the template for this generated type comment go to\r
17  * Window>Preferences>Java>Code Generation>Code and Comments\r
18  */\r
19 public class TestIDNARef extends TestFmwk {\r
20     public static void main(String[] args) throws Exception {\r
21         new TestIDNARef().run(args);\r
22     }\r
23     private StringPrepParseException unassignedException = new StringPrepParseException("",StringPrepParseException.UNASSIGNED_ERROR);\r
24     public void TestToUnicode() throws Exception{\r
25         try{\r
26             for(int i=0; i<TestData.asciiIn.length; i++){\r
27                 // test StringBuffer toUnicode\r
28                 doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.DEFAULT, null);\r
29                 doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.ALLOW_UNASSIGNED, null);\r
30                 //doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.USE_STD3_RULES, null); \r
31                 //doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.USE_STD3_RULES|IDNAReference.ALLOW_UNASSIGNED, null); \r
32         \r
33             }\r
34         }catch(java.lang.ExceptionInInitializerError ex){\r
35             warnln("Could not load NamePrepTransform data");\r
36         }catch(java.lang.NoClassDefFoundError ex){\r
37             warnln("Could not load NamePrepTransform data");\r
38         } \r
39     }\r
40     \r
41     public void TestToASCII() throws Exception{\r
42         try{\r
43             for(int i=0; i<TestData.asciiIn.length; i++){\r
44                 // test StringBuffer toUnicode\r
45                 doTestToASCII(new String(TestData.unicodeIn[i]),TestData.asciiIn[i],IDNAReference.DEFAULT, null);\r
46                 doTestToASCII(new String(TestData.unicodeIn[i]),TestData.asciiIn[i],IDNAReference.ALLOW_UNASSIGNED, null);\r
47                 //doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.USE_STD3_RULES, null); \r
48                 //doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.USE_STD3_RULES|IDNAReference.ALLOW_UNASSIGNED, null); \r
49         \r
50             }\r
51         }catch(java.lang.ExceptionInInitializerError ex){\r
52             warnln("Could not load NamePrepTransform data");\r
53         }catch(java.lang.NoClassDefFoundError ex){\r
54             warnln("Could not load NamePrepTransform data");\r
55         } \r
56     }\r
57     \r
58     public void TestIDNToASCII() throws Exception{\r
59         try{\r
60             for(int i=0; i<TestData.domainNames.length; i++){\r
61                 doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.DEFAULT, null);\r
62                 doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.ALLOW_UNASSIGNED, null);\r
63                 doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.USE_STD3_RULES, null);\r
64                 doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.ALLOW_UNASSIGNED|IDNAReference.USE_STD3_RULES, null);\r
65             }\r
66             \r
67             for(int i=0; i<TestData.domainNames1Uni.length; i++){\r
68                 doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNAReference.DEFAULT, null);\r
69                 doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNAReference.ALLOW_UNASSIGNED, null);\r
70                 doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNAReference.USE_STD3_RULES, null);\r
71                 doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNAReference.ALLOW_UNASSIGNED|IDNAReference.USE_STD3_RULES, null);\r
72     \r
73             }\r
74         }catch(java.lang.ExceptionInInitializerError ex){\r
75             warnln("Could not load NamePrepTransform data");\r
76         }catch(java.lang.NoClassDefFoundError ex){\r
77             warnln("Could not load NamePrepTransform data");\r
78         } \r
79     }\r
80     public void TestIDNToUnicode() throws Exception{\r
81         try{\r
82             for(int i=0; i<TestData.domainNames.length; i++){\r
83                 doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.DEFAULT, null);\r
84                 doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.ALLOW_UNASSIGNED, null);\r
85                 doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.USE_STD3_RULES, null);\r
86                 doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.ALLOW_UNASSIGNED|IDNAReference.USE_STD3_RULES, null);\r
87             }\r
88             for(int i=0; i<TestData.domainNamesToASCIIOut.length; i++){\r
89                 doTestIDNToUnicode(TestData.domainNamesToASCIIOut[i],TestData.domainNamesToUnicodeOut[i],IDNAReference.DEFAULT, null);\r
90                 doTestIDNToUnicode(TestData.domainNamesToASCIIOut[i],TestData.domainNamesToUnicodeOut[i],IDNAReference.ALLOW_UNASSIGNED, null);\r
91             }\r
92         }catch(java.lang.ExceptionInInitializerError ex){\r
93             warnln("Could not load NamePrepTransform data");\r
94         }catch(java.lang.NoClassDefFoundError ex){\r
95             warnln("Could not load NamePrepTransform data");\r
96         } \r
97     }\r
98     \r
99     private void doTestToUnicode(String src, String expected, int options, Object expectedException) \r
100                 throws Exception{\r
101         StringBuffer inBuf = new StringBuffer(src);\r
102         UCharacterIterator inIter = UCharacterIterator.getInstance(src);\r
103         try{\r
104             \r
105             StringBuffer out = IDNAReference.convertToUnicode(src,options);\r
106             if(expected!=null && out != null && !out.toString().equals(expected)){\r
107                 errln("convertToUnicode did not return expected result with options : "+ options + \r
108                       " Expected: " + prettify(expected)+" Got: "+prettify(out));\r
109             }\r
110             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
111                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");\r
112             }\r
113         }catch(StringPrepParseException ex){\r
114             if(expectedException == null || !ex.equals(expectedException)){\r
115                 errln("convertToUnicode did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());\r
116             }\r
117         }\r
118         try{\r
119             \r
120             StringBuffer out = IDNAReference.convertToUnicode(inBuf,options);\r
121             if(expected!=null && out != null && !out.toString().equals(expected)){\r
122                errln("convertToUnicode did not return expected result with options : "+ options + \r
123                      " Expected: " + prettify(expected)+" Got: "+out);\r
124             }\r
125             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
126                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");\r
127             }\r
128         }catch(StringPrepParseException ex){\r
129             if(expectedException == null || !ex.equals(expectedException)){\r
130                 errln("convertToUnicode did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());\r
131             }\r
132         }\r
133         \r
134         try{\r
135             StringBuffer out = IDNAReference.convertToUnicode(inIter,options);\r
136             if(expected!=null && out != null && !out.toString().equals(expected)){\r
137                errln("convertToUnicode did not return expected result with options : "+ options +\r
138                      " Expected: " + prettify(expected)+" Got: "+prettify(out));\r
139             }\r
140             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
141                 errln("Did not get the expected exception. The operation succeeded!");\r
142             }\r
143         }catch(StringPrepParseException ex){\r
144             if(expectedException == null || !ex.equals(expectedException)){\r
145                 errln("Did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());\r
146             }\r
147         }\r
148     }\r
149     \r
150     private void doTestIDNToUnicode(String src, String expected, int options, Object expectedException) \r
151                 throws Exception{\r
152         StringBuffer inBuf = new StringBuffer(src);\r
153         UCharacterIterator inIter = UCharacterIterator.getInstance(src);\r
154         try{\r
155             \r
156             StringBuffer out = IDNAReference.convertIDNToUnicode(src,options);\r
157             if(expected!=null && out != null && !out.toString().equals(expected)){\r
158                 errln("convertToUnicode did not return expected result with options : "+ options + \r
159                       " Expected: " + prettify(expected)+" Got: "+prettify(out));\r
160             }\r
161             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
162                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");\r
163             }\r
164         }catch(StringPrepParseException ex){\r
165             if(expectedException == null || !expectedException.equals(ex)){\r
166                 errln("convertToUnicode did not get the expected exception for source: " +src +" Got:  "+ ex.toString());\r
167             }\r
168         }\r
169         try{\r
170             StringBuffer out = IDNAReference.convertIDNToUnicode(inBuf,options);\r
171             if(expected!=null && out != null && !out.toString().equals(expected)){\r
172                errln("convertToUnicode did not return expected result with options : "+ options + \r
173                      " Expected: " + prettify(expected)+" Got: "+out);\r
174             }\r
175             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
176                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");\r
177             }\r
178         }catch(StringPrepParseException ex){\r
179             if(expectedException == null || !expectedException.equals(ex)){\r
180                 errln("convertToUnicode did not get the expected exception for source: " +src +" Got:  "+ ex.toString());\r
181             }\r
182         }\r
183         \r
184         try{\r
185             StringBuffer out = IDNAReference.convertIDNToUnicode(inIter,options);\r
186             if(expected!=null && out != null && !out.toString().equals(expected)){\r
187                errln("convertToUnicode did not return expected result with options : "+ options +\r
188                      " Expected: " + prettify(expected)+" Got: "+prettify(out));\r
189             }\r
190             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
191                 errln("Did not get the expected exception. The operation succeeded!");\r
192             }\r
193         }catch(StringPrepParseException ex){\r
194             if(expectedException == null || !expectedException.equals(ex)){\r
195                 errln("Did not get the expected exception for source: " +src +" Got:  "+ ex.toString());\r
196             }\r
197         }\r
198     }\r
199     private void doTestToASCII(String src, String expected, int options, Object expectedException) \r
200                 throws Exception{\r
201         StringBuffer inBuf = new StringBuffer(src);\r
202         UCharacterIterator inIter = UCharacterIterator.getInstance(src);\r
203         try{\r
204             \r
205             StringBuffer out = IDNAReference.convertToASCII(src,options);\r
206             if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){\r
207                 errln("convertToASCII did not return expected result with options : "+ options + \r
208                       " Expected: " + expected+" Got: "+out);\r
209             }           \r
210             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
211                 errln("convertToASCII did not get the expected exception. The operation succeeded!");\r
212             }\r
213         }catch(StringPrepParseException ex){\r
214             if(expectedException == null || !expectedException.equals(ex)){\r
215                 errln("convertToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());\r
216             }\r
217         }\r
218         \r
219         try{            \r
220             StringBuffer out = IDNAReference.convertToASCII(inBuf,options);\r
221             if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){\r
222                errln("convertToASCII did not return expected result with options : "+ options + \r
223                      " Expected: " + expected+" Got: "+out);\r
224             }\r
225             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
226                 errln("convertToASCII did not get the expected exception. The operation succeeded!");\r
227             }\r
228         }catch(StringPrepParseException ex){\r
229             if(expectedException == null || !expectedException.equals(ex)){\r
230                 errln("convertToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());\r
231             }\r
232         }\r
233         \r
234         try{\r
235             StringBuffer out = IDNAReference.convertToASCII(inIter,options);\r
236             if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){\r
237                errln("convertToASCII did not return expected result with options : "+ options +\r
238                      " Expected: " + expected+" Got: "+ out);\r
239             }\r
240             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
241                 errln("convertToASCII did not get the expected exception. The operation succeeded!");\r
242             }\r
243         }catch(StringPrepParseException ex){\r
244             if(expectedException == null || !expectedException.equals(ex)){\r
245                 errln("convertToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());\r
246             }\r
247         }\r
248     }\r
249     private void doTestIDNToASCII(String src, String expected, int options, Object expectedException) \r
250                 throws Exception{\r
251         \r
252         StringBuffer inBuf = new StringBuffer(src);\r
253         UCharacterIterator inIter = UCharacterIterator.getInstance(src);\r
254         try{\r
255             \r
256             StringBuffer out = IDNAReference.convertIDNToASCII(src,options);\r
257             if(expected!=null && out != null && !out.toString().equals(expected)){\r
258                 errln("convertToIDNAReferenceASCII did not return expected result with options : "+ options + \r
259                       " Expected: " + expected+" Got: "+out);\r
260             }\r
261             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
262                 errln("convertToIDNAReferenceASCII did not get the expected exception. The operation succeeded!");\r
263             }\r
264         }catch(StringPrepParseException ex){\r
265             if(expectedException == null || !ex.equals(expectedException)){\r
266                 errln("convertToIDNAReferenceASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());\r
267             }\r
268         }\r
269         try{\r
270             StringBuffer out = IDNAReference.convertIDNtoASCII(inBuf,options);\r
271             if(expected!=null && out != null && !out.toString().equals(expected)){\r
272                errln("convertToIDNAReferenceASCII did not return expected result with options : "+ options + \r
273                      " Expected: " + expected+" Got: "+out);\r
274             }           \r
275             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
276                 errln("convertToIDNAReferenceSCII did not get the expected exception. The operation succeeded!");\r
277             }\r
278         }catch(StringPrepParseException ex){\r
279             if(expectedException == null || !ex.equals(expectedException)){\r
280                 errln("convertToIDNAReferenceSCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());\r
281             }\r
282         }\r
283         \r
284         try{\r
285             StringBuffer out = IDNAReference.convertIDNtoASCII(inIter,options);\r
286             if(expected!=null && out != null && !out.toString().equals(expected)){\r
287                errln("convertIDNToASCII did not return expected result with options : "+ options +\r
288                      " Expected: " + expected+" Got: "+ out);\r
289             }\r
290             \r
291             if(expectedException!=null && !unassignedException.equals(expectedException)){\r
292                 errln("convertIDNToASCII did not get the expected exception. The operation succeeded!");\r
293             }\r
294         }catch(StringPrepParseException ex){\r
295             if(expectedException == null || !ex.equals(expectedException)){\r
296                 errln("convertIDNToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());\r
297             }\r
298         }\r
299     }\r
300     public void TestConformance()throws Exception{\r
301         try{\r
302             for(int i=0; i<TestData.conformanceTestCases.length;i++){\r
303                 \r
304                 TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];\r
305                 if(testCase.expected != null){\r
306                     //Test toASCII\r
307                     doTestToASCII(testCase.input,testCase.output,IDNAReference.DEFAULT,testCase.expected);\r
308                     doTestToASCII(testCase.input,testCase.output,IDNAReference.ALLOW_UNASSIGNED,testCase.expected);\r
309                 }\r
310                 //Test toUnicode\r
311                 //doTestToUnicode(testCase.input,testCase.output,IDNAReference.DEFAULT,testCase.expected);\r
312             }\r
313         }catch(java.lang.ExceptionInInitializerError ex){\r
314             warnln("Could not load NamePrepTransform data");\r
315         }catch(java.lang.NoClassDefFoundError ex){\r
316             warnln("Could not load NamePrepTransform data");\r
317         } \r
318     }\r
319     public void TestNamePrepConformance() throws Exception{\r
320         try{\r
321             NamePrepTransform namePrep = NamePrepTransform.getInstance();\r
322             for(int i=0; i<TestData.conformanceTestCases.length;i++){\r
323                 TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];\r
324                 UCharacterIterator iter = UCharacterIterator.getInstance(testCase.input);\r
325                 try{\r
326                     StringBuffer output = namePrep.prepare(iter,NamePrepTransform.NONE);\r
327                     if(testCase.output !=null && output!=null && !testCase.output.equals(output.toString())){\r
328                         errln("Did not get the expected output. Expected: " + prettify(testCase.output)+\r
329                               " Got: "+ prettify(output) );\r
330                     }\r
331                     if(testCase.expected!=null && !unassignedException.equals(testCase.expected)){\r
332                         errln("Did not get the expected exception. The operation succeeded!");\r
333                     }\r
334                 }catch(StringPrepParseException ex){\r
335                     if(testCase.expected == null || !ex.equals(testCase.expected)){\r
336                         errln("Did not get the expected exception for source: " +testCase.input +" Got:  "+ ex.toString());\r
337                     }\r
338                 }\r
339                 \r
340                 try{\r
341                     iter.setToStart();\r
342                     StringBuffer output = namePrep.prepare(iter,NamePrepTransform.ALLOW_UNASSIGNED);\r
343                     if(testCase.output !=null && output!=null && !testCase.output.equals(output.toString())){\r
344                         errln("Did not get the expected output. Expected: " + prettify(testCase.output)+\r
345                               " Got: "+ prettify(output) );\r
346                     }\r
347                     if(testCase.expected!=null && !unassignedException.equals(testCase.expected)){\r
348                         errln("Did not get the expected exception. The operation succeeded!");\r
349                     }\r
350                 }catch(StringPrepParseException ex){\r
351                     if(testCase.expected == null || !ex.equals(testCase.expected)){\r
352                         errln("Did not get the expected exception for source: " +testCase.input +" Got:  "+ ex.toString());\r
353                     }\r
354                 }\r
355             }\r
356         }catch(java.lang.ExceptionInInitializerError e){\r
357             warnln("Could not load NamePrepTransformData");\r
358         }catch(java.lang.NoClassDefFoundError ex){\r
359             warnln("Could not load NamePrepTransform data");\r
360         } \r
361         \r
362     }\r
363     public void TestErrorCases() throws Exception{\r
364         try{\r
365             for(int i=0; i < TestData.errorCases.length; i++){\r
366                 TestData.ErrorCase errCase = TestData.errorCases[i];\r
367                 if(errCase.testLabel==true){\r
368                     // Test ToASCII\r
369                     doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.DEFAULT,errCase.expected);\r
370                     doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.ALLOW_UNASSIGNED,errCase.expected);\r
371                     if(errCase.useSTD3ASCIIRules){\r
372                         doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.USE_STD3_RULES,errCase.expected);\r
373                     }\r
374                 }\r
375                 if(errCase.useSTD3ASCIIRules!=true){\r
376                     \r
377                     // Test IDNToASCII\r
378                     doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.DEFAULT,errCase.expected);\r
379                     doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.ALLOW_UNASSIGNED,errCase.expected);\r
380                     \r
381                 }else{\r
382                     doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.USE_STD3_RULES,errCase.expected);\r
383                 }\r
384                 \r
385                 //TestToUnicode\r
386                 if(errCase.testToUnicode==true){\r
387                     if(errCase.useSTD3ASCIIRules!=true){\r
388                         // Test IDNToUnicode\r
389                         doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNAReference.DEFAULT,errCase.expected);\r
390                         doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNAReference.ALLOW_UNASSIGNED,errCase.expected);\r
391                     \r
392                     }else{\r
393                         doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNAReference.USE_STD3_RULES,errCase.expected);\r
394                     }\r
395                 }\r
396             }\r
397         }catch(java.lang.ExceptionInInitializerError ex){\r
398             warnln("Could not load NamePrepTransform data");\r
399         }catch(java.lang.NoClassDefFoundError ex){\r
400             warnln("Could not load NamePrepTransform data");\r
401         } \r
402     }\r
403     private void doTestCompare(String s1, String s2, boolean isEqual){\r
404         try{\r
405             int retVal = IDNAReference.compare(s1,s2,IDNAReference.DEFAULT);\r
406             if(isEqual==true && retVal != 0){\r
407                 errln("Did not get the expected result for s1: "+ prettify(s1)+ \r
408                       " s2: "+prettify(s2));\r
409             }\r
410             retVal = IDNAReference.compare(new StringBuffer(s1), new StringBuffer(s2), IDNAReference.DEFAULT);\r
411             if(isEqual==true && retVal != 0){\r
412                 errln("Did not get the expected result for s1: "+ prettify(s1)+ \r
413                      " s2: "+prettify(s2));\r
414             }\r
415             retVal = IDNAReference.compare(UCharacterIterator.getInstance(s1), UCharacterIterator.getInstance(s2), IDNAReference.DEFAULT);\r
416             if(isEqual==true && retVal != 0){\r
417                 errln("Did not get the expected result for s1: "+ prettify(s1)+ \r
418                      " s2: "+prettify(s2));\r
419             }\r
420         }catch(Exception e){\r
421             e.printStackTrace();\r
422             errln("Unexpected exception thrown by IDNAReference.compare");\r
423         }\r
424         \r
425         try{\r
426             int retVal = IDNAReference.compare(s1,s2,IDNAReference.ALLOW_UNASSIGNED);\r
427             if(isEqual==true && retVal != 0){\r
428                 errln("Did not get the expected result for s1: "+ prettify(s1)+ \r
429                       " s2: "+prettify(s2));\r
430             }\r
431             retVal = IDNAReference.compare(new StringBuffer(s1), new StringBuffer(s2), IDNAReference.ALLOW_UNASSIGNED);\r
432             if(isEqual==true && retVal != 0){\r
433                 errln("Did not get the expected result for s1: "+ prettify(s1)+ \r
434                      " s2: "+prettify(s2));\r
435             }\r
436             retVal = IDNAReference.compare(UCharacterIterator.getInstance(s1), UCharacterIterator.getInstance(s2), IDNAReference.ALLOW_UNASSIGNED);\r
437             if(isEqual==true && retVal != 0){\r
438                 errln("Did not get the expected result for s1: "+ prettify(s1)+ \r
439                      " s2: "+prettify(s2));\r
440             }\r
441         }catch(Exception e){\r
442             errln("Unexpected exception thrown by IDNAReference.compare");\r
443         }     \r
444     }\r
445     public void TestCompare() throws Exception{\r
446         String www = "www.";\r
447         String com = ".com";\r
448         StringBuffer source = new StringBuffer(www);\r
449         StringBuffer uni0   = new StringBuffer(www);\r
450         StringBuffer uni1   = new StringBuffer(www);\r
451         StringBuffer ascii0 = new StringBuffer(www);\r
452         StringBuffer ascii1 = new StringBuffer(www);\r
453 \r
454         uni0.append(TestData.unicodeIn[0]);\r
455         uni0.append(com);\r
456 \r
457         uni1.append(TestData.unicodeIn[1]);\r
458         uni1.append(com);\r
459 \r
460         ascii0.append(TestData.asciiIn[0]);\r
461         ascii0.append(com);\r
462 \r
463         ascii1.append(TestData.asciiIn[1]);\r
464         ascii1.append(com);\r
465         try{\r
466             for(int i=0;i< TestData.unicodeIn.length; i++){\r
467     \r
468                 // for every entry in unicodeIn array\r
469                 // prepend www. and append .com\r
470                 source.setLength(4);\r
471                 source.append(TestData.unicodeIn[i]);\r
472                 source.append(com);\r
473                 \r
474                 // a) compare it with itself\r
475                 doTestCompare(source.toString(),source.toString(),true);\r
476             \r
477                 // b) compare it with asciiIn equivalent\r
478                 doTestCompare(source.toString(),www+TestData.asciiIn[i]+com,true);\r
479             \r
480                 // c) compare it with unicodeIn not equivalent\r
481                 if(i==0){\r
482                     doTestCompare(source.toString(), uni1.toString(), false);\r
483                 }else{\r
484                     doTestCompare(source.toString(),uni0.toString(), false);\r
485                 }\r
486                 // d) compare it with asciiIn not equivalent\r
487                 if(i==0){\r
488                     doTestCompare(source.toString(),ascii1.toString(), false);\r
489                 }else{\r
490                     doTestCompare(source.toString(),ascii0.toString(), false);\r
491                 }\r
492             }\r
493         }catch(java.lang.ExceptionInInitializerError ex){\r
494             warnln("Could not load NamePrepTransform data");\r
495         }catch(java.lang.NoClassDefFoundError ex){\r
496             warnln("Could not load NamePrepTransform data");\r
497         } \r
498     }\r
499 \r
500     //  test and ascertain\r
501     //  func(func(func(src))) == func(src)\r
502     public void doTestChainingToASCII(String source)throws Exception{\r
503         StringBuffer expected; \r
504         StringBuffer chained;\r
505         \r
506         // test convertIDNToASCII\r
507         expected = IDNAReference.convertIDNToASCII(source,IDNAReference.DEFAULT);\r
508         chained = expected;\r
509         for(int i=0; i< 4; i++){\r
510             chained = IDNAReference.convertIDNtoASCII(chained,IDNAReference.DEFAULT);\r
511         }\r
512         if(!expected.toString().equals(chained.toString())){\r
513             errln("Chaining test failed for convertIDNToASCII");\r
514         }\r
515         // test convertIDNToA\r
516         expected = IDNAReference.convertToASCII(source,IDNAReference.DEFAULT);\r
517         chained = expected;\r
518         for(int i=0; i< 4; i++){\r
519             chained = IDNAReference.convertToASCII(chained,IDNAReference.DEFAULT);\r
520         }\r
521         if(!expected.toString().equals(chained.toString())){\r
522             errln("Chaining test failed for convertToASCII");\r
523         } \r
524         \r
525     }\r
526     //  test and ascertain\r
527     //  func(func(func(src))) == func(src)\r
528     public void doTestChainingToUnicode(String source)throws Exception{\r
529         StringBuffer expected; \r
530         StringBuffer chained;\r
531         \r
532         // test convertIDNToUnicode\r
533         expected = IDNAReference.convertIDNToUnicode(source,IDNAReference.DEFAULT);\r
534         chained = expected;\r
535         for(int i=0; i< 4; i++){\r
536             chained = IDNAReference.convertIDNToUnicode(chained,IDNAReference.DEFAULT);\r
537         }\r
538         if(!expected.toString().equals(chained.toString())){\r
539             errln("Chaining test failed for convertIDNToUnicode");\r
540         }\r
541         // test convertIDNToA\r
542         expected = IDNAReference.convertToUnicode(source,IDNAReference.DEFAULT);\r
543         chained = expected;\r
544         for(int i=0; i< 4; i++){\r
545             chained = IDNAReference.convertToUnicode(chained,IDNAReference.DEFAULT);\r
546         }\r
547         if(!expected.toString().equals(chained.toString())){\r
548             errln("Chaining test failed for convertToUnicode");\r
549         }\r
550 \r
551     }\r
552     public void TestChaining() throws Exception{\r
553         try{\r
554             for(int i=0; i< TestData.unicodeIn.length; i++){\r
555                 doTestChainingToASCII(new String(TestData.unicodeIn[i]));\r
556             }\r
557             for(int i=0; i< TestData.asciiIn.length; i++){\r
558                 doTestChainingToUnicode(TestData.asciiIn[i]);\r
559             }\r
560         }catch(java.lang.ExceptionInInitializerError ex){\r
561             warnln("Could not load NamePrepTransform data");\r
562         }catch(java.lang.NoClassDefFoundError ex){\r
563             warnln("Could not load NamePrepTransform data");\r
564         } \r
565     }\r
566     public void TestRootLabelSeparator() throws Exception{\r
567         String www = "www.";\r
568         String com = ".com."; /*root label separator*/\r
569         StringBuffer source = new StringBuffer(www);\r
570         StringBuffer uni0   = new StringBuffer(www);\r
571         StringBuffer uni1   = new StringBuffer(www);\r
572         StringBuffer ascii0 = new StringBuffer(www);\r
573         StringBuffer ascii1 = new StringBuffer(www);\r
574 \r
575         uni0.append(TestData.unicodeIn[0]);\r
576         uni0.append(com);\r
577 \r
578         uni1.append(TestData.unicodeIn[1]);\r
579         uni1.append(com);\r
580 \r
581         ascii0.append(TestData.asciiIn[0]);\r
582         ascii0.append(com);\r
583 \r
584         ascii1.append(TestData.asciiIn[1]);\r
585         ascii1.append(com);\r
586         try{\r
587             for(int i=0;i< TestData.unicodeIn.length; i++){\r
588     \r
589                 // for every entry in unicodeIn array\r
590                 // prepend www. and append .com\r
591                 source.setLength(4);\r
592                 source.append(TestData.unicodeIn[i]);\r
593                 source.append(com);\r
594                 \r
595                 // a) compare it with itself\r
596                 doTestCompare(source.toString(),source.toString(),true);\r
597             \r
598                 // b) compare it with asciiIn equivalent\r
599                 doTestCompare(source.toString(),www+TestData.asciiIn[i]+com,true);\r
600             \r
601                 // c) compare it with unicodeIn not equivalent\r
602                 if(i==0){\r
603                     doTestCompare(source.toString(), uni1.toString(), false);\r
604                 }else{\r
605                     doTestCompare(source.toString(),uni0.toString(), false);\r
606                 }\r
607                 // d) compare it with asciiIn not equivalent\r
608                 if(i==0){\r
609                     doTestCompare(source.toString(),ascii1.toString(), false);\r
610                 }else{\r
611                     doTestCompare(source.toString(),ascii0.toString(), false);\r
612                 }\r
613             }\r
614         }catch(java.lang.ExceptionInInitializerError ex){\r
615             warnln("Could not load NamePrepTransform data");\r
616         }catch(java.lang.NoClassDefFoundError ex){\r
617             warnln("Could not load NamePrepTransform data");\r
618         } \r
619 \r
620     }\r
621 }\r