Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
17   126   10   2.43
6   44   0.59   7
7     1.43  
1    
 
  ComponentFinderTemplate       Line # 35 17 0% 10 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Oct 29, 2007
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2007-2010 the original author or authors.
15    */
16    package org.fest.swing.finder;
17   
18    import static org.fest.swing.timing.Pause.pause;
19    import static org.fest.util.Strings.concat;
20   
21    import java.awt.Component;
22    import java.util.concurrent.TimeUnit;
23   
24    import org.fest.swing.core.*;
25    import org.fest.swing.exception.WaitTimedOutError;
26    import org.fest.swing.fixture.ComponentFixture;
27   
28    /**
29    * Understands a template for <code>{@link Component}</code> finders.
30    * @param <T> the type of component this finder can search.
31    *
32    * @author Yvonne Wang
33    * @author Alex Ruiz
34    */
 
35    public abstract class ComponentFinderTemplate<T extends Component> {
36   
37    static final long TIMEOUT = 5000;
38   
39    private long timeout = TIMEOUT;
40   
41    private final ComponentMatcher matcher;
42    private final String searchDescription;
43   
44    /**
45    * Creates a new </code>{@link ComponentFinderTemplate}</code>.
46    * @param componentName the name of the {@code Component} to find.
47    * @param componentType the type of the {@code Component} to find.
48    */
 
49  20 toggle protected ComponentFinderTemplate(String componentName, Class<? extends T> componentType) {
50  20 this(new NameMatcher(componentName, componentType, true));
51    }
52   
53    /**
54    * Creates a new </code>{@link ComponentFinderTemplate}</code>.
55    * @param matcher specifies the search criteria to use when looking up a {@code Component}.
56    */
 
57  29 toggle protected ComponentFinderTemplate(GenericTypeMatcher<? extends T> matcher) {
58  29 this((ComponentMatcher)matcher);
59    }
60   
61    /**
62    * Creates a new </code>{@link ComponentFinderTemplate}</code>.
63    * @param componentType the type of the {@code Component} to find.
64    */
 
65  29 toggle protected ComponentFinderTemplate(Class<? extends T> componentType) {
66  29 this(new TypeMatcher(componentType, true));
67    }
68   
 
69  82 toggle private ComponentFinderTemplate(ComponentMatcher matcher) {
70  4 if (matcher == null) throw new NullPointerException("The matcher should not be null");
71  78 this.matcher = matcher;
72  78 searchDescription = concat("component to be found using matcher ", matcher);
73    }
74   
75    /**
76    * Sets the timeout for this finder. The {@code Component} to find should be found within the given time period.
77    * @param newTimeout the period of time the search should be performed.
78    * @param unit the time unit for <code>timeout</code>.
79    * @return this finder.
80    * @throws NullPointerException if the time unit is <code>null</code>.
81    * @throws IllegalArgumentException if the timeout is a negative number.
82    */
 
83  22 toggle protected ComponentFinderTemplate<T> withTimeout(long newTimeout, TimeUnit unit) {
84  11 if (unit == null) throw new NullPointerException("Time unit cannot be null");
85  11 return withTimeout(unit.toMillis(newTimeout));
86    }
87   
88    /**
89    * Sets the timeout for this finder. The {@code Component} to find should be found within the given time period.
90    * @param newTimeout the number of milliseconds before stopping the search.
91    * @return this finder.
92    * @throws IllegalArgumentException if the timeout is a negative number.
93    */
 
94  34 toggle protected ComponentFinderTemplate<T> withTimeout(long newTimeout) {
95  11 if (newTimeout < 0) throw new IllegalArgumentException("Timeout cannot be a negative number");
96  23 this.timeout = newTimeout;
97  23 return this;
98    }
99   
100    /**
101    * Finds a component by name or type using the given robot.
102    * @param robot contains the underlying finding to delegate the search to.
103    * @return a fixture capable of managing the found component.
104    * @throws WaitTimedOutError if a component with the given name or of the given type could not be found.
105    */
106    public abstract ComponentFixture<T> using(Robot robot);
107   
108    /**
109    * Finds the component using either by name or type.
110    * @param robot contains the underlying finding to delegate the search to.
111    * @return the found component.
112    * @throws WaitTimedOutError if a component with the given name or of the given type could not be found.
113    */
 
114  56 toggle protected final T findComponentWith(Robot robot) {
115  56 ComponentFoundCondition condition = new ComponentFoundCondition(searchDescription, robot.finder(), matcher);
116  56 pause(condition, timeout);
117  44 return cast(condition.found());
118    }
119   
120    /**
121    * Casts the given {@code Component} to the type supported by this finder.
122    * @param c the given {@code Component}.
123    * @return the given {@code Component} casted to the type supported by this finder.
124    */
125    protected abstract T cast(Component c);
126    }