KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > testelement > AbstractTestElement


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testelement/AbstractTestElement.java,v 1.36.2.3 2004/10/13 00:30:31 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy 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,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19  package org.apache.jmeter.testelement;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.apache.jmeter.control.NextIsNullException;
30 import org.apache.jmeter.samplers.Sampler;
31 import org.apache.jmeter.testelement.property.CollectionProperty;
32 import org.apache.jmeter.testelement.property.JMeterProperty;
33 import org.apache.jmeter.testelement.property.MapProperty;
34 import org.apache.jmeter.testelement.property.MultiProperty;
35 import org.apache.jmeter.testelement.property.NullProperty;
36 import org.apache.jmeter.testelement.property.PropertyIterator;
37 import org.apache.jmeter.testelement.property.PropertyIteratorImpl;
38 import org.apache.jmeter.testelement.property.StringProperty;
39 import org.apache.jmeter.testelement.property.TestElementProperty;
40 import org.apache.jmeter.threads.JMeterContext;
41 import org.apache.jmeter.threads.JMeterContextService;
42 import org.apache.jorphan.logging.LoggingManager;
43 import org.apache.log.Logger;
44
45 /**
46  * @version $Revision: 1.36.2.3 $ on $Date: 2004/10/13 00:30:31 $
47  */

