KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > engine > util > ValueReplacer


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/engine/util/ValueReplacer.java,v 1.14 2004/02/13 02:40:55 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.engine.util;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.apache.jmeter.config.ConfigTestElement;
32 import org.apache.jmeter.functions.InvalidVariableException;
33 import org.apache.jmeter.testelement.TestElement;
34 import org.apache.jmeter.testelement.TestPlan;
35 import org.apache.jmeter.testelement.property.CollectionProperty;
36 import org.apache.jmeter.testelement.property.JMeterProperty;
37 import org.apache.jmeter.testelement.property.MultiProperty;
38 import org.apache.jmeter.testelement.property.PropertyIterator;
39 import org.apache.jmeter.testelement.property.StringProperty;
40 import org.apache.jmeter.threads.JMeterContextService;
41 import org.apache.jmeter.threads.JMeterVariables;
42 import org.apache.jorphan.logging.LoggingManager;
43 import org.apache.log.Logger;
44
45 /**
46  * @author Michael Stover
47  * @author <a HREF="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
48  * @version $Revision: 1.14 $ updated on $Date: 2004/02/13 02:40:55 $
49  */

50 public class ValueReplacer
51 {
52     transient private static Logger log = LoggingManager.getLoggerForClass();
53     CompoundVariable masterFunction = new CompoundVariable();
54     Map JavaDoc variables = new HashMap JavaDoc();
55
56     public ValueReplacer()
57     {
58     }
59
60     public ValueReplacer(TestPlan tp)
61     {
62         setUserDefinedVariables(tp.getUserDefinedVariables());
63     }
64
65     public void setUserDefinedVariables(Map JavaDoc variables)
66     {
67         this.variables = variables;
68     }
69
70     public void replaceValues(TestElement el) throws InvalidVariableException
71     {
72         Collection JavaDoc newProps =
73             replaceValues(
74                 el.propertyIterator(),
75                 new ReplaceStringWithFunctions(masterFunction, variables));
76         setProperties(el, newProps);
77     }
78
79     private void setProperties(TestElement el, Collection JavaDoc newProps)
80     {
81         Iterator JavaDoc iter = newProps.iterator();
82         el.clear();
83         while(iter.hasNext())
84         {
85             el.setProperty((JMeterProperty)iter.next());
86         }
87     }
88     
89     public void reverseReplace(TestElement el) throws InvalidVariableException
90     {
91         Collection JavaDoc newProps =
92             replaceValues(
93                 el.propertyIterator(),
94                 new ReplaceFunctionsWithStrings(masterFunction, variables));
95         setProperties(el, newProps);
96     }
97         
98     public void undoReverseReplace(TestElement el)
99         throws InvalidVariableException
100     {
101         Collection JavaDoc newProps =
102             replaceValues(
103                 el.propertyIterator(),
104                 new UndoVariableReplacement(masterFunction, variables));
105         setProperties(el, newProps);
106     }
107
108     public void addVariable(String JavaDoc name, String JavaDoc value)
109     {
110         variables.put(name, value);
111     }
112
113     /**
114      * Add all the given variables to this replacer's variables map.
115      *
116      * @param vars A map of variable name-value pairs (String-to-String).
117      */

118     public void addVariables(Map JavaDoc vars)
119     {
120         variables.putAll(vars);
121     }
122
123     private Collection JavaDoc replaceValues(
124         PropertyIterator iter,
125         ValueTransformer transform)
126         throws InvalidVariableException
127     {
128         List JavaDoc props = new LinkedList JavaDoc();
129         while(iter.hasNext())
130         {
131             JMeterProperty val = iter.next();
132             if (log.isDebugEnabled())
133             {
134                 log.debug("About to replace in property of tipe: "
135                   +val.getClass()+": "+val);
136             }
137             if (val instanceof StringProperty)
138             {
139                 val = transform.transformValue((StringProperty) val);
140                 if (log.isDebugEnabled())
141                 {
142                     log.debug("Replacement result: " +val);
143                 }
144             }
145             else if (val instanceof MultiProperty)
146             {
147                 MultiProperty multiVal = (MultiProperty)val;
148                 Collection JavaDoc newValues =
149                     replaceValues(multiVal.iterator(), transform);
150                 multiVal.clear();
151                 Iterator JavaDoc propIter = newValues.iterator();
152                 while(propIter.hasNext())
153                 {
154                     multiVal.addProperty((JMeterProperty) propIter.next());
155                 }
156                 if (log.isDebugEnabled())
157                 {
158                     log.debug("Replacement result: " +multiVal);
159                 }
160             }
161             else {
162                 if (log.isDebugEnabled())
163                 {
164                     log.debug("Won't replace.");
165                 }
166             }
167             props.add(val);
168         }
169         return props;
170     }
171
172     
173
174     public static class Test extends TestCase
175     {
176         TestPlan variables;
177
178         public Test(String JavaDoc name)
179         {
180             super(name);
181         }
182
183         public void setUp()
184         {
185             variables = new TestPlan();
186             variables.addParameter("server", "jakarta.apache.org");
187             variables.addParameter("username", "jack");
188             variables.addParameter("password", "jacks_password");
189             variables.addParameter("regex", ".*");
190             JMeterVariables vars = new JMeterVariables();
191             vars.put("server", "jakarta.apache.org");
192             JMeterContextService.getContext().setVariables(vars);
193             JMeterContextService.getContext().setSamplingStarted(true);
194         }
195         
196         
197
198         public void testReverseReplacement() throws Exception JavaDoc
199         {
200             ValueReplacer replacer = new ValueReplacer(variables);
201             assertTrue(
202                 variables.getUserDefinedVariables().containsKey("server"));
203             assertTrue(replacer.variables.containsKey("server"));
204             TestElement element = new TestPlan();
205             element.setProperty(
206                 new StringProperty("domain", "jakarta.apache.org"));
207             List JavaDoc args = new ArrayList JavaDoc();
208             args.add("username is jack");
209             args.add("jacks_password");
210             element.setProperty(new CollectionProperty("args", args));
211             replacer.reverseReplace(element);
212             assertEquals("${server}", element.getPropertyAsString("domain"));
213             args = (List JavaDoc) element.getProperty("args").getObjectValue();
214             assertEquals(
215                 "${password}",
216                 ((JMeterProperty) args.get(1)).getStringValue());
217         }
218
219         public void testReplace() throws Exception JavaDoc
220         {
221             ValueReplacer replacer = new ValueReplacer();
222             replacer.setUserDefinedVariables(
223                 variables.getUserDefinedVariables());
224             TestElement element = new ConfigTestElement();
225             element.setProperty(new StringProperty("domain", "${server}"));
226             replacer.replaceValues(element);
227             log.debug("domain property = " + element.getProperty("domain"));
228             element.setRunningVersion(true);
229             assertEquals(
230                 "jakarta.apache.org",
231                 element.getPropertyAsString("domain"));
232         }
233
234         /* (non-Javadoc)
235          * @see junit.framework.TestCase#tearDown()
236          */

237         protected void tearDown() throws Exception JavaDoc
238         {
239             JMeterContextService.getContext().setSamplingStarted(false);
240         }
241     }
242 }
243
Popular Tags