Author: pmouawad
Date: Sun Nov 19 11:04:53 2017
New Revision: 1815725

URL: http://svn.apache.org/viewvc?rev=1815725&view=rev
Log:
Bug 61760 - Add "__isPropDefined" and "__isVarDefined" functions to know if 
property or variable exist
Contributed by Orimarko
Bugzilla Id: 61760

Added:
    jmeter/trunk/src/functions/org/apache/jmeter/functions/IsPropDefined.java   
(with props)
    jmeter/trunk/src/functions/org/apache/jmeter/functions/IsVarDefined.java   
(with props)
    jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsPropDefined.java   
(with props)
    jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsVarDefined.java   
(with props)
Modified:
    jmeter/trunk/xdocs/changes.xml
    jmeter/trunk/xdocs/usermanual/functions.xml

Added: jmeter/trunk/src/functions/org/apache/jmeter/functions/IsPropDefined.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/src/functions/org/apache/jmeter/functions/IsPropDefined.java?rev=1815725&view=auto
==============================================================================
--- jmeter/trunk/src/functions/org/apache/jmeter/functions/IsPropDefined.java 
(added)
+++ jmeter/trunk/src/functions/org/apache/jmeter/functions/IsPropDefined.java 
Sun Nov 19 11:04:53 2017
@@ -0,0 +1,71 @@
+/*
+ * 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;
+
+/**
+ * Test if a JMeter property is defined
+ * @since 4.0
+ */
+public class IsPropDefined extends AbstractFunction {
+       private static final List<String> 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 Boolean.toString(propertyValue != null);
+       }
+
+       @Override
+       public void setParameters(Collection<CompoundVariable> 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<String> getArgumentDesc() {
+               return desc;
+       }
+
+}

Propchange: 
jmeter/trunk/src/functions/org/apache/jmeter/functions/IsPropDefined.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jmeter/trunk/src/functions/org/apache/jmeter/functions/IsPropDefined.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jmeter/trunk/src/functions/org/apache/jmeter/functions/IsVarDefined.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/src/functions/org/apache/jmeter/functions/IsVarDefined.java?rev=1815725&view=auto
==============================================================================
--- jmeter/trunk/src/functions/org/apache/jmeter/functions/IsVarDefined.java 
(added)
+++ jmeter/trunk/src/functions/org/apache/jmeter/functions/IsVarDefined.java 
Sun Nov 19 11:04:53 2017
@@ -0,0 +1,71 @@
+/*
+ * 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;
+
+/**
+ * Test if a JMeter variable is defined
+ * @since 4.0
+ */
+public class IsVarDefined extends AbstractFunction {
+
+       private static final List<String> 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 Boolean.toString(variableValue != null);
+       }
+
+       @Override
+       public void setParameters(Collection<CompoundVariable> 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<String> getArgumentDesc() {
+               return desc;
+       }
+
+}

Propchange: 
jmeter/trunk/src/functions/org/apache/jmeter/functions/IsVarDefined.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jmeter/trunk/src/functions/org/apache/jmeter/functions/IsVarDefined.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsPropDefined.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsPropDefined.java?rev=1815725&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsPropDefined.java 
(added)
+++ jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsPropDefined.java 
Sun Nov 19 11:04:53 2017
@@ -0,0 +1,101 @@
+/*
+ * 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 {@link IsPropDefined} Function
+ * 
+ * @see IsPropDefined
+ *
+ */
+public class TestIsPropDefined extends JMeterTestCase {
+       protected AbstractFunction isPropDefined;
+
+       private SampleResult result;
+
+       private Collection<CompoundVariable> 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);
+       }
+
+}

Propchange: 
jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsPropDefined.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsPropDefined.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsVarDefined.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsVarDefined.java?rev=1815725&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsVarDefined.java 
(added)
+++ jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsVarDefined.java Sun 
Nov 19 11:04:53 2017
@@ -0,0 +1,101 @@
+/*
+ * 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 {@link IsVarDefined} Function
+ * 
+ * @see IsVarDefined
+ *
+ */
+public class TestIsVarDefined extends JMeterTestCase {
+       protected AbstractFunction isVarDefined;
+
+       private SampleResult result;
+
+       private Collection<CompoundVariable> 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);
+       }
+
+}

Propchange: 
jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsVarDefined.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jmeter/trunk/test/src/org/apache/jmeter/functions/TestIsVarDefined.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1815725&r1=1815724&r2=1815725&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml [utf-8] (original)
+++ jmeter/trunk/xdocs/changes.xml [utf-8] Sun Nov 19 11:04:53 2017
@@ -135,6 +135,7 @@ Summary
     <li><bug>61593</bug>Remove Detail, Add, Add from Clipboard, Delete buttons 
