1 17 package org.sape.carbon.services.devtools.junit.launcher; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.HashSet ; 22 import java.util.Set ; 23 import java.util.Map ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Properties ; 27 import java.util.Enumeration ; 28 import java.util.TreeSet ; 29 30 import javax.swing.JFrame ; 31 import java.awt.BorderLayout ; 32 import java.awt.Container ; 33 import javax.swing.JScrollPane ; 34 import javax.swing.JPanel ; 35 import java.awt.GridBagLayout ; 36 import java.awt.GridBagConstraints ; 37 import javax.swing.JLabel ; 38 import javax.swing.JCheckBox ; 39 import java.awt.Insets ; 40 import javax.swing.JButton ; 41 import java.awt.FlowLayout ; 42 import java.awt.event.WindowAdapter ; 43 import java.awt.event.WindowEvent ; 44 import java.awt.Dimension ; 45 import java.awt.Toolkit ; 46 import javax.swing.ImageIcon ; 47 48 import java.awt.event.ActionListener ; 49 import java.awt.event.ActionEvent ; 50 import org.sape.carbon.core.util.reflection.ClassFinder; 51 import junit.framework.TestCase; 52 import junit.swingui.TestRunner; 53 54 import java.io.IOException ; 55 56 70 public class TestLauncher implements ActionListener , Runnable { 71 77 public static Set selectedClasses = new HashSet (); 78 79 84 private static final Object WINDOW_NOTIFIER = new Object (); 85 86 87 private static final Insets emptyInset = new Insets (0, 0, 0, 0); 88 89 90 private static final Insets widthInset = new Insets (0, 5, 0, 5); 91 92 93 private static final String PACKAGE_LOCATION = "/org/sape/carbon/services/devtools/junit/launcher/"; 94 95 96 private static final String PROPERTY_FILE_LOCATION = PACKAGE_LOCATION + "launcher.properties"; 97 98 99 private static final String JUNIT_LOGO_LOCATION = PACKAGE_LOCATION + "junitlogo.gif"; 100 101 102 private static final String REMOVE_ICON_LOCATION = PACKAGE_LOCATION + "remove.gif"; 103 104 105 106 private static final String CARBON_LOGO_LOCATION = PACKAGE_LOCATION + "carbonlogo.gif"; 107 108 109 private static final String EXCLUDE_PREFIX = "EXCLUDE."; 110 111 112 private static final String DEPEND_PREFIX = "DEPEND."; 113 114 115 private static final String ACTION_DONE = "ACTION_DONE"; 116 117 118 private static final String ACTION_CHECK_ALL = "ACTION_CHECK_ALL"; 119 120 124 private static final String ACTION_REMOVE_DEPEND = "ACTION_REMOVE_DEPEND"; 125 126 127 protected Toolkit toolkit = Toolkit.getDefaultToolkit(); 128 129 130 protected JFrame applicationFrame; 131 132 133 protected JCheckBox selectAllCheckbox; 134 135 139 protected Map checkBoxToClassNameMap; 140 141 143 protected Map classNameToCheckBoxMap; 144 145 146 protected Set inputClassNames; 147 148 149 protected Set excludedClassNames; 150 151 156 protected Map dependMap; 157 158 163 protected Map dependButtonMap; 164 165 170 public static void main(String args[]) { 171 TestLauncher testSelector = new TestLauncher(); 172 testSelector.runAsThread(); 173 174 testSelector = null; 176 177 if(!selectedClasses.isEmpty()) { 178 TestRunner.main(new String [] {"-noloading", LauncherSelectedTest.class.getName()}); 179 } 180 } 181 182 185 public TestLauncher() { 186 checkBoxToClassNameMap = new HashMap (); 187 classNameToCheckBoxMap = new HashMap (); 188 dependButtonMap = new HashMap (); 189 excludedClassNames = new HashSet (); 190 dependMap = new HashMap (); 191 loadPropertiesFile(); 192 addAllTests(); 193 } 194 195 202 protected void loadPropertiesFile() { 203 Properties properties = new Properties (); 204 205 try { 206 properties.load( 207 getClass().getResourceAsStream(PROPERTY_FILE_LOCATION)); 208 } catch (IOException e) {} 209 210 Enumeration propertyNames = properties.propertyNames(); 211 while(propertyNames.hasMoreElements()) { 212 String propertyName = (String ) propertyNames.nextElement(); 213 String propertyValue = properties.getProperty(propertyName); 214 215 if(propertyName.startsWith(EXCLUDE_PREFIX)) { 216 addExcludedClass(propertyValue); 217 } 218 else if(propertyName.startsWith(DEPEND_PREFIX)) { 219 String className = propertyName.substring(DEPEND_PREFIX.length()); 220 addDepend(propertyValue, className); 221 } 222 } 223 } 224 225 231 protected void addExcludedClass(String excludedClassName) { 232 excludedClassNames.add(excludedClassName); 233 } 234 235 242 protected void addDepend(String dependName, String className) { 243 if(!dependMap.containsKey(dependName)) { 244 dependMap.put(dependName, new ArrayList ()); 245 } 246 247 ((List )dependMap.get(dependName)).add(className); 248 } 249 250 255 protected void addAllTests() { 256 ClassFinder classFinder = new ClassFinder(TestCase.class, "org.sape.carbon"); 257 Set testClasses = classFinder.getClasses(); 258 259 ClassFinder perfClassFinder = new ClassFinder(TestCase.class,"PerformanceTest"); 260 Set performanceClasses = perfClassFinder.getClasses(); 261 testClasses.removeAll(performanceClasses); 262 testClasses.removeAll(excludedClassNames); 263 264 setInputClassNames(testClasses); 265 } 266 267 274 public void setInputClassNames(Set inputClassNames) { 275 this.inputClassNames = new TreeSet (inputClassNames); 277 } 278 279 282 public void run() { 283 getUserInput(); 284 } 285 286 291 public void runAsThread() { 292 Thread testSelectorThread = new Thread (this); 293 testSelectorThread.start(); 294 synchronized(WINDOW_NOTIFIER) { 295 try { 296 WINDOW_NOTIFIER.wait(); 297 } 298 catch (InterruptedException e) {} 299 } 300 } 301 302 309 public void getUserInput() { 310 applicationFrame = new JFrame ("Carbon Test Launcher"); 312 applicationFrame.setIconImage( 313 toolkit.getImage(getClass().getResource(CARBON_LOGO_LOCATION))); 314 315 applicationFrame.addWindowListener(new WindowCloser()); 316 Container contentPane = applicationFrame.getContentPane(); 317 contentPane.setLayout(new BorderLayout ()); 318 JScrollPane scrollPane = new JScrollPane (createTestHarnessPanel()); 319 320 contentPane.add(scrollPane); 321 contentPane.add(createButtonPanel(), BorderLayout.NORTH); 322 323 adjustFrameSize(); 325 326 applicationFrame.setVisible(true); 327 } 328 329 335 protected void adjustFrameSize() { 336 applicationFrame.pack(); 337 Dimension screenSize = toolkit.getScreenSize(); 338 Dimension frameSize = applicationFrame.getSize(); 339 double newHeight = frameSize.getHeight(); 340 double newWidth = frameSize.getWidth(); 341 342 if(newHeight > screenSize.getHeight() * 0.8) { 343 newHeight = screenSize.getHeight() * 0.8; 344 newWidth = newWidth + 20; 346 } 347 if(newWidth > screenSize.getWidth() * 0.8) { 348 newWidth = screenSize.getWidth() * 0.8; 349 } 350 frameSize.setSize(newWidth, newHeight); 351 applicationFrame.setSize(frameSize); 352 } 353 354 361 protected JPanel createButtonPanel() { 362 JPanel buttonPanel = new JPanel (); 363 buttonPanel.setLayout(new FlowLayout (FlowLayout.LEFT)); 364 365 JButton doneButton = 366 new JButton ("Run jUnit!", new ImageIcon (getClass().getResource(JUNIT_LOGO_LOCATION))); 367 doneButton.setActionCommand(ACTION_DONE); 368 doneButton.addActionListener(this); 369 buttonPanel.add(doneButton); 370 371 Iterator dependIterator = dependMap.keySet().iterator(); 372 while(dependIterator.hasNext()) { 373 String dependName = (String ) dependIterator.next(); 374 JButton dependButton = 375 new JButton (dependName, new ImageIcon (getClass().getResource(REMOVE_ICON_LOCATION))); 376 377 dependButton.setActionCommand(ACTION_REMOVE_DEPEND); 378 dependButton.addActionListener(this); 379 dependButtonMap.put(dependButton, dependName); 380 buttonPanel.add(dependButton); 381 } 382 383 return buttonPanel; 384 } 385 386 392 protected JPanel createTestHarnessPanel() { 393 JPanel testHarnessPanel = new JPanel (); 394 GridBagLayout gridBagLayout = new GridBagLayout (); 395 testHarnessPanel.setLayout(gridBagLayout); 396 397 addHeaderRows(testHarnessPanel, gridBagLayout); 399 400 Iterator inputClassNamesIterator = inputClassNames.iterator(); 402 while(inputClassNamesIterator.hasNext()) { 403 String className = (String ) inputClassNamesIterator.next(); 404 addRow(testHarnessPanel, gridBagLayout, className); 405 } 406 407 return testHarnessPanel; 408 } 409 410 420 protected void addHeaderRows(JPanel testHarnessPanel, GridBagLayout gridBagLayout) { 421 GridBagConstraints gridBagConstraints = new GridBagConstraints (); 422 423 selectAllCheckbox = new JCheckBox (); 424 selectAllCheckbox.setSelected(true); 425 selectAllCheckbox.setActionCommand(ACTION_CHECK_ALL); 426 selectAllCheckbox.addActionListener(this); 427 gridBagConstraints.anchor = GridBagConstraints.WEST; 428 gridBagConstraints.gridwidth = GridBagConstraints.RELATIVE; 429 gridBagConstraints.insets = widthInset; 430 gridBagLayout.setConstraints(selectAllCheckbox, gridBagConstraints); 431 testHarnessPanel.add(selectAllCheckbox); 432 gridBagConstraints.anchor = GridBagConstraints.CENTER; 433 gridBagConstraints.gridwidth = 1; 434 gridBagConstraints.insets = emptyInset; 435 436 JLabel selectAllLabel = new JLabel ("Activate All Tests"); 437 gridBagConstraints.anchor = GridBagConstraints.WEST; 438 gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; 439 gridBagConstraints.insets = widthInset; 440 gridBagLayout.setConstraints(selectAllLabel, gridBagConstraints); 441 testHarnessPanel.add(selectAllLabel); 442 gridBagConstraints.anchor = GridBagConstraints.CENTER; 443 gridBagConstraints.gridwidth = 1; 444 gridBagConstraints.insets = emptyInset; 445 } 446 447 454 protected void addRow(JPanel testHarnessPanel, GridBagLayout gridBagLayout, String className) { 455 GridBagConstraints gridBagConstraints = new GridBagConstraints (); 456 457 JCheckBox selectedCheckbox = new JCheckBox (); 458 selectedCheckbox.setSelected(true); 459 gridBagConstraints.anchor = GridBagConstraints.WEST; 460 gridBagConstraints.gridwidth = GridBagConstraints.RELATIVE; 461 gridBagConstraints.insets = widthInset; 462 gridBagLayout.setConstraints(selectedCheckbox, gridBagConstraints); 463 testHarnessPanel.add(selectedCheckbox); 464 gridBagConstraints.anchor = GridBagConstraints.CENTER; 465 gridBagConstraints.gridwidth = 1; 466 gridBagConstraints.insets = emptyInset; 467 468 469 JLabel classNameLabel = new JLabel (className); 470 471 gridBagConstraints.anchor = GridBagConstraints.WEST; 472 gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; 473 gridBagConstraints.insets = widthInset; 474 gridBagLayout.setConstraints(classNameLabel, gridBagConstraints); 475 testHarnessPanel.add(classNameLabel); 476 gridBagConstraints.anchor = GridBagConstraints.CENTER; 477 gridBagConstraints.gridwidth = 1; 478 gridBagConstraints.insets = emptyInset; 479 480 checkBoxToClassNameMap.put(selectedCheckbox, className); 481 classNameToCheckBoxMap.put(className, selectedCheckbox); 482 } 483 484 490 protected void setAllCheckboxes(boolean checkboxState) { 491 Set keySet = checkBoxToClassNameMap.keySet(); 492 Iterator keySetIterator = keySet.iterator(); 493 while(keySetIterator.hasNext()) { 494 JCheckBox selectedCheckBox = (JCheckBox ) keySetIterator.next(); 495 selectedCheckBox.setSelected(checkboxState); 496 } 497 } 498 499 505 protected void setDependsCheckboxes(String dependName, boolean checkboxState) { 506 List dependClassesList = (List ) dependMap.get(dependName); 507 if(dependClassesList != null) { 508 Iterator dependClassesIterator = dependClassesList.iterator(); 509 while(dependClassesIterator.hasNext()) { 510 String dependClassName = (String ) dependClassesIterator.next(); 511 512 Iterator classNameIterator = classNameToCheckBoxMap.keySet().iterator(); 513 while(classNameIterator.hasNext()) { 514 String className = (String ) classNameIterator.next(); 515 if(className.startsWith(dependClassName)) { 516 JCheckBox classCheckBox = (JCheckBox ) classNameToCheckBoxMap.get(className); 517 classCheckBox.setSelected(checkboxState); 518 } 519 } 520 } 521 } 522 } 523 524 528 protected void closeAndReturn() { 529 Set entrySet = checkBoxToClassNameMap.entrySet(); 530 Iterator entrySetIterator = entrySet.iterator(); 531 while(entrySetIterator.hasNext()) { 532 Map.Entry mapEntry = (Map.Entry ) entrySetIterator.next(); 533 JCheckBox selectedCheckBox = (JCheckBox ) mapEntry.getKey(); 534 String className = (String ) mapEntry.getValue(); 535 536 if(selectedCheckBox.isSelected()) { 537 selectedClasses.add(className); 538 } 539 } 540 541 applicationFrame.dispose(); 542 543 synchronized(WINDOW_NOTIFIER) { 544 WINDOW_NOTIFIER.notifyAll(); 545 } 546 } 547 548 553 public void actionPerformed(ActionEvent evt) { 554 if(ACTION_DONE.equals(evt.getActionCommand())) { 555 closeAndReturn(); 556 } 557 else if(ACTION_CHECK_ALL.equals(evt.getActionCommand())) { 558 selectAllCheckbox.setSelected(true); 559 setAllCheckboxes(true); 560 } 561 else if(ACTION_REMOVE_DEPEND.equals(evt.getActionCommand())) { 562 String dependName = (String ) dependButtonMap.get(evt.getSource()); 563 setDependsCheckboxes(dependName, false); 564 } 565 } 566 567 570 protected class WindowCloser extends WindowAdapter { 571 576 public void windowClosing(WindowEvent evt) { 577 setAllCheckboxes(false); 578 closeAndReturn(); 579 } 580 } 581 } 582 | Popular Tags |