View | Details | Raw Unified | Return to bug 61760
Collapse All | Expand All

(-)src/functions/org/apache/jmeter/functions/IsPropDefined.java (+67 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  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.functions;
20
21
import java.util.Collection;
22
import java.util.LinkedList;
23
import java.util.List;
24
25
import org.apache.jmeter.engine.util.CompoundVariable;
26
import org.apache.jmeter.samplers.SampleResult;
27
import org.apache.jmeter.samplers.Sampler;
28
import org.apache.jmeter.util.JMeterUtils;
29
30
public class IsPropDefined extends AbstractFunction {
31
	private static final List<String> desc = new LinkedList<>();
32
	private static final String KEY = "__isPropDefined";
33
34
	// Number of parameters expected - used to reject invalid calls
35
	private static final int MIN_PARAMETER_COUNT = 1;
36
	private static final int MAX_PARAMETER_COUNT = 1;
37
38
	static {
39
		desc.add(JMeterUtils.getResString("property_name_param"));
40
	}
41
42
	private CompoundVariable[] values;
43
44
	@Override
45
	public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
46
		String propertyName = values[0].execute();
47
		String propertyValue = JMeterUtils.getProperty(propertyName);
48
		return isTrueString(propertyValue != null);
49
	}
50
51
	@Override
52
	public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
53
		checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
54
		values = parameters.toArray(new CompoundVariable[parameters.size()]);
55
	}
56
57
	@Override
58
	public String getReferenceKey() {
59
		return KEY;
60
	}
61
62
	@Override
63
	public List<String> getArgumentDesc() {
64
		return desc;
65
	}
66
67
}
(-)src/functions/org/apache/jmeter/functions/IsVarDefined.java (+67 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  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.functions;
20
21
import java.util.Collection;
22
import java.util.LinkedList;
23
import java.util.List;
24
25
import org.apache.jmeter.engine.util.CompoundVariable;
26
import org.apache.jmeter.samplers.SampleResult;
27
import org.apache.jmeter.samplers.Sampler;
28
import org.apache.jmeter.util.JMeterUtils;
29
30
public class IsVarDefined extends AbstractFunction {
31
32
	private static final List<String> desc = new LinkedList<>();
33
	private static final String KEY = "__isVarDefined";
34
	// Number of parameters expected - used to reject invalid calls
35
	private static final int MIN_PARAMETER_COUNT = 1;
36
	private static final int MAX_PARAMETER_COUNT = 1;
37
38
	static {
39
		desc.add(JMeterUtils.getResString("evalvar_name_param"));
40
	}
41
42
	private CompoundVariable[] values;
43
44
	@Override
45
	public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
46
		String variableName = values[0].execute();
47
		String variableValue = getVariables().get(variableName);
48
		return isTrueString(variableValue != null);
49
	}
50
51
	@Override
52
	public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
53
		checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
54
		values = parameters.toArray(new CompoundVariable[parameters.size()]);
55
	}
56
57
	@Override
58
	public String getReferenceKey() {
59
		return KEY;
60
	}
61
62
	@Override
63
	public List<String> getArgumentDesc() {
64
		return desc;
65
	}
