001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: *
042: *
043: * $Id$ $Revision$ $Date$
044: *
045: */
046:
047: package org.netbeans.jemmy.util;
048:
049: import java.util.StringTokenizer;
050:
051: import org.netbeans.jemmy.operators.Operator.DefaultStringComparator;
052: import org.netbeans.jemmy.operators.Operator.StringComparator;
053:
054: /**
055: *
056: * Implementation of org.netbeans.jemmy.ComponentChooser interface.
057: * Class can be used to find component by its field/methods values converted to String.<br>
058: *
059: * Example:
060: * <pre>
061: * JLabel label = JLabelOperator.findJLabel(frm0, new StringPropChooser("getText=JLabel",
062: * false, true));
063: * </pre>
064: * @author Alexandre Iline (alexandre.iline@sun.com)
065: */
066:
067: public class StringPropChooser extends PropChooser {
068:
069: private StringComparator comparator;
070:
071: /**
072: * Constructs a StringPropChooser object.
073: * @param propNames Names of methods/fields
074: * @param params Parameters values for methods. <BR>
075: * @param classes Parameters classes.
076: * @param results Objects to compare converted to String method/field values to.
077: * @param comparator Defines string comparision criteria.
078: */
079: public StringPropChooser(String[] propNames, Object[][] params,
080: Class[][] classes, String[] results,
081: StringComparator comparator) {
082: super (propNames, params, classes, results);
083: this .comparator = comparator;
084: }
085:
086: /**
087: * Constructs a StringPropChooser object.
088: * @param propNames Names of methods/fields
089: * @param params Parameters values for methods. <BR>
090: * @param classes Parameters classes.
091: * @param results Objects to compare converted to String method/field values to.
092: * @param ce Compare exactly.<BR>
093: * If true, compare exactly (<value>.toString().equals(<result>)) <BR>
094: * If false, compare as substring (<value>.toString().indexOf(<result>) != -1)
095: * @param ccs Compare case sensitive. <BR>
096: * if false convert both <value>.toString() and <result> to uppercase before comparison.
097: */
098: public StringPropChooser(String[] propNames, Object[][] params,
099: Class[][] classes, String[] results, boolean ce, boolean ccs) {
100: this (propNames, params, classes, results,
101: new DefaultStringComparator(ce, ccs));
102: }
103:
104: /**
105: * Constructs a StringPropChooser object.
106: * @param propNames Names of methods/fields
107: * @param results Objects to compare converted to String method/field values to.
108: * @param comparator Defines string comparision criteria.
109: */
110: public StringPropChooser(String[] propNames, String[] results,
111: StringComparator comparator) {
112: this (propNames, (Object[][]) null, (Class[][]) null, results,
113: comparator);
114: }
115:
116: /**
117: * Constructs a StringPropChooser object.
118: * @param propNames Names of methods/fields
119: * @param results Objects to compare converted to String method/field values to.
120: * @param ce Compare exactly.
121: * @param ccs Compare case sensitive.
122: * @param @deprecated Use constructors with <code>StringComparator</code> parameters.
123: */
124: public StringPropChooser(String[] propNames, String[] results,
125: boolean ce, boolean ccs) {
126: this (propNames, (Object[][]) null, (Class[][]) null, results,
127: ce, ccs);
128: }
129:
130: /**
131: * Constructs a StringPropChooser object.
132: * @param props Method/field names && values <BR>
133: * Like "getText=button;isVisible=true"
134: * @param semicolonChar Method(field) names separator.
135: * @param equalChar Method(field) name - expected value separator.
136: * @param params Parameters values for methods.
137: * @param classes Parameters classes.
138: * @param comparator Defines string comparision criteria.
139: */
140: public StringPropChooser(String props, String semicolonChar,
141: String equalChar, Object[][] params, Class[][] classes,
142: StringComparator comparator) {
143: this (cutToArray(props, semicolonChar, equalChar, true), params,
144: classes, cutToArray(props, semicolonChar, equalChar,
145: false), comparator);
146: }
147:
148: /**
149: * Constructs a StringPropChooser object.
150: * @param props Method/field names && values <BR>
151: * Like "getText=button;isVisible=true"
152: * @param semicolonChar Method(field) names separator.
153: * @param equalChar Method(field) name - expected value separator.
154: * @param params Parameters values for methods.
155: * @param classes Parameters classes.
156: * @param ce Compare exactly.
157: * @param ccs Compare case sensitive.
158: * @param @deprecated Use constructors with <code>StringComparator</code> parameters.
159: */
160: public StringPropChooser(String props, String semicolonChar,
161: String equalChar, Object[][] params, Class[][] classes,
162: boolean ce, boolean ccs) {
163: this (cutToArray(props, semicolonChar, equalChar, true), params,
164: classes, cutToArray(props, semicolonChar, equalChar,
165: false), ce, ccs);
166: }
167:
168: /**
169: * Constructs a StringPropChooser object.
170: * @param props Method/field names && values
171: * @param semicolonChar Method(field) names separator.
172: * @param equalChar Method(field) name - expected value separator.
173: * @param comparator Defines string comparision criteria.
174: */
175: public StringPropChooser(String props, String semicolonChar,
176: String equalChar, StringComparator comparator) {
177: this (props, semicolonChar, equalChar, (Object[][]) null,
178: (Class[][]) null, comparator);
179: }
180:
181: /**
182: * Constructs a StringPropChooser object.
183: * @param props Method/field names && values
184: * @param semicolonChar Method(field) names separator.
185: * @param equalChar Method(field) name - expected value separator.
186: * @param ce Compare exactly.
187: * @param ccs Compare case sensitive.
188: * @param @deprecated Use constructors with <code>StringComparator</code> parameters.
189: */
190: public StringPropChooser(String props, String semicolonChar,
191: String equalChar, boolean ce, boolean ccs) {
192: this (props, semicolonChar, equalChar, (Object[][]) null,
193: (Class[][]) null, ce, ccs);
194: }
195:
196: /**
197: * Constructs a StringPropChooser object.
198: * @param props Method/field names && values <BR>
199: * ";" is used as a method(field) names separator. <BR>
200: * "=" is used as a method(field) name - expected value separator.
201: * @param params Parameters values for methods.
202: * @param classes Parameters classes.
203: * @param comparator Defines string comparision criteria.
204: */
205: public StringPropChooser(String props, Object[][] params,
206: Class[][] classes, StringComparator comparator) {
207: this (props, ";", "=", params, classes, comparator);
208: }
209:
210: /**
211: * Constructs a StringPropChooser object.
212: * @param props Method/field names && values <BR>
213: * ";" is used as a method(field) names separator. <BR>
214: * "=" is used as a method(field) name - expected value separator.
215: * @param params Parameters values for methods.
216: * @param classes Parameters classes.
217: * @param ce Compare exactly.
218: * @param ccs Compare case sensitive.
219: * @param @deprecated Use constructors with <code>StringComparator</code> parameters.
220: */
221: public StringPropChooser(String props, Object[][] params,
222: Class[][] classes, boolean ce, boolean ccs) {
223: this (props, ";", "=", params, classes, ce, ccs);
224: }
225:
226: /**
227: * Constructs a StringPropChooser object.
228: * @param props Method/field names && values
229: * ";" is used as a method(field) names separator. <BR>
230: * "=" is used as a method(field) name - expected value separator.
231: * @param comparator Defines string comparision criteria.
232: */
233: public StringPropChooser(String props, StringComparator comparator) {
234: this (props, (Object[][]) null, (Class[][]) null, comparator);
235: }
236:
237: /**
238: * Constructs a StringPropChooser object.
239: * @param props Method/field names && values
240: * ";" is used as a method(field) names separator. <BR>
241: * "=" is used as a method(field) name - expected value separator.
242: * @param ce Compare exactly.
243: * @param ccs Compare case sensitive.
244: * @param @deprecated Use constructors with <code>StringComparator</code> parameters.
245: */
246: public StringPropChooser(String props, boolean ce, boolean ccs) {
247: this (props, (Object[][]) null, (Class[][]) null, ce, ccs);
248: }
249:
250: /**
251: * @see org.netbeans.jemmy.ComponentChooser
252: */
253: public String getDescription() {
254: String result = "";
255: for (int i = 0; i < propNames.length; i++) {
256: if (!result.equals("")) {
257: result = result + ";";
258: }
259: result = result + propNames[i] + "=" + (String) results[i];
260: }
261: return ("Component by properties array\n : " + result);
262: }
263:
264: /**
265: * Method to check property.
266: * Compares "value".toString() to (String)etalon according ce and ccs constructor parameters.
267: * @param value Method/field value
268: * @param etalon Object to compare to.
269: * @return true if the value matches the etalon.
270: */
271: protected boolean checkProperty(Object value, Object etalon) {
272: return (comparator.equals(value.toString(), (String) etalon));
273: }
274:
275: /*split string to array*/
276: private static String[] cutToArray(String resources,
277: String semicolon, String equal, boolean names) {
278: StringTokenizer token = new StringTokenizer(resources,
279: semicolon);
280: String[] props = new String[token.countTokens()];
281: String nextProp;
282: int ind = 0;
283: while (token.hasMoreTokens()) {
284: nextProp = token.nextToken();
285: StringTokenizer subtoken = new StringTokenizer(nextProp,
286: equal);
287: if (subtoken.countTokens() == 2) {
288: props[ind] = subtoken.nextToken();
289: if (!names) {
290: props[ind] = subtoken.nextToken();
291: }
292: } else {
293: props[ind] = null;
294: }
295: ind++;
296: }
297: return (props);
298: }
299:
300: }
|