Author: pmouawad
Date: Sat May 12 08:12:59 2018
New Revision: 1831451

URL: http://svn.apache.org/viewvc?rev=1831451&view=rev
Log:
Bug 62323 - Add function "__ThreadGroupName" function to obtain ThreadGroup name
Mainly contributed by Orimarko
Bugzilla Id: 62323

Added:
    
jmeter/trunk/src/functions/org/apache/jmeter/functions/AbstractFunctionByKey.java
   (with props)
    jmeter/trunk/src/functions/org/apache/jmeter/functions/ThreadGroupName.java 
  (with props)
Modified:
    jmeter/trunk/test/src/org/apache/jmeter/functions/TestSimpleFunctions.java
    
jmeter/trunk/test/src/org/apache/jmeter/testelement/property/PackageTest.java
    jmeter/trunk/xdocs/changes.xml
    jmeter/trunk/xdocs/usermanual/functions.xml

Added: 
jmeter/trunk/src/functions/org/apache/jmeter/functions/AbstractFunctionByKey.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/src/functions/org/apache/jmeter/functions/AbstractFunctionByKey.java?rev=1831451&view=auto
==============================================================================
--- 
jmeter/trunk/src/functions/org/apache/jmeter/functions/AbstractFunctionByKey.java
 (added)
+++ 
jmeter/trunk/src/functions/org/apache/jmeter/functions/AbstractFunctionByKey.java
 Sat May 12 08:12:59 2018
@@ -0,0 +1,65 @@
+/*
+ * 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 org.apache.jmeter.engine.util.CompoundVariable;
+
+/**
+ * 
+ * Abstract Function initialized by key and parameters count
+ * 
+ * @since 4.1
+ *
+ */
+abstract class AbstractFunctionByKey extends AbstractFunction {
+
+    private final String key;
+    private final int parametersCount;
+
+    private Object[] values;
+
+    public AbstractFunctionByKey(String key, int parametersCount) {
+        this.key = key;
+        this.parametersCount = parametersCount;
+    }
+
+    public void setParameters(Collection<CompoundVariable> parameters, Integer 
min, Integer max)
+            throws InvalidVariableException {
+        checkParameterCount(parameters, min, max);
+        values = parameters.toArray();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String getReferenceKey() {
+        return key;
+    }
+    
+    protected final Object[] getParameterValues() {
+        return values;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setParameters(Collection<CompoundVariable> parameters) throws 
InvalidVariableException {
+        checkParameterCount(parameters, parametersCount);
+    }
+}

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

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

Added: 
jmeter/trunk/src/functions/org/apache/jmeter/functions/ThreadGroupName.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/src/functions/org/apache/jmeter/functions/ThreadGroupName.java?rev=1831451&view=auto
==============================================================================
--- jmeter/trunk/src/functions/org/apache/jmeter/functions/ThreadGroupName.java 
(added)
+++ jmeter/trunk/src/functions/org/apache/jmeter/functions/ThreadGroupName.java 
Sat May 12 08:12:59 2018
@@ -0,0 +1,62 @@
+/*
+ * 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.LinkedList;
+import java.util.List;
+
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.samplers.Sampler;
+import org.apache.jmeter.threads.JMeterContext;
+import org.apache.jmeter.threads.JMeterContextService;
+
+/**
+ * Returns Thread Group Name
+ * 
+ * @since 4.1
+ */
+public class ThreadGroupName extends AbstractFunctionByKey {
+    private static final String KEY = "__ThreadGroupName"; //$NON-NLS-1$
+    
+    private static final List<String> DESC = new LinkedList<>();
+
+    public ThreadGroupName() {
+        super(KEY, 0); //$NON-NLS-1$
+    }
+
+    @Override
+    /**
+     * Get current thread group using sampler's context
+     */
+    public String execute(SampleResult previousResult, Sampler currentSampler) 
throws InvalidVariableException {
+        JMeterContext context = null;
+        if (currentSampler != null) {
+            context = currentSampler.getThreadContext();
+        } else {
+            context = JMeterContextService.getContext();
+        }
+        return context.getThreadGroup().getName();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public List<String> getArgumentDesc() {
+        return DESC;
+    }
+}

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

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

Modified: 
jmeter/trunk/test/src/org/apache/jmeter/functions/TestSimpleFunctions.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/functions/TestSimpleFunctions.java?rev=1831451&r1=1831450&r2=1831451&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/functions/TestSimpleFunctions.java 
(original)
+++ jmeter/trunk/test/src/org/apache/jmeter/functions/TestSimpleFunctions.java 
Sat May 12 08:12:59 2018
@@ -26,11 +26,13 @@ import java.util.UUID;
 
 import org.apache.jmeter.engine.util.CompoundVariable;
 import org.apache.jmeter.junit.JMeterTestCase;
+import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
 import org.apache.jmeter.samplers.SampleResult;
 import org.apache.jmeter.services.FileServer;
 import org.apache.jmeter.threads.JMeterContext;
 import org.apache.jmeter.threads.JMeterContextService;
 import org.apache.jmeter.threads.JMeterVariables;
+import org.apache.jmeter.threads.ThreadGroup;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -43,7 +45,7 @@ public class TestSimpleFunctions extends
 
     @Before
     public void setUp() {
-        result = new SampleResult();
+        result = new SampleResult();        
         jmctx = JMeterContextService.getContext();
         String data = "The quick brown fox";
         result.setResponseData(data, null);
@@ -170,4 +172,27 @@ public class TestSimpleFunctions extends
             FileServer.getFileServer().setScriptName(null);
         }
     }
+
+    @Test
+       public void testThreadGroupName() throws Exception {
+               AbstractFunctionByKey function = new ThreadGroupName();
+               try {
+                       HTTPSamplerProxy httpRequest = new HTTPSamplerProxy();
+                       ThreadGroup threadGroup = new ThreadGroup();
+                       threadGroup.setName("ThreadGroup-1");
+                       JMeterContext context = 
JMeterContextService.getContext();
+                       context.setCurrentSampler(httpRequest);
+                       context.setThreadGroup(threadGroup);
+                       String ret = function.execute(result, httpRequest);
+                       assertEquals("ThreadGroup-1", ret);
+               } finally {
+                       FileServer.getFileServer().setScriptName(null);
+               }
+       }
+
+    @Test
+    public void testThreadGroupNameParameterCount() throws Exception {
+        AbstractFunctionByKey function = new ThreadGroupName();
+        checkInvalidParameterCounts(function, 0, 0);
+    }
 }

Modified: 
jmeter/trunk/test/src/org/apache/jmeter/testelement/property/PackageTest.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/testelement/property/PackageTest.java?rev=1831451&r1=1831450&r2=1831451&view=diff
==============================================================================
--- 
jmeter/trunk/test/src/org/apache/jmeter/testelement/property/PackageTest.java 
(original)
+++ 
jmeter/trunk/test/src/org/apache/jmeter/testelement/property/PackageTest.java 
Sat May 12 08:12:59 2018
@@ -260,6 +260,13 @@ public class PackageTest {
         }
 
     }