66
67
}
(-)test/src/org/apache/jmeter/functions/TestIsPropDefined.java (+103 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  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.functions;
20
21
import static org.junit.Assert.assertEquals;
22
23
import java.util.Collection;
24
import java.util.LinkedList;
25
26
import org.apache.jmeter.engine.util.CompoundVariable;
27
import org.apache.jmeter.junit.JMeterTestCase;
28
import org.apache.jmeter.samplers.SampleResult;
29
import org.apache.jmeter.threads.JMeterContext;
30
import org.apache.jmeter.threads.JMeterContextService;
31
import org.apache.jmeter.threads.JMeterVariables;
32
import org.junit.Before;
33
import org.junit.Test;
34
/**
35
 * 
36
 * Test IsPropDefined Function
37
 * 
38
 * @see IsPropDefined
39
 * @author orim
40
 *
41
 */
42
43
public class TestIsPropDefined extends JMeterTestCase {
44
	protected AbstractFunction isPropDefined;
45
46
	private SampleResult result;
47
48
	private Collection<CompoundVariable> params;
49
50
	private JMeterVariables vars;
51
52
	private JMeterContext jmctx;
53
54
	@Before
55
	public void setUp() {
56
		isPropDefined = new IsPropDefined();
57
		result = new SampleResult();
58
		jmctx = JMeterContextService.getContext();
59
		String data = "dummy data";
60
		result.setResponseData(data, null);
61
		vars = new JMeterVariables();
62
		jmctx.setVariables(vars);
63
		jmctx.setPreviousResult(result);
64
		params = new LinkedList<>();
65
	}
66
67
	@Test
68
	public void testParameterCountIsPropDefined() throws Exception {
69
		checkInvalidParameterCounts(isPropDefined, 1, 1);
70
	}
71
72
	@Test
73
	public void testIsPropDefined() throws Exception {
74
		params.add(new CompoundVariable("file.encoding"));
75
		isPropDefined.setParameters(params);
76
		String returnValue = isPropDefined.execute(result, null);
77
		assertEquals("true", returnValue);
78
	}
79
80
	@Test
81
	public void testIsPropNotDefined() throws Exception {
82
		params.add(new CompoundVariable("emptyProperty"));
83
		isPropDefined.setParameters(params);
84
		String returnValue = isPropDefined.execute(result, null);
85
		assertEquals("false", returnValue);
86
	}
87
88
	@Test
89
	public void testIsPropNotDefinedOnlyVarDefined() throws Exception {
90
		vars.put("emptyProperty", "emptyPropertyValue");
91
		params.add(new CompoundVariable("emptyProperty"));
92
		isPropDefined.setParameters(params);
93
		String returnValue = isPropDefined.execute(result, null);
94
		assertEquals("false", returnValue);
95
	}
96
97
	@Test(expected = InvalidVariableException.class)
98
	public void testIsPropDefinedError() throws Exception {
99
		isPropDefined.setParameters(params);
100
		isPropDefined.execute(result, null);
101
	}
102
103
}
(-)test/src/org/apache/jmeter/functions/TestIsVarDefined.java (+102 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  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.functions;
20
21
import static org.junit.Assert.assertEquals;
22
23
import java.util.Collection;
24
import java.util.LinkedList;
25
26
import org.apache.jmeter.engine.util.CompoundVariable;
27
import org.apache.jmeter.junit.JMeterTestCase;
28
import org.apache.jmeter.samplers.SampleResult;
29
import org.apache.jmeter.threads.JMeterContext;
30
import org.apache.jmeter.threads.JMeterContextService;
31
import org.apache.jmeter.threads.JMeterVariables;
32
import org.junit.Before;
33
import org.junit.Test;
34
/**
35
 * 
36
 * Test IsVarDefined Function
37
 * 
38
 * @see IsVarDefined
39
 * @author orim
40
 *
41
 */
42
public class TestIsVarDefined extends JMeterTestCase {
43
	protected AbstractFunction isVarDefined;
44
45
	private SampleResult result;
46
47
	private Collection<CompoundVariable> params;
48
49
	private JMeterVariables vars;
50
51
	private JMeterContext jmctx;
52
53
	@Before
54
	public void setUp() {
55
		isVarDefined = new IsVarDefined();
56
		result = new SampleResult();
57
		jmctx = JMeterContextService.getContext();
58
		String data = "dummy data";
59
		result.setResponseData(data, null);
60
		vars = new JMeterVariables();
61
		jmctx.setVariables(vars);
62
		jmctx.setPreviousResult(result);
63
		params = new LinkedList<>();
64
	}
65
66
	@Test
67
	public void testParameterCountIsPropDefined() throws Exception {
68
		checkInvalidParameterCounts(isVarDefined, 1, 1);
69
	}
70
71
	@Test
72
	public void testIsVarNotDefinedOnlyPropDefined() throws Exception {
73
		params.add(new CompoundVariable("file.encoding"));
74
		isVarDefined.setParameters(params);
75
		String returnValue = isVarDefined.execute(result, null);
76
		assertEquals("false", returnValue);
77
	}
78
79
	@Test
80
	public void testIsVarDefined() throws Exception {
81
		vars.put("varName", "");
82
		params.add(new CompoundVariable("varName"));
83
		isVarDefined.setParameters(params);
84
		String returnValue = isVarDefined.execute(result, null);
85
		assertEquals("true", returnValue);
86
	}
87
88
	@Test
89
	public void testIsVarNotDefined() throws Exception {
90
		params.add(new CompoundVariable("emptyProperty"));
91
		isVarDefined.setParameters(params);
92
		String returnValue = isVarDefined.execute(result, null);
93
		assertEquals("false", returnValue);
94
	}
95
96
	@Test(expected = InvalidVariableException.class)
97
	public void testIsVarDefinedError() throws Exception {
98
		isVarDefined.setParameters(params);
99
		isVarDefined.execute(result, null);
100
	}
101
102
}
(-)xdocs/changes.xml (+1 lines)
Lines 135-140 Link Here
135
    <li><bug>61593</bug>Remove Detail, Add, Add from Clipboard, Delete buttons in Function Helper GUI</li>
135
    <li><bug>61593</bug>Remove Detail, Add, Add from Clipboard, Delete buttons in Function Helper GUI</li>
136
    <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>
136
    <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>
137
    <li><bug>61735</bug>Add <code>__dateTimeConvert</code> function to provide date formats conversions. Based on a contribution by orimarko at gmail.com</li>
137
    <li><bug>61735</bug>Add <code>__dateTimeConvert</code> function to provide date formats conversions. Based on a contribution by orimarko at gmail.com</li>
138
    <li><bug>61760</bug>Add <code>__isPropDefined</code> and <code>__isVarDefined</code> functions to provide property and variable check. Based on a contribution by orimarko at gmail.com</li>
138
</ul>
139
</ul>
139
140
140
<h3>I18N</h3>
141
<h3>I18N</h3>
(-)xdocs/usermanual/functions.xml (+20 lines)
Lines 1614-1619 Link Here
1614
        <property name="Name of variable" required="No">The name of the variable to set.</property>
1614
        <property name="Name of variable" required="No">The name of the variable to set.</property>
1615
    </properties>
1615
    </properties>
1616
</component>
1616
</component>
1617
<component index="&sect-num;.5.35" name="__isPropDefined">
1618
    <description>
1619
        <p>The __isPropDefined function returns true if property exists or false if not.</p>
1620
    </description>
1621
    <properties>
1622
        <property name="Property Name" required="Yes">
1623
            The Property Name to be used to check if defined
1624
        </property>        
1625
    </properties>
1626
</component>
1627
<component index="&sect-num;.5.35" name="__isVarDefined">
1628
    <description>
1629
        <p>The isVarDefined function returns true if variable exists or false if not.</p>
1630
    </description>
1631
    <properties>
1632
        <property name="Variable Name" required="Yes">
1633
            The Variable Name to be used to check if defined
1634
        </property>        
1635
    </properties>
1636
</component>
1617
</subsection>
1637
</subsection>
1618
1638
1619
<subsection name="&sect-num;.6 Pre-defined Variables" anchor="predefinedvars">
1639
<subsection name="&sect-num;.6 Pre-defined Variables" anchor="predefinedvars">

Return to bug 61760