Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.testelement.property » [javadoc | source]
    1   // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testelement/property/AbstractProperty.java,v 1.23 2005/07/12 20:50:25 mstover1 Exp $
    2   /*
    3    * Copyright 2001-2004 The Apache Software Foundation.
    4    * 
    5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
    6    * use this file except in compliance with the License. You may obtain a copy
    7    * of the License at
    8    * 
    9    * http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   13    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   14    * License for the specific language governing permissions and limitations
   15    * under the License.
   16    *  
   17    */
   18   
   19   // $Header:
   20   // /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testelement/property/AbstractProperty.java,v
   21   // 1.19 2004/02/27 11:45:54 sebb Exp $
   22   /*
   23    * Copyright 2003-2004 The Apache Software Foundation.
   24    * 
   25    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
   26    * use this file except in compliance with the License. You may obtain a copy
   27    * of the License at
   28    * 
   29    * http://www.apache.org/licenses/LICENSE-2.0
   30    * 
   31    * Unless required by applicable law or agreed to in writing, software
   32    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   33    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   34    * License for the specific language governing permissions and limitations
   35    * under the License.
   36    *  
   37    */
   38   
   39   package org.apache.jmeter.testelement.property;
   40   
   41   import java.util.Collection;
   42   import java.util.Iterator;
   43   import java.util.Map;
   44   
   45   import org.apache.jmeter.testelement.TestElement;
   46   import org.apache.jorphan.logging.LoggingManager;
   47   import org.apache.log.Logger;
   48   
   49   /**
   50    * @version $Revision: 1.23 $
   51    */
   52   public abstract class AbstractProperty implements JMeterProperty {
   53   	protected static final Logger log = LoggingManager.getLoggerForClass();
   54   
   55   	private String name;
   56   
   57   	transient private boolean runningVersion = false;
   58   
   59   	// private static StringProperty defaultProperty = new StringProperty();
   60   
   61   	public AbstractProperty(String name) {
   62   		if (name == null)
   63   			throw new IllegalArgumentException("Name cannot be null");
   64   		this.name = name;
   65   	}
   66   
   67   	public AbstractProperty() {
   68   		this("");
   69   	}
   70   
   71   	protected boolean isEqualType(JMeterProperty prop) {
   72   		if (this.getClass().equals(prop.getClass())) {
   73   			return true;
   74   		} else {
   75   			return false;
   76   		}
   77   	}
   78   
   79   	/*
   80   	 * (non-Javadoc)
   81   	 * 
   82   	 * @see JMeterProperty#isRunningVersion()
   83   	 */
   84   	public boolean isRunningVersion() {
   85   		return runningVersion;
   86   	}
   87   
   88   	/*
   89   	 * (non-Javadoc)
   90   	 * 
   91   	 * @see JMeterProperty#getName()
   92   	 */
   93   	public String getName() {
   94   		return name;
   95   	}
   96   
   97   	public void setName(String name) {
   98   		if (name == null)
   99   			throw new IllegalArgumentException("Name cannot be null");
  100   		this.name = name;
  101   	}
  102   
  103   	/*
  104   	 * (non-Javadoc)
  105   	 * 
  106   	 * @see JMeterProperty#setRunningVersion(boolean)
  107   	 */
  108   	public void setRunningVersion(boolean runningVersion) {
  109   		this.runningVersion = runningVersion;
  110   	}
  111   
  112   	protected PropertyIterator getIterator(Collection values) {
  113   		return new PropertyIteratorImpl(values);
  114   	}
  115   
  116   	/*
  117   	 * (non-Javadoc)
  118   	 * 
  119   	 * @see Object#clone()
  120   	 */
  121   	public Object clone() {
  122   		try {
  123   			AbstractProperty prop = (AbstractProperty) this.getClass().newInstance();
  124   			prop.name = name;
  125   			prop.runningVersion = runningVersion;
  126   			return prop;
  127   		} catch (InstantiationException e) {
  128   			return null;
  129   		} catch (IllegalAccessException e) {
  130   			return null;
  131   		}
  132   	}
  133   
  134   	/**
  135   	 * Returns 0 if string is invalid or null.
  136   	 * 
  137   	 * @see JMeterProperty#getIntValue()
  138   	 */
  139   	public int getIntValue() {
  140   		String val = getStringValue();
  141   		if (val == null) {
  142   			return 0;
  143   		}
  144   		try {
  145   			return Integer.parseInt(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#getLongValue()
  155   	 */
  156   	public long getLongValue() {
  157   		String val = getStringValue();
  158   		if (val == null) {
  159   			return 0;
  160   		}
  161   		try {
  162   			return Long.parseLong(val);
  163   		} catch (NumberFormatException e) {
  164   			return 0;
  165   		}
  166   	}
  167   
  168   	/**
  169   	 * Returns 0 if string is invalid or null.
  170   	 * 
  171   	 * @see JMeterProperty#getDoubleValue()
  172   	 */
  173   	public double getDoubleValue() {
  174   		String val = getStringValue();
  175   		if (val == null) {
  176   			return 0;
  177   		}
  178   		try {
  179   			return Double.parseDouble(val);
  180   		} catch (NumberFormatException e) {
  181   			log.error("Tried to parse a non-number string to an integer", e);
  182   			return 0;
  183   		}
  184   	}
  185   
  186   	/**
  187   	 * Returns 0 if string is invalid or null.
  188   	 * 
  189   	 * @see JMeterProperty#getFloatValue()
  190   	 */
  191   	public float getFloatValue() {
  192   		String val = getStringValue();
  193   		if (val == null) {
  194   			return 0;
  195   		}
  196   		try {
  197   			return Float.parseFloat(val);
  198   		} catch (NumberFormatException e) {
  199   			log.error("Tried to parse a non-number string to an integer", e);
  200   			return 0;
  201   		}
  202   	}
  203   
  204   	/**
  205   	 * Returns false if string is invalid or null.
  206   	 * 
  207   	 * @see JMeterProperty#getBooleanValue()
  208   	 */
  209   	public boolean getBooleanValue() {
  210   		String val = getStringValue();
  211   		if (val == null) {
  212   			return false;
  213   		}
  214   		return Boolean.valueOf(val).booleanValue();
  215   	}
  216   
  217   	/**
  218   	 * Determines if the two objects are equal by comparing names and values
  219   	 * 
  220   	 * @return true if names are equal and values are equal (or both null)
  221   	 */
  222   	public boolean equals(Object o) {
  223   		if (!(o instanceof JMeterProperty))
  224   			return false;
  225   		if (this == o)
  226   			return true;
  227   		JMeterProperty jpo = (JMeterProperty) o;
  228   		if (!name.equals(jpo.getName()))
  229   			return false;
  230   		String s1 = getStringValue();
  231   		String s2 = jpo.getStringValue();
  232   		return s1 == null ? s2 == null : s1.equals(s2);
  233   	}
  234   
  235   	public int hashCode() {
  236   		int result = 17;
  237   		result = result * 37 + name.hashCode();// name cannot be null
  238   		String s = getStringValue();
  239   		result = result * 37 + (s == null ? 0 : s.hashCode());
  240   		return result;
  241   	}
  242   
  243   	/**
  244   	 * Compares two JMeterProperty object values. N.B. Does not compare names
  245   	 * 
  246   	 * @param arg0
  247   	 *            JMeterProperty to compare against
  248   	 * @return 0 if equal values or both values null; -1 otherwise
  249   	 * @see Comparable#compareTo(Object)
  250   	 */
  251   	public int compareTo(Object arg0) {
  252   		if (arg0 instanceof JMeterProperty) {
  253   			// We don't expect the string values to ever be null. But (as in
  254   			// bug 19499) sometimes they are. So have null compare less than
  255   			// any other value. Log a warning so we can try to find the root
  256   			// cause of the null value.
  257   			String val = getStringValue();
  258   			String val2 = ((JMeterProperty) arg0).getStringValue();
  259   			if (val == null) {
  260   				log.warn("Warning: Unexpected null value for property: " + name);
  261   
  262   				if (val2 == null) {
  263   					// Two null values -- return equal
  264   					return 0;
  265   				} else {
  266   					return -1;
  267   				}
  268   			}
  269   			return val.compareTo(val2);
  270   		} else {
  271   			return -1;
  272   		}
  273   	}
  274   
  275   	/**
  276   	 * Get the property type for this property. Used to convert raw values into
  277   	 * JMeterProperties.
  278   	 */
  279   	protected Class getPropertyType() {
  280   		return getClass();
  281   	}
  282   
  283   	protected JMeterProperty getBlankProperty() {
  284   		try {
  285   			JMeterProperty prop = (JMeterProperty) getPropertyType().newInstance();
  286   			if (prop instanceof NullProperty) {
  287   				return new StringProperty();
  288   			}
  289   			return prop;
  290   		} catch (Exception e) {
  291   			return new StringProperty();
  292   		}
  293   	}
  294   
  295   	protected static JMeterProperty getBlankProperty(Object item) {
  296   		if (item == null) {
  297   			return new NullProperty();
  298   		}
  299   		if (item instanceof String) {
  300   			return new StringProperty("", item.toString());
  301   		} else if (item instanceof Boolean) {
  302   			return new BooleanProperty("", ((Boolean) item).booleanValue());
  303   		} else if (item instanceof Float) {
  304   			return new FloatProperty("", ((Float) item).floatValue());
  305   		} else if (item instanceof Double) {
  306   			return new DoubleProperty("", ((Double) item).doubleValue());
  307   		} else if (item instanceof Integer) {
  308   			return new IntegerProperty("", ((Integer) item).intValue());
  309   		} else if (item instanceof Long) {
  310   			return new LongProperty("", ((Long) item).longValue());
  311   		} else if (item instanceof Long) {
  312   			return new LongProperty("", ((Long) item).longValue());
  313   		} else {
  314   			return new StringProperty("", item.toString());
  315   		}
  316   	}
  317   
  318   	protected Collection normalizeList(Collection coll) {
  319   		Iterator iter = coll.iterator();
  320   		Collection newColl = null;
  321   		while (iter.hasNext()) {
  322   			Object item = iter.next();
  323   			if (newColl == null) {
  324   				try {
  325   					newColl = (Collection) coll.getClass().newInstance();
  326   				} catch (Exception e) {
  327   					log.error("Bad collection", e);
  328   				}
  329   			}
  330   			newColl.add(convertObject(item));
  331   		}
  332   		if (newColl != null) {
  333   			return newColl;
  334   		} else {
  335   			return coll;
  336   		}
  337   	}
  338   
  339   	/**
  340   	 * Given a Map, it converts the Map into a collection of JMeterProperty
  341   	 * objects, appropriate for a MapProperty object.
  342   	 */
  343   	protected Map normalizeMap(Map coll) {
  344   		Iterator iter = coll.keySet().iterator();
  345   		Map newColl = null;
  346   		while (iter.hasNext()) {
  347   			Object item = iter.next();
  348   			Object prop = coll.get(item);
  349   			if (newColl == null) {
  350   				try {
  351   					newColl = (Map) coll.getClass().newInstance();
  352   				} catch (Exception e) {
  353   					log.error("Bad collection", e);
  354   				}
  355   			}
  356   			newColl.put(item, convertObject(prop));
  357   		}
  358   		if (newColl != null) {
  359   			return newColl;
  360   		} else {
  361   			return coll;
  362   		}
  363   	}
  364   
  365   	public static JMeterProperty createProperty(Object item) {
  366   		JMeterProperty prop = makeProperty(item);
  367   		if (prop == null) {
  368   			prop = getBlankProperty(item);
  369   		}
  370   		return prop;
  371   	}
  372   
  373   	/**
  374   	 * @param item
  375   	 * @return
  376   	 */
  377   	protected static JMeterProperty makeProperty(Object item) {
  378   		if (item instanceof JMeterProperty) {
  379   			return (JMeterProperty) item;
  380   		}
  381   		if (item instanceof TestElement) {
  382   			return new TestElementProperty(((TestElement) item).getPropertyAsString(TestElement.NAME),
  383   					(TestElement) item);
  384   		}
  385   		if (item instanceof Collection) {
  386   			return new CollectionProperty("" + item.hashCode(), (Collection) item);
  387   		}
  388   		if (item instanceof Map) {
  389   			return new MapProperty("" + item.hashCode(), (Map) item);
  390   		}
  391   		return null;
  392   	}
  393   
  394   	protected JMeterProperty convertObject(Object item) {
  395   		JMeterProperty prop = makeProperty(item);
  396   		if (prop == null) {
  397   			prop = getBlankProperty();
  398   			prop.setName("" + item.hashCode());
  399   			prop.setObjectValue(item);
  400   		}
  401   		return prop;
  402   	}
  403   
  404   	/**
  405   	 * Provides the string representation of the property.
  406   	 * 
  407   	 * @return the string value
  408   	 */
  409   	public String toString() {
  410   		// N.B. Other classes rely on this returning just the string.
  411   		return getStringValue();
  412   	}
  413   
  414   	/*
  415   	 * (non-Javadoc)
  416   	 * 
  417   	 * @see org.apache.jmeter.testelement.property.JMeterProperty#mergeIn(org.apache.jmeter.testelement.property.JMeterProperty)
  418   	 */
  419   	public void mergeIn(JMeterProperty prop) {
  420   	}
  421   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.testelement.property » [javadoc | source]