+    
+    @Test
+    public void testMapPropertyEmptyConstructor() throws Exception {
+        MapProperty mapProperty = new MapProperty();
+        mapProperty.addProperty(new BooleanProperty("test", true));
+    }
+    
 
     @Test
     public void testNullEquality() throws Exception {

Modified: jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1831451&r1=1831450&r2=1831451&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml [utf-8] (original)
+++ jmeter/trunk/xdocs/changes.xml [utf-8] Sat May 12 08:12:59 2018
@@ -121,8 +121,8 @@ this behaviour, set <code>httpclient.res
 <h3>Functions</h3>
 <ul>
     <li><bug>62178</bug>Add default value to <code>__V</code> function. 
Contributed by orimarko at gmail.com</li>
+    <li><bug>62178</bug>Add function <code>__ThreadGroupName</code> function 
to obtain ThreadGroup name. Mainly contributed by orimarko at gmail.com</li>
 </ul>
-
 <h3>I18N</h3>
 <ul>
 </ul>

Modified: jmeter/trunk/xdocs/usermanual/functions.xml
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/xdocs/usermanual/functions.xml?rev=1831451&r1=1831450&r2=1831451&view=diff
==============================================================================
--- jmeter/trunk/xdocs/usermanual/functions.xml (original)
+++ jmeter/trunk/xdocs/usermanual/functions.xml Sat May 12 08:12:59 2018
@@ -108,6 +108,7 @@ Alternatively, just use <code>/</code> i
         <tr><th>Type of 
function</th><th>Name</th><th>Comment</th><th>Since</th></tr>
         <!-- N.B. the leading space is needed to ensure the content is 
processed -->
         <tr><td>Information</td><td> <a 
href="#__threadNum">threadNum</a></td><td>get thread 
number</td><td>1.X</td></tr>
+        <tr><td>Information</td><td> <a 
href="#__threadGroupName">threadGroupName</a></td><td>get thread group 
name</td><td>4.1</td></tr>
         <tr><td>Information</td><td> <a 
href="#__samplerName">samplerName</a></td><td>get the sampler name 
(label)</td><td>2.5</td></tr>
         <tr><td>Information</td><td> <a 
href="#__machineIP">machineIP</a></td><td>get the local machine IP 
address</td><td>2.6</td></tr>
         <tr><td>Information</td><td> <a 
href="#__machineName">machineName</a></td><td>get the local machine 
name</td><td>1.X</td></tr>
@@ -373,7 +374,7 @@ If you want to have a count that increme
 </properties>
 </component>
 
-<component index="&sect-num;.5.3" name="__threadNum">
+<component index="&sect-num;.5.3a" name="__threadNum">
 <description><p>The thread number function simply returns the number of the 
thread currently
 being executed.  These numbers are independent of ThreadGroup, meaning thread 
#1 in one threadgroup
 is indistinguishable from thread #1 in another threadgroup, from the point of 
view of this function.</p>
@@ -384,6 +385,21 @@ is indistinguishable from thread #1 in a
 </p>
 </description>
 <note>
+This function does not work in any Configuration elements (e.g. User Defined 
Variables) as these are run from a separate thread.
+Nor does it make sense to use it on the Test Plan.
+</note>
+</component>
+
+<component index="&sect-num;.5.3b" name="__threadGroupName">
+<description><p>The thread group name function simply returns the name of the 
thread group
+being executed.</p>
+
+<p>There are no arguments for this function.</p>
+<p>Usage Example:
+<source>${__threadGroupName}</source> 
+</p>
+</description>
+<note>
 This function does not work in any Configuration elements (e.g. User Defined 
Variables) as these are run from a separate thread.
 Nor does it make sense to use it on the Test Plan.
 </note>


Reply via email to