Clover coverage report - JFCUnit Test Coverage
Coverage timestamp: Mon Dec 20 2004 23:38:10 MST
file stats: LOC: 153   Methods: 11
NCLOC: 62   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JMenuItemFinder.java 100% 81% 90.9% 85.3%
coverage coverage
 1   
 package junit.extensions.jfcunit.finder;
 2   
 
 3   
 import java.awt.Component;
 4   
 
 5   
 import javax.swing.Icon;
 6   
 import javax.swing.JMenuItem;
 7   
 
 8   
 
 9   
 /**
 10   
  * A generic component finder which uses just the type (class) for filtering.
 11   
  * The pattern syntax can be found at the Jakarta RegExp API Documentation in {@link org.apache.regexp.RE}.
 12   
  * This class delegates the matching of the Icon to an instance of {@link junit.extensions.jfcunit.finder.IconMatcher}
 13   
  *
 14   
  * @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
 15   
  */
 16   
 public class JMenuItemFinder extends Finder {
 17   
     /**
 18   
      * The matcher for the icon of the {@link javax.swing.AbstractButton} component.
 19   
      */
 20   
     private IconMatcher m_iconMatcher;
 21   
 
 22   
     /**
 23   
      * The text of the component.
 24   
      */
 25   
     private String m_text = null;
 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 the component.
 34   
      *
 35   
      * @param text    The desired pattern for the text of the component.
 36   
      */
 37  164
     public JMenuItemFinder(final String text) {
 38  164
         this(text, null, false);
 39   
     }
 40   
 
 41   
     /**
 42   
      * Constructor accepting all arguments needed to filter the component.
 43   
      *
 44   
      * @param text                The desired pattern for the text of the component.
 45   
      * @param caseIndependent    Whether the match should be case independent (true) or not (false)
 46   
      */
 47  1
     public JMenuItemFinder(final String text, final boolean caseIndependent) {
 48  1
         this(text, null, caseIndependent);
 49   
     }
 50   
 
 51   
     /**
 52   
      * Constructor accepting all arguments needed to filter the component.
 53   
      *
 54   
      * @param icon   The desired pattern for the icon of the component.
 55   
      */
 56  2
     public JMenuItemFinder(final Icon icon) {
 57  2
         this(null, icon, false);
 58   
     }
 59   
 
 60   
     /**
 61   
      * Constructor accepting all arguments needed to filter the component.
 62   
      *
 63   
      * @param text   The desired pattern for the text of the component.
 64   
      * @param icon   The desired pattern for the icon of the component.
 65   
      */
 66  7
     public JMenuItemFinder(final String text, final Icon icon) {
 67  7
         this(text, icon, false);
 68   
     }
 69   
 
 70   
     /**
 71   
      * Constructor accepting all arguments needed to filter the component.
 72   
      *
 73   
      * @param text               The desired pattern for the text of the component.
 74   
      * @param icon               The desired pattern for the icon of the component.
 75   
      * @param caseIndependent    Whether the match should be case independent (true) or not (false)
 76   
      */
 77  186
     public JMenuItemFinder(final String text, final Icon icon,
 78   
         final boolean caseIndependent) {
 79  186
         setText(text);
 80  186
         setIcon(icon);
 81  186
         this.m_caseIndependent = caseIndependent;
 82  186
         recreatePatternMatcher(m_text, caseIndependent);
 83   
     }
 84   
 
 85   
     /**
 86   
      * Set the finder into a case independent mode.
 87   
      * @param ignoreCase true if case should be ignored.
 88   
      */
 89  0
     public void setCaseIndependent(final boolean ignoreCase) {
 90  0
         super.setCaseIndependent(ignoreCase);
 91  0
         m_caseIndependent = ignoreCase;
 92  0
         recreatePatternMatcher(m_text, m_caseIndependent);
 93   
     }
 94   
 
 95   
     /**
 96   
      * Set the icon to be matched.
 97   
      * @param icon Icon to be matched.
 98   
      */
 99  187
     public final void setIcon(final Icon icon) {
 100  187
         try {
 101  187
             if (icon == null) {
 102  174
                 m_iconMatcher = null;
 103   
             } else {
 104  13
                 this.m_iconMatcher = new IconMatcher(icon);
 105   
             }
 106   
         } catch (InterruptedException ie) {
 107  0
             this.m_iconMatcher = null;
 108   
         }
 109   
     }
 110   
 
 111   
     /**
 112   
      * Get the icon to be matched.
 113   
      * @return Icon to be matched.
 114   
      */
 115  2
     public final Icon getIcon() {
 116  2
         return m_iconMatcher.getIcon();
 117   
     }
 118   
 
 119   
     /**
 120   
      * Set the text to be matched.
 121   
      * @param text to be matched to the menu item.
 122   
      */
 123  187
     public final void setText(final String text) {
 124  187
         m_text = text;
 125  187
         recreatePatternMatcher(m_text, m_caseIndependent);
 126   
     }
 127   
 
 128   
     /**
 129   
      * Get the text to be matched.
 130   
      * @return String text for the menu item.
 131   
      */
 132  2
     public final String getText() {
 133  2
         return m_text;
 134   
     }
 135   
 
 136   
     /**
 137   
      * Method that returns true if the given component matches the search
 138   
      * criteria.
 139   
      *
 140   
      * @param comp   The component to test
 141   
      * @return true if this component is a match
 142   
      */
 143  5296
     public boolean testComponent(final Component comp) {
 144   
         // since menuItem might not be visible, we cannot use isValidForProcessing()
 145  5296
         return ((comp != null) && comp instanceof JMenuItem
 146   
         && ((m_text == null) || evaluate(
 147   
             ((JMenuItem) comp).getText(),
 148   
             m_text))
 149   
         && ((m_iconMatcher == null)
 150   
         || m_iconMatcher.matches(((JMenuItem) comp).getIcon())));
 151   
     }
 152   
 }
 153