changeset 725:75cf7ccbbff2

VarsExecutor bugfixes + more tests
author Devel 2
date Fri, 01 Dec 2017 13:22:16 +0100
parents 4e3b671d6cdf
children af5daf4a3879
files stress-tester/src/main/java/com/passus/st/vars/VarsExecutor.java stress-tester/src/test/java/com/passus/st/vars/VarsExecutorTest.java stress-tester/src/test/java/com/passus/st/vars/VarsUtilsTest.java
diffstat 3 files changed, 52 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/vars/VarsExecutor.java	Fri Dec 01 13:18:12 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/vars/VarsExecutor.java	Fri Dec 01 13:22:16 2017 +0100
@@ -11,31 +11,32 @@
  */
 public class VarsExecutor {
 
-    public void execute(String content, ByteBuff result, List<Var> vars, Object context) {
-        execute(content.getBytes(DEFAULT_CHARSET), result, vars, context);
+    public boolean execute(String content, ByteBuff result, List<Var> vars, Object context) {
+        return execute(content.getBytes(DEFAULT_CHARSET), result, vars, context);
     }
 
-    public void execute(byte[] data, ByteBuff result, List<Var> vars, Object context) {
-        execute(data, 0, data.length, result, vars, context);
+    public boolean execute(byte[] data, ByteBuff result, List<Var> vars, Object context) {
+        return execute(data, 0, data.length, result, vars, context);
     }
 
-    public void execute(byte[] data, int startOffer, int endOffset, ByteBuff result, List<Var> vars, Object context) {
+    public boolean execute(byte[] data, int startOffer, int endOffset, ByteBuff result, List<Var> vars, Object context) {
+        boolean processed = false;
         if (vars == null || vars.isEmpty()) {
-            return;
+            return processed;
         }
 
         int endIndex = endOffset - 1;
         int offset = 0;
         for (Var var : vars) {
             if (offset > endIndex) {
-                return;
+                return processed;
             }
 
             int varStartIndex = var.getStart();
             int varEndIndex = var.getEnd();
 
             if (varEndIndex > endIndex) {
-                return;
+                return processed;
             }
 
             int length = varStartIndex - offset;
@@ -48,10 +49,20 @@
                 } else {
                     result.append(value.toString().getBytes(DEFAULT_CHARSET));
                 }
+
+                processed = true;
+            } else {
+                result.append(data, varStartIndex, varEndIndex - varStartIndex + 1);
             }
 
             offset = varEndIndex + 1;
         }
+
+        if (processed && offset <= endIndex) {
+            result.append(data, offset, endIndex - offset + 1);
+        }
+
+        return processed;
     }
 
 }
--- a/stress-tester/src/test/java/com/passus/st/vars/VarsExecutorTest.java	Fri Dec 01 13:18:12 2017 +0100
+++ b/stress-tester/src/test/java/com/passus/st/vars/VarsExecutorTest.java	Fri Dec 01 13:22:16 2017 +0100
@@ -6,6 +6,8 @@
 import com.passus.st.filter.HttpMessageFieldExtractorFactory;
 import java.util.List;
 import static org.testng.AssertJUnit.assertEquals;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 /**
@@ -14,16 +16,38 @@
  */
 public class VarsExecutorTest {
 
-    @Test
-    public void testExecute() {
-        String content = "a ${var1} b ${var2} c ${var3}";
-        VarsExecutor executor = new VarsExecutor();
-        VarsCompiler compiler = new VarsCompiler();
+    private final VarsExecutor executor = new VarsExecutor();
+    private final VarsCompiler compiler = new VarsCompiler();
+
+    private final ByteBuff result = new HeapByteBuff();
+
+    @DataProvider(name = "contents")
+    public Object[][] contents() {
+        return new Object[][]{
+            {"${var1}", true, "abcdef"},
+            {"${var1}${var3}", true, "abcdefa"},
+            {"a ${var1} b ${var2}", true, "a abcdef b abc"},
+            {"a ${var1} b ${var2} c ${unresolvable}", true, "a abcdef b abc c ${unresolvable}"},
+            {"a ${var1} ${bad@var%name}", true, "a abcdef ${bad@var%name}"},
+            {"${var1}g", true, "abcdefg"},
+            {"${var", false, null},
+            {"${unresolvable}", false, null}
+        };
+    }
+
+    @AfterMethod
+    public void afterMethod() {
+        result.clear();
+    }
+
+    @Test(dataProvider = "contents")
+    public void testExecute(String content, boolean processed, String expectedResult) {
         List<Var> vars = compiler.search(content);
-        ByteBuff result = new HeapByteBuff();
-
-        executor.execute(content.getBytes(), result, vars, new TestContext());
-        assertEquals("a abcdef b abc c a", result.toString());
+        boolean res = executor.execute(content.getBytes(), result, vars, new TestContext());
+        assertEquals(processed, res);
+        if (res) {
+            assertEquals(expectedResult, result.toString());
+        }
     }
 
     public static class TestContext {
--- a/stress-tester/src/test/java/com/passus/st/vars/VarsUtilsTest.java	Fri Dec 01 13:18:12 2017 +0100
+++ b/stress-tester/src/test/java/com/passus/st/vars/VarsUtilsTest.java	Fri Dec 01 13:22:16 2017 +0100
@@ -1,6 +1,5 @@
 package com.passus.st.vars;
 
-import com.passus.data.ByteString;
 import java.util.Objects;
 import static org.testng.AssertJUnit.*;
 import org.testng.annotations.DataProvider;