Coverage Report - org.eclipse.swtbot.swt.finder.matchers.WithItem
 
Classes in this File Line Coverage Branch Coverage Complexity
WithItem
89%
17/19
100%
4/4
1.429
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2009,2010 SWTBot Committers and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  * 
 8  
  * Contributors:
 9  
  *     Ketan Patel - initial API and implementation (Bug 259860)
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.matchers;
 12  
 
 13  
 import java.lang.reflect.InvocationTargetException;
 14  
 import java.util.ArrayList;
 15  
 
 16  
 import org.eclipse.swt.widgets.Item;
 17  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 18  
 import org.hamcrest.Description;
 19  
 import org.hamcrest.Factory;
 20  
 import org.hamcrest.Matcher;
 21  
 
 22  
 /**
 23  
  * @author Ketan Patel (Bug 259860)
 24  
  * @version $Id$
 25  
  */
 26  
 public class WithItem<T extends Item> extends AbstractMatcher<T> {
 27  
         private final Matcher<?>        itemMatcher;
 28  
         private final ArrayList<T>        matches;
 29  
 
 30  
         /**
 31  
          * Matches widgets which contains <code>item(s)</code>, as returned by <code>getItems</code> method, that match
 32  
          * given matcher...i.e CTabFolder with Item with text "xyz"
 33  
          * 
 34  
          * @param itemMatcher the item matcher
 35  
          */
 36  9
         WithItem(Matcher<?> itemMatcher) {
 37  9
                 this.itemMatcher = itemMatcher;
 38  9
                 matches = new ArrayList<T>();
 39  9
         }
 40  
 
 41  
         public void describeTo(Description description) {
 42  1
                 description.appendText("with item matching (");
 43  1
                 this.itemMatcher.describeTo(description);
 44  1
                 description.appendText(")");
 45  1
         }
 46  
 
 47  
         protected boolean doMatch(Object obj) {
 48  8
                 boolean result = false;
 49  
                 try {
 50  32
                         for (T item : getItems(obj)) {
 51  24
                                 if (this.itemMatcher.matches(item)) {
 52  18
                                         this.matches.add(item);
 53  18
                                         result = true;
 54  
                                 }
 55  
                         }
 56  0
                 } catch (Exception e) {
 57  
                         // do nothing
 58  
                 }
 59  8
                 return result;
 60  
         }
 61  
 
 62  
         @SuppressWarnings("unchecked")
 63  
         private T[] getItems(Object obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
 64  8
                 return (T[]) SWTUtils.invokeMethod(obj, "getItems");
 65  
         }
 66  
 
 67  
         public ArrayList<T> getAllMatches() {
 68  8
                 return this.matches;
 69  
         }
 70  
 
 71  
         public T get(int index) {
 72  0
                 return this.matches.get(index);
 73  
         }
 74  
 
 75  
         /**
 76  
          * Returns a matcher that matches objects containing an item that matches the matcher.
 77  
          * <p>
 78  
          * <strong>Note:</strong> This invokes getItems method on the object and expects to see an array as a return value.
 79  
          * </p>
 80  
          * 
 81  
          * @param matcher the matcher.
 82  
          * @return a matcher.
 83  
          */
 84  
         @Factory
 85  
         public static <T extends Item> WithItem<T> withItem(Matcher<?> matcher) {
 86  9
                 return new WithItem<T>(matcher);
 87  
         }
 88  
 }