|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NamedComponentMatcherTemplate | Line # 35 | 21 | 0% | 15 | 0 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| No Tests | |||
| 1 | /* | |
| 2 | * Created on Jan 12, 2009 | |
| 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 @2009-2010 the original author or authors. | |
| 15 | */ | |
| 16 | package org.fest.swing.core.matcher; | |
| 17 | ||
| 18 | import static org.fest.swing.util.Strings.areEqualOrMatch; | |
| 19 | import static org.fest.swing.util.Strings.match; | |
| 20 | import static org.fest.util.Objects.areEqual; | |
| 21 | import static org.fest.util.Strings.quote; | |
| 22 | ||
| 23 | import java.awt.Component; | |
| 24 | import java.util.regex.Pattern; | |
| 25 | ||
| 26 | import org.fest.swing.core.GenericTypeMatcher; | |
| 27 | ||
| 28 | /** | |
| 29 | * Understands a template for matching components by name. Subclasses are free to add other properties to use as search | |
| 30 | * criteria. | |
| 31 | * @param <T> the type of <code>Component</code> supported by this matcher. | |
| 32 | * | |
| 33 | * @author Alex Ruiz | |
| 34 | */ | |
| 35 | public abstract class NamedComponentMatcherTemplate<T extends Component> extends GenericTypeMatcher<T> { | |
| 36 | ||
| 37 | /** | |
| 38 | * Indicates that a property value to use as search criteria has not been set. | |
| 39 | */ | |
| 40 | protected static final Object ANY = new Object() { | |
| 41 | 1 |
@Override public String toString() { return "<Any>"; } |
| 42 | }; | |
| 43 | ||
| 44 | /** The component name to match. **/ | |
| 45 | protected final Object name; | |
| 46 | ||
| 47 | /** | |
| 48 | * Creates a new </code>{@link NamedComponentMatcherTemplate}</code>. | |
| 49 | * @param supportedType the type supported by this matcher. | |
| 50 | * @throws NullPointerException if the given type is <code>null</code>. | |
| 51 | */ | |
| 52 | 6 |
protected NamedComponentMatcherTemplate(Class<T> supportedType) { |
| 53 | 6 | super(supportedType); |
| 54 | 6 | this.name = ANY; |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Creates a new </code>{@link NamedComponentMatcherTemplate}</code>. | |
| 59 | * @param supportedType the type supported by this matcher. | |
| 60 | * @param name the component name to match. | |
| 61 | * @throws NullPointerException if the given type is <code>null</code>. | |
| 62 | */ | |
| 63 | 128 |
protected NamedComponentMatcherTemplate(Class<T> supportedType, Object name) { |
| 64 | 128 | super(supportedType); |
| 65 | 128 | this.name = name; |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Returns the component name to match surrounded by double quotes. If the component name has not been set, it will | |
| 70 | * return <code>{@link #ANY}</code>. This method is commonly used in implementations of <code>toString</code>. | |
| 71 | * @return the component name to match surrounded by double quotes, or <code>{@link #ANY}</code> if the component name | |
| 72 | * has not been set. | |
| 73 | */ | |
| 74 | 7 |
protected final Object quotedName() { |
| 75 | 7 | return quoted(name); |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Returns the given property value to match surrounded by double quotes. If the property has not been set, it will | |
| 80 | * return <code>{@link #ANY}</code>. This method is commonly used in implementations of <code>toString</code>. | |
| 81 | * @param propertyValue the given property value. | |
| 82 | * @return the given property value to match surrounded by double quotes, or <code>{@link #ANY}</code> if the property | |
| 83 | * value has not been set. | |
| 84 | */ | |
| 85 | 15 |
protected final Object quoted(Object propertyValue) { |
| 86 | 2 | if (ANY.equals(propertyValue)) return ANY; |
| 87 | 1 | if (propertyValue instanceof Pattern) return quote(((Pattern)propertyValue).pattern()); |
| 88 | 12 | return quote(propertyValue); |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * Indicates whether the given value matches the name in this matcher. It always returns <code>true</code> if this | |
| 93 | * matcher's name is <code>{@link #ANY}</code>. | |
| 94 | * @param actual the actual component name. | |
| 95 | * @return <code>true</code> if this matcher's name is <code>ANY</code> or if both the actual name is equal to the one | |
| 96 | * in this matcher. Otherwise <code>false</code>. | |
| 97 | */ | |
| 98 | 128 |
protected final boolean isNameMatching(String actual) { |
| 99 | 72 | if (ANY.equals(name)) return true; |
| 100 | 56 | return areEqual(name, actual); |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Indicates whether the given value matches the expected value in this matcher. Matching is performed as follows: | |
| 105 | * <ol> | |
| 106 | * <li>it always returns <code>true</code> if the expected value is <code>{@link #ANY}</code></li> | |
| 107 | * <li>if both the expected and actual values are <code>String</code>s, it checks for equality first. If this fails, | |
| 108 | * it tries to match the values assuming the expected value can be a regular expression</li> | |
| 109 | * <li>if the expected value is a <code>{@link Pattern}</code> and the actual value is a | |
| 110 | * <code>{@link CharSequence}</code>, regular expression matching is performed</li> | |
| 111 | * <li>otherwise, it checks that both the expected and actual values are equal</li> | |
| 112 | * </ol> | |
| 113 | * @param expected the expected value in this matcher. | |
| 114 | * @param actual the actual property value. | |
| 115 | * @return <code>true</code> if the values match, otherwise <code>false</code>. | |
| 116 | */ | |
| 117 | 102 |
protected final boolean arePropertyValuesMatching(Object expected, Object actual) { |
| 118 | 10 | if (ANY.equals(expected)) return true; |
| 119 | 92 | if (expected instanceof String && actual instanceof String) |
| 120 | 65 | return areEqualOrMatch((String)expected, (String)actual); |
| 121 | 27 | if (expected instanceof Pattern && actual instanceof CharSequence) |
| 122 | 22 | return match((Pattern)expected, (CharSequence)actual); |
| 123 | 5 | return areEqual(expected, actual); |
| 124 | } | |
| 125 | ||
| 126 | } | |
|
||||||||||||