KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > testelement > property > FunctionProperty


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testelement/property/FunctionProperty.java,v 1.14 2004/02/22 19:13:15 sebb Exp $
2
/*
3  * Copyright 2003-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.property;
20
21 import org.apache.jmeter.engine.util.CompoundVariable;
22 import org.apache.jmeter.testelement.TestElement;
23 import org.apache.jmeter.threads.JMeterContext;
24 import org.apache.jmeter.threads.JMeterContextService;
25
26 /**
27  * @version $Revision: 1.14 $
28  */

29 public class FunctionProperty extends AbstractProperty
30 {
31     CompoundVariable function;
32     int testIteration = -1;
33     String JavaDoc cacheValue;
34
35     public FunctionProperty(String JavaDoc name, CompoundVariable func)
36     {
37         super(name);
38         function = func;
39     }
40
41     public FunctionProperty()
42     {
43         super();
44     }
45
46     public void setObjectValue(Object JavaDoc v)
47     {
48         if (v instanceof CompoundVariable && !isRunningVersion())
49         {
50             function = (CompoundVariable) v;
51         }
52         else
53         {
54             cacheValue = v.toString();
55         }
56     }
57
58     public boolean equals(Object JavaDoc o)
59     {
60         if (o instanceof FunctionProperty)
61         {
62             if (function != null)
63             {
64                 return function.equals(((JMeterProperty) o).getObjectValue());
65             }
66         }
67         return false;
68     }
69
70     /**
71      * Executes the function (and caches the value for the duration of the test
72      * iteration) if the property is a running version. Otherwise, the raw
73      * string representation of the function is provided.
74      * @see JMeterProperty#getStringValue()
75      */

76     public String JavaDoc getStringValue()
77     {
78         log.debug("Calling getStringValue from FunctionProperty");
79         log.debug("boogedy boogedy");
80         JMeterContext ctx = JMeterContextService.getContext();//Expensive, so do once
81
if (!isRunningVersion()
82             || !ctx.isSamplingStarted())
83         {
84             log.debug("Not running version, return raw function string");
85             return function.getRawParameters();
86         }
87         else
88         {
89             log.debug("Running version, executing function");
90             int iter =
91                 ctx.getVariables().getIteration();
92             if (iter < testIteration)
93             {
94                 testIteration = -1;
95             }
96             if (iter > testIteration || cacheValue == null)
97             {
98                 testIteration = iter;
99                 cacheValue = function.execute();
100             }
101             return cacheValue;
102         }
103     }
104
105     /**
106      * @see JMeterProperty#getObjectValue()
107      */

108     public Object JavaDoc getObjectValue()
109     {
110         return function;
111     }
112
113     public Object JavaDoc clone()
114     {
115         FunctionProperty prop = (FunctionProperty) super.clone();
116         prop.cacheValue = cacheValue;
117         prop.testIteration = testIteration;
118         prop.function = function;
119         return prop;
120     }
121
122     /**
123      * @see JMeterProperty#recoverRunningVersion(TestElement)
124      */

125     public void recoverRunningVersion(TestElement owner)
126     {
127         cacheValue = null;
128     }
129 }
130
Popular Tags