1 18 19 package org.apache.jmeter.junit; 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectOutputStream ; 24 import java.io.Serializable ; 25 import java.rmi.RemoteException ; 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import junit.framework.Test; 35 import junit.framework.TestSuite; 36 37 import org.apache.jmeter.engine.util.CompoundVariable; 38 import org.apache.jmeter.functions.Function; 39 import org.apache.jmeter.gui.GuiPackage; 40 import org.apache.jmeter.gui.JMeterGUIComponent; 41 import org.apache.jmeter.gui.UnsharedComponent; 42 import org.apache.jmeter.gui.action.ActionRouter; 43 import org.apache.jmeter.gui.tree.JMeterTreeListener; 44 import org.apache.jmeter.gui.tree.JMeterTreeModel; 45 import org.apache.jmeter.gui.tree.JMeterTreeNode; 46 import org.apache.jmeter.save.SaveService; 47 import org.apache.jmeter.testbeans.TestBean; 48 import org.apache.jmeter.testbeans.gui.TestBeanGUI; 49 import org.apache.jmeter.testelement.TestElement; 50 import org.apache.jmeter.testelement.property.JMeterProperty; 51 import org.apache.jmeter.testelement.property.PropertyIterator; 52 import org.apache.jmeter.util.JMeterUtils; 53 import org.apache.jorphan.logging.LoggingManager; 54 import org.apache.jorphan.reflect.ClassFinder; 55 import org.apache.jorphan.util.JOrphanUtils; 56 import org.apache.log.Logger; 57 58 import org.jdom.Document; 59 import org.jdom.Element; 60 import org.jdom.input.SAXBuilder; 61 62 65 public class JMeterTest extends JMeterTestCase 66 { 67 private static Logger log = LoggingManager.getLoggerForClass(); 68 69 private static Map guiTitles; 70 private static Map guiTags; 71 private static Map funcTitles; 72 73 public JMeterTest(String name) 74 { 75 super(name); 76 } 77 78 90 private TestElement testItem; 92 public JMeterTest(String testName, TestElement te) 93 { 94 super(testName); testItem=te; 96 } 97 98 private Serializable serObj; 100 public JMeterTest(String testName, Serializable ser) 101 { 102 super(testName); serObj=ser; 104 } 105 106 private JMeterGUIComponent guiItem; 108 public JMeterTest(String testName, JMeterGUIComponent gc) 109 { 110 super(testName); guiItem=gc; 112 } 113 114 private Function funcItem; 116 117 private static boolean classPathShown=false; 119 public JMeterTest(String testName, Function fi) 120 { 121 super(testName); funcItem=fi; 123 } 124 125 128 public static Test suite() throws Exception { 129 JMeterTreeModel treeModel = new JMeterTreeModel(); 131 JMeterTreeListener treeLis = new JMeterTreeListener(treeModel); 132 treeLis.setActionHandler(ActionRouter.getInstance()); 133 GuiPackage.getInstance(treeLis, treeModel); 134 try { 135 org.apache.jmeter.gui.MainFrame main = 137 new org.apache.jmeter.gui.MainFrame( 138 ActionRouter.getInstance(), 139 treeModel, 140 treeLis); 141 } 142 catch (RuntimeException e) 143 { 144 System.out.println("Cannot create MainFrame: "+e); 145 } 146 147 TestSuite suite = new TestSuite(); 148 suite.addTest(new JMeterTest("createTitleSet")); 149 suite.addTest(new JMeterTest("createTagSet")); 150 suite.addTest(suiteGUIComponents()); 151 suite.addTest(suiteSerializableElements()); 152 suite.addTest(suiteTestElements()); 153 suite.addTest(suiteBeanComponents()); 154 suite.addTest(new JMeterTest("createFunctionSet")); 155 suite.addTest(suiteFunctions()); 156 suite.addTest(new JMeterTest("checkGuiSet")); 157 suite.addTest(new JMeterTest("checkFunctionSet")); 158 return suite; 159 } 160 161 164 public void createTitleSet() throws Exception 165 { 166 guiTitles = new HashMap (90); 167 168 String compref = "../xdocs/usermanual/component_reference.xml"; 169 SAXBuilder bldr = new SAXBuilder(); 170 Document doc; 171 doc = bldr.build(compref); 172 Element root = doc.getRootElement(); 173 Element body = root.getChild("body"); 174 List sections = body.getChildren("section"); 175 for (int i = 0; i< sections.size();i++){ 176 List components = ((Element) sections.get(i)).getChildren("component"); 177 for (int j = 0; j <components.size();j++){ 178 Element comp = (Element) components.get(j); 179 guiTitles.put(comp.getAttributeValue("name"),Boolean.FALSE); 180 } 181 } 182 guiTitles.put("Root",Boolean.FALSE); 184 guiTitles.put("Example Sampler",Boolean.FALSE); 185 } 186 187 190 public void createTagSet() throws Exception 191 { 192 guiTags = new HashMap (90); 193 194 String compref = "../xdocs/usermanual/component_reference.xml"; 195 SAXBuilder bldr = new SAXBuilder(); 196 Document doc; 197 doc = bldr.build(compref); 198 Element root = doc.getRootElement(); 199 Element body = root.getChild("body"); 200 List sections = body.getChildren("section"); 201 for (int i = 0; i< sections.size();i++){ 202 List components = ((Element) sections.get(i)).getChildren("component"); 203 for (int j = 0; j <components.size();j++){ 204 Element comp = (Element) components.get(j); 205 guiTags.put(comp.getAttributeValue("tag"),Boolean.FALSE); 206 } 207 } 208 } 209 210 213 public void createFunctionSet() throws Exception 214 { 215 funcTitles = new HashMap (20); 216 217 String compref = "../xdocs/usermanual/functions.xml"; 218 SAXBuilder bldr = new SAXBuilder(); 219 Document doc; 220 doc = bldr.build(compref); 221 Element root = doc.getRootElement(); 222 Element body = root.getChild("body"); 223 Element section = body.getChild("section"); 224 List sections = section.getChildren("subsection"); 225 for (int i = 0; i< sections.size();i++){ 226 List components = ((Element) sections.get(i)).getChildren("component"); 227 for (int j = 0; j <components.size();j++){ 228 Element comp = (Element) components.get(j); 229 funcTitles.put(comp.getAttributeValue("name"),Boolean.FALSE); 230 } 231 } 232 } 233 234 private int scanprintMap(Map m, String t) 235 { 236 Set s = m.keySet(); 237 int unseen = 0; 238 if (s.size() == 0) return 0; 239 Iterator i = s.iterator(); 240 while (i.hasNext()) 241 { 242 Object key = i.next(); 243 if (!m.get(key).equals(Boolean.TRUE)) 244 { 245 if(key.equals("SSL Manager")) { 247 continue; 248 } 249 if (unseen == 0) { 251 System.out.println("\nNames remaining in "+t+" Map:"); 252 } 253 unseen++; 254 System.out.println(key); 255 } 256 } 257 return unseen; 258 } 259 public void checkGuiSet() throws Exception 260 { 261 guiTitles.remove("Example Sampler"); assertEquals("Should not have any names left over",0,scanprintMap(guiTitles,"GUI")); 263 } 264 265 public void checkFunctionSet() throws Exception 266 { 267 assertEquals("Should not have any names left over",0,scanprintMap(funcTitles,"Function")); 268 } 269 272 private static Test suiteGUIComponents() throws Exception 273 { 274 TestSuite suite = new TestSuite("GuiComponents"); 275 Iterator iter = getObjects(JMeterGUIComponent.class).iterator(); 276 while (iter.hasNext()) 277 { 278 JMeterGUIComponent item = (JMeterGUIComponent) iter.next(); 279 if (item instanceof JMeterTreeNode) 280 { 281 System.out.println("INFO: JMeterGUIComponent: skipping all tests "+item.getClass().getName()); 282 continue; 283 } 284 TestSuite ts = new TestSuite(item.getClass().getName()); 285 ts.addTest(new JMeterTest("GUIComponents1",item)); 286 if (item instanceof TestBeanGUI) 287 { 288 System.out.println("INFO: JMeterGUIComponent: skipping some tests "+item.getClass().getName()); 289 } 290 else 291 { 292 ts.addTest(new JMeterTest("GUIComponents2",item)); 293 ts.addTest(new JMeterTest("runGUITitle",item)); 294 } 295 suite.addTest(ts); 296 } 297 return suite; 298 } 299 300 303 private static Test suiteFunctions() throws Exception 304 { 305 TestSuite suite = new TestSuite("Functions"); 306 Iterator iter = getObjects(Function.class).iterator(); 307 while (iter.hasNext()) 308 { 309 Object item = iter.next(); 310 if (item.getClass().equals(CompoundVariable.class)) 311 { 312 continue; 313 } 314 TestSuite ts = new TestSuite(item.getClass().getName()); 315 ts.addTest(new JMeterTest("runFunction",(Function) item)); 316 ts.addTest(new JMeterTest("runFunction2",(Function) item)); 317 suite.addTest(ts); 318 } 319 return suite; 320 } 321 322 323 326 private static Test suiteBeanComponents() throws Exception 327 { 328 TestSuite suite = new TestSuite("BeanComponents"); 329 Iterator iter = getObjects(TestBean.class).iterator(); 330 while (iter.hasNext()) 331 { 332 Class c = iter.next().getClass(); 333 try { 334 JMeterGUIComponent item = new TestBeanGUI(c); 335 TestSuite ts = new TestSuite(item.getClass().getName()); 337 ts.addTest(new JMeterTest("GUIComponents2",item)); 338 ts.addTest(new JMeterTest("runGUITitle",item)); 339 suite.addTest(ts); 340 } 341 catch(IllegalArgumentException e) 342 { 343 System.out.println("Cannot create test for "+c.getName()+" "+e); 344 e.printStackTrace(System.out); 345 } 346 } 347 return suite; 348 } 349 350 353 public void runGUITitle() throws Exception 354 { 355 if (guiTitles.size() > 0) { 356 String title = guiItem.getStaticLabel(); 357 boolean ct =guiTitles.containsKey(title); 358 if (ct) guiTitles.put(title,Boolean.TRUE); if ( (title.indexOf("(ALPHA") == -1) 361 && 362 (title.indexOf("(BETA") == -1) 363 && 364 (!title.equals("Example1")) && 366 (!title.equals("Example2")) 367 ) 368 { assertTrue("component_reference.xml needs '"+title+"' anchor for "+guiItem.getClass().getName(),ct); 370 } 371 } 372 } 373 374 377 public void runFunction() throws Exception 378 { 379 if (funcTitles.size() > 0) { 380 String title = funcItem.getReferenceKey(); 381 boolean ct =funcTitles.containsKey(title); 382 if (ct) funcTitles.put(title,Boolean.TRUE); if ( title.indexOf("(ALPHA") == -1 385 && title.indexOf("(EXPERIMENTAL") == -1 386 ) 387 { assertTrue("function.xml needs '"+title+"' entry for "+funcItem.getClass().getName(),ct); 389 } 390 } 391 } 392 393 396 public void runFunction2() throws Exception 397 { 398 Iterator i = funcItem.getArgumentDesc().iterator(); 399 while (i.hasNext()) 400 { 401 Object o = i.next(); 402 assertTrue("Description must be a String",o instanceof String ); 403 assertFalse("Description must not start with [refkey" 404 ,((String )o).startsWith("[refkey")); 405 } 406 } 407 408 411 public void GUIComponents1() throws Exception 412 { 413 String name = guiItem.getClass().getName(); 414 415 assertEquals( 416 "Name should be same as static label for " + name, 417 guiItem.getStaticLabel(), 418 guiItem.getName()); 419 if (!name.endsWith("TestBeanGUI")) 420 { 421 try{ 422 String label = guiItem.getLabelResource(); 423 assertTrue(label.length() > 0); 424 assertFalse("'"+label+"' should be in resource file for "+name 425 ,JMeterUtils.getResString(label).startsWith(JMeterUtils.RES_KEY_PFX)); 426 } 427 catch(UnsupportedOperationException uoe) 428 { 429 log.warn("Class has not yet implemented getLabelResource "+name); 430 } 431 } 432 } 433 434 435 438 public void GUIComponents2() throws Exception 439 { 440 String name = guiItem.getClass().getName(); 441 442 444 TestElement el = guiItem.createTestElement(); 445 assertNotNull( 446 name+".createTestElement should be non-null ", el); 447 assertEquals( 448 "GUI-CLASS: Failed on " + name, 449 name, 450 el.getPropertyAsString(TestElement.GUI_CLASS)); 451 452 assertEquals( 453 "NAME: Failed on " + name, 454 guiItem.getName(), 455 el.getPropertyAsString(TestElement.NAME)); 456 assertEquals( 457 "TEST-CLASS: Failed on " + name, 458 el.getClass().getName(), 459 el.getPropertyAsString(TestElement.TEST_CLASS)); 460 TestElement el2 = guiItem.createTestElement(); 461 el.setProperty(TestElement.NAME, "hey, new name!:"); 462 el.setProperty("NOT", "Shouldn't be here"); 463 if (!(guiItem instanceof UnsharedComponent)) 464 { 465 assertEquals( 466 "SHARED: Failed on " + name, 467 "", 468 el2.getPropertyAsString("NOT")); 469 } 470 log.debug("Saving element: " + el.getClass()); 471 el = 472 SaveService.createTestElement( 473 SaveService.getConfigForTestElement(null, el)); 474 log.debug("Successfully saved"); 475 guiItem.configure(el); 476 assertEquals( 477 "CONFIGURE-TEST: Failed on " + name, 478 el.getPropertyAsString(TestElement.NAME), 479 guiItem.getName()); 480 guiItem.modifyTestElement(el2); 481 assertEquals( 482 "Modify Test: Failed on " + name, 483 "hey, new name!:", 484 el2.getPropertyAsString(TestElement.NAME)); 485 } 486 487 488 491 private static Test suiteSerializableElements() throws Exception 492 { 493 TestSuite suite = new TestSuite("SerializableElements"); 494 Iterator iter = getObjects(Serializable .class).iterator(); 495 while (iter.hasNext()) 496 { 497 Serializable serObj = (Serializable ) iter.next(); 498 if (serObj.getClass().getName().endsWith("_Stub")) 499 { 500 continue; 501 } 502 TestSuite ts = new TestSuite(serObj.getClass().getName()); 503 ts.addTest(new JMeterTest("runSerialTest",serObj)); 504 suite.addTest(ts); 505 } 506 return suite; 507 } 508 509 512 public void runSerialTest() throws Exception 513 { 514 try 515 { 516 ByteArrayOutputStream bytes = new ByteArrayOutputStream (); 517 ObjectOutputStream out = new ObjectOutputStream (bytes); 518 out.writeObject(serObj); 519 out.close(); 520 ObjectInputStream in = 521 new ObjectInputStream ( 522 new ByteArrayInputStream (bytes.toByteArray())); 523 Object readObject = in.readObject(); 524 in.close(); 525 assertEquals( 526 "deserializing class: " + serObj.getClass().getName(), 527 serObj.getClass(), 528 readObject.getClass()); 529 } 530 catch (Throwable e) 531 { 532 fail("serialization of "+serObj.getClass().getName()+" failed: "+e); 533 } 534 } 535 536 539 private static Test suiteTestElements() throws Exception 540 { 541 TestSuite suite = new TestSuite("TestElements"); 542 Iterator iter = getObjects(TestElement.class).iterator(); 543 while (iter.hasNext()) 544 { 545 TestElement item = (TestElement) iter.next(); 546 TestSuite ts = new TestSuite(item.getClass().getName()); 547 ts.addTest(new JMeterTest("runTestElement",item)); 548 suite.addTest(ts); 549 } 550 return suite; 551 } 552 553 556 public void runTestElement() throws Exception 557 { 558 checkElementCloning(testItem); 559 assertTrue( 560 testItem.getClass().getName() + " must implement Serializable", 561 testItem instanceof Serializable ); 562 } 563 564 private static Collection getObjects(Class extendsClass) throws Exception 565 { 566 String exName = extendsClass.getName(); 567 Object myThis = ""; 568 Iterator classes = 569 ClassFinder 570 .findClassesThatExtend( 571 JMeterUtils.getSearchPaths(), 572 new Class [] { extendsClass }) 573 .iterator(); 574 List objects = new LinkedList (); 575 String n=""; 576 boolean caughtError=true; 577 Throwable caught=null; 578 try { 579 while (classes.hasNext()) 580 { 581 n = (String ) classes.next(); 582 if (n.endsWith("RemoteJMeterEngineImpl")) 584 { 585 continue; } 587 Class c = null; 588 try 589 { 590 c = Class.forName(n); 591 try 592 { 593 objects.add(c.newInstance()); 595 } 596 catch (InstantiationException e) 597 { 598 caught=e; 599 try 601 { 602 objects.add( 604 c.getConstructor( 605 new Class [] { Object .class }).newInstance( 606 new Object [] { myThis })); 607 } 608 catch (NoSuchMethodException f) 609 { 610 System.out.println("WARN: "+exName+": NoSuchMethodException "+n); 612 } 613 } 614 } 615 catch (NoClassDefFoundError e) 616 { 617 System.out.println("WARN: "+exName+": NoClassDefFoundError "+n); 619 } 620 catch (IllegalAccessException e) 621 { 622 caught=e; 623 System.out.println("WARN: "+exName+": IllegalAccessException "+n); 624 } 626 catch (Exception e) 631 { 632 caught=e; 633 if ((e instanceof RemoteException ) 634 ||e.getClass().getName().equals("java.awt.HeadlessException") ) 636 { 637 System.out.println("WARN: "+"Error creating "+n+" "+e.toString()); 638 } 639 else 640 { 641 throw new Exception ("Error creating "+n+" "+e.toString()); 642 } 643 } 644 } 645 caughtError=false; 646 } 647 finally 648 { 649 if (caughtError) 650 { 651 System.out.println("Last class="+n); 652 System.out.println("objects.size="+objects.size()); 653 System.out.println("Last error="+caught); 654 } 655 } 656 657 if (objects.size() == 0){ 658 System.out.println("No classes found that extend "+exName+". Check the following:"); 659 System.out.println("Search paths are:"); 660 String ss[] = JMeterUtils.getSearchPaths(); 661 for (int i=0;i<ss.length;i++){ 662 System.out.println(ss[i]); 663 } 664 if (!classPathShown){ System.out.println("Class path is:"); 666 String cp=System.getProperty("java.class.path"); 667 String cpe[]= JOrphanUtils.split(cp,java.io.File.pathSeparator); 668 for (int i=0;i<cpe.length;i++) 669 { 670 System.out.println(cpe[i]); 671 } 672 classPathShown=true; 673 } 674 } 675 return objects; 676 } 677 678 private static void cloneTesting(TestElement item, TestElement clonedItem) 679 { 680 assertTrue(item != clonedItem); 681 assertEquals( 682 "CLONE-SAME-CLASS: testing " + item.getClass().getName(), 683 item.getClass().getName(), 684 clonedItem.getClass().getName()); 685 } 686 687 private static void checkElementCloning(TestElement item) 688 { 689 TestElement clonedItem = (TestElement) item.clone(); 690 cloneTesting(item, clonedItem); 691 PropertyIterator iter2 = item.propertyIterator(); 692 while (iter2.hasNext()) 693 { 694 JMeterProperty item2 = iter2.next(); 695 assertEquals(item2.getStringValue(), clonedItem.getProperty(item2.getName()).getStringValue()); 697 assertTrue(item2 != clonedItem.getProperty(item2.getName())); 698 } 699 } 700 } 701 | Popular Tags |