48 public abstract class AbstractTestElement implements TestElement, Serializable JavaDoc
49 {
50     private static final Logger log = LoggingManager.getLoggerForClass();
51
52     private Map JavaDoc propMap = Collections.synchronizedMap(new HashMap JavaDoc());
53     private Set JavaDoc temporaryProperties;
54
55     private boolean runningVersion = false;
56
57     // Thread-specific variables saved here to save recalculation
58
private transient JMeterContext threadContext = null;
59     private transient String JavaDoc threadName = null;
60     
61     public Object JavaDoc clone()
62     {
63         TestElement clonedElement = null;
64         try
65         {
66             clonedElement = (TestElement) this.getClass().newInstance();
67
68             PropertyIterator iter = propertyIterator();
69             while (iter.hasNext())
70             {
71                 clonedElement.setProperty((JMeterProperty) iter.next().clone());
72             }
73             clonedElement.setRunningVersion(runningVersion);
74         }
75         catch (Exception JavaDoc e)
76         {}
77         return clonedElement;
78     }
79
80     public void clear()
81     {
82         propMap.clear();
83     }
84
85     public void removeProperty(String JavaDoc key)
86     {
87         propMap.remove(key);
88     }
89
90     public boolean equals(Object JavaDoc o)
91     {
92         if (o instanceof AbstractTestElement)
93         {
94             return ((AbstractTestElement) o).propMap.equals(propMap);
95         }
96         else
97         {
98             return false;
99         }
100     }
101
102     /*
103      * URGENT:
104      * TODO - sort out equals and hashCode() - at present equal instances can/will have
105      * different hashcodes - problem is, when a proper hashcode is used, tests stop working,
106      * e.g. listener data disappears when switching views...
107      * This presumably means that instances currently regarded as equal, aren't really equal...
108      *
109      * (non-Javadoc)
110      * @see java.lang.Object#hashCode()
111      */

112 // This would be sensible, but does not work:
113
// public int hashCode()
114
// {
115
// return propMap.hashCode();
116
// }
117

118     public void addTestElement(TestElement el)
119     {
120         mergeIn(el);
121     }
122
123     public void setName(String JavaDoc name)
124     {
125         setProperty(new StringProperty(TestElement.NAME, name));
126     }
127
128     public String JavaDoc getName()
129     {
130         return getProperty(TestElement.NAME).getStringValue();
131     }
132
133     /**
134      * Get the named property.
135      * If it doesn't exist, a new NullProperty object is created
136      * with the same name and returned.
137      */

138     public JMeterProperty getProperty(String JavaDoc key)
139     {
140         JMeterProperty prop = (JMeterProperty) propMap.get(key);
141         if (prop == null)
142         {
143 // TODO URGENT - does it make sense to create "different" NullProperty items for each key?
144
// Or would it be better to create them all with a key of "" ?
145
prop = new NullProperty(key);
146         }
147         return prop;
148     }
149
150     public void traverse(TestElementTraverser traverser)
151     {
152         PropertyIterator iter = propertyIterator();
153         traverser.startTestElement(this);
154         while (iter.hasNext())
155         {
156             traverseProperty(traverser, iter.next());
157         }
158         traverser.endTestElement(this);
159     }
160
161     protected void traverseProperty(
162         TestElementTraverser traverser,
163         JMeterProperty value)
164     {
165         traverser.startProperty(value);
166         if (value instanceof TestElementProperty)
167         {
168             ((TestElement) value.getObjectValue()).traverse(traverser);
169         }
170         else if (value instanceof CollectionProperty)
171         {
172             traverseCollection((CollectionProperty) value, traverser);
173         }
174         else if (value instanceof MapProperty)
175         {
176             traverseMap((MapProperty) value, traverser);
177         }
178         traverser.endProperty(value);
179     }
180
181     protected void traverseMap(MapProperty map, TestElementTraverser traverser)
182     {
183         PropertyIterator iter = map.valueIterator();
184         while (iter.hasNext())
185         {
186             traverseProperty(traverser, iter.next());
187         }
188     }
189
190     protected void traverseCollection(
191         CollectionProperty col,
192         TestElementTraverser traverser)
193     {
194         PropertyIterator iter = col.iterator();
195         while (iter.hasNext())
196         {
197             traverseProperty(traverser, iter.next());
198         }
199     }
200
201     public int getPropertyAsInt(String JavaDoc key)
202     {
203         return getProperty(key).getIntValue();
204     }
205
206     public boolean getPropertyAsBoolean(String JavaDoc key)
207     {
208         return getProperty(key).getBooleanValue();
209     }
210
211     public boolean getPropertyAsBoolean(String JavaDoc key,boolean defaultVal)
212     {
213         JMeterProperty jmp = getProperty(key);
214         return jmp instanceof NullProperty ? defaultVal: jmp.getBooleanValue();
215     }
216
217     public float getPropertyAsFloat(String JavaDoc key)
218     {
219         return getProperty(key).getFloatValue();
220     }
221
222     public long getPropertyAsLong(String JavaDoc key)
223     {
224         return getProperty(key).getLongValue();
225     }
226
227     public double getPropertyAsDouble(String JavaDoc key)
228     {
229         return getProperty(key).getDoubleValue();
230     }
231
232     public String JavaDoc getPropertyAsString(String JavaDoc key)
233     {
234         return getProperty(key).getStringValue();
235     }
236
237     protected void addProperty(JMeterProperty property)
238     {
239         if (isRunningVersion())
240         {
241             setTemporary(property);
242         }
243         else
244         {
245             clearTemporary(property);
246         }
247         JMeterProperty prop = getProperty(property.getName());
248
249         if (prop instanceof NullProperty
250             || (prop instanceof StringProperty
251                 && prop.getStringValue().equals("")))
252         {
253             propMap.put(property.getName(), property);
254         }
255         else
256         {
257             prop.mergeIn(property);
258         }
259     }
260     
261     protected void clearTemporary(JMeterProperty property)
262     {
263         if(temporaryProperties != null)
264         {
265             temporaryProperties.remove(property);
266         }
267     }
268
269     /**
270      * Log the properties of the test element
271      * @see TestElement#setProperty(JMeterProperty)
272      */

273     protected void logProperties()
274     {
275         if (log.isDebugEnabled())
276         {
277             PropertyIterator iter = propertyIterator();
278             while (iter.hasNext())
279             {
280                 JMeterProperty prop = iter.next();
281                 log.debug(
282                     "Property "
283                         + prop.getName()
284                         + " is temp? "
285                         + isTemporary(prop)
286                         + " and is a "
287                         + prop.getObjectValue());
288             }
289         }
290     }
291
292     public void setProperty(JMeterProperty property)
293     {
294         if (isRunningVersion())
295         {
296             if (getProperty(property.getName()) instanceof NullProperty)
297             {
298                 addProperty(property);
299             }
300             else
301             {
302                 getProperty(property.getName()).setObjectValue(
303                     property.getObjectValue());
304             }
305         }
306         else
307         {
308             propMap.put(property.getName(), property);
309         }
310     }
311
312     public void setProperty(String JavaDoc name, String JavaDoc value)
313     {
314         setProperty(new StringProperty(name, value));
315     }
316
317     public PropertyIterator propertyIterator()
318     {
319         return new PropertyIteratorImpl(propMap.values());
320     }
321
322     protected void mergeIn(TestElement element)
323     {
324         PropertyIterator iter = element.propertyIterator();
325         while (iter.hasNext())
326         {
327             JMeterProperty prop = iter.next();
328             addProperty(prop);
329         }
330     }
331
332     /**
333      * Returns the runningVersion.
334      */

335     public boolean isRunningVersion()
336     {
337         return runningVersion;
338     }
339
340     /**
341      * Sets the runningVersion.
342      * @param runningVersion the runningVersion to set
343      */

344     public void setRunningVersion(boolean runningVersion)
345     {
346         this.runningVersion = runningVersion;
347         PropertyIterator iter = propertyIterator();
348         while (iter.hasNext())
349         {
350             iter.next().setRunningVersion(runningVersion);
351         }
352     }
353
354     public void recoverRunningVersion()
355     {
356         Iterator JavaDoc iter = propMap.entrySet().iterator();
357         while (iter.hasNext())
358         {
359             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
360             JMeterProperty prop = (JMeterProperty)entry.getValue();
361             if (isTemporary(prop))
362             {
363                 iter.remove();
364                 clearTemporary(prop);
365             }
366             else
367             {
368                 prop.recoverRunningVersion(this);
369             }
370         }
371         emptyTemporary();
372     }
373     
374     protected void emptyTemporary()
375     {
376         if(temporaryProperties != null)
377         {
378             temporaryProperties.clear();
379         }
380     }
381
382     protected Sampler nextIsNull() throws NextIsNullException
383     {
384         return null;
385     }
386     /* (non-Javadoc)
387      * @see org.apache.jmeter.testelement.TestElement#isTemporary(org.apache.jmeter.testelement.property.JMeterProperty)
388      */

389     public boolean isTemporary(JMeterProperty property)
390     {
391         if(temporaryProperties == null)
392         {
393             return false;
394         }
395         else
396         {
397             return temporaryProperties.contains(property);
398         }
399     }
400
401     /* (non-Javadoc)
402      * @see org.apache.jmeter.testelement.TestElement#setTemporary(org.apache.jmeter.testelement.property.JMeterProperty)
403      */

404     public void setTemporary(JMeterProperty property)
405     {
406         if(temporaryProperties == null)
407         {
408             temporaryProperties = new HashSet JavaDoc();
409         }
410         temporaryProperties.add(property);
411         if(property instanceof MultiProperty)
412         {
413             PropertyIterator iter = ((MultiProperty)property).iterator();
414             while(iter.hasNext())
415             {
416                 setTemporary(iter.next());
417             }
418         }
419     }
420
421     /**
422      * @return Returns the threadContext.
423      */

424     public JMeterContext getThreadContext() {
425         if (threadContext == null)
426         {
427         /*
428          * Only samplers have the thread context set up by JMeterThread at present,
429          * so suppress the warning for now
430          */

431 // log.warn("ThreadContext was not set up - should only happen in JUnit testing..."
432
// ,new Throwable("Debug"));
433
threadContext = JMeterContextService.getContext();
434         }
435         return threadContext;
436     }
437     /**
438      * @param inthreadContext The threadContext to set.
439      */

440     public void setThreadContext(JMeterContext inthreadContext) {
441         if (threadContext != null)
442         {
443             if (inthreadContext != threadContext)
444             throw new RuntimeException JavaDoc("Attempting to reset the thread context");
445         }
446         this.threadContext = inthreadContext;
447     }
448     /**
449      * @return Returns the threadName.
450      */

451     public String JavaDoc getThreadName() {
452         return threadName;
453     }
454     /**
455      * @param inthreadName The threadName to set.
456      */

457     public void setThreadName(String JavaDoc inthreadName) {
458         if (threadName != null)
459         {
460             if (!threadName.equals(inthreadName))
461             throw new RuntimeException JavaDoc("Attempting to reset the thread name");
462         }
463         this.threadName = inthreadName;
464     }
465
466     /* (non-Javadoc)
467      * @see org.apache.jmeter.testelement.TestElement#threadFinished()
468      */

469     public void threadFinished() {
470     }
471
472     /* (non-Javadoc)
473      * @see org.apache.jmeter.testelement.TestElement#threadStarted()
474      */

475     public void threadStarted() {
476     }
477     
478     // Default implementation
479
public boolean canRemove(){
480         return true;
481     }
482 }
483
Popular Tags