Clover coverage report - JFCUnit Test Coverage
Coverage timestamp: Mon Dec 20 2004 23:38:10 MST
file stats: LOC: 211   Methods: 9
NCLOC: 114   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractWindowFinder.java 76.9% 82% 77.8% 80%
coverage coverage
 1   
 package junit.extensions.jfcunit.finder;
 2   
 
 3   
 import java.util.ArrayList;
 4   
 import java.util.List;
 5   
 
 6   
 import java.awt.Container;
 7   
 import java.awt.Frame;
 8   
 import java.awt.Window;
 9   
 import javax.swing.JDialog;
 10   
 import javax.swing.SwingUtilities;
 11   
 import junit.extensions.jfcunit.WindowMonitor;
 12   
 
 13   
 /**
 14   
  * Class for checking if the ({@link java.awt.Window}) component being searched for has been found.
 15   
  * This class can be used to check only windows that have titles (Frame/Dialog and their sub-classes)
 16   
  * The pattern syntax can be found at the Jakarta RegExp API Documentation in {@link org.apache.regexp.RE}.
 17   
  *
 18   
  * @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
 19   
  */
 20   
 public abstract class AbstractWindowFinder
 21   
     extends Finder {
 22   
     /**
 23   
      * The title of the component.
 24   
      */
 25   
     private String m_title;
 26   
 
 27   
     /**
 28   
      * A boolean specifying whether the filtration is case insensitive.
 29   
      */
 30   
     private boolean m_caseIndependent = false;
 31   
 
 32   
     /**
 33   
      * Constructor accepting all arguments needed to filter component.
 34   
      *
 35   
      * @param str    The desired pattern for the title of the component.
 36   
      */
 37  332
     public AbstractWindowFinder(final String str) {
 38  332
         this(str, false);
 39   
     }
 40   
 
 41   
     /**
 42   
      * Constructor accepting all arguments needed to filter component.
 43   
      *
 44   
      * @param title                The desired pattern for the title of the component.
 45   
      * @param caseIndependent    Whether the match should be case independent (true) or not (false)
 46   
      */
 47  344
     public AbstractWindowFinder(final String title, final boolean caseIndependent) {
 48  344
         m_title = title;
 49  344
         m_caseIndependent = caseIndependent;
 50  344
         recreatePatternMatcher(m_title, caseIndependent);
 51   
     }
 52   
 
 53   
     /**
 54   
      * Set the title for the finder.
 55   
      * @param title String title to be set.
 56   
      */
 57  4
     public final void setTitle(final String title) {
 58  4
         m_title = title;
 59  4
         recreatePatternMatcher(m_title, m_caseIndependent);
 60   
     }
 61   
 
 62   
     /**
 63   
      * Set the finder into a case independent mode.
 64   
      * @param ignoreCase true if case should be ignored.
 65   
      */
 66  0
     public void setCaseIndependent(final boolean ignoreCase) {
 67  0
         super.setCaseIndependent(ignoreCase);
 68  0
         m_caseIndependent = ignoreCase;
 69  0
         recreatePatternMatcher(m_title, m_caseIndependent);
 70   
     }
 71   
 
 72   
     /**
 73   
      * Returns the caseIndependent.
 74   
      * @return boolean
 75   
      */
 76  0
     public boolean isCaseIndependent() {
 77  0
         return m_caseIndependent;
 78   
     }
 79   
 
 80   
     /**
 81   
      * Returns the title.
 82   
      * @return String
 83   
      */
 84  5
     public final String getTitle() {
 85  5
         return m_title;
 86   
     }
 87   
 
 88   
     /**
 89   
      * Returns a set of all the Windows that are currently visible and the title
 90   
      * contains the given titlematch string.
 91   
      *
 92   
      * @param pRet     The list of already filtered and accepted windows.
 93   
      * @param windows The array of windows to filter and add.
 94   
      * @param finder  The FrameFinder which is used to filter using a title match
 95   
      * @return Set of Window objects.
 96   
      */
 97  465
     public static List getWindows(final List pRet, final Container[] windows,
 98   
                                    final Finder finder) {
 99  465
         List ret = pRet;
 100   
 
 101  465
         if (ret == null) {
 102  0
             ret = new ArrayList();
 103   
         }
 104   
 
 105  465
         if ((windows == null) || (finder == null)) {
 106  0
             return ret;
 107   
         }
 108   
 
 109  465
         Window window;
 110   
 
 111  465
         for (int i = 0; i < windows.length; i++) {
 112  332
             Container c = windows[i];
 113  332
             if (c instanceof Window) {
 114  332
                 window = (Window) windows[i];
 115   
             } else {
 116  0
                 window = SwingUtilities.getWindowAncestor(c);
 117   
             }
 118  332
             if (!finder.testComponent(window)) {
 119  137
                 getWindows(
 120   
                     ret,
 121   
                     window.getOwnedWindows(),
 122   
                     finder);
 123  137
                 continue;
 124   
             }
 125   
 
 126  195
             String title = "";
 127   
 
 128  195
             if (window instanceof Frame) {
 129  97
                 title = ((Frame) window).getTitle();
 130  98
             } else if (window instanceof JDialog) {
 131  98
                 title = ((JDialog) window).getTitle();
 132   
             }
 133   
 
 134   
             // if this test is run through any of the TestRunner classes which
 135   
             // brings up a GUI window, we need to skip that frame
 136  195
             if ("JUnit".equalsIgnoreCase(title)) {
 137  0
                 continue;
 138   
             }
 139   
 
 140  195
             if (!ret.contains(window)) {
 141  155
                 ret.add(window);
 142   
             }
 143   
 
 144   
             // add any windows owned by the current 'frame' object
 145  195
             getWindows(
 146   
                 ret,
 147   
                 window.getOwnedWindows(),
 148   
                 finder);
 149   
         }
 150   
 
 151  465
         return ret;
 152   
     }
 153   
 
 154   
     /**
 155   
      * Find the components matching this finder.
 156   
      * Windows searched will be owned by the containers given.
 157   
      *
 158   
      * @param owners Owner windows which should be searched.
 159   
      * @return Set contain the windows matching the finder.
 160   
      */
 161  106
     public List findAll(final Container[] owners) {
 162  106
         List retSet = new ArrayList();
 163  106
         int wait = Math.max(
 164   
             0,
 165   
             getWait());
 166  106
         long date = System.currentTimeMillis() + (wait * 1000);
 167   
 
 168  106
         do {
 169  133
             if (owners == null) {
 170  131
                 retSet = getWindows(
 171   
                     retSet,
 172   
                     WindowMonitor.getWindows(),
 173   
                     this);
 174   
             } else {
 175  2
                 retSet = getWindows(
 176   
                     retSet,
 177   
                     owners,
 178   
                     this);
 179   
             }
 180   
 
 181  133
             if (retSet.size() == 0) {
 182  75
                 pause(date);
 183   
             }
 184   
         }
 185  133
         while ((retSet.size() == 0) && (System.currentTimeMillis() < date));
 186   
 
 187  106
         return retSet;
 188   
     }
 189   
 
 190   
     /**
 191   
      * This method checks whether the winTitle and the title strings are equal.
 192   
      * Note: If the title string is null, we need to return all showing windows,
 193   
      * so 'true' is returned.
 194   
      *
 195   
      * @param winTitle The title of the window being checked
 196   
      * @return True if either the filter string is null or if the
 197   
      *         filter string is a substring of the window title
 198   
      */
 199  295
     protected final boolean checkIfTitleMatches(final String winTitle) {
 200  295
         boolean value = ((m_title == null)
 201   
                          || ((winTitle != null) && evaluate(winTitle, m_title)));
 202   
 
 203  295
         if (getDebug()) {
 204  0
             System.err.println("Comparing window title(" + value + "): "
 205   
                                + winTitle + " match " + m_title);
 206   
         }
 207   
 
 208  295
         return value;
 209   
     }
 210   
 }
 211