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.engine.util;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.jmeter.config.ConfigTestElement;
025: import org.apache.jmeter.junit.JMeterTestCase;
026: import org.apache.jmeter.testelement.TestElement;
027: import org.apache.jmeter.testelement.TestPlan;
028: import org.apache.jmeter.testelement.property.CollectionProperty;
029: import org.apache.jmeter.testelement.property.JMeterProperty;
030: import org.apache.jmeter.testelement.property.StringProperty;
031: import org.apache.jmeter.threads.JMeterContextService;
032: import org.apache.jmeter.threads.JMeterVariables;
033:
034: /**
035: * @author Michael Stover
036: * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
037: * @version $Revision: 325648 $ updated on $Date: 2005-08-18 21:38:49 +0100 (Thu, 18 Aug 2005) $
038: */
039: public class TestValueReplacer extends JMeterTestCase {
040: TestPlan variables;
041:
042: public TestValueReplacer(String name) {
043: super (name);
044: }
045:
046: public void setUp() {
047: variables = new TestPlan();
048: variables.addParameter("server", "jakarta.apache.org");
049: variables.addParameter("username", "jack");
050: // The following used to be jacks_password, but the Arguments class uses
051: // HashMap for which the order is not defined.
052: variables.addParameter("password", "his_password");
053: variables.addParameter("regex", ".*");
054: JMeterVariables vars = new JMeterVariables();
055: vars.put("server", "jakarta.apache.org");
056: JMeterContextService.getContext().setVariables(vars);
057: JMeterContextService.getContext().setSamplingStarted(true);
058: }
059:
060: public void testReverseReplacement() throws Exception {
061: ValueReplacer replacer = new ValueReplacer(variables);
062: assertTrue(variables.getUserDefinedVariables().containsKey(
063: "server"));
064: assertTrue(replacer.containsKey("server"));
065: TestElement element = new TestPlan();
066: element.setProperty(new StringProperty("domain",
067: "jakarta.apache.org"));
068: List args = new ArrayList();
069: args.add("username is jack");
070: args.add("his_password");
071: element.setProperty(new CollectionProperty("args", args));
072: replacer.reverseReplace(element);
073: assertEquals("${server}", element.getPropertyAsString("domain"));
074: args = (List) element.getProperty("args").getObjectValue();
075: assertEquals("username is ${username}", ((JMeterProperty) args
076: .get(0)).getStringValue());
077: assertEquals("${password}", ((JMeterProperty) args.get(1))
078: .getStringValue());
079: }
080:
081: public void testReplace() throws Exception {
082: ValueReplacer replacer = new ValueReplacer();
083: replacer.setUserDefinedVariables(variables
084: .getUserDefinedVariables());
085: TestElement element = new ConfigTestElement();
086: element.setProperty(new StringProperty("domain", "${server}"));
087: replacer.replaceValues(element);
088: //log.debug("domain property = " + element.getProperty("domain"));
089: element.setRunningVersion(true);
090: assertEquals("jakarta.apache.org", element
091: .getPropertyAsString("domain"));
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see junit.framework.TestCase#tearDown()
098: */
099: protected void tearDown() throws Exception {
100: JMeterContextService.getContext().setSamplingStarted(false);
101: }
102: }
|