From fe4e4df598fdd60e6bd9f32278cb07418bd9b5e1 Mon Sep 17 00:00:00 2001 From: Lucas Pouzac Date: Mon, 17 Mar 2014 18:53:27 +0100 Subject: [PATCH] Add function __env --- .gitignore | 5 +- .../src/kg/apc/jmeter/JMeterPluginsUtils.java | 20 ++ .../kg/apc/jmeter/JMeterPluginsUtilsTest.java | 29 ++- standard/src/kg/apc/jmeter/functions/Env.java | 79 ++++++++ .../test/kg/apc/jmeter/functions/EnvTest.java | 189 ++++++++++++++++++ 5 files changed, 319 insertions(+), 3 deletions(-) create mode 100644 standard/src/kg/apc/jmeter/functions/Env.java create mode 100644 standard/test/kg/apc/jmeter/functions/EnvTest.java diff --git a/.gitignore b/.gitignore index c19a5724f..be1704b82 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,7 @@ eg_globals.xml /extras/target/ /extraslibs/target/ /standard/target/ -*~ \ No newline at end of file +*~ +.classpath +.project +.settings/ diff --git a/common/src/kg/apc/jmeter/JMeterPluginsUtils.java b/common/src/kg/apc/jmeter/JMeterPluginsUtils.java index f58a748e4..0a05f83b2 100644 --- a/common/src/kg/apc/jmeter/JMeterPluginsUtils.java +++ b/common/src/kg/apc/jmeter/JMeterPluginsUtils.java @@ -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; + } } diff --git a/common/test/kg/apc/jmeter/JMeterPluginsUtilsTest.java b/common/test/kg/apc/jmeter/JMeterPluginsUtilsTest.java index da9fa42c7..8bc591e94 100644 --- a/common/test/kg/apc/jmeter/JMeterPluginsUtilsTest.java +++ b/common/test/kg/apc/jmeter/JMeterPluginsUtilsTest.java @@ -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; @@ -15,6 +21,7 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; + import static org.junit.Assert.*; /** @@ -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(); @@ -219,7 +226,7 @@ public void testTableModelRowsToCollectionPropertyEval() { assertEquals("[[1, 2], [3, 4]]", result.toString()); } - + /** * Test of getFloatFromString method, of class JMeterPluginsUtils. */ @@ -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 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")); + } + } + + } diff --git a/standard/src/kg/apc/jmeter/functions/Env.java b/standard/src/kg/apc/jmeter/functions/Env.java new file mode 100644 index 000000000..32a2f1235 --- /dev/null +++ b/standard/src/kg/apc/jmeter/functions/Env.java @@ -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 desc = new LinkedList(); + 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 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 getArgumentDesc() { + return desc; + } +} diff --git a/standard/test/kg/apc/jmeter/functions/EnvTest.java b/standard/test/kg/apc/jmeter/functions/EnvTest.java new file mode 100644 index 000000000..fd28d33f2 --- /dev/null +++ b/standard/test/kg/apc/jmeter/functions/EnvTest.java @@ -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 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 parameters = new ArrayList(); + 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 parameters = new ArrayList(); + 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 parameters = new ArrayList(); + 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 parameters = new ArrayList(); + 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 parameters = new ArrayList(); + 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 parameters = new ArrayList(); + Env instance = new Env(); + instance.setParameters(parameters); + } + + @Test(expected = InvalidVariableException.class) + public void testSetParametersException2() throws Exception { + System.out.println("setParameters"); + Collection parameters = new ArrayList(); + 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 result = instance.getArgumentDesc(); + assertEquals(3, result.size()); + } +} \ No newline at end of file