1 16 17 package org.apache.taglibs.standard.lang.jstl.test; 18 19 import java.io.BufferedInputStream ; 20 import java.io.BufferedOutputStream ; 21 import java.io.DataInput ; 22 import java.io.DataInputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.PrintStream ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import javax.servlet.jsp.JspException ; 36 import javax.servlet.jsp.PageContext ; 37 38 import org.apache.taglibs.standard.lang.jstl.Evaluator; 39 import org.apache.taglibs.standard.lang.jstl.test.beans.Factory; 40 41 58 59 public class EvaluationTest 60 { 61 65 69 74 public EvaluationTest () 75 { 76 } 77 78 84 public static void runTests (DataInput pIn, 85 PrintStream pOut) 86 throws IOException 87 { 88 PageContext context = createTestContext (); 89 90 while (true) { 91 String str = pIn.readLine (); 92 if (str == null) break; 93 if (str.startsWith ("#") || 94 "".equals (str.trim ())) { 95 pOut.println (str); 96 } 97 else { 98 String typeStr = pIn.readLine (); 99 pOut.println ("Expression: " + str); 100 101 try { 102 Class cl = parseClassName (typeStr); 103 pOut.println ("ExpectedType: " + cl); 104 Evaluator e = new Evaluator (); 105 Object val = e.evaluate ("test", str, cl, null, context); 106 pOut.println ("Evaluates to: " + val); 107 if (val != null) { 108 pOut.println ("With type: " + val.getClass ().getName ()); 109 } 110 pOut.println (); 111 } 112 catch (JspException exc) { 113 pOut.println ("Causes an error: " + exc); 114 } 115 catch (ClassNotFoundException exc) { 116 pOut.println ("Causes an error: " + exc); 117 } 118 } 119 } 120 121 } 122 123 128 static Class parseClassName (String pClassName) 129 throws ClassNotFoundException 130 { 131 String c = pClassName.trim (); 132 if ("boolean".equals (c)) { 133 return Boolean.TYPE; 134 } 135 else if ("byte".equals (c)) { 136 return Byte.TYPE; 137 } 138 else if ("char".equals (c)) { 139 return Character.TYPE; 140 } 141 else if ("short".equals (c)) { 142 return Short.TYPE; 143 } 144 else if ("int".equals (c)) { 145 return Integer.TYPE; 146 } 147 else if ("long".equals (c)) { 148 return Long.TYPE; 149 } 150 else if ("float".equals (c)) { 151 return Float.TYPE; 152 } 153 else if ("double".equals (c)) { 154 return Double.TYPE; 155 } 156 else { 157 return Class.forName (pClassName); 158 } 159 } 160 161 167 public static void runTests (File pInputFile, 168 File pOutputFile) 169 throws IOException 170 { 171 FileInputStream fin = null; 172 FileOutputStream fout = null; 173 try { 174 fin = new FileInputStream (pInputFile); 175 BufferedInputStream bin = new BufferedInputStream (fin); 176 DataInputStream din = new DataInputStream (bin); 177 178 try { 179 fout = new FileOutputStream (pOutputFile); 180 BufferedOutputStream bout = new BufferedOutputStream (fout); 181 PrintStream pout = new PrintStream (bout); 182 183 runTests (din, pout); 184 185 pout.flush (); 186 } 187 finally { 188 if (fout != null) { 189 fout.close (); 190 } 191 } 192 } 193 finally { 194 if (fin != null) { 195 fin.close (); 196 } 197 } 198 } 199 200 206 public static boolean isDifferentFiles (DataInput pIn1, 207 DataInput pIn2) 208 throws IOException 209 { 210 while (true) { 211 String str1 = pIn1.readLine (); 212 String str2 = pIn2.readLine (); 213 if (str1 == null && 214 str2 == null) { 215 return false; 216 } 217 else if (str1 == null || 218 str2 == null) { 219 return true; 220 } 221 else { 222 if (!str1.equals (str2)) { 223 return true; 224 } 225 } 226 } 227 } 228 229 235 public static boolean isDifferentFiles (File pFile1, 236 File pFile2) 237 throws IOException 238 { 239 FileInputStream fin1 = null; 240 try { 241 fin1 = new FileInputStream (pFile1); 242 BufferedInputStream bin1 = new BufferedInputStream (fin1); 243 DataInputStream din1 = new DataInputStream (bin1); 244 245 FileInputStream fin2 = null; 246 try { 247 fin2 = new FileInputStream (pFile2); 248 BufferedInputStream bin2 = new BufferedInputStream (fin2); 249 DataInputStream din2 = new DataInputStream (bin2); 250 251 return isDifferentFiles (din1, din2); 252 } 253 finally { 254 if (fin2 != null) { 255 fin2.close (); 256 } 257 } 258 } 259 finally { 260 if (fin1 != null) { 261 fin1.close (); 262 } 263 } 264 } 265 266 274 static PageContext createTestContext () 275 { 276 PageContext ret = new PageContextImpl (); 277 278 ret.setAttribute ("val1a", "page-scoped1", PageContext.PAGE_SCOPE); 280 ret.setAttribute ("val1b", "request-scoped1", PageContext.REQUEST_SCOPE); 281 ret.setAttribute ("val1c", "session-scoped1", PageContext.SESSION_SCOPE); 282 ret.setAttribute ("val1d", "app-scoped1", PageContext.APPLICATION_SCOPE); 283 284 { 286 Bean1 b1 = new Bean1 (); 287 b1.setBoolean1 (true); 288 b1.setByte1 ((byte) 12); 289 b1.setShort1 ((short) -124); 290 b1.setChar1 ('b'); 291 b1.setInt1 (4); 292 b1.setLong1 (222423); 293 b1.setFloat1 ((float) 12.4); 294 b1.setDouble1 (89.224); 295 b1.setString1 ("hello"); 296 b1.setStringArray1 (new String [] { 297 "string1", 298 "string2", 299 "string3", 300 "string4" 301 }); 302 { 303 List l = new ArrayList (); 304 l.add (new Integer (14)); 305 l.add ("another value"); 306 l.add (b1.getStringArray1 ()); 307 b1.setList1 (l); 308 } 309 { 310 Map m = new HashMap (); 311 m.put ("key1", "value1"); 312 m.put (new Integer (14), "value2"); 313 m.put (new Long (14), "value3"); 314 m.put ("recurse", b1); 315 b1.setMap1 (m); 316 } 317 ret.setAttribute ("bean1a", b1); 318 319 Bean1 b2 = new Bean1 (); 320 b2.setInt2 (new Integer (-224)); 321 b2.setString2 ("bean2's string"); 322 b1.setBean1 (b2); 323 324 Bean1 b3 = new Bean1 (); 325 b3.setDouble1 (1422.332); 326 b3.setString2 ("bean3's string"); 327 b2.setBean2 (b3); 328 } 329 330 { 332 ret.setAttribute ("pbean1", Factory.createBean1 ()); 333 ret.setAttribute ("pbean2", Factory.createBean2 ()); 334 ret.setAttribute ("pbean3", Factory.createBean3 ()); 335 ret.setAttribute ("pbean4", Factory.createBean4 ()); 336 ret.setAttribute ("pbean5", Factory.createBean5 ()); 337 ret.setAttribute ("pbean6", Factory.createBean6 ()); 338 ret.setAttribute ("pbean7", Factory.createBean7 ()); 339 } 340 341 { 343 Map m = new HashMap (); 344 m.put ("emptyArray", new Object [0]); 345 m.put ("nonemptyArray", new Object [] {"abc"}); 346 m.put ("emptyList", new ArrayList ()); 347 { 348 List l = new ArrayList (); 349 l.add ("hello"); 350 m.put ("nonemptyList", l); 351 } 352 m.put ("emptyMap", new HashMap ()); 353 { 354 Map m2 = new HashMap (); 355 m2.put ("a", "a"); 356 m.put ("nonemptyMap", m2); 357 } 358 m.put ("emptySet", new HashSet ()); 359 { 360 Set s = new HashSet (); 361 s.add ("hello"); 362 m.put ("nonemptySet", s); 363 } 364 ret.setAttribute ("emptyTests", m); 365 } 366 367 return ret; 368 } 369 370 377 public static void main (String [] pArgs) 378 throws IOException 379 { 380 if (pArgs.length != 2 && 381 pArgs.length != 3) { 382 usage (); 383 System.exit (1); 384 } 385 386 File in = new File (pArgs [0]); 387 File out = new File (pArgs [1]); 388 389 runTests (in, out); 390 391 if (pArgs.length > 2) { 392 File compare = new File (pArgs [2]); 393 if (isDifferentFiles (out, compare)) { 394 System.out.println ("Test failure - output file " + 395 out + 396 " differs from expected output file " + 397 compare); 398 } 399 else { 400 System.out.println ("tests passed"); 401 } 402 } 403 } 404 405 static void usage () 407 { 408 System.err.println ("usage: java org.apache.taglibs.standard.lang.jstl.test.EvaluationTest {input file} {output file} [{compare file}]"); 409 } 410 411 413 } 414 | Popular Tags |