use of org.apache.jmeter.testelement.property.JMeterProperty in project jmeter by apache.
the class CookieManager method save.
/**
* Save the static cookie data to a file.
* <p>
* Cookies are only taken from the GUI - runtime cookies are not included.
*
* @param authFile
* name of the file to store the cookies into. If the name is
* relative, the system property <code>user.dir</code> will be
* prepended
* @throws IOException
* when writing to that file fails
*/
public void save(String authFile) throws IOException {
File file = new File(authFile);
if (!file.isAbsolute()) {
file = new File(// $NON-NLS-1$
System.getProperty("user.dir") + File.separator + authFile);
}
try (PrintWriter writer = new PrintWriter(new FileWriter(file))) {
// TODO Charset ?
// $NON-NLS-1$
writer.println("# JMeter generated Cookie file");
long now = System.currentTimeMillis();
for (JMeterProperty jMeterProperty : getCookies()) {
Cookie cook = (Cookie) jMeterProperty.getObjectValue();
final long expiresMillis = cook.getExpiresMillis();
if (expiresMillis == 0 || expiresMillis > now) {
// only save unexpired cookies
writer.println(cookieToString(cook));
}
}
writer.flush();
}
}
use of org.apache.jmeter.testelement.property.JMeterProperty in project jmeter by apache.
the class TestLoopController method testBug54467.
@Test
public void testBug54467() throws Exception {
JMeterContext jmctx = JMeterContextService.getContext();
LoopController loop = new LoopController();
Map<String, String> variables = new HashMap<>();
ReplaceStringWithFunctions transformer = new ReplaceStringWithFunctions(new CompoundVariable(), variables);
jmctx.setVariables(new JMeterVariables());
StringProperty prop = new StringProperty(LoopController.LOOPS, "${__Random(1,12,)}");
JMeterProperty newProp = transformer.transformValue(prop);
newProp.setRunningVersion(true);
loop.setProperty(newProp);
loop.addTestElement(new TestSampler("random run"));
loop.setRunningVersion(true);
loop.initialize();
int loops = loop.getLoops();
for (int i = 0; i < loops; i++) {
Sampler s = loop.next();
assertNotNull(s);
}
assertNull(loop.next());
}
use of org.apache.jmeter.testelement.property.JMeterProperty in project jmeter by apache.
the class TestValueReplacer method testReverseReplacementXml.
@Test
public void testReverseReplacementXml() throws Exception {
ValueReplacer replacer = new ValueReplacer(variables);
assertTrue(variables.getUserDefinedVariables().containsKey("bounded_regex"));
assertTrue(variables.getUserDefinedVariables().containsKey("normal_regex"));
assertTrue(replacer.containsKey("bounded_regex"));
assertTrue(replacer.containsKey("normal_regex"));
TestElement element = new TestPlan();
element.setProperty(new StringProperty("domain", "<this><is>xml</this></is>"));
List<Object> argsin = new ArrayList<>();
argsin.add("<this><is>xml</this></is>");
argsin.add("And I say: Hello World.");
element.setProperty(new CollectionProperty("args", argsin));
replacer.reverseReplace(element, true);
@SuppressWarnings("unchecked") List<JMeterProperty> args = (List<JMeterProperty>) element.getProperty("args").getObjectValue();
assertEquals("${bounded_regex}", element.getPropertyAsString("domain"));
assertEquals("${bounded_regex}", args.get(0).getStringValue());
}
use of org.apache.jmeter.testelement.property.JMeterProperty in project jmeter by apache.
the class TestSwitchController method testFunction.
/*
* N.B. Requires ApacheJMeter_functions.jar to be on the classpath,
* otherwise the function cannot be resolved.
*/
@Test
public void testFunction() throws Exception {
JMeterContext jmctx = JMeterContextService.getContext();
Map<String, String> variables = new HashMap<>();
ReplaceStringWithFunctions transformer = new ReplaceStringWithFunctions(new CompoundVariable(), variables);
jmctx.setVariables(new JMeterVariables());
JMeterVariables jmvars = jmctx.getVariables();
jmvars.put("VAR", "100");
StringProperty prop = new StringProperty(SwitchController.SWITCH_VALUE, "${__counter(TRUE,VAR)}");
JMeterProperty newProp = transformer.transformValue(prop);
newProp.setRunningVersion(true);
GenericController controller = new GenericController();
SwitchController switch_cont = new SwitchController();
switch_cont.setProperty(newProp);
controller.addTestElement(new TestSampler("before"));
controller.addTestElement(switch_cont);
switch_cont.addTestElement(new TestSampler("0"));
switch_cont.addTestElement(new TestSampler("1"));
switch_cont.addTestElement(new TestSampler("2"));
switch_cont.addTestElement(new TestSampler("3"));
controller.addTestElement(new TestSampler("after"));
controller.initialize();
assertEquals("100", jmvars.get("VAR"));
for (int i = 1; i <= 3; i++) {
assertEquals("Loop " + i, "before", nextName(controller));
assertEquals("Loop " + i, "" + i, nextName(controller));
assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
assertEquals("Loop " + i, "after", nextName(controller));
assertNull(nextName(controller));
}
int i = 4;
assertEquals("Loop " + i, "before", nextName(controller));
assertEquals("Loop " + i, "0", nextName(controller));
assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
assertEquals("Loop " + i, "after", nextName(controller));
assertNull(nextName(controller));
assertEquals("4", jmvars.get("VAR"));
}
use of org.apache.jmeter.testelement.property.JMeterProperty in project jmeter by apache.
the class PackageTest method testParseExample14.
// Escaped dollar commma and backslash with missing function reference
@Test
public void testParseExample14() throws Exception {
StringProperty prop = new StringProperty("html", "\\$a \\, \\\\ \\x \\ ${__missing(a)} \\$b \\, \\\\ cd");
JMeterProperty newProp = transformer.transformValue(prop);
newProp.setRunningVersion(true);
// N.B. Backslashes are removed before dollar, comma and backslash
assertEquals("$a , \\ \\x \\ ${__missing(a)} $b , \\ cd", newProp.getStringValue());
}
Aggregations