001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.modifier;
020:
021: import java.io.Serializable;
022:
023: import org.apache.jmeter.config.Argument;
024: import org.apache.jmeter.engine.event.LoopIterationEvent;
025: import org.apache.jmeter.processor.PreProcessor;
026: import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
027: import org.apache.jmeter.samplers.Sampler;
028: import org.apache.jmeter.testelement.AbstractTestElement;
029: import org.apache.jmeter.testelement.TestListener;
030: import org.apache.jmeter.testelement.property.PropertyIterator;
031: import org.apache.jmeter.testelement.property.TestElementProperty;
032:
033: /**
034: * This modifier will replace any single http sampler's url parameter value with
035: * a value from a given range - thereby "masking" the value set in the http
036: * sampler. The parameter names must match exactly, and the parameter value must
037: * be preset to "*" to diferentiate between duplicate parameter names.
038: * <P>
039: * For example, if you set up the modifier with a lower bound of 1, an upper
040: * bound of 10, and an increment of 2, and run the loop 12 times, the parameter
041: * will have the following values (one per loop): 1, 3, 5, 7, 9, 1, 3, 5, 7, 9,
042: * 1, 3
043: * <P>
044: * The {@link ParamMask} object contains most of the logic for stepping through
045: * this loop. You can make large modifications to this modifier's behaviour by
046: * changing one or two method implementations there.
047: *
048: * @author David La France
049: * @see ParamMask
050: * @version $Revision: 493789 $ updated on $Date: 2007-01-07 18:10:21 +0000 (Sun, 07 Jan 2007) $
051: */
052: public class ParamModifier extends AbstractTestElement implements
053: TestListener, PreProcessor, Serializable {
054:
055: /*
056: * ------------------------------------------------------------------------
057: * Fields
058: * ------------------------------------------------------------------------
059: */
060:
061: /**
062: * The key used to find the ParamMask object in the HashMap.
063: */
064: private final static String MASK = "ParamModifier.mask";
065:
066: /*
067: * ------------------------------------------------------------------------
068: * Constructors
069: * ------------------------------------------------------------------------
070: */
071:
072: /**
073: * Default constructor.
074: */
075: public ParamModifier() {
076: setProperty(new TestElementProperty(MASK, new ParamMask()));
077: }
078:
079: public ParamMask getMask() {
080: return (ParamMask) getProperty(MASK).getObjectValue();
081: }
082:
083: public void testStarted() {
084: getMask().resetValue();
085: }
086:
087: public void testStarted(String host) {
088: getMask().resetValue();
089: }
090:
091: public void testEnded() {
092: }
093:
094: public void testEnded(String host) {
095: }
096:
097: /*
098: * ------------------------------------------------------------------------
099: * Methods implemented from interface org.apache.jmeter.config.Modifier
100: * ------------------------------------------------------------------------
101: */
102:
103: /**
104: * Modifies an entry object to replace the value of any url parameter that
105: * matches a defined mask.
106: *
107: */
108: public void process() {
109: Sampler sam = getThreadContext().getCurrentSampler();
110: HTTPSamplerBase sampler = null;
111: if (!(sam instanceof HTTPSamplerBase)) {
112: return;
113: } else {
114: sampler = (HTTPSamplerBase) sam;
115: }
116: boolean modified = false;
117: PropertyIterator iter = sampler.getArguments().iterator();
118: while (iter.hasNext()) {
119: Argument arg = (Argument) iter.next().getObjectValue();
120: modified = modifyArgument(arg);
121: if (modified) {
122: break;
123: }
124: }
125: }
126:
127: /*
128: * ------------------------------------------------------------------------
129: * Methods
130: * ------------------------------------------------------------------------
131: */
132:
133: /**
134: * Helper method for {@link #modifyEntry} Replaces a parameter's value if
135: * the parameter name matches the mask name and the value is a '*'.
136: *
137: * @param arg
138: * an {@link Argument} representing a http parameter
139: * @return <code>true</code>if the value was replaced
140: */
141: private boolean modifyArgument(Argument arg) {
142: // if a mask for this argument exists
143: if (arg.getName().equals(getMask().getFieldName())) {
144: // values to be masked must be set in the WebApp to "*"
145: if ("*".equals(arg.getValue())) {
146: arg.setValue(getMask().getNextValue());
147: return true;
148: }
149: }
150: return false;
151: }
152:
153: /**
154: * @see TestListener#testIterationStart(LoopIterationEvent)
155: */
156: public void testIterationStart(LoopIterationEvent event) {
157: }
158: }
|