1 16 package org.apache.myfaces.renderkit.html.util; 17 18 import junit.framework.AssertionFailedError; 19 import junit.framework.TestCase; 20 21 39 public class JavascriptUtilsTest 40 extends TestCase 41 { 42 44 public JavascriptUtilsTest(String s) 45 { 46 super(s); 47 } 48 49 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 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 |