jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject svn commit: r1859198 - /jmeter/trunk/test/src/org/apache/jmeter/functions/TestStringtoFile.java
Date Mon, 13 May 2019 19:54:17 GMT
Author: fschumacher
Date: Mon May 13 19:54:17 2019
New Revision: 1859198

URL: http://svn.apache.org/viewvc?rev=1859198&view=rev
Log:
Try to make tests run on the windows jenkins slave again (even when workspace is not cleaned
up)

Modified:
    jmeter/trunk/test/src/org/apache/jmeter/functions/TestStringtoFile.java

Modified: jmeter/trunk/test/src/org/apache/jmeter/functions/TestStringtoFile.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/functions/TestStringtoFile.java?rev=1859198&r1=1859197&r2=1859198&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/functions/TestStringtoFile.java (original)
+++ jmeter/trunk/test/src/org/apache/jmeter/functions/TestStringtoFile.java Mon May 13 19:54:17
2019
@@ -18,28 +18,27 @@
 package org.apache.jmeter.functions;
 
 import java.io.File;
-import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
 import java.util.Collection;
-import java.util.LinkedList;
+import java.util.stream.Collectors;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.jmeter.engine.util.CompoundVariable;
 import org.apache.jmeter.junit.JMeterTestCase;
 import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.services.FileServer;
 import org.apache.jmeter.threads.JMeterContext;
 import org.apache.jmeter.threads.JMeterContextService;
 import org.apache.jmeter.threads.JMeterVariables;
 import org.apache.jmeter.util.JMeterUtils;
-import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Tests for {@link StringToFile}
@@ -47,12 +46,10 @@ import org.slf4j.LoggerFactory;
 public class TestStringtoFile extends JMeterTestCase {
     protected AbstractFunction function;
     private SampleResult result;
-    private Collection<CompoundVariable> params;
-    private static final Logger log = LoggerFactory.getLogger(TestSimpleFunctions.class);
-    private static final String DIR_NAME = "dirTest";
     private static final String FILENAME = "test.txt";
     private static final String STRING_TO_WRITE = "test";
     private static final String ENCODING = StandardCharsets.UTF_8.toString();
+
     @Rule
     public TemporaryFolder tempFolder = new TemporaryFolder();
 
@@ -64,18 +61,6 @@ public class TestStringtoFile extends JM
         JMeterVariables vars = new JMeterVariables();
         jmctx.setVariables(vars);
         jmctx.setPreviousResult(result);
-        params = new LinkedList<>();
-    }
-
-    @Before
-    @After
-    public void deleteFileBeforeAfterTest() {
-        File file = new File(FILENAME);
-        try {
-            Files.deleteIfExists(file.toPath());
-        } catch (IOException e) {
-            Assert.fail("File " + FILENAME + "should not exist");
-        }
     }
 
     @Test
@@ -85,48 +70,33 @@ public class TestStringtoFile extends JM
 
     @Test
     public void testWriteToFile() throws Exception {
-        params.add(new CompoundVariable(FILENAME));
-        params.add(new CompoundVariable(STRING_TO_WRITE));
-        params.add(new CompoundVariable("true"));
-        params.add(new CompoundVariable(ENCODING));
-        function.setParameters(params);
-        String returnValue = function.execute(result, null);
-        Assert.assertTrue("This method 'Stringtofile' should have successfully run",
-                Boolean.parseBoolean(returnValue));
+        try {
+            function.setParameters(functionParams(FILENAME, STRING_TO_WRITE, "true", ENCODING));
+            String returnValue = function.execute(result, null);
+            Assert.assertTrue("This method 'Stringtofile' should have successfully run",
+                    Boolean.parseBoolean(returnValue));
+        } finally {
+            Files.deleteIfExists(new File(FileServer.resolveBaseRelativeName(FILENAME)).toPath());
+        }
     }
 
     @Test
     public void testWriteToFileWhenDirectoryDoesntExist() throws Exception {
-        String pathDirectory = File.separator + DIR_NAME;
-        File dir = new File(pathDirectory);
-        if (dir.exists()) {
-            deleteDir(dir);
-        }
-        String pathname = pathDirectory + File.separator + FILENAME;
-        params.add(new CompoundVariable(pathname));
-        params.add(new CompoundVariable(STRING_TO_WRITE));
-        params.add(new CompoundVariable("true"));
-        params.add(new CompoundVariable(ENCODING));
-        function.setParameters(params);
+        File dir = tempFolder.newFolder();
+        Files.delete(dir.toPath());
+        String pathname = Paths.get(dir.getAbsolutePath(), FILENAME).toString();
+        function.setParameters(functionParams(pathname, STRING_TO_WRITE, "true", ENCODING));
         String returnValue = function.execute(result, null);
         Assert.assertFalse("This method 'Stringtofile' should fail to run since directory
does not exist", 
                 Boolean.parseBoolean(returnValue));
     }
 
     @Test
