forked from undera/jmeter-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lucas Pouzac
committed
Mar 17, 2014
1 parent
5ac6b5c
commit fe4e4df
Showing
5 changed files
with
319 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,7 @@ eg_globals.xml | |
/extras/target/ | ||
/extraslibs/target/ | ||
/standard/target/ | ||
*~ | ||
*~ | ||
.classpath | ||
.project | ||
.settings/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package kg.apc.jmeter.functions; | ||
|
||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import kg.apc.jmeter.JMeterPluginsUtils; | ||
|
||
import org.apache.jmeter.engine.util.CompoundVariable; | ||
import org.apache.jmeter.functions.AbstractFunction; | ||
import org.apache.jmeter.functions.InvalidVariableException; | ||
import org.apache.jmeter.samplers.SampleResult; | ||
import org.apache.jmeter.samplers.Sampler; | ||
import org.apache.jmeter.threads.JMeterVariables; | ||
import org.apache.jmeter.util.JMeterUtils; | ||
|
||
public class Env extends AbstractFunction { | ||
|
||
private static final List<String> desc = new LinkedList<String>(); | ||
private static final String KEY = "__env"; | ||
|
||
// Number of parameters expected - used to reject invalid calls | ||
private static final int MIN_PARAMETER_COUNT = 1; | ||
private static final int MAX_PARAMETER_COUNT = 3; | ||
|
||
static { | ||
desc.add("Name of environment variable"); | ||
desc.add("Name of variable in which to store the result (optional)"); | ||
desc.add("Default value"); | ||
} | ||
private CompoundVariable[] values; | ||
|
||
/** | ||
* No-arg constructor. | ||
*/ | ||
public Env() { | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public synchronized String execute(SampleResult previousResult, Sampler currentSampler) | ||
throws InvalidVariableException { | ||
String propertyName = values[0].execute(); | ||
String propertyDefault = propertyName; | ||
if (values.length > 2) { // We have a 3rd parameter | ||
propertyDefault = values[2].execute(); | ||
} | ||
String propertyValue = JMeterPluginsUtils.getEnvDefault(propertyName, propertyDefault); | ||
if (values.length > 1) { | ||
String variableName = values[1].execute(); | ||
if (variableName.length() > 0) {// Allow for empty name | ||
final JMeterVariables variables = getVariables(); | ||
if (variables != null) { | ||
variables.put(variableName, propertyValue); | ||
} | ||
} | ||
} | ||
return propertyValue; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException { | ||
checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT); | ||
values = parameters.toArray(new CompoundVariable[0]); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public String getReferenceKey() { | ||
return KEY; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public List<String> getArgumentDesc() { | ||
return desc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package kg.apc.jmeter.functions; | ||
|
||
import org.apache.jmeter.threads.JMeterContextService; | ||
|
||
import kg.apc.emulators.TestJMeterUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.jmeter.engine.util.CompoundVariable; | ||
import org.apache.jmeter.functions.InvalidVariableException; | ||
import org.apache.jmeter.samplers.SampleResult; | ||
import org.apache.jmeter.samplers.Sampler; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* | ||
* @author undera | ||
*/ | ||
public class EnvTest { | ||
|
||
private static String key, value; | ||
|
||
public EnvTest() { | ||
} | ||
|
||
@BeforeClass | ||
public static void setUpClass() throws Exception { | ||
TestJMeterUtils.createJmeterEnv(); | ||
Map<String, String> env = System.getenv(); | ||
if (!env.isEmpty()) { | ||
key = env.keySet().iterator().next(); | ||
value = env.get(key); | ||
} | ||
} | ||
|
||
@AfterClass | ||
public static void tearDownClass() throws Exception { | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
} | ||
|
||
/** | ||
* Test of execute method, of class Env. | ||
*/ | ||
@Test | ||
public void testExecute() throws Exception { | ||
System.out.println("execute 1"); | ||
SampleResult previousResult = null; | ||
Sampler currentSampler = null; | ||
assertNull(JMeterContextService.getContext().getVariables().get("toto")); | ||
Collection<CompoundVariable> parameters = new ArrayList<CompoundVariable>(); | ||
parameters.add(new CompoundVariable(key)); | ||
Env instance = new Env(); | ||
instance.setParameters(parameters); | ||
String result = instance.execute(previousResult, currentSampler); | ||
assertEquals(value, result); | ||
assertNull(JMeterContextService.getContext().getVariables().get("toto")); | ||
} | ||
|
||
@Test | ||
public void testExecute_1() throws Exception { | ||
System.out.println("execute 1"); | ||
SampleResult previousResult = null; | ||
Sampler currentSampler = null; | ||
|
||
assertNull(JMeterContextService.getContext().getVariables().get("toto")); | ||
Collection<CompoundVariable> parameters = new ArrayList<CompoundVariable>(); | ||
parameters.add(new CompoundVariable(key)); | ||
parameters.add(new CompoundVariable("toto")); | ||
Env instance = new Env(); | ||
instance.setParameters(parameters); | ||
String result = instance.execute(previousResult, currentSampler); | ||
assertEquals(value, result); | ||
assertNotNull(JMeterContextService.getContext().getVariables().remove("toto")); | ||
} | ||
|
||
@Test | ||
public void testExecute_2() throws Exception { | ||
System.out.println("execute 1"); | ||
SampleResult previousResult = null; | ||
Sampler currentSampler = null; | ||
assertNull(JMeterContextService.getContext().getVariables().get("toto")); | ||
Collection<CompoundVariable> parameters = new ArrayList<CompoundVariable>(); | ||
String overrideKey = key + "testExecute_2"; | ||
parameters.add(new CompoundVariable(overrideKey)); | ||
Env instance = new Env(); | ||
instance.setParameters(parameters); | ||
String result = instance.execute(previousResult, currentSampler); | ||
assertEquals(overrideKey, result); | ||
assertNull(JMeterContextService.getContext().getVariables().get("toto")); | ||
} | ||
|
||
@Test | ||
public void testExecute_3() throws Exception { | ||
System.out.println("execute 1"); | ||
SampleResult previousResult = null; | ||
Sampler currentSampler = null; | ||
assertNull(JMeterContextService.getContext().getVariables().get("toto")); | ||
Collection<CompoundVariable> parameters = new ArrayList<CompoundVariable>(); | ||
String overrideKey = key + "testExecute_3"; | ||
String defaultValue = "defaultValue"; | ||
parameters.add(new CompoundVariable(overrideKey)); | ||
parameters.add(new CompoundVariable("")); | ||
parameters.add(new CompoundVariable(defaultValue)); | ||
Env instance = new Env(); | ||
instance.setParameters(parameters); | ||
String result = instance.execute(previousResult, currentSampler); | ||
assertEquals(defaultValue, result); | ||
assertNull(JMeterContextService.getContext().getVariables().get("toto")); | ||
} | ||
|
||
/** | ||
* Test of setParameters method, of class Env. | ||
*/ | ||
@Test | ||
public void testSetParameters() throws Exception { | ||
System.out.println("setParameters"); | ||
Collection<CompoundVariable> parameters = new ArrayList<CompoundVariable>(); | ||
parameters.add(new CompoundVariable(key)); | ||
Env instance = new Env(); | ||
instance.setParameters(parameters); | ||
|
||
// second parameter | ||
parameters.add(new CompoundVariable("save_variable")); | ||
instance.setParameters(parameters); | ||
|
||
// third parameter | ||
parameters.add(new CompoundVariable("default_value")); | ||
instance.setParameters(parameters); | ||
} | ||
|
||
@Test(expected = InvalidVariableException.class) | ||
public void testSetParametersException() throws Exception { | ||
System.out.println("setParameters"); | ||
Collection<CompoundVariable> parameters = new ArrayList<CompoundVariable>(); | ||
Env instance = new Env(); | ||
instance.setParameters(parameters); | ||
} | ||
|
||
@Test(expected = InvalidVariableException.class) | ||
public void testSetParametersException2() throws Exception { | ||
System.out.println("setParameters"); | ||
Collection<CompoundVariable> parameters = new ArrayList<CompoundVariable>(); | ||
parameters.add(new CompoundVariable(key)); | ||
parameters.add(new CompoundVariable("save_variable")); | ||
parameters.add(new CompoundVariable("default_value")); | ||
parameters.add(new CompoundVariable("Error")); | ||
Env instance = new Env(); | ||
instance.setParameters(parameters); | ||
} | ||
|
||
/** | ||
* Test of getReferenceKey method, of class Env. | ||
*/ | ||
@Test | ||
public void testGetReferenceKey() { | ||
System.out.println("getReferenceKey"); | ||
Env instance = new Env(); | ||
String expResult = "__env"; | ||
String result = instance.getReferenceKey(); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
/** | ||
* Test of getArgumentDesc method, of class Env. | ||
*/ | ||
@Test | ||
public void testGetArgumentDesc() { | ||
System.out.println("getArgumentDesc"); | ||
Env instance = new Env(); | ||
List<String> result = instance.getArgumentDesc(); | ||
assertEquals(3, result.size()); | ||
} | ||
} |