KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > modifier > ParamModifier


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/modifier/ParamModifier.java,v 1.13.2.1 2004/07/03 02:07:54 sebb Exp $
2
/*
3  * Copyright 2002-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.protocol.http.modifier;
20
21 import java.io.Serializable JavaDoc;
22
23 import org.apache.jmeter.config.Argument;
24 import org.apache.jmeter.engine.event.LoopIterationEvent;
25 import org.apache.jmeter.processor.PreProcessor;
26 import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
27 import org.apache.jmeter.samplers.Sampler;
28 import org.apache.jmeter.testelement.AbstractTestElement;
29 import org.apache.jmeter.testelement.TestListener;
30 import org.apache.jmeter.testelement.property.PropertyIterator;
31 import org.apache.jmeter.testelement.property.TestElementProperty;
32
33 /**
34  * This modifier will replace any single http sampler's url parameter value
35  * with a value from a given range - thereby "masking" the value set in the
36  * http sampler. The parameter names must match exactly, and the parameter
37  * value must be preset to "*" to diferentiate between duplicate parameter
38  * names.
39  * <P>
40  * For example, if you set up the modifier with a lower bound of 1, an upper
41  * bound of 10, and an increment of 2, and run the loop 12 times, the parameter
42  * will have the following values (one per loop): 1, 3, 5, 7, 9, 1, 3, 5, 7, 9,
43  * 1, 3
44  * <P>
45  * The {@link ParamMask} object contains most of the logic for stepping through
46  * this loop. You can make large modifications to this modifier's behaviour by
47  * changing one or two method implementations there.
48  *
49  * @author David La France
50  * @see ParamMask
51  * @version $Revision: 1.13.2.1 $ updated on $Date: 2004/07/03 02:07:54 $
52  */

53 public class ParamModifier
54     extends AbstractTestElement
55     implements TestListener, PreProcessor, Serializable JavaDoc
56 {
57
58     /*
59      * ------------------------------------------------------------------------
60      * Fields
61      * ------------------------------------------------------------------------
62      */

63
64     /**
65      * The key used to find the ParamMask object in the HashMap.
66      */

67     private final static String JavaDoc MASK = "ParamModifier.mask";
68
69     /*
70      * ------------------------------------------------------------------------
71      * Constructors
72      * ------------------------------------------------------------------------
73      */

74
75     /**
76      * Default constructor.
77      */

78     public ParamModifier()
79     {
80         setProperty(new TestElementProperty(MASK, new ParamMask()));
81     }
82
83     public ParamMask getMask()
84     {
85         return (ParamMask) getProperty(MASK).getObjectValue();
86     }
87
88     public void testStarted()
89     {
90         getMask().resetValue();
91     }
92
93     public void testStarted(String JavaDoc host)
94     {
95         getMask().resetValue();
96     }
97
98     public void testEnded()
99     {
100     }
101
102     public void testEnded(String JavaDoc host)
103     {
104     }
105
106     /*
107      * ------------------------------------------------------------------------
108      * Methods implemented from interface org.apache.jmeter.config.Modifier
109      * ------------------------------------------------------------------------
110      */

111
112     /**
113      * Modifies an entry object to replace the value of any url parameter that
114      * matches a defined mask.
115      *
116      */

117     public void process()
118     {
119         Sampler sam = getThreadContext().getCurrentSampler();
120         HTTPSamplerBase sampler = null;
121         if (!(sam instanceof HTTPSamplerBase))
122         {
123             return;
124         }
125         else
126         {
127             sampler = (HTTPSamplerBase) sam;
128         }
129         boolean modified = false;
130         PropertyIterator iter = sampler.getArguments().iterator();
131         while (iter.hasNext())
132         {
133             Argument arg = (Argument) iter.next().getObjectValue();
134             modified = modifyArgument(arg);
135             if (modified)
136             {
137                 break;
138             }
139         }
140     }
141
142     /*
143      * ------------------------------------------------------------------------
144      * Methods
145      * ------------------------------------------------------------------------
146      */

147
148     /**
149      * Helper method for {@link #modifyEntry} Replaces a parameter's value if
150      * the parameter name matches the mask name and the value is a '*'.
151      *
152      * @param arg an {@link Argument} representing a http parameter
153      * @return <code>true</code>if the value was replaced
154      */

155     private boolean modifyArgument(Argument arg)
156     {
157         // if a mask for this argument exists
158
if (arg.getName().equals(getMask().getFieldName()))
159         {
160             // values to be masked must be set in the WebApp to "*"
161
if ("*".equals(arg.getValue()))
162             {
163                 arg.setValue(getMask().getNextValue());
164                 return true;
165             }
166         }
167         return false;
168     }
169
170     /**
171      * @see TestListener#testIterationStart(LoopIterationEvent)
172      */

173     public void testIterationStart(LoopIterationEvent event)
174     {
175     }
176 }
177
Popular Tags