-    public void testWriteToFileWhenDirectoryExist() throws InvalidVariableException {
-        File dir = null;
-        try {
-            dir = tempFolder.newFolder(DIR_NAME);
-        } catch (IOException e1) {
-            Assert.fail("can't create the directory");
-        }
-        String pathname = dir.getAbsolutePath() + File.separator + FILENAME;
-        params.add(new CompoundVariable(pathname));
-        params.add(new CompoundVariable(STRING_TO_WRITE));
-        params.add(new CompoundVariable("true"));
-        params.add(new CompoundVariable(ENCODING));
-        function.setParameters(params);
+    public void testWriteToFileWhenDirectoryExist() throws Exception {
+        File dir = tempFolder.newFolder();
+        dir.deleteOnExit();
+        String pathname = Paths.get(dir.getAbsolutePath(), FILENAME).toString();
+        function.setParameters(functionParams(pathname, STRING_TO_WRITE, "true", ENCODING));
         String returnValue = function.execute(result, null);
         Assert.assertTrue("This method 'Stringtofile' should have successfully run if parent
directory already exists",
                 Boolean.parseBoolean(returnValue));
@@ -134,39 +104,33 @@ public class TestStringtoFile extends JM
 
     @Test
     public void testWriteToFileOptParamWayToWriteIsNull() throws Exception {
-        params.add(new CompoundVariable(FILENAME));
-        params.add(new CompoundVariable(STRING_TO_WRITE));
-        function.setParameters(params);
-        String returnValue = function.execute(result, null);
-        Assert.assertTrue("This method 'Stringtofile' should have successfully run with empty
append",
-                Boolean.parseBoolean(returnValue));
+        try {
+            function.setParameters(functionParams(FILENAME, STRING_TO_WRITE));
+            String returnValue = function.execute(result, null);
+            Assert.assertTrue("This method 'Stringtofile' should have successfully run with
empty append",
+                    Boolean.parseBoolean(returnValue));
+        } finally {
+            Files.deleteIfExists(new File(FileServer.resolveBaseRelativeName(FILENAME)).toPath());
+        }
     }
 
     @Test
     public void testWriteToFileOptParamEncodingIsNull() throws Exception {
-        params.add(new CompoundVariable(FILENAME));
-        params.add(new CompoundVariable(STRING_TO_WRITE));
-        params.add(new CompoundVariable("true"));
-        function.setParameters(params);
-        String returnValue = function.execute(result, null);
-        Assert.assertTrue("This method 'Stringtofile' should have successfully run with no
charset",
-                Boolean.parseBoolean(returnValue));
-
+        try {
+            function.setParameters(functionParams(FILENAME, STRING_TO_WRITE, "true"));
+            String returnValue = function.execute(result, null);
+            Assert.assertTrue("This method 'Stringtofile' should have successfully run with
no charset",
+                    Boolean.parseBoolean(returnValue));
+        } finally {
+            Files.deleteIfExists(new File(FileServer.resolveBaseRelativeName(FILENAME)).toPath());
+        }
     }
 
     @Test
     public void testWriteToFileEncodingNotSupported() throws Exception {
-        File file = null;
-        try {
-            file = tempFolder.newFile(FILENAME);
-        } catch (IOException e1) {
-            Assert.fail("Can't create the file successfully");
-        }
-        params.add(new CompoundVariable(file.getAbsolutePath()));
-        params.add(new CompoundVariable(STRING_TO_WRITE));
-        params.add(new CompoundVariable("true"));
-        params.add(new CompoundVariable("UTF-20"));
-        function.setParameters(params);
+        File file = tempFolder.newFile();
+        file.deleteOnExit();
+        function.setParameters(functionParams(file.getAbsolutePath(), STRING_TO_WRITE, "true",
"UTF-20"));
         String returnValue = function.execute(result, null);
         Assert.assertFalse("This method 'Stringtofile' should have failed to run with wrong
charset",
                 Boolean.parseBoolean(returnValue));
@@ -174,17 +138,9 @@ public class TestStringtoFile extends JM
 
     @Test
     public void testWriteToFileEncodingNotLegal() throws Exception {
-        File file = null;
-        try {
-            file = tempFolder.newFile(FILENAME);
-        } catch (IOException e1) {
-            Assert.fail("Can't create the file successfully");
-        }
-        params.add(new CompoundVariable(file.getAbsolutePath()));
-        params.add(new CompoundVariable(STRING_TO_WRITE));
-        params.add(new CompoundVariable("true"));
-        params.add(new CompoundVariable("UTFéé"));
-        function.setParameters(params);
+        File file = tempFolder.newFile();
+        file.deleteOnExit();
+        function.setParameters(functionParams(file.getAbsolutePath(), STRING_TO_WRITE, "true",
"UTFéé"));
         String returnValue = function.execute(result, null);
         Assert.assertFalse("This method 'Stringtofile' should have failed to run with illegal
chars in charset",
                 Boolean.parseBoolean(returnValue));
@@ -192,18 +148,10 @@ public class TestStringtoFile extends JM
 
     @Test
     public void testWriteToFileIOException() throws Exception {
-        File file = null;
-        try {
-            file = tempFolder.newFile(FILENAME);
-        } catch (IOException e1) {
-            Assert.fail("Can't create the file successfully");
-        }
-        file.setWritable(false);
-        params.add(new CompoundVariable(file.getAbsolutePath()));
-        params.add(new CompoundVariable(STRING_TO_WRITE));
-        params.add(new CompoundVariable("true"));
-        params.add(new CompoundVariable("UTF-8"));
-        function.setParameters(params);
+        File file = tempFolder.newFile();
+        file.deleteOnExit();
+        Assert.assertTrue(file.getAbsolutePath() + " should be set read-only", file.setWritable(false));
+        function.setParameters(functionParams(file.getAbsolutePath(), STRING_TO_WRITE, "true",
ENCODING));
         String returnValue = function.execute(result, null);
         Assert.assertFalse("This method 'Stringtofile' should have failed to run with non
writable folder",
                 Boolean.parseBoolean(returnValue));
@@ -211,28 +159,16 @@ public class TestStringtoFile extends JM
 
     @Test
     public void testWriteToFileRequiredFilePathIsNull() throws Exception {
-        params.add(new CompoundVariable(null));
-        params.add(new CompoundVariable(STRING_TO_WRITE));
-        params.add(new CompoundVariable("true"));
-        params.add(new CompoundVariable(ENCODING));
-        function.setParameters(params);
+        function.setParameters(functionParams(null, STRING_TO_WRITE, "true", ENCODING));
         String returnValue = function.execute(result, null);
         Assert.assertFalse("This method 'Stringtofile' should fail to run with null file",
Boolean.parseBoolean(returnValue));
     }
 
     @Test
     public void testWriteToFileRequiredStringIsNull() throws Exception {
-        File file = null;
-        try {
-            file = tempFolder.newFile(FILENAME);
-        } catch (IOException e1) {
-            Assert.fail("Can't create the file successfully");
-        }
-        params.add(new CompoundVariable(file.getAbsolutePath()));
-        params.add(new CompoundVariable(""));
-        params.add(new CompoundVariable("true"));
-        params.add(new CompoundVariable(ENCODING));
-        function.setParameters(params);
+        File file = tempFolder.newFile();
+        file.deleteOnExit();
+        function.setParameters(functionParams(file.getAbsolutePath(), "", "true", ENCODING));
         String returnValue = function.execute(result, null);
         Assert.assertTrue("This method 'Stringtofile' should succeed with empty String to
write", 
                 Boolean.parseBoolean(returnValue));
@@ -240,17 +176,9 @@ public class TestStringtoFile extends JM
 
     @Test
     public void testOverwrite() throws Exception {
-        File file = null;
-        try {
-            file = tempFolder.newFile(FILENAME);
-        } catch (IOException e1) {
-            Assert.fail("Can't create the file successfully");
-        }
-        params.add(new CompoundVariable(file.getAbsolutePath()));
-        params.add(new CompoundVariable(STRING_TO_WRITE));
-        params.add(new CompoundVariable("false"));
-        params.add(new CompoundVariable(ENCODING));
-        function.setParameters(params);
+        File file = tempFolder.newFile();
+        file.deleteOnExit();
+        function.setParameters(functionParams(file.getAbsolutePath(), STRING_TO_WRITE, "false",
ENCODING));
         String returnValue = function.execute(result, null);
         Assert.assertTrue("This method 'Stringtofile' should have successfully run",
                 Boolean.parseBoolean(returnValue));
@@ -260,25 +188,23 @@ public class TestStringtoFile extends JM
 
     @Test
     public void testAppend() throws Exception {
-        File file = null;
-        try {
-            file = tempFolder.newFile(FILENAME);
-        } catch (IOException e1) {
-            Assert.fail("Can't create the file successfully");
-        }
-        params.add(new CompoundVariable(file.getAbsolutePath()));
-        params.add(new CompoundVariable(STRING_TO_WRITE));
-        params.add(new CompoundVariable("true"));
-        params.add(new CompoundVariable(ENCODING));
-        function.setParameters(params);
-        String returnValue = function.execute(result, null);
-        returnValue = function.execute(result, null);
-        Assert.assertTrue("This method 'Stringtofile' should have successfully run",
-                Boolean.parseBoolean(returnValue));
+        File file = tempFolder.newFile();
+        file.deleteOnExit();
+        function.setParameters(functionParams(file.getAbsolutePath(), STRING_TO_WRITE, "true",
ENCODING));
+        Assert.assertTrue("First call to 'Stringtofile' should succeed",
+                Boolean.parseBoolean(function.execute(result, null)));
+        Assert.assertTrue("Second call to 'Stringtofile' should succeed",
+                Boolean.parseBoolean(function.execute(result, null)));
         String res = FileUtils.readFileToString(file, ENCODING).trim();
         Assert.assertEquals("The string should be 'testtest'", "testtest", res);
     }
 
+    private Collection<CompoundVariable> functionParams(String... args) {
+        return Arrays.asList(args).stream()
+                .map(CompoundVariable::new)
+                .collect(Collectors.toList());
+    }
+
     @Test
     public void testDescription() {
         Assert.assertEquals("Function 'stringtofile' should have successfully reading the
configuration file 'messages.properties'", 
@@ -286,21 +212,4 @@ public class TestStringtoFile extends JM
                 function.getArgumentDesc().get(0));
     }
 
-    private static boolean deleteDir(File dir) {
-        if (dir.isDirectory()) {
-            String[] children = dir.list();
-            for (int i = 0; i < children.length; i++) {
-                boolean success = deleteDir(new File(dir, children[i]));
-                if (!success) {
-                    return false;
-                }
-            }
-        }
-        try {
-            Files.deleteIfExists(dir.toPath());
-            return true;
-        } catch (IOException e) {
-            return false;
-        }
-    }
 }



Mime
View raw message