in Function Helper GUI</li>
     <li><bug>61724</bug>Add <code>__digest</code> function to provide 
computing of Hashes (SHA-XXX, MDX). Based on a contribution by orimarko at 
gmail.com</li>
     <li><bug>61735</bug>Add <code>__dateTimeConvert</code> function to provide 
date formats conversions. Based on a contribution by orimarko at gmail.com</li>
+    <li><bug>61760</bug>Add <code>__isPropDefined</code> and 
<code>__isVarDefined</code> functions to know if property or variable exist. 
Contributed by orimarko at gmail.com</li>
 </ul>
 
 <h3>I18N</h3>

Modified: jmeter/trunk/xdocs/usermanual/functions.xml
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/xdocs/usermanual/functions.xml?rev=1815725&r1=1815724&r2=1815725&view=diff
==============================================================================
--- jmeter/trunk/xdocs/usermanual/functions.xml (original)
+++ jmeter/trunk/xdocs/usermanual/functions.xml Sun Nov 19 11:04:53 2017
@@ -134,13 +134,15 @@ Alternatively, just use <code>/</code> i
         <tr><td>Scripting</td><td> <a 
href="#__javaScript">javaScript</a></td><td>process JavaScript 
(Nashorn)</td><td>1.9</td></tr>
         <tr><td>Scripting</td><td> <a 
href="#__jexl2">jexl2</a></td><td>evaluate a Commons Jexl2 
expression</td><td>jexl2(2.1.1)</td></tr>
         <tr><td>Scripting</td><td> <a 
href="#__jexl3">jexl3</a></td><td>evaluate a Commons Jexl3 
expression</td><td>jexl3 (3.0)</td></tr>
+        <tr><td>Properties</td><td> <a 
href="#__isPropDefined">isPropDefined</a> </td><td>Test if a property 
exists</td><td>4.0</td></tr>
         <tr><td>Properties</td><td> <a href="#__property">property</a> 
</td><td>read a property</td><td>2.0</td></tr>
         <tr><td>Properties</td><td> <a href="#__P">P</a></td><td>read a 
property (shorthand method)</td><td>2.0</td></tr>
         <tr><td>Properties</td><td> <a 
href="#__setProperty">setProperty</a></td><td>set a JMeter 
property</td><td>2.1</td></tr>
         <tr><td>Variables</td><td> <a href="#__split">split</a></td><td>Split 
a string into variables</td><td>2.0.2</td></tr>
-        <tr><td>Variables</td><td> <a href="#__V">V</a></td><td>evaluate a 
variable name</td><td>2.3RC3</td></tr>
         <tr><td>Variables</td><td> <a href="#__eval">eval</a></td><td>evaluate 
a variable expression</td><td>2.3.1</td></tr>
         <tr><td>Variables</td><td> <a 
href="#__evalVar">evalVar</a></td><td>evaluate an expression stored in a 
variable</td><td>2.3.1</td></tr>
+        <tr><td>Properties</td><td> <a href="#__isVarDefined">isVarDefined</a> 
</td><td>Test if a variable exists</td><td>4.0</td></tr>
+        <tr><td>Variables</td><td> <a href="#__V">V</a></td><td>evaluate a 
variable name</td><td>2.3RC3</td></tr>
         <tr><td>String</td><td> <a 
href="#__regexFunction">regexFunction</a></td><td>parse previous response using 
a regular expression</td><td>1.X</td></tr>
         <tr><td>String</td><td> <a 
href="#__escapeOroRegexpChars">escapeOroRegexpChars</a></td><td>quote meta 
chars used by ORO regular expression</td><td>2.9</td></tr>
         <tr><td>String</td><td> <a href="#__char">char</a></td><td>generate 
Unicode char values from a list of numbers</td><td>2.3.3</td></tr>
@@ -1614,6 +1616,26 @@ becomes:
         <property name="Name of variable" required="No">The name of the 
variable to set.</property>
     </properties>
 </component>
+<component index="&sect-num;.5.35" name="__isPropDefined">
+    <description>
+        <p>The __isPropDefined function returns true if property exists or 
false if not.</p>
+    </description>
+    <properties>
+        <property name="Property Name" required="Yes">
+            The Property Name to be used to check if defined
+        </property>        
+    </properties>
+</component>
+<component index="&sect-num;.5.35" name="__isVarDefined">
+    <description>
+        <p>The isVarDefined function returns true if variable exists or false 
if not.</p>
+    </description>
+    <properties>
+        <property name="Variable Name" required="Yes">
+            The Variable Name to be used to check if defined
+        </property>        
+    </properties>
+</component>
 </subsection>
 
 <subsection name="&sect-num;.6 Pre-defined Variables" anchor="predefinedvars">


Reply via email to