--- src/functions/org/apache/jmeter/functions/IsPropDefined.java (nonexistent) +++ src/functions/org/apache/jmeter/functions/IsPropDefined.java (working copy) @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.jmeter.functions; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import org.apache.jmeter.engine.util.CompoundVariable; +import org.apache.jmeter.samplers.SampleResult; +import org.apache.jmeter.samplers.Sampler; +import org.apache.jmeter.util.JMeterUtils; + +public class IsPropDefined extends AbstractFunction { + private static final List desc = new LinkedList<>(); + private static final String KEY = "__isPropDefined"; + + // Number of parameters expected - used to reject invalid calls + private static final int MIN_PARAMETER_COUNT = 1; + private static final int MAX_PARAMETER_COUNT = 1; + + static { + desc.add(JMeterUtils.getResString("property_name_param")); + } + + private CompoundVariable[] values; + + @Override + public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { + String propertyName = values[0].execute(); + String propertyValue = JMeterUtils.getProperty(propertyName); + return isTrueString(propertyValue != null); + } + + @Override + public void setParameters(Collection parameters) throws InvalidVariableException { + checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT); + values = parameters.toArray(new CompoundVariable[parameters.size()]); + } + + @Override + public String getReferenceKey() { + return KEY; + } + + @Override + public List getArgumentDesc() { + return desc; + } + +} --- src/functions/org/apache/jmeter/functions/IsVarDefined.java (nonexistent) +++ src/functions/org/apache/jmeter/functions/IsVarDefined.java (working copy) @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.jmeter.functions; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import org.apache.jmeter.engine.util.CompoundVariable; +import org.apache.jmeter.samplers.SampleResult; +import org.apache.jmeter.samplers.Sampler; +import org.apache.jmeter.util.JMeterUtils; + +public class IsVarDefined extends AbstractFunction { + + private static final List desc = new LinkedList<>(); + private static final String KEY = "__isVarDefined"; + // Number of parameters expected - used to reject invalid calls + private static final int MIN_PARAMETER_COUNT = 1; + private static final int MAX_PARAMETER_COUNT = 1; + + static { + desc.add(JMeterUtils.getResString("evalvar_name_param")); + } + + private CompoundVariable[] values; + + @Override + public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { + String variableName = values[0].execute(); + String variableValue = getVariables().get(variableName); + return isTrueString(variableValue != null); + } + + @Override + public void setParameters(Collection parameters) throws InvalidVariableException { + checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT); + values = parameters.toArray(new CompoundVariable[parameters.size()]); + } + + @Override + public String getReferenceKey() { + return KEY; + } + + @Override + public List getArgumentDesc() { + return desc; + } + +} --- test/src/org/apache/jmeter/functions/TestIsPropDefined.java (nonexistent) +++ test/src/org/apache/jmeter/functions/TestIsPropDefined.java (working copy) @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.jmeter.functions; + +import static org.junit.Assert.assertEquals; + +import java.util.Collection; +import java.util.LinkedList; + +import org.apache.jmeter.engine.util.CompoundVariable; +import org.apache.jmeter.junit.JMeterTestCase; +import org.apache.jmeter.samplers.SampleResult; +import org.apache.jmeter.threads.JMeterContext; +import org.apache.jmeter.threads.JMeterContextService; +import org.apache.jmeter.threads.JMeterVariables; +import org.junit.Before; +import org.junit.Test; +/** + * + * Test IsPropDefined Function + * + * @see IsPropDefined + * @author orim + * + */ + +public class TestIsPropDefined extends JMeterTestCase { + protected AbstractFunction isPropDefined; + + private SampleResult result; + + private Collection params; + + private JMeterVariables vars; + + private JMeterContext jmctx; + + @Before + public void setUp() { + isPropDefined = new IsPropDefined(); + result = new SampleResult(); + jmctx = JMeterContextService.getContext(); + String data = "dummy data"; + result.setResponseData(data, null); + vars = new JMeterVariables(); + jmctx.setVariables(vars); + jmctx.setPreviousResult(result); + params = new LinkedList<>(); + } + + @Test + public void testParameterCountIsPropDefined() throws Exception { + checkInvalidParameterCounts(isPropDefined, 1, 1); + } + + @Test + public void testIsPropDefined() throws Exception { + params.add(new CompoundVariable("file.encoding")); + isPropDefined.setParameters(params); + String returnValue = isPropDefined.execute(result, null); + assertEquals("true", returnValue); + } + + @Test + public void testIsPropNotDefined() throws Exception { + params.add(new CompoundVariable("emptyProperty")); + isPropDefined.setParameters(params); + String returnValue = isPropDefined.execute(result, null); + assertEquals("false", returnValue); + } + + @Test + public void testIsPropNotDefinedOnlyVarDefined() throws Exception { + vars.put("emptyProperty", "emptyPropertyValue"); + params.add(new CompoundVariable("emptyProperty")); + isPropDefined.setParameters(params); + String returnValue = isPropDefined.execute(result, null); + assertEquals("false", returnValue); + } + + @Test(expected = InvalidVariableException.class) + public void testIsPropDefinedError() throws Exception { + isPropDefined.setParameters(params); + isPropDefined.execute(result, null); + } + +} --- test/src/org/apache/jmeter/functions/TestIsVarDefined.java (nonexistent) +++ test/src/org/apache/jmeter/functions/TestIsVarDefined.java (working copy) @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.jmeter.functions; + +import static org.junit.Assert.assertEquals; + +import java.util.Collection; +import java.util.LinkedList; + +import org.apache.jmeter.engine.util.CompoundVariable; +import org.apache.jmeter.junit.JMeterTestCase; +import org.apache.jmeter.samplers.SampleResult; +import org.apache.jmeter.threads.JMeterContext; +import org.apache.jmeter.threads.JMeterContextService; +import org.apache.jmeter.threads.JMeterVariables; +import org.junit.Before; +import org.junit.Test; +/** + * + * Test IsVarDefined Function + * + * @see IsVarDefined + * @author orim + * + */ +public class TestIsVarDefined extends JMeterTestCase { + protected AbstractFunction isVarDefined; + + private SampleResult result; + + private Collection params; + + private JMeterVariables vars; + + private JMeterContext jmctx; + + @Before + public void setUp() { + isVarDefined = new IsVarDefined(); + result = new SampleResult(); + jmctx = JMeterContextService.getContext(); + String data = "dummy data"; + result.setResponseData(data, null); + vars = new JMeterVariables(); + jmctx.setVariables(vars); + jmctx.setPreviousResult(result); + params = new LinkedList<>(); + } + + @Test + public void testParameterCountIsPropDefined() throws Exception { + checkInvalidParameterCounts(isVarDefined, 1, 1); + } + + @Test + public void testIsVarNotDefinedOnlyPropDefined() throws Exception { + params.add(new CompoundVariable("file.encoding")); + isVarDefined.setParameters(params); + String returnValue = isVarDefined.execute(result, null); + assertEquals("false", returnValue); + } + + @Test + public void testIsVarDefined() throws Exception { + vars.put("varName", ""); + params.add(new CompoundVariable("varName")); + isVarDefined.setParameters(params); + String returnValue = isVarDefined.execute(result, null); + assertEquals("true", returnValue); + } + + @Test + public void testIsVarNotDefined() throws Exception { + params.add(new CompoundVariable("emptyProperty")); + isVarDefined.setParameters(params); + String returnValue = isVarDefined.execute(result, null); + assertEquals("false", returnValue); + } + + @Test(expected = InvalidVariableException.class) + public void testIsVarDefinedError() throws Exception { + isVarDefined.setParameters(params); + isVarDefined.execute(result, null); + } + +} --- xdocs/changes.xml (revision 1815708) +++ xdocs/changes.xml (working copy) @@ -135,6 +135,7 @@
  • 61593Remove Detail, Add, Add from Clipboard, Delete buttons in Function Helper GUI
  • 61724Add __digest function to provide computing of Hashes (SHA-XXX, MDX). Based on a contribution by orimarko at gmail.com
  • 61735Add __dateTimeConvert function to provide date formats conversions. Based on a contribution by orimarko at gmail.com
  • +
  • 61760Add __isPropDefined and __isVarDefined functions to provide property and variable check. Based on a contribution by orimarko at gmail.com
  • I18N

    --- xdocs/usermanual/functions.xml (revision 1815708) +++ xdocs/usermanual/functions.xml (working copy) @@ -1614,6 +1614,26 @@ The name of the variable to set. + + +

    The __isPropDefined function returns true if property exists or false if not.

    +
    + + + The Property Name to be used to check if defined + + +
    + + +

    The isVarDefined function returns true if variable exists or false if not.

    +
    + + + The Variable Name to be used to check if defined + + +