KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > util > JavascriptUtilsTest


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.renderkit.html.util;
17
18 import junit.framework.AssertionFailedError;
19 import junit.framework.TestCase;
20
21 /**
22  * @author Manfred Geiler (latest modification by $Author: matze $)
23  * @author Anton Koinov
24  * @version $Revision: 1.4 $ $Date: 2004/10/13 11:50:59 $
25  * $Log: JavascriptUtilsTest.java,v $
26  * Revision 1.4 2004/10/13 11:50:59 matze
27  * renamed packages to org.apache
28  *
29  * Revision 1.3 2004/07/09 02:44:55 dave0000
30  * More efficient implementation
31  *
32  * Revision 1.2 2004/07/01 22:01:09 mwessendorf
33  * ASF switch
34  *
35  * Revision 1.1 2004/04/29 14:25:20 manolito
36  * javascript function name bugfix
37  *
38  */

39 public class JavascriptUtilsTest
40         extends TestCase
41 {
42     //private static final Log log = LogFactory.getLog(JavascriptUtilsTest.class);
43

44     public JavascriptUtilsTest(String JavaDoc s)
45     {
46         super(s);
47     }
48     
49     /**
50      * We use the fact that the first 128 characters match their UTF-8 byte
51      * encoding in our code. Make sure that is really the case.
52      */

53     public void testUTF()
54     {
55         for (char c = 0; c < 128; c++)
56         {
57             byte[] b = Character.toString(c).getBytes();
58             try {
59                 assertTrue(b.length == 1 && c == b[0]);
60             } catch (AssertionFailedError e) {
61                 System.err.println(
62                     "Incompatible: " + c + " -> " + b.length + ":" + b[0]);
63                 throw e;
64             }
65         }
66     }
67     
68     public void testGetValidJavascriptName()
69     {
70         String JavaDoc s, r;
71
72         s = "x";
73         r = JavascriptUtils.getValidJavascriptName(s, false);
74         System.out.println(s + " --> " + r);
75         assertEquals("x", r);
76
77         s = "x1";
78         r = JavascriptUtils.getValidJavascriptName(s, false);
79         System.out.println(s + " --> " + r);
80         assertEquals("x1", r);
81
82         s = "x:y";
83         r = JavascriptUtils.getValidJavascriptName(s, false);
84         System.out.println(s + " --> " + r);
85         assertEquals("x_3Ay", r);
86
87         s = "x-y";
88         r = JavascriptUtils.getValidJavascriptName(s, false);
89         System.out.println(s + " --> " + r);
90         assertEquals("x_2Dy", r);
91
92         s = "x_y";
93         r = JavascriptUtils.getValidJavascriptName(s, false);
94         System.out.println(s + " --> " + r);
95         assertEquals("x_5Fy", r);
96
97         s = "x-_";
98         r = JavascriptUtils.getValidJavascriptName(s, false);
99         System.out.println(s + " --> " + r);
100         assertEquals("x_2D_5F", r);
101
102         s = "a:b:c:d";
103         r = JavascriptUtils.getValidJavascriptName(s, false);
104         System.out.println(s + " --> " + r);
105         assertEquals("a_3Ab_3Ac_3Ad", r);
106
107         s = "x-1:y-2";
108         r = JavascriptUtils.getValidJavascriptName(s, false);
109         System.out.println(s + " --> " + r);
110         assertEquals("x_2D1_3Ay_2D2", r);
111
112         s = "x\r\n";
113         r = JavascriptUtils.getValidJavascriptName(s, false);
114         System.out.println(s + " --> " + r);
115         assertEquals("x_0D_0A", r);
116
117         s = "x\u2297";
118         r = JavascriptUtils.getValidJavascriptName(s, false);
119         System.out.println(s + " --> " + r);
120         assertEquals("x_E28A97", r);
121
122         s = "if";
123         r = JavascriptUtils.getValidJavascriptName(s, false);
124         System.out.println(s + " --> " + r);
125         assertEquals("if", r);
126
127         s = "if";
128         r = JavascriptUtils.getValidJavascriptName(s, true);
129         System.out.println(s + " --> " + r);
130         assertEquals("if_", r);
131     }
132 }
133
Popular Tags