Skip to content

Commit

Permalink
Add function __env
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Pouzac committed Mar 17, 2014
1 parent 5ac6b5c commit fe4e4df
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ eg_globals.xml
/extras/target/
/extraslibs/target/
/standard/target/
*~
*~
.classpath
.project
.settings/
20 changes: 20 additions & 0 deletions common/src/kg/apc/jmeter/JMeterPluginsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,24 @@ public void mouseEntered(MouseEvent e) {
public void mouseExited(MouseEvent e) {
}
}

/**
* Get a String value (environment) with default if not present.
*
* @param propName
* the name of the environment variable.
* @param defaultVal
* the default value.
* @return The PropDefault value
*/
public static String getEnvDefault(String propName, String defaultVal) {
String ans = defaultVal;
String value = System.getenv(propName);
if(value != null) {
ans = value.trim();
} else if (defaultVal != null) {
ans = defaultVal.trim();
}
return ans;
}
}
29 changes: 27 additions & 2 deletions common/test/kg/apc/jmeter/JMeterPluginsUtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package kg.apc.jmeter;

import javax.swing.BorderFactory;

import org.apache.jmeter.gui.util.VerticalPanel;

import java.awt.Component;
import java.nio.ByteBuffer;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JPanel;

import kg.apc.emulators.TestJMeterUtils;

import org.apache.jmeter.gui.util.PowerTableModel;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.testelement.property.CollectionProperty;
Expand All @@ -15,6 +21,7 @@
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.*;

/**
Expand Down Expand Up @@ -79,7 +86,7 @@ public void testByteBufferToString() {
@Test
public void testByteBufferToString2() {
System.out.println("byteBufferToString2");

ByteBuffer buf = ByteBuffer.allocateDirect(2014);
buf.put("My Test".getBytes());
buf.flip();
Expand Down Expand Up @@ -219,7 +226,7 @@ public void testTableModelRowsToCollectionPropertyEval() {
assertEquals("[[1, 2], [3, 4]]", result.toString());
}


/**
* Test of getFloatFromString method, of class JMeterPluginsUtils.
*/
Expand All @@ -244,4 +251,22 @@ public void testDoBestCSVSetup() {
SampleSaveConfiguration conf = new SampleSaveConfiguration();
JMeterPluginsUtils.doBestCSVSetup(conf);
}


/**
* Test of getEnvDefault method, of class JMeterPluginsUtils.
*/
@Test
public void testGetEnvDefault() {
System.out.println("getEnvDefault");
TestJMeterUtils.createJmeterEnv();
Map<String, String> env = System.getenv();
if (!env.isEmpty()) {
String key = env.keySet().iterator().next();
assertEquals(env.get(key), JMeterPluginsUtils.getEnvDefault(key, "testGetEnvDefault"));
assertEquals("testGetEnvDefault", JMeterPluginsUtils.getEnvDefault(key + "testGetEnvDefault", "testGetEnvDefault"));
}
}


}
79 changes: 79 additions & 0 deletions standard/src/kg/apc/jmeter/functions/Env.java
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;
}
}
189 changes: 189 additions & 0 deletions standard/test/kg/apc/jmeter/functions/EnvTest.java
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());
}
}

0 comments on commit fe4e4df

Please sign in to comment.