|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Operator.java | 78.6% | 87.5% | 71.4% | 82.7% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit.tools;
|
|
| 2 |
|
|
| 3 |
import org.apache.regexp.RE;
|
|
| 4 |
import org.apache.regexp.RESyntaxException;
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
/**
|
|
| 8 |
* Abstract class for defining call back classes to test whether a {@link java.awt.Component}
|
|
| 9 |
* that is being searched for has been found.
|
|
| 10 |
*
|
|
| 11 |
* @author kevin Wilson
|
|
| 12 |
*/
|
|
| 13 |
public final class Operator { |
|
| 14 |
/**
|
|
| 15 |
* This is the lock for synchronizing checks of the cache.
|
|
| 16 |
*/
|
|
| 17 |
private static Object s_lock = new Object(); |
|
| 18 |
|
|
| 19 |
/**
|
|
| 20 |
* Pattern which was cached.
|
|
| 21 |
*/
|
|
| 22 |
private static String s_cachePattern = null; |
|
| 23 |
|
|
| 24 |
/**
|
|
| 25 |
* Cached regular expression.
|
|
| 26 |
*/
|
|
| 27 |
private static RE s_cacheRE = null; |
|
| 28 |
|
|
| 29 |
/**
|
|
| 30 |
* Constructor should be private.
|
|
| 31 |
*/
|
|
| 32 | 0 |
private Operator() {
|
| 33 |
} |
|
| 34 |
|
|
| 35 |
/**
|
|
| 36 |
* Evaluate the current operation.
|
|
| 37 |
* @param leftside String to be compared.
|
|
| 38 |
* @param rightside String or Regular expression.
|
|
| 39 |
* @param operation See the Operation subclass for values.
|
|
| 40 |
* @param ignoreCase Ignore the case of the values.
|
|
| 41 |
* @return result of operation
|
|
| 42 |
*/
|
|
| 43 | 30 |
public static boolean evaluate(final String leftside, |
| 44 |
final String rightside, final int operation, final boolean ignoreCase) { |
|
| 45 | 30 |
String left = leftside; |
| 46 | 30 |
String right = rightside; |
| 47 |
|
|
| 48 | 30 |
if (left == null) { |
| 49 |
// If left and right are null then return true.
|
|
| 50 |
// otherwise return false.
|
|
| 51 | 1 |
return right == null; |
| 52 |
} |
|
| 53 |
|
|
| 54 | 29 |
if (ignoreCase) {
|
| 55 | 17 |
if (left != null) { |
| 56 | 17 |
left = left.toUpperCase(); |
| 57 |
} |
|
| 58 |
|
|
| 59 | 17 |
if (right != null) { |
| 60 | 17 |
right = right.toUpperCase(); |
| 61 |
} |
|
| 62 |
} |
|
| 63 |
|
|
| 64 | 29 |
if (operation == Operation.CONTAINS) {
|
| 65 | 4 |
return (left.indexOf(right) >= 0);
|
| 66 | 25 |
} else if (operation == Operation.ENDSWITH) { |
| 67 | 4 |
return (left.endsWith(right));
|
| 68 | 21 |
} else if (operation == Operation.STARTSWITH) { |
| 69 | 4 |
return (left.startsWith(right));
|
| 70 | 17 |
} else if (operation == Operation.EQUALS) { |
| 71 | 10 |
return (left.equals(right));
|
| 72 | 7 |
} else if (operation == Operation.MATCH) { |
| 73 | 7 |
return matchPattern(left, right);
|
| 74 |
} |
|
| 75 |
|
|
| 76 | 0 |
throw new java.lang.IllegalStateException( |
| 77 |
"Invalid operation for finder:" + operation);
|
|
| 78 |
} |
|
| 79 |
|
|
| 80 |
/**
|
|
| 81 |
* Get the regular expression implementing the pattern.
|
|
| 82 |
* @param pattern String representation of RE.
|
|
| 83 |
* @return RE Class representation of RE.
|
|
| 84 |
*/
|
|
| 85 | 7 |
private static RE getPattern(final String pattern) { |
| 86 | 7 |
synchronized (s_lock) {
|
| 87 | 7 |
if ((pattern != null) && pattern.equals(s_cachePattern)) { |
| 88 | 4 |
return s_cacheRE;
|
| 89 |
} |
|
| 90 |
|
|
| 91 | 3 |
try {
|
| 92 | 3 |
s_cachePattern = pattern; |
| 93 |
|
|
| 94 | 3 |
if (pattern == null) { |
| 95 | 0 |
s_cacheRE = new RE(""); |
| 96 |
} else {
|
|
| 97 | 3 |
s_cacheRE = new RE(pattern);
|
| 98 |
} |
|
| 99 |
} catch (RESyntaxException ex) {
|
|
| 100 | 0 |
s_cacheRE = null;
|
| 101 |
} |
|
| 102 |
} |
|
| 103 |
|
|
| 104 | 3 |
return s_cacheRE;
|
| 105 |
} |
|
| 106 |
|
|
| 107 |
/**
|
|
| 108 |
* This method is used to filter components' attributes based on a pattern specified by the user.
|
|
| 109 |
* For example, to search for all windows with title matching 'testWindow*'.
|
|
| 110 |
* Note: If the patternString is null, we need to avoid the <code>PatternCompiler.compile()</code>
|
|
| 111 |
* throwing a NullPointerException and in this case, return true if the componentAttribute
|
|
| 112 |
* is also null.
|
|
| 113 |
* The pattern syntax can be found at the Jakarta RegExp API Documentation in {@link org.apache.regexp.RE}.
|
|
| 114 |
*
|
|
| 115 |
* @param componentAttribute The attribute text of the component to match against
|
|
| 116 |
* @param patternString The pattern to match with
|
|
| 117 |
* @return boolean whether the pattern is contained within the components' attribute text
|
|
| 118 |
*/
|
|
| 119 | 7 |
private static boolean matchPattern(final String componentAttribute, |
| 120 |
final String patternString) {
|
|
| 121 | 7 |
RE re = getPattern(patternString); |
| 122 | 7 |
boolean value = re.match(componentAttribute);
|
| 123 |
|
|
| 124 | 7 |
return ((componentAttribute != null) && (re != null) && value); |
| 125 |
} |
|
| 126 |
|
|
| 127 |
/**
|
|
| 128 |
* Operations for comparing strings.
|
|
| 129 |
* <p>Title: class Operator.Operations</p>
|
|
| 130 |
* <p>Description: Operation are specified in the
|
|
| 131 |
* Operator.evaluate() method.</p>
|
|
| 132 |
* <p>Copyright: Copyright (c) 2004</p>
|
|
| 133 |
* <p>Company: JFCUnit OpenSource project.</p>
|
|
| 134 |
* @author Kevin Wilson
|
|
| 135 |
* @version 1.0
|
|
| 136 |
*/
|
|
| 137 |
public static final class Operation { |
|
| 138 |
/**
|
|
| 139 |
* String so for the OP codes.
|
|
| 140 |
*/
|
|
| 141 |
private static final String[] CODE_STRINGS = new String[] { |
|
| 142 |
"match", "startswith", "endswith", "equals", "contains" |
|
| 143 |
}; |
|
| 144 |
|
|
| 145 |
/**
|
|
| 146 |
* Match the item using a Regular expression.
|
|
| 147 |
*/
|
|
| 148 |
public static final int MATCH = 0; |
|
| 149 |
|
|
| 150 |
/**
|
|
| 151 |
* Check that the value starts with the given
|
|
| 152 |
* value.
|
|
| 153 |
*/
|
|
| 154 |
public static final int STARTSWITH = 1; |
|
| 155 |
|
|
| 156 |
/**
|
|
| 157 |
* Check that the value ends with the given
|
|
| 158 |
* value.
|
|
| 159 |
*/
|
|
| 160 |
public static final int ENDSWITH = 2; |
|
| 161 |
|
|
| 162 |
/**
|
|
| 163 |
* Check that the values are equal.
|
|
| 164 |
*/
|
|
| 165 |
public static final int EQUALS = 3; |
|
| 166 |
|
|
| 167 |
/**
|
|
| 168 |
* Check that the value is contained.
|
|
| 169 |
*/
|
|
| 170 |
public static final int CONTAINS = 4; |
|
| 171 |
|
|
| 172 |
/**
|
|
| 173 |
* Private constructor. This class should not be instanciated.
|
|
| 174 |
*/
|
|
| 175 | 0 |
private Operation() {
|
| 176 |
} |
|
| 177 |
|
|
| 178 |
/**
|
|
| 179 |
* Convert the String to a Operation code.
|
|
| 180 |
* @param code String version of the Operation.
|
|
| 181 |
* @return int version of the operation.
|
|
| 182 |
*/
|
|
| 183 | 8 |
public static int getOperation(final String code) { |
| 184 | 27 |
for (int i = 0; i < CODE_STRINGS.length; i++) { |
| 185 | 27 |
if (CODE_STRINGS[i].equalsIgnoreCase(code)) {
|
| 186 | 8 |
return i;
|
| 187 |
} |
|
| 188 |
} |
|
| 189 |
|
|
| 190 | 0 |
throw new java.lang.IllegalArgumentException("Operation not found:" |
| 191 |
+ code); |
|
| 192 |
} |
|
| 193 |
|
|
| 194 |
/**
|
|
| 195 |
* Get the string version of the operation.
|
|
| 196 |
* @param code int Finder.OP_code
|
|
| 197 |
* @return String version of the operation.
|
|
| 198 |
*/
|
|
| 199 | 5 |
public static String toString(final int code) { |
| 200 | 5 |
if ((code < 0) || (code >= CODE_STRINGS.length)) {
|
| 201 | 0 |
throw new IllegalArgumentException("Invalid Operation code:" |
| 202 |
+ code); |
|
| 203 |
} |
|
| 204 |
|
|
| 205 | 5 |
return CODE_STRINGS[code];
|
| 206 |
} |
|
| 207 |
} |
|
| 208 |
} |
|
| 209 |
|
|
||||||||||