1 18 19 package org.apache.jmeter.testelement.property; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.List ; 24 25 import org.apache.jmeter.junit.JMeterTestCase; 26 import org.apache.jmeter.testelement.TestElement; 27 28 31 public class CollectionProperty extends MultiProperty 32 { 33 protected Collection value; 34 private Collection savedValue; 35 36 public CollectionProperty(String name, Collection value) 37 { 38 super(name); 39 this.value = normalizeList(value); 40 } 41 42 public CollectionProperty() 43 { 44 super(); 45 value = new ArrayList (); 46 } 47 48 public boolean equals(Object o) 49 { 50 if (o instanceof CollectionProperty) 51 { 52 if (value != null) 53 { 54 return value.equals(((JMeterProperty) o).getObjectValue()); 55 } 56 } 57 return false; 58 } 59 60 public int hashCode() 61 { 62 return (value == null ? 0 : value.hashCode()); 63 } 64 65 public void remove(String prop) 66 { 67 PropertyIterator iter = iterator(); 68 while (iter.hasNext()) 69 { 70 if (iter.next().getName().equals(prop)) 71 { 72 iter.remove(); 73 } 74 } 75 } 76 77 public void set(int index, String prop) 78 { 79 if (value instanceof List ) 80 { 81 ((List ) value).set(index, new StringProperty(prop, prop)); 82 } 83 } 84 85 public void set(int index, JMeterProperty prop) 86 { 87 if (value instanceof List ) 88 { 89 ((List ) value).set(index, prop); 90 } 91 } 92 93 public JMeterProperty get(int row) 94 { 95 if (value instanceof List ) 96 { 97 return (JMeterProperty) ((List ) value).get(row); 98 } 99 else 100 { 101 return null; 102 } 103 } 104 105 public void remove(int index) 106 { 107 if (value instanceof List ) 108 { 109 ((List ) value).remove(index); 110 } 111 } 112 113 public void setObjectValue(Object v) 114 { 115 if (v instanceof Collection ) 116 { 117 setCollection((Collection ) v); 118 } 119 120 } 121 122 public PropertyIterator iterator() 123 { 124 return getIterator(value); 125 } 126 127 130 public String getStringValue() 131 { 132 return value.toString(); 133 } 134 135 138 public Object getObjectValue() 139 { 140 return value; 141 } 142 143 public int size() 144 { 145 return value.size(); 146 } 147 148 151 public Object clone() 152 { 153 CollectionProperty prop = (CollectionProperty) super.clone(); 154 prop.value = cloneCollection(); 155 return prop; 156 } 157 158 private Collection cloneCollection() 159 { 160 try 161 { 162 Collection newCol = (Collection ) value.getClass().newInstance(); 163 PropertyIterator iter = iterator(); 164 while (iter.hasNext()) 165 { 166 newCol.add(iter.next().clone()); 167 } 168 return newCol; 169 } 170 catch (Exception e) 171 { 172 log.error("Couldn't clone collection", e); 173 return value; 174 } 175 } 176 177 public void setCollection(Collection coll) 178 { 179 value = normalizeList(coll); 180 } 181 182 public void clear() 183 { 184 value.clear(); 185 } 186 187 191 public void addProperty(JMeterProperty prop) 192 { 193 value.add(prop); 194 } 195 196 public void addItem(Object item) 197 { 198 addProperty(convertObject(item)); 199 } 200 201 206 protected Class getPropertyType() 207 { 208 if (value.size() > 0) 209 { 210 return value.iterator().next().getClass(); 211 } 212 else 213 { 214 return NullProperty.class; 215 } 216 } 217 218 221 public void recoverRunningVersion(TestElement owner) 222 { 223 if (savedValue != null) 224 { 225 value = savedValue; 226 } 227 recoverRunningVersionOfSubElements(owner); 228 } 229 230 public static class Test extends JMeterTestCase 231 { 232 public Test(String name) 233 { 234 super(name); 235 } 236 237 public void testAddingProperties() throws Exception 238 { 239 CollectionProperty coll = new CollectionProperty(); 240 coll.addItem("joe"); 241 coll.addProperty(new FunctionProperty()); 242 assertEquals("joe",coll.get(0).getName()); 243 assertEquals( 244 "org.apache.jmeter.testelement.property.FunctionProperty", 245 coll.get(1).getClass().getName()); 246 } 247 } 248 249 252 public void setRunningVersion(boolean running) 253 { 254 super.setRunningVersion(running); 255 if(running) 256 { 257 savedValue = value; 258 } 259 else 260 { 261 savedValue = null; 262 } 263 } 264 } 265 | Popular Tags |