001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations
015: * under the License.
016: *
017: */
018:
019: package org.apache.jmeter.testelement.property;
020:
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.jmeter.testelement.TestElement;
026: import org.apache.jorphan.logging.LoggingManager;
027: import org.apache.log.Logger;
028:
029: /**
030: * @version $Revision: 597913 $
031: */
032: public abstract class AbstractProperty implements JMeterProperty {
033: //TODO consider using private logs for each derived class
034: protected static final Logger log = LoggingManager
035: .getLoggerForClass();
036:
037: private String name;
038:
039: transient private boolean runningVersion = false;
040:
041: // private static StringProperty defaultProperty = new StringProperty();
042:
043: public AbstractProperty(String name) {
044: if (name == null)
045: throw new IllegalArgumentException("Name cannot be null");
046: this .name = name;
047: }
048:
049: public AbstractProperty() {
050: this ("");
051: }
052:
053: protected boolean isEqualType(JMeterProperty prop) {
054: if (this .getClass().equals(prop.getClass())) {
055: return true;
056: } else {
057: return false;
058: }
059: }
060:
061: /*
062: * (non-Javadoc)
063: *
064: * @see JMeterProperty#isRunningVersion()
065: */
066: public boolean isRunningVersion() {
067: return runningVersion;
068: }
069:
070: /*
071: * (non-Javadoc)
072: *
073: * @see JMeterProperty#getName()
074: */
075: public String getName() {
076: return name;
077: }
078:
079: public void setName(String name) {
080: if (name == null)
081: throw new IllegalArgumentException("Name cannot be null");
082: this .name = name;
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see JMeterProperty#setRunningVersion(boolean)
089: */
090: public void setRunningVersion(boolean runningVersion) {
091: this .runningVersion = runningVersion;
092: }
093:
094: protected PropertyIterator getIterator(Collection values) {
095: return new PropertyIteratorImpl(values);
096: }
097:
098: /*
099: * (non-Javadoc)
100: *
101: * @see Object#clone()
102: */
103: public Object clone() {
104: try {
105: AbstractProperty prop = (AbstractProperty) this .getClass()
106: .newInstance();
107: prop.name = name;
108: prop.runningVersion = runningVersion;
109: return prop;
110: } catch (InstantiationException e) {
111: throw new AssertionError(e); // clone should never return null
112: } catch (IllegalAccessException e) {
113: throw new AssertionError(e); // clone should never return null
114: }
115: }
116:
117: /**
118: * Returns 0 if string is invalid or null.
119: *
120: * @see JMeterProperty#getIntValue()
121: */
122: public int getIntValue() {
123: String val = getStringValue();
124: if (val == null) {
125: return 0;
126: }
127: try {
128: return Integer.parseInt(val);
129: } catch (NumberFormatException e) {
130: return 0;
131: }
132: }
133:
134: /**
135: * Returns 0 if string is invalid or null.
136: *
137: * @see JMeterProperty#getLongValue()
138: */
139: public long getLongValue() {
140: String val = getStringValue();
141: if (val == null) {
142: return 0;
143: }
144: try {
145: return Long.parseLong(val);
146: } catch (NumberFormatException e) {
147: return 0;
148: }
149: }
150:
151: /**
152: * Returns 0 if string is invalid or null.
153: *
154: * @see JMeterProperty#getDoubleValue()
155: */
156: public double getDoubleValue() {
157: String val = getStringValue();
158: if (val == null) {
159: return 0;
160: }
161: try {
162: return Double.parseDouble(val);
163: } catch (NumberFormatException e) {
164: log.error(
165: "Tried to parse a non-number string to an integer",
166: e);
167: return 0;
168: }
169: }
170:
171: /**
172: * Returns 0 if string is invalid or null.
173: *
174: * @see JMeterProperty#getFloatValue()
175: */
176: public float getFloatValue() {
177: String val = getStringValue();
178: if (val == null) {
179: return 0;
180: }
181: try {
182: return Float.parseFloat(val);
183: } catch (NumberFormatException e) {
184: log.error(
185: "Tried to parse a non-number string to an integer",
186: e);
187: return 0;
188: }
189: }
190:
191: /**
192: * Returns false if string is invalid or null.
193: *
194: * @see JMeterProperty#getBooleanValue()
195: */
196: public boolean getBooleanValue() {
197: String val = getStringValue();
198: if (val == null) {
199: return false;
200: }
201: return Boolean.valueOf(val).booleanValue();
202: }
203:
204: /**
205: * Determines if the two objects are equal by comparing names and values
206: *
207: * @return true if names are equal and values are equal (or both null)
208: */
209: public boolean equals(Object o) {
210: if (!(o instanceof JMeterProperty))
211: return false;
212: if (this == o)
213: return true;
214: JMeterProperty jpo = (JMeterProperty) o;
215: if (!name.equals(jpo.getName()))
216: return false;
217: String s1 = getStringValue();
218: String s2 = jpo.getStringValue();
219: return s1 == null ? s2 == null : s1.equals(s2);
220: }
221:
222: public int hashCode() {
223: int result = 17;
224: result = result * 37 + name.hashCode();// name cannot be null
225: String s = getStringValue();
226: result = result * 37 + (s == null ? 0 : s.hashCode());
227: return result;
228: }
229:
230: /**
231: * Compares two JMeterProperty object values. N.B. Does not compare names
232: *
233: * @param arg0
234: * JMeterProperty to compare against
235: * @return 0 if equal values or both values null; -1 otherwise
236: * @see Comparable#compareTo(Object)
237: */
238: public int compareTo(Object arg0) {
239: if (arg0 instanceof JMeterProperty) {
240: // We don't expect the string values to ever be null. But (as in
241: // bug 19499) sometimes they are. So have null compare less than
242: // any other value. Log a warning so we can try to find the root
243: // cause of the null value.
244: String val = getStringValue();
245: String val2 = ((JMeterProperty) arg0).getStringValue();
246: if (val == null) {
247: log
248: .warn("Warning: Unexpected null value for property: "
249: + name);
250:
251: if (val2 == null) {
252: // Two null values -- return equal
253: return 0;
254: } else {
255: return -1;
256: }
257: }
258: return val.compareTo(val2);
259: } else {
260: return -1;
261: }
262: }
263:
264: /**
265: * Get the property type for this property. Used to convert raw values into
266: * JMeterProperties.
267: */
268: protected Class getPropertyType() {
269: return getClass();
270: }
271:
272: protected JMeterProperty getBlankProperty() {
273: try {
274: JMeterProperty prop = (JMeterProperty) getPropertyType()
275: .newInstance();
276: if (prop instanceof NullProperty) {
277: return new StringProperty();
278: }
279: return prop;
280: } catch (Exception e) {
281: return new StringProperty();
282: }
283: }
284:
285: protected static JMeterProperty getBlankProperty(Object item) {
286: if (item == null) {
287: return new NullProperty();
288: }
289: if (item instanceof String) {
290: return new StringProperty("", item.toString());
291: } else if (item instanceof Boolean) {
292: return new BooleanProperty("", ((Boolean) item)
293: .booleanValue());
294: } else if (item instanceof Float) {
295: return new FloatProperty("", ((Float) item).floatValue());
296: } else if (item instanceof Double) {
297: return new DoubleProperty("", ((Double) item).doubleValue());
298: } else if (item instanceof Integer) {
299: return new IntegerProperty("", ((Integer) item).intValue());
300: } else if (item instanceof Long) {
301: return new LongProperty("", ((Long) item).longValue());
302: } else if (item instanceof Long) {
303: return new LongProperty("", ((Long) item).longValue());
304: } else {
305: return new StringProperty("", item.toString());
306: }
307: }
308:
309: protected Collection normalizeList(Collection coll) {
310: Iterator iter = coll.iterator();
311: Collection newColl = null;
312: while (iter.hasNext()) {
313: Object item = iter.next();
314: if (newColl == null) {
315: try {
316: newColl = (Collection) coll.getClass()
317: .newInstance();
318: } catch (Exception e) {
319: log.error("Bad collection", e);
320: }
321: }
322: newColl.add(convertObject(item));
323: }
324: if (newColl != null) {
325: return newColl;
326: } else {
327: return coll;
328: }
329: }
330:
331: /**
332: * Given a Map, it converts the Map into a collection of JMeterProperty
333: * objects, appropriate for a MapProperty object.
334: */
335: protected Map normalizeMap(Map coll) {
336: Iterator iter = coll.entrySet().iterator();
337: Map newColl = null;
338: while (iter.hasNext()) {
339: Map.Entry entry = (Map.Entry) iter.next();
340: Object item = entry.getKey();
341: Object prop = entry.getValue();
342: if (newColl == null) {
343: try {
344: newColl = (Map) coll.getClass().newInstance();
345: } catch (Exception e) {
346: log.error("Bad collection", e);
347: }
348: }
349: newColl.put(item, convertObject(prop));
350: }
351: if (newColl != null) {
352: return newColl;
353: } else {
354: return coll;
355: }
356: }
357:
358: public static JMeterProperty createProperty(Object item) {
359: JMeterProperty prop = makeProperty(item);
360: if (prop == null) {
361: prop = getBlankProperty(item);
362: }
363: return prop;
364: }
365:
366: /**
367: * Create a JMeterProperty from an object.
368: *
369: * @param item object to be turned into a propery
370: * @return the JMeterProperty
371: */
372: protected static JMeterProperty makeProperty(Object item) {
373: if (item instanceof JMeterProperty) {
374: return (JMeterProperty) item;
375: }
376: if (item instanceof TestElement) {
377: return new TestElementProperty(((TestElement) item)
378: .getPropertyAsString(TestElement.NAME),
379: (TestElement) item);
380: }
381: if (item instanceof Collection) {
382: return new CollectionProperty("" + item.hashCode(),
383: (Collection) item);
384: }
385: if (item instanceof Map) {
386: return new MapProperty("" + item.hashCode(), (Map) item);
387: }
388: return null;
389: }
390:
391: protected JMeterProperty convertObject(Object item) {
392: JMeterProperty prop = makeProperty(item);
393: if (prop == null) {
394: prop = getBlankProperty();
395: prop.setName("" + item.hashCode());
396: prop.setObjectValue(item);
397: }
398: return prop;
399: }
400:
401: /**
402: * Provides the string representation of the property.
403: *
404: * @return the string value
405: */
406: public String toString() {
407: // N.B. Other classes rely on this returning just the string.
408: return getStringValue();
409: }
410:
411: /*
412: * (non-Javadoc)
413: *
414: * @see org.apache.jmeter.testelement.property.JMeterProperty#mergeIn(org.apache.jmeter.testelement.property.JMeterProperty)
415: */
416: public void mergeIn(JMeterProperty prop) {
417: }
418: }
|