1 16 package net.sf.cglib.util; 17 18 import net.sf.cglib.CodeGenTestCase; 19 import junit.framework.*; 20 21 public class TestXmlParsing extends CodeGenTestCase { 22 private static final String ATTRIBUTE_STR = "attribute"; 23 private static final String ATTRIBUTE_DIRECTIVE_STR = "directive.attribute"; 24 private static final String BODY_STR = "body"; 25 private static final String DECLARATION_STR = "declaration"; 26 private static final String DOBODY_STR = "doBody"; 27 private static final String ELEMENT_STR = "element"; 28 private static final String FALLBACK_STR = "fallback"; 29 private static final String FORWARD_STR = "forward"; 30 private static final String GET_PROPERTY_STR = "getProperty"; 31 private static final String INCLUDE_STR = "include"; 32 private static final String INCLUDE_DIRECTIVE_STR = "directive.include"; 33 private static final String INVOKE_STR = "invoke"; 34 private static final String OUTPUT_STR = "output"; 35 private static final String PAGE_DIRECTIVE_STR = "directive.page"; 36 private static final String PARAMS_STR = "params"; 37 private static final String PARAM_STR = "param"; 38 private static final String PLUGIN_STR = "plugin"; 39 private static final String ROOT_STR = "root"; 40 private static final String SET_PROPERTY_STR = "setProperty"; 41 private static final String TAG_DIRECTIVE_STR = "directive.tag"; 42 private static final String TEXT_STR = "text"; 43 private static final String USE_BEAN_STR = "useBean"; 44 private static final String VARIABLE_DIRECTIVE_STR = "variable"; 45 46 private static final int ATTRIBUTE_IDX = 0; 47 private static final int ATTRIBUTE_DIRECTIVE_IDX = 1; 48 private static final int BODY_IDX = 2; 49 private static final int DECLARATION_IDX = 3; 50 private static final int DOBODY_IDX = 4; 51 private static final int ELEMENT_IDX = 5; 52 private static final int FALLBACK_IDX = 6; 53 private static final int FORWARD_IDX = 7; 54 private static final int GET_PROPERTY_IDX = 8; 55 private static final int INCLUDE_IDX = 9; 56 private static final int INCLUDE_DIRECTIVE_IDX = 10; 57 private static final int INVOKE_IDX = 11; 58 private static final int OUTPUT_IDX = 12; 59 private static final int PAGE_DIRECTIVE_IDX = 13; 60 private static final int PARAMS_IDX = 14; 61 private static final int PARAM_IDX = 15; 62 private static final int PLUGIN_IDX = 16; 63 private static final int ROOT_IDX = 17; 64 private static final int SET_PROPERTY_IDX = 18; 65 private static final int TAG_DIRECTIVE_IDX = 19; 66 private static final int TEXT_IDX = 20; 67 private static final int USE_BEAN_IDX = 21; 68 private static final int VARIABLE_DIRECTIVE_IDX = 22; 69 70 private static final String [] M1 = { 71 ATTRIBUTE_STR, 72 ATTRIBUTE_DIRECTIVE_STR, 73 BODY_STR, 74 DECLARATION_STR, 75 DOBODY_STR, 76 ELEMENT_STR, 77 FALLBACK_STR, 78 FORWARD_STR, 79 GET_PROPERTY_STR, 80 INCLUDE_STR, 81 INCLUDE_DIRECTIVE_STR, 82 INVOKE_STR, 83 OUTPUT_STR, 84 PAGE_DIRECTIVE_STR, 85 PARAMS_STR, 86 PARAM_STR, 87 PLUGIN_STR, 88 ROOT_STR, 89 SET_PROPERTY_STR, 90 TAG_DIRECTIVE_STR, 91 TEXT_STR, 92 USE_BEAN_STR, 93 VARIABLE_DIRECTIVE_STR 94 }; 95 96 private static final int[] M2 = { 97 ATTRIBUTE_IDX, 98 ATTRIBUTE_DIRECTIVE_IDX, 99 BODY_IDX, 100 DECLARATION_IDX, 101 DOBODY_IDX, 102 ELEMENT_IDX, 103 FALLBACK_IDX, 104 FORWARD_IDX, 105 GET_PROPERTY_IDX, 106 INCLUDE_IDX, 107 INCLUDE_DIRECTIVE_IDX, 108 INVOKE_IDX, 109 OUTPUT_IDX, 110 PAGE_DIRECTIVE_IDX, 111 PARAMS_IDX, 112 PARAM_IDX, 113 PLUGIN_IDX, 114 ROOT_IDX, 115 SET_PROPERTY_IDX, 116 TAG_DIRECTIVE_IDX, 117 TEXT_IDX, 118 USE_BEAN_IDX, 119 VARIABLE_DIRECTIVE_IDX 120 }; 121 122 private static final StringSwitcher SWITCHER = StringSwitcher.create(M1, M2, true); 123 124 public int switcher(String s) { 125 return SWITCHER.intValue(s); 126 } 127 128 public int interned(String s) { 129 if (s == ATTRIBUTE_STR) { 130 return ATTRIBUTE_IDX; 131 } else if (s == ATTRIBUTE_DIRECTIVE_STR) { 132 return ATTRIBUTE_DIRECTIVE_IDX; 133 } else if (s == BODY_STR) { 134 return BODY_IDX; 135 } else if (s == DECLARATION_STR) { 136 return DECLARATION_IDX; 137 } else if (s == DOBODY_STR) { 138 return DOBODY_IDX; 139 } else if (s == ELEMENT_STR) { 140 return ELEMENT_IDX; 141 } else if (s == FALLBACK_STR) { 142 return FALLBACK_IDX; 143 } else if (s == FORWARD_STR) { 144 return FORWARD_IDX; 145 } else if (s == GET_PROPERTY_STR) { 146 return GET_PROPERTY_IDX; 147 } else if (s == INCLUDE_STR) { 148 return INCLUDE_IDX; 149 } else if (s == INCLUDE_DIRECTIVE_STR) { 150 return INCLUDE_DIRECTIVE_IDX; 151 } else if (s == INVOKE_STR) { 152 return INVOKE_IDX; 153 } else if (s == OUTPUT_STR) { 154 return OUTPUT_IDX; 155 } else if (s == PAGE_DIRECTIVE_STR) { 156 return PAGE_DIRECTIVE_IDX; 157 } else if (s == PARAMS_STR) { 158 return PARAMS_IDX; 159 } else if (s == PARAM_STR) { 160 return PARAM_IDX; 161 } else if (s == PLUGIN_STR) { 162 return PLUGIN_IDX; 163 } else if (s == ROOT_STR) { 164 return ROOT_IDX; 165 } else if (s == SET_PROPERTY_STR) { 166 return SET_PROPERTY_IDX; 167 } else if (s == TAG_DIRECTIVE_STR) { 168 return TAG_DIRECTIVE_IDX; 169 } else if (s == TEXT_STR) { 170 return TEXT_IDX; 171 } else if (s == USE_BEAN_STR) { 172 return USE_BEAN_IDX; 173 } else if (s == VARIABLE_DIRECTIVE_STR) { 174 return VARIABLE_DIRECTIVE_IDX; 175 } 176 return -1; 177 } 178 179 public int elseIf(String s) { 180 if (s.equals(ATTRIBUTE_STR)) { 181 return ATTRIBUTE_IDX; 182 } else if (s.equals(ATTRIBUTE_DIRECTIVE_STR)) { 183 return ATTRIBUTE_DIRECTIVE_IDX; 184 } else if (s.equals(BODY_STR)) { 185 return BODY_IDX; 186 } else if (s.equals(DECLARATION_STR)) { 187 return DECLARATION_IDX; 188 } else if (s.equals(DOBODY_STR)) { 189 return DOBODY_IDX; 190 } else if (s.equals(ELEMENT_STR)) { 191 return ELEMENT_IDX; 192 } else if (s.equals(FALLBACK_STR)) { 193 return FALLBACK_IDX; 194 } else if (s.equals(FORWARD_STR)) { 195 return FORWARD_IDX; 196 } else if (s.equals(GET_PROPERTY_STR)) { 197 return GET_PROPERTY_IDX; 198 } else if (s.equals(INCLUDE_STR)) { 199 return INCLUDE_IDX; 200 } else if (s.equals(INCLUDE_DIRECTIVE_STR)) { 201 return INCLUDE_DIRECTIVE_IDX; 202 } else if (s.equals(INVOKE_STR)) { 203 return INVOKE_IDX; 204 } else if (s.equals(OUTPUT_STR)) { 205 return OUTPUT_IDX; 206 } else if (s.equals(PAGE_DIRECTIVE_STR)) { 207 return PAGE_DIRECTIVE_IDX; 208 } else if (s.equals(PARAMS_STR)) { 209 return PARAMS_IDX; 210 } else if (s.equals(PARAM_STR)) { 211 return PARAM_IDX; 212 } else if (s.equals(PLUGIN_STR)) { 213 return PLUGIN_IDX; 214 } else if (s.equals(ROOT_STR)) { 215 return ROOT_IDX; 216 } else if (s.equals(SET_PROPERTY_STR)) { 217 return SET_PROPERTY_IDX; 218 } else if (s.equals(TAG_DIRECTIVE_STR)) { 219 return TAG_DIRECTIVE_IDX; 220 } else if (s.equals(TEXT_STR)) { 221 return TEXT_IDX; 222 } else if (s.equals(USE_BEAN_STR)) { 223 return USE_BEAN_IDX; 224 } else if (s.equals(VARIABLE_DIRECTIVE_STR)) { 225 return VARIABLE_DIRECTIVE_IDX; 226 } 227 return -1; 228 } 229 230 public void testStartElement() throws Throwable { 231 int numWords = 10000; 232 int reps = 1000; 233 234 String [] words = new String [numWords]; 235 String [] interned = new String [numWords]; 236 for (int i = 0; i < words.length; i++) { 237 interned[i] = M1[(int)(Math.random() * M1.length)].intern(); 238 words[i] = new String (interned[i]); 239 } 240 long total1 = 0; 241 long total2 = 0; 242 long total3 = 0; 243 for (int i = 0; i < reps; i++) { 245 for (int j = 0; j < numWords; j++) { 246 total1 += elseIf(words[j]); 247 total2 += interned(interned[j]); 248 total3 += switcher(words[j]); 249 } 250 } 251 if (total1 != total2 || total1 != total3) { 252 fail("totals are not equal"); 253 } 254 255 long t0 = System.currentTimeMillis(); 256 for (int i = 0; i < reps; i++) { 257 for (int j = 0; j < numWords; j++) { 258 elseIf(words[j]); 259 } 260 } 261 long t1 = System.currentTimeMillis(); 262 for (int i = 0; i < reps; i++) { 263 for (int j = 0; j < numWords; j++) { 264 interned(interned[j]); 265 } 266 } 267 long t2 = System.currentTimeMillis(); 268 for (int i = 0; i < reps; i++) { 269 for (int j = 0; j < numWords; j++) { 270 switcher(words[j]); 271 } 272 } 273 long t3 = System.currentTimeMillis(); 274 275 System.err.println("elseif: " + (t1 - t0) + "ms"); 276 System.err.println("intern: " + (t2 - t1) + "ms"); 277 System.err.println("switch: " + (t3 - t2) + "ms"); 278 } 279 280 public TestXmlParsing(String testName) { 281 super(testName); 282 } 283 284 public static void main(String [] args) { 285 junit.textui.TestRunner.run(suite()); 286 } 287 288 public static Test suite() { 289 return new TestSuite(TestXmlParsing.class); 290 } 291 292 public void perform(ClassLoader loader) throws Throwable { 293 } 294 295 public void testFailOnMemoryLeak() throws Throwable { 296 } 297 298 } 299 | Popular Tags |