Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 297   Methods: 30
NCLOC: 246   Classes: 5
 
 Source file Conditionals Statements Methods TOTAL
TestSelector.java 0% 0% 0% 0%
coverage
 1   
 package abbot.editor;
 2   
 
 3   
 import java.awt.*;
 4   
 import java.awt.event.*;
 5   
 import java.util.*;
 6   
 import java.util.List;
 7   
 
 8   
 import javax.swing.*;
 9   
 import javax.swing.border.EmptyBorder;
 10   
 import javax.swing.event.*;
 11   
 
 12   
 import junit.runner.*;
 13   
 import abbot.Log;
 14   
 import abbot.i18n.Strings;
 15   
 import abbot.util.PathClassLoader;
 16   
 import abbot.editor.widgets.ArrayEditor;
 17   
 import junit.extensions.abbot.ScriptTestCollector;
 18   
 
 19   
 /**
 20   
  * A test class selector. A simple dialog to pick the name of a test suite.
 21   
  * Allows user to set the class path searched for script suite classes.
 22   
  * Thanks to JUnit for this code.  
 23   
  */
 24   
 public class TestSelector extends JDialog {
 25   
     /** If the selected item is this value, then use no test case class. */
 26   
     public static final String TEST_NONE = "<None>";
 27   
 
 28   
     private JList fList;
 29   
     private ArrayEditor pathEditor;
 30   
     private JButton fOk;
 31   
     private String fSelectedItem;
 32   
     
 33   
     /**
 34   
      * Renders TestFailures in a JList
 35   
      */
 36   
     static class TestCellRenderer extends DefaultListCellRenderer {
 37   
         Icon fLeafIcon;
 38   
         Icon fSuiteIcon;
 39   
         
 40  0
         public TestCellRenderer() {
 41  0
             fLeafIcon= UIManager.getIcon("Tree.leafIcon");
 42  0
             fSuiteIcon= UIManager.getIcon("Tree.closedIcon");
 43   
         }
 44   
         
 45  0
         public Component getListCellRendererComponent(
 46   
                                                       JList list, Object value, int modelIndex, 
 47   
                                                       boolean isSelected, boolean cellHasFocus) {
 48  0
             Component c= super.getListCellRendererComponent(list, value, modelIndex, isSelected, cellHasFocus);
 49  0
             String displayString= displayString((String)value);
 50   
             
 51  0
             if (displayString.startsWith("AllTests"))
 52  0
                 setIcon(fSuiteIcon);
 53   
             else
 54  0
                 setIcon(fLeafIcon);
 55   
                 
 56  0
             setText(displayString);
 57  0
             return c;
 58   
         }
 59   
         
 60  0
         public static String displayString(String className) {
 61  0
             int typeIndex= className.lastIndexOf('.');
 62  0
             if (typeIndex < 0) 
 63  0
                 return className;
 64  0
             return className.substring(typeIndex+1) + " - " + className.substring(0, typeIndex);
 65   
         }
 66   
         
 67  0
         public static boolean matchesKey(String s, char ch) {
 68  0
             return ch == Character.toUpperCase(s.charAt(typeIndex(s)));
 69   
         }
 70   
         
 71  0
         private static int typeIndex(String s) {
 72  0
             int typeIndex= s.lastIndexOf('.');
 73  0
             int i= 0;
 74  0
             if (typeIndex > 0) 
 75  0
                 i= typeIndex+1;
 76  0
             return i;
 77   
         }
 78   
     }
 79   
     
 80   
     protected class DoubleClickListener extends MouseAdapter {
 81  0
         public void mouseClicked(MouseEvent e) {
 82  0
             if (e.getClickCount() == 2) {
 83  0
                 okSelected();
 84   
             }
 85   
         }
 86   
     }
 87   
     
 88   
     protected class KeySelectListener extends KeyAdapter {
 89  0
         public void keyTyped(KeyEvent e) {
 90  0
             keySelectTestClass(e.getKeyChar());
 91   
         }
 92   
     }
 93   
 
 94  0
     public TestSelector(Frame parent, String classPath) {
 95  0
         super(parent, true);
 96  0
         setSize(350, 300);
 97  0
         setResizable(false);
 98  0
         setLocationRelativeTo(parent);
 99  0
         setTitle(Strings.get("TestSelector.title"));
 100   
         
 101  0
         fList= new JList();
 102  0
         fList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 103  0
         fList.setCellRenderer(new TestCellRenderer());
 104  0
         setCollector(classPath);
 105  0
         JScrollPane listScroll = new JScrollPane(fList);
 106   
 
 107  0
         String[] paths = PathClassLoader.convertPathToFilenames(classPath);
 108  0
         pathEditor = new ArrayEditor(paths);
 109   
 
 110  0
         JScrollPane scroll = new JScrollPane(pathEditor);
 111  0
         JPanel path = new JPanel(new BorderLayout());
 112  0
         JLabel label = new JLabel(Strings.get("selector.classpath"));
 113  0
         path.add(label, BorderLayout.NORTH);
 114  0
         path.add(scroll, BorderLayout.CENTER);
 115   
 
 116  0
         JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
 117   
                                           listScroll, path);
 118  0
         split.setBorder(null);
 119   
 
 120  0
         JButton cancel =
 121   
             new JButton(UIManager.getString("OptionPane.cancelButtonText"));
 122  0
         JButton none = new JButton(Strings.get("None"));
 123  0
         JLabel desc = new JLabel(Strings.get("SelectTest"));
 124  0
         fOk = new JButton(UIManager.getString("OptionPane.okButtonText"));
 125  0
         fOk.setEnabled(false);
 126  0
         getRootPane().setDefaultButton(fOk);
 127   
 
 128  0
         defineLayout(desc, split, fOk, none, cancel);
 129  0
         addListeners(fOk, none, cancel);
 130   
     }
 131   
     
 132  0
     public void setCollector(String classPath) {
 133   
         // Ensure abbot.jar is always in the class path
 134  0
         String fallback = System.getProperty("java.class.path");
 135  0
         if (fallback.indexOf("abbot.jar") == -1) {
 136  0
             fallback = System.getProperty("abbot.class.path");
 137  0
             if (fallback.indexOf("abbot.jar") == -1) {
 138  0
                 Log.warn("abbot.jar not found in classpath");
 139   
             }
 140   
         }
 141  0
         classPath += System.getProperty("path.separator") + fallback;
 142   
 
 143  0
         ClassLoader cl = new PathClassLoader(classPath);
 144  0
         TestCollector collector = new ScriptTestCollector(cl);
 145  0
         final Object[] list = createTestList(collector).toArray();
 146  0
         setTestList(list);
 147   
     }
 148   
 
 149  0
     private void setTestList(final Object[] list) {
 150  0
         if (!SwingUtilities.isEventDispatchThread()) {
 151  0
             SwingUtilities.invokeLater(new Runnable() {
 152  0
                 public void run() {
 153  0
                     setTestList(list);
 154   
                 }
 155   
             });
 156  0
             return;
 157   
         }
 158  0
         try {
 159  0
             getParent().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
 160  0
             fList.setModel(new AbstractListModel() {
 161  0
                 public int getSize() { return list.length; }
 162  0
                 public Object getElementAt(int i) { return list[i]; }
 163   
             });
 164   
         } finally {
 165  0
             getParent().setCursor(Cursor.getDefaultCursor());
 166   
         }
 167   
     }
 168   
 
 169  0
     private void addListeners(final JButton ok, JButton none, JButton cancel) {
 170  0
         cancel.addActionListener(new ActionListener() {
 171  0
                 public void actionPerformed(ActionEvent e) {
 172  0
                     dispose();
 173   
                 }
 174   
             });
 175   
         
 176  0
         none.addActionListener(new ActionListener() {
 177  0
                 public void actionPerformed(ActionEvent e) {
 178  0
                     fSelectedItem = TEST_NONE;
 179  0
                     dispose();
 180   
                 }
 181   
             });
 182  0
         ok.addActionListener(new ActionListener() {
 183  0
                 public void actionPerformed(ActionEvent e) {
 184  0
                     okSelected();
 185   
                 }
 186   
             });
 187   
 
 188  0
         fList.addMouseListener(new DoubleClickListener());
 189  0
         fList.addKeyListener(new KeySelectListener());
 190  0
         fList.addListSelectionListener(new ListSelectionListener() {
 191  0
                 public void valueChanged(ListSelectionEvent e) {
 192  0
                     checkEnableOK(e);
 193   
                 }
 194   
             });
 195   
 
 196  0
         addWindowListener(new WindowAdapter() {
 197  0
                 public void windowClosing(WindowEvent e) {
 198  0
                     dispose();
 199   
                 }
 200   
             });
 201   
 
 202  0
         pathEditor.addActionListener(new ActionListener() {
 203  0
             public void actionPerformed(ActionEvent e) {
 204  0
                 Object[] values = pathEditor.getValues();
 205  0
                 final StringBuffer buf = new StringBuffer();
 206  0
                 for (int i=0;i < values.length;i++) {
 207  0
                     buf.append(values[i]);
 208  0
                     buf.append(System.getProperty("path.separator"));
 209   
                 }
 210  0
                 new Thread("Available classes loader") {
 211  0
                     public void run() {
 212  0
                         setCollector(buf.toString());
 213   
                     }
 214   
                 }.start();
 215   
             }
 216   
         });
 217   
     }
 218   
     
 219  0
     private void defineLayout(Component desc, Component center,
 220   
                               Component ok, Component none, Component cancel) {
 221  0
         getContentPane().setLayout(new BorderLayout());
 222  0
         ((JPanel)getContentPane()).setBorder(new EmptyBorder(4,4,4,4));
 223  0
         getContentPane().add(desc, BorderLayout.NORTH);
 224  0
         getContentPane().add(center, BorderLayout.CENTER);
 225   
         
 226  0
         JPanel buttons = new JPanel(new GridLayout(1, 0));
 227  0
         buttons.add(ok);
 228  0
         buttons.add(none);
 229  0
         buttons.add(cancel);
 230  0
         getContentPane().add(buttons, BorderLayout.SOUTH);
 231   
     }
 232   
     
 233  0
     public void checkEnableOK(ListSelectionEvent e) {
 234  0
         fOk.setEnabled(fList.getSelectedIndex() != -1);
 235   
     }
 236   
     
 237  0
     public void okSelected() {
 238  0
         fSelectedItem= (String)fList.getSelectedValue();
 239  0
         dispose();
 240   
     }
 241   
     
 242  0
     public boolean isEmpty() {
 243  0
         return fList.getModel().getSize() == 0;
 244   
     }
 245   
     
 246  0
     public void keySelectTestClass(char ch) {
 247  0
         ListModel model= fList.getModel();
 248  0
         if (!Character.isJavaIdentifierStart(ch))
 249  0
             return;
 250  0
         for (int i= 0; i < model.getSize(); i++) {
 251  0
             String s= (String)model.getElementAt(i);
 252  0
             if (TestCellRenderer.matchesKey(s, Character.toUpperCase(ch))) {
 253  0
                 fList.setSelectedIndex(i);
 254  0
                 fList.ensureIndexIsVisible(i);
 255  0
                 return;
 256   
             }
 257   
         }
 258  0
         Toolkit.getDefaultToolkit().beep();
 259   
     }
 260   
     
 261  0
     public String getSelectedItem() {
 262  0
         return fSelectedItem;
 263   
     }
 264   
 
 265  0
     private List createTestList(TestCollector collector) {
 266  0
         Enumeration each= collector.collectTests();
 267  0
         Vector v= new Vector();
 268  0
         Vector displayVector= new Vector();
 269  0
         while(each.hasMoreElements()) {
 270  0
             String s= (String)each.nextElement();
 271  0
             v.add(s);
 272  0
             displayVector.add(TestCellRenderer.displayString(s));
 273   
         }
 274  0
         if (v.size() > 0)
 275  0
             Sorter.sortStrings(displayVector, 0,
 276   
                                displayVector.size()-1,
 277   
                                new ParallelSwapper(v));
 278  0
         return new ArrayList(v);
 279   
     }
 280   
     
 281   
     private class ParallelSwapper implements Sorter.Swapper {
 282   
         Vector fOther;
 283   
         
 284  0
         ParallelSwapper(Vector other) {
 285  0
             fOther= other;
 286   
         }
 287  0
         public void swap(Vector values, int left, int right) {
 288  0
             Object tmp= values.elementAt(left); 
 289  0
             values.setElementAt(values.elementAt(right), left); 
 290  0
             values.setElementAt(tmp, right);
 291  0
             Object tmp2= fOther.elementAt(left);
 292  0
             fOther.setElementAt(fOther.elementAt(right), left);
 293  0
             fOther.setElementAt(tmp2, right);
 294   
         }            
 295   
     }
 296   
 }
 297