You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by fs...@apache.org on 2022/04/18 15:41:41 UTC

[jmeter] 02/16: Get rid of error prone warnings because of missing explicit default character encoding

This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git

commit 1e93c67a59673fe4d71a3a710c80bb485965b355
Author: Felix Schumacher <fe...@internetallee.de>
AuthorDate: Mon Apr 18 15:46:26 2022 +0200

    Get rid of error prone warnings because of missing explicit default character encoding
    
    While we are in the sources, use try-with, isEmpty and other minor clean-ups.
    Don't encode ASCII printable characters as unicode sequences, as they are
    decoded by Java anyway and are obfuscating the code unnecessarily.
    Remove unused method variables and parameters, when possible.
    Use ArrayList instead of LinkedList, when possible (more will follow).
---
 .../jmeter/assertions/TestJSONPathAssertion.java   | 40 ++++++++++++----------
 .../jmeter/assertions/XMLSchemaAssertionTest.java  | 31 ++++++++++-------
 .../test/java/org/apache/jmeter/JMeterTest.java    | 11 ++++--
 .../apache/jmeter/junit/JMeterTestCaseJUnit.java   |  4 +--
 .../jmeter/resources/ResourceKeyUsageTest.java     |  6 ++--
 .../org/apache/jmeter/save/TestSaveService.java    | 10 +++---
 .../protocol/http/control/TestAuthManager.java     | 12 +++++--
 .../protocol/http/proxy/TestHttpRequestHdr.java    | 23 ++++++-------
 .../protocol/http/sampler/PostWriterTest.java      | 22 +++++++-----
 .../TestHTTPSamplersAgainstHttpMirrorServer.java   |  7 ++--
 10 files changed, 97 insertions(+), 69 deletions(-)

diff --git a/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java b/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java
index 125662f790..e9349efb1e 100644
--- a/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java
+++ b/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java
@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.nio.charset.Charset;
 import java.util.Locale;
 
 import org.apache.jmeter.samplers.SampleResult;
@@ -86,7 +87,7 @@ class TestJSONPathAssertion {
     }, delimiterString=";")
     void testGetResult_pathsWithOneResult(String data, String jsonPath, String expectedResult) {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData(data.getBytes());
+        samplerResult.setResponseData(data.getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath(jsonPath);
@@ -101,7 +102,7 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_positive_regexp() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
+        samplerResult.setResponseData("{\"myval\": 123}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval");
@@ -112,7 +113,7 @@ class TestJSONPathAssertion {
         assertEquals(expResult.getName(), result.getName());
         assertFalse(result.isFailure());
 
-        samplerResult.setResponseData("{\"myval\": 456}".getBytes());
+        samplerResult.setResponseData("{\"myval\": 456}".getBytes(Charset.defaultCharset()));
         AssertionResult result2 = instance.getResult(samplerResult);
         assertFalse(result2.isFailure());
     }
@@ -120,7 +121,7 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_positive_invert() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
+        samplerResult.setResponseData("{\"myval\": 123}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval");
@@ -136,7 +137,7 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_not_regexp() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": \"some complicated value\"}".getBytes());
+        samplerResult.setResponseData("{\"myval\": \"some complicated value\"}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval");
@@ -153,7 +154,8 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_complex_map() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": { \"a\": 23, \"b\": 42, \"c\": \"something\" } }".getBytes());
+        samplerResult.setResponseData(
+                "{\"myval\": { \"a\": 23, \"b\": 42, \"c\": \"something\" } }".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval");
@@ -167,7 +169,7 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_negative() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
+        samplerResult.setResponseData("{\"myval\": 123}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval");
@@ -182,7 +184,7 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_negative_invert() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
+        samplerResult.setResponseData("{\"myval\": 123}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval");
@@ -198,7 +200,7 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_null() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": null}".getBytes());
+        samplerResult.setResponseData("{\"myval\": null}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval");
@@ -213,7 +215,7 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_null_not_found() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": 123}".getBytes());
+        samplerResult.setResponseData("{\"myval\": 123}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval");
@@ -228,7 +230,7 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_null_novalidate() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": null}".getBytes());
+        samplerResult.setResponseData("{\"myval\": null}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval");
@@ -242,7 +244,7 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_no_such_path() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": null}".getBytes());
+        samplerResult.setResponseData("{\"myval\": null}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.notexist");
@@ -256,7 +258,8 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_list_negative() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": [{\"test\":1},{\"test\":2},{\"test\":3}]}".getBytes());
+        samplerResult.setResponseData(
+                "{\"myval\": [{\"test\":1},{\"test\":2},{\"test\":3}]}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval[*].test");
@@ -274,7 +277,7 @@ class TestJSONPathAssertion {
         // we now consider an indefinite path with no assertion value
         // an error and set the AssertionResult to failure
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": []}".getBytes());
+        samplerResult.setResponseData("{\"myval\": []}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval[*]");
@@ -288,7 +291,7 @@ class TestJSONPathAssertion {
     @Test
     void testGetResult_inverted_null() {
         SampleResult samplerResult = new SampleResult();
-        samplerResult.setResponseData("{\"myval\": [{\"key\": null}]}".getBytes());
+        samplerResult.setResponseData("{\"myval\": [{\"key\": null}]}".getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.myval[*].key");
@@ -305,7 +308,7 @@ class TestJSONPathAssertion {
     void testGetResult_match_msg_problem() {
         SampleResult samplerResult = new SampleResult();
         String str = "{\"execution\":[{\"scenario\":{\"requests\":[{\"headers\":{\"headerkey\":\"header value\"}}]}}]}";
-        samplerResult.setResponseData(str.getBytes());
+        samplerResult.setResponseData(str.getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.execution[0].scenario.requests[0].headers");
@@ -338,7 +341,7 @@ class TestJSONPathAssertion {
                 "   }\n" +
                 " }\n" +
                 "}";
-        samplerResult.setResponseData(str.getBytes());
+        samplerResult.setResponseData(str.getBytes(Charset.defaultCharset()));
 
         JSONPathAssertion instance = new JSONPathAssertion();
         instance.setJsonPath("$.contact.info.ngn_number");
@@ -361,7 +364,8 @@ class TestJSONPathAssertion {
             Locale.setDefault(Locale.US);
             SampleResult samplerResult = new SampleResult();
 
-            samplerResult.setResponseData("{\"myval\": [{\"test\":0.0000123456789}]}".getBytes());
+            samplerResult.setResponseData(
+                    "{\"myval\": [{\"test\":0.0000123456789}]}".getBytes(Charset.defaultCharset()));
 
             JSONPathAssertion instance = new JSONPathAssertion();
             instance.setJsonPath("$.myval[*].test");
diff --git a/src/components/src/test/java/org/apache/jmeter/assertions/XMLSchemaAssertionTest.java b/src/components/src/test/java/org/apache/jmeter/assertions/XMLSchemaAssertionTest.java
index ca3143734f..236625a1b5 100644
--- a/src/components/src/test/java/org/apache/jmeter/assertions/XMLSchemaAssertionTest.java
+++ b/src/components/src/test/java/org/apache/jmeter/assertions/XMLSchemaAssertionTest.java
@@ -25,6 +25,7 @@ import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.jmeter.junit.JMeterTestCase;
 import org.apache.jmeter.samplers.SampleResult;
@@ -52,19 +53,21 @@ public class XMLSchemaAssertionTest extends JMeterTestCase {
     }
 
     private ByteArrayOutputStream readBA(String name) throws IOException {
-        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(findTestFile(name)));
         ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
-        int len = 0;
-        byte[] data = new byte[512];
-        while ((len = bis.read(data)) >= 0) {
-            baos.write(data, 0, len);
+        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(findTestFile(name)))) {
+            int len = 0;
+            byte[] data = new byte[512];
+            while ((len = bis.read(data)) >= 0) {
+                baos.write(data, 0, len);
+            }
         }
-        bis.close();
         return baos;
     }
 
     private byte[] readFile(String name) throws IOException {
-        return readBA(name).toByteArray();
+        try (ByteArrayOutputStream baos = readBA(name)) {
+            return baos.toByteArray();
+        }
     }
 
     @Test
@@ -151,9 +154,10 @@ public class XMLSchemaAssertionTest extends JMeterTestCase {
 
     @Test
     public void testXMLTrailingContent() throws Exception {
-        ByteArrayOutputStream baos = readBA("testfiles/XMLSchematest.xml");
-        baos.write("extra".getBytes()); // TODO - charset?
-        result.setResponseData(baos.toByteArray());
+        try (ByteArrayOutputStream baos = readBA("testfiles/XMLSchematest.xml")) {
+            baos.write("extra".getBytes(StandardCharsets.UTF_8));
+            result.setResponseData(baos.toByteArray());
+        }
         assertion.setXsdFileName(findTestPath("testfiles/XMLSchema-pass.xsd"));
         AssertionResult res = assertion.getResult(jmctx.getPreviousResult());
         testLog.debug("isError() " + res.isError() + " isFailure() " + res.isFailure());
@@ -165,9 +169,10 @@ public class XMLSchemaAssertionTest extends JMeterTestCase {
 
     @Test
     public void testXMLTrailingWhitespace() throws Exception {
-        ByteArrayOutputStream baos = readBA("testfiles/XMLSchematest.xml");
-        baos.write(" \t\n".getBytes()); // TODO - charset?
-        result.setResponseData(baos.toByteArray());
+        try (ByteArrayOutputStream baos = readBA("testfiles/XMLSchematest.xml")) {
+            baos.write(" \t\n".getBytes(StandardCharsets.UTF_8));
+            result.setResponseData(baos.toByteArray());
+        }
         assertion.setXsdFileName(findTestPath("testfiles/XMLSchema-pass.xsd"));
         AssertionResult res = assertion.getResult(jmctx.getPreviousResult());
         testLog.debug("xisError() " + res.isError() + " isFailure() " + res.isFailure());
diff --git a/src/core/src/test/java/org/apache/jmeter/JMeterTest.java b/src/core/src/test/java/org/apache/jmeter/JMeterTest.java
index b70e5a29dc..77fec89f51 100644
--- a/src/core/src/test/java/org/apache/jmeter/JMeterTest.java
+++ b/src/core/src/test/java/org/apache/jmeter/JMeterTest.java
@@ -22,8 +22,11 @@ import static org.junit.Assert.fail;
 
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.jmeter.junit.JMeterTestCase;
 import org.apache.jmeter.report.config.ConfigurationException;
@@ -59,7 +62,8 @@ public class JMeterTest extends JMeterTestCase implements JMeterSerialTest {
                 + "        <collectionProp name=\"Arguments.arguments\"/>\n" + "      </elementProp>\n"
                 + "      <stringProp name=\"TestPlan.user_define_classpath\"></stringProp></TestPlan>"
                 + "    <hashTree/></hashTree></jmeterTestPlan>";
-        try (FileWriter fw = new FileWriter(temp);
+        try (FileOutputStream os = new FileOutputStream(temp);
+                Writer fw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
                 BufferedWriter out = new BufferedWriter(fw)) {
             out.write(testPlan);
         }
@@ -102,7 +106,8 @@ public class JMeterTest extends JMeterTestCase implements JMeterSerialTest {
                 + "          <stringProp name=\"CONNECT\">${__Random(1,5)}</stringProp>\n"
                 + "        </kg.apc.jmeter.samplers.DummySampler></hashTree></hashTree>\n"
                 + "  </hashTree></jmeterTestPlan><hashTree/></hashTree>\n" + "</jmeterTestPlan>";
-        try (FileWriter fw = new FileWriter(temp);
+        try (FileOutputStream os = new FileOutputStream(temp);
+                Writer fw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
                 BufferedWriter out = new BufferedWriter(fw)) {
             out.write(testPlan);
         }
diff --git a/src/core/src/test/java/org/apache/jmeter/junit/JMeterTestCaseJUnit.java b/src/core/src/test/java/org/apache/jmeter/junit/JMeterTestCaseJUnit.java
index 41b2f146ee..a653f95cab 100644
--- a/src/core/src/test/java/org/apache/jmeter/junit/JMeterTestCaseJUnit.java
+++ b/src/core/src/test/java/org/apache/jmeter/junit/JMeterTestCaseJUnit.java
@@ -21,8 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.File;
 import java.nio.charset.Charset;
+import java.util.ArrayList;
 import java.util.Collection;
-import java.util.LinkedList;
 import java.util.Locale;
 import java.util.MissingResourceException;
 
@@ -110,7 +110,7 @@ public abstract class JMeterTestCaseJUnit extends TestCase {
 
     protected void checkInvalidParameterCounts(AbstractFunction func, int minimum)
             throws Exception {
-        Collection<CompoundVariable> parms = new LinkedList<>();
+        Collection<CompoundVariable> parms = new ArrayList<>();
         for (int c = 0; c < minimum; c++) {
             assertThrows(InvalidVariableException.class, () -> func.setParameters(parms));
             parms.add(new CompoundVariable());
diff --git a/src/core/src/test/java/org/apache/jmeter/resources/ResourceKeyUsageTest.java b/src/core/src/test/java/org/apache/jmeter/resources/ResourceKeyUsageTest.java
index eb9551a643..b9af52655f 100644
--- a/src/core/src/test/java/org/apache/jmeter/resources/ResourceKeyUsageTest.java
+++ b/src/core/src/test/java/org/apache/jmeter/resources/ResourceKeyUsageTest.java
@@ -26,6 +26,7 @@ import java.io.FileReader;
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.MissingResourceException;
@@ -51,6 +52,7 @@ public class ResourceKeyUsageTest {
 
     // Check that calls to getResString use a valid property key name
     @Test
+    @SuppressWarnings("CatchAndPrintStackTrace")
     public void checkResourceReferences() throws Exception {
         String resourceName = "/org/apache/jmeter/resources/messages.properties";
         PropertyResourceBundle messagePRB = getRAS(resourceName);
@@ -66,7 +68,7 @@ public class ResourceKeyUsageTest {
                 if (name.endsWith(".java")) {
                   BufferedReader fileReader = null;
                   try {
-                    fileReader = new BufferedReader(new FileReader(file));
+                    fileReader = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8));
                     String s;
                     while ((s = fileReader.readLine()) != null) {
                         if (s.matches("\\s*//.*")) { // leading comment
@@ -89,7 +91,7 @@ public class ResourceKeyUsageTest {
                         }
                     }
                 } catch (IOException e) {
-                    e.printStackTrace();
+                      e.printStackTrace();
                 } finally {
                     JOrphanUtils.closeQuietly(fileReader);
                 }
diff --git a/src/dist-check/src/test/java/org/apache/jmeter/save/TestSaveService.java b/src/dist-check/src/test/java/org/apache/jmeter/save/TestSaveService.java
index 8c654e9e6c..9b44acc7d7 100644
--- a/src/dist-check/src/test/java/org/apache/jmeter/save/TestSaveService.java
+++ b/src/dist-check/src/test/java/org/apache/jmeter/save/TestSaveService.java
@@ -25,12 +25,13 @@ import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
-import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.nio.charset.Charset;
 import java.util.List;
 
 import org.apache.jmeter.junit.JMeterTestCase;
@@ -128,8 +129,8 @@ public class TestSaveService extends JMeterTestCase {
 
         final FileStats outputStats;
         try (ByteArrayInputStream ins = new ByteArrayInputStream(out.toByteArray());
-                Reader insReader = new InputStreamReader(ins);
-                BufferedReader bufferedReader = new BufferedReader(insReader)) {
+             Reader insReader = new InputStreamReader(ins, Charset.defaultCharset());
+             BufferedReader bufferedReader = new BufferedReader(insReader)) {
             outputStats = computeFileStats(bufferedReader);
         }
         // We only check the length of the result. Comparing the
@@ -167,7 +168,8 @@ public class TestSaveService extends JMeterTestCase {
         if (testFile == null || !testFile.exists()) {
             return FileStats.NO_STATS;
         }
-        try (FileReader fileReader = new FileReader(testFile);
+        try (FileInputStream fis = new FileInputStream(testFile);
+             InputStreamReader fileReader = new InputStreamReader(fis, Charset.defaultCharset());
                 BufferedReader bufferedReader = new BufferedReader(fileReader)) {
             return computeFileStats(bufferedReader);
         }
diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/control/TestAuthManager.java b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/control/TestAuthManager.java
index e3b41ab9b9..eee4a21257 100644
--- a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/control/TestAuthManager.java
+++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/control/TestAuthManager.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue;
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
+import java.nio.charset.Charset;
 import java.nio.file.Files;
 
 import org.apache.jmeter.junit.JMeterTestCase;
@@ -88,7 +89,8 @@ public class TestAuthManager extends JMeterTestCase {
     @Test
     public void testAddFileWithoutDomainAndRealmWithMechanism() throws IOException {
         File authFile = File.createTempFile("auth", ".txt");
-        Files.write(authFile.toPath(), "http://example.com\tuser\tpassword\t\t\tBASIC_DIGEST".getBytes());
+        Files.write(authFile.toPath(),
+                "http://example.com\tuser\tpassword\t\t\tBASIC_DIGEST".getBytes(Charset.defaultCharset()));
         AuthManager manager = new AuthManager();
         manager.addFile(authFile.getAbsolutePath());
         Authorization authForURL = manager.getAuthForURL(new URL("http://example.com"));
@@ -98,7 +100,9 @@ public class TestAuthManager extends JMeterTestCase {
     @Test
     public void testAddFileWithDomainAndRealmAndDefaultMechanism() throws IOException {
         File authFile = File.createTempFile("auth", ".txt");
-        Files.write(authFile.toPath(), "http://example.com\tuser\tpassword\tdomain\tEXAMPLE.COM\tBASIC_DIGEST".getBytes());
+        Files.write(authFile.toPath(),
+                "http://example.com\tuser\tpassword\tdomain\tEXAMPLE.COM\tBASIC_DIGEST"
+                        .getBytes(Charset.defaultCharset()));
         AuthManager manager = new AuthManager();
         manager.addFile(authFile.getAbsolutePath());
         Authorization authForURL = manager.getAuthForURL(new URL("http://example.com"));
@@ -109,7 +113,9 @@ public class TestAuthManager extends JMeterTestCase {
     @Test
     public void testAddFileWithDomainAndRealmAndMechanism() throws IOException {
         File authFile = File.createTempFile("auth", ".txt");
-        Files.write(authFile.toPath(), "http://example.com\tuser\tpassword\tdomain\tEXAMPLE.COM\tKERBEROS".getBytes());
+        Files.write(authFile.toPath(),
+                "http://example.com\tuser\tpassword\tdomain\tEXAMPLE.COM\tKERBEROS"
+                        .getBytes(Charset.defaultCharset()));
         AuthManager manager = new AuthManager();
         manager.addFile(authFile.getAbsolutePath());
         Authorization authForURL = manager.getAuthForURL(new URL("http://example.com"));
diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/proxy/TestHttpRequestHdr.java b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/proxy/TestHttpRequestHdr.java
index 82c5b46bcf..bbfc09e3d2 100644
--- a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/proxy/TestHttpRequestHdr.java
+++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/proxy/TestHttpRequestHdr.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.net.URLEncoder;
+import java.nio.charset.Charset;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
@@ -328,7 +329,7 @@ public class TestHttpRequestHdr extends JMeterTestCase {
         // A HTTP GET request, with UTF-8 encoding
         contentEncoding = "UTF-8";
         param1Value = "yes";
-        param2Value = "0+5 -\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85";
+        param2Value = "0+5 -|\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85";
         param2ValueEncoded = URLEncoder.encode(param2Value, contentEncoding);
         testGetRequest =
             "GET " + url
@@ -394,7 +395,7 @@ public class TestHttpRequestHdr extends JMeterTestCase {
         // A HTTP POST request, with UTF-8 encoding
         contentEncoding = "UTF-8";
         param1Value = "yes";
-        param2Value = "0+5 -\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85";
+        param2Value = "0+5 -|\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85";
         param2ValueEncoded = URLEncoder.encode(param2Value, contentEncoding);
         postBody = "param1=" + param1Value + "&param2=" + param2ValueEncoded + "\r\n";
         testPostRequest =
@@ -525,10 +526,9 @@ public class TestHttpRequestHdr extends JMeterTestCase {
     @Test
     public void testParse1() throws Exception {// no space after :
         HttpRequestHdr req = new HttpRequestHdr();
-        ByteArrayInputStream bis = null;
-        bis = new ByteArrayInputStream("GET xxx HTTP/1.0\r\nname:value \r\n".getBytes("ISO-8859-1"));
-        req.parse(bis);
-        bis.close();
+        try (ByteArrayInputStream bis = new ByteArrayInputStream("GET xxx HTTP/1.0\r\nname:value \r\n".getBytes("ISO-8859-1"))) {
+            req.parse(bis);
+        }
         HeaderManager mgr = req.getHeaderManager();
         Header header;
         mgr.getHeaders();
@@ -541,10 +541,9 @@ public class TestHttpRequestHdr extends JMeterTestCase {
     @Test
     public void testParse2() throws Exception {// spaces after :
         HttpRequestHdr req = new HttpRequestHdr();
-        ByteArrayInputStream bis = null;
-        bis = new ByteArrayInputStream("GET xxx HTTP/1.0\r\nname:           value \r\n".getBytes("ISO-8859-1"));
-        req.parse(bis);
-        bis.close();
+        try (ByteArrayInputStream bis = new ByteArrayInputStream("GET xxx HTTP/1.0\r\nname:           value \r\n".getBytes("ISO-8859-1"))) {
+            req.parse(bis);
+        }
         HeaderManager mgr = req.getHeaderManager();
         Header header;
         mgr.getHeaders();
@@ -679,12 +678,12 @@ public class TestHttpRequestHdr extends JMeterTestCase {
     }
 
     private int getBodyLength(String postBody, String contentEncoding) throws IOException {
-        if(contentEncoding != null && contentEncoding.length() > 0) {
+        if(contentEncoding != null && !contentEncoding.isEmpty()) {
             return postBody.getBytes(contentEncoding).length;
         }
         else {
             // Most browsers use ISO-8859-1 as default encoding, even if spec says UTF-8
-            return postBody.getBytes().length; // TODO - charset?
+            return postBody.getBytes(Charset.defaultCharset()).length; // TODO - charset?
         }
     }
 }
diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/PostWriterTest.java b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/PostWriterTest.java
index e771aefb55..8de0d14c69 100644
--- a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/PostWriterTest.java
+++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/PostWriterTest.java
@@ -32,6 +32,8 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -68,7 +70,8 @@ public class PostWriterTest {
         postWriter=new PostWriter();
 
         // Create the test file content
-        TEST_FILE_CONTENT = "foo content &?=01234+56789-\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052".getBytes(UTF_8);
+        TEST_FILE_CONTENT = "foo content &?=01234+56789-|\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052"
+                .getBytes(UTF_8);
 
         // create a temporary file to make sure we always have a file to give to the PostWriter
         // Whereever we are or Whatever the current path is.
@@ -176,7 +179,7 @@ public class PostWriterTest {
         postWriter.sendPostData(connection, sampler);
 
         checkNoContentType(connection);
-        byte[] expectedUrl = "title=mytitle&description=mydescription".getBytes(); // TODO - charset?
+        byte[] expectedUrl = "title=mytitle&description=mydescription".getBytes(StandardCharsets.UTF_8);
         checkContentLength(connection, expectedUrl.length);
         checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
         expectedUrl = "title=mytitle&description=mydescription".getBytes(UTF_8);
@@ -238,7 +241,7 @@ public class PostWriterTest {
         checkContentLength(connection, TEST_FILE_CONTENT.length);
         checkArraysHaveSameContent(TEST_FILE_CONTENT, connection.getOutputStreamContent());
         // Check that other encoding is not the current encoding
-        checkArraysHaveDifferentContent(new String(TEST_FILE_CONTENT) // TODO - charset?
+        checkArraysHaveDifferentContent(new String(TEST_FILE_CONTENT, Charset.defaultCharset()) // TODO - charset?
             .getBytes(otherEncoding), connection.getOutputStreamContent());
 
         // If we have both file as body, and form data, then only form data will be sent
@@ -249,7 +252,8 @@ public class PostWriterTest {
         postWriter.sendPostData(connection, sampler);
 
         checkNoContentType(connection);
-        byte[] expectedUrl = "title=mytitle&description=mydescription".getBytes(); // TODO - charset?
+        byte[] expectedUrl = "title=mytitle&description=mydescription"
+                .getBytes(Charset.defaultCharset()); // TODO - charset?
         checkContentLength(connection, expectedUrl.length);
         checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
     }
@@ -619,11 +623,11 @@ public class PostWriterTest {
 
     /** setup commons parts of HTTPSampler with a no filename. */
     private void setupNoFilename(HTTPSampler httpSampler) {
-        setupFilepart(sampler, "upload", null, "application/octet-stream");
+        setupFilepart(httpSampler, "upload", null, "application/octet-stream");
     }
 
     private void setupFilepart(HTTPSampler httpSampler) {
-        setupFilepart(sampler, "upload", temporaryFile, "text/plain");
+        setupFilepart(httpSampler, "upload", temporaryFile, "text/plain");
     }
 
     private void setupFilepart(HTTPSampler httpSampler, String fileField, File file, String mimeType) {
@@ -636,7 +640,7 @@ public class PostWriterTest {
     }
 
     private void setupFormData(HTTPSampler httpSampler, String titleValue, String descriptionValue) {
-        setupFormData(sampler, false, titleValue, descriptionValue);
+        setupFormData(httpSampler, false, titleValue, descriptionValue);
     }
 
     private void setupFormData(HTTPSampler httpSampler, boolean isEncoded, String titleValue, String descriptionValue) {
@@ -740,7 +744,7 @@ public class PostWriterTest {
             output.write(titleValue.getBytes(contentEncoding));
         }
         else {
-            output.write(titleValue.getBytes()); // TODO - charset?
+            output.write(titleValue.getBytes(Charset.defaultCharset())); // TODO - charset?
         }
         output.write(CRLF);
         output.write(DASH_DASH);
@@ -762,7 +766,7 @@ public class PostWriterTest {
             output.write(descriptionValue.getBytes(contentEncoding));
         }
         else {
-            output.write(descriptionValue.getBytes()); // TODO - charset?
+            output.write(descriptionValue.getBytes(Charset.defaultCharset())); // TODO - charset?
         }
         output.write(CRLF);
         output.write(DASH_DASH);
diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplersAgainstHttpMirrorServer.java b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplersAgainstHttpMirrorServer.java
index 9b4e0a3801..1ca676f0c6 100644
--- a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplersAgainstHttpMirrorServer.java
+++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplersAgainstHttpMirrorServer.java
@@ -25,6 +25,7 @@ import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.net.URLEncoder;
+import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.Locale;
 import java.util.regex.Matcher;
@@ -117,7 +118,7 @@ public class TestHTTPSamplersAgainstHttpMirrorServer extends JMeterTestCaseJUnit
             protected void setUp() throws Exception {
                 httpServer = TestHTTPMirrorThread.startHttpMirror(MIRROR_PORT);
                 // Create the test file content
-                TEST_FILE_CONTENT = "some foo content &?=01234+56789-\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85"
+                TEST_FILE_CONTENT = "some foo content &?=01234+56789-|\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85"
                         .getBytes(StandardCharsets.UTF_8);
 
                 // create a temporary file to make sure we always have a file to give to the PostWriter
@@ -1457,7 +1458,7 @@ public class TestHTTPSamplersAgainstHttpMirrorServer extends JMeterTestCaseJUnit
         if (contentEncoding != null) {
             output.write(titleValue.getBytes(contentEncoding));
         } else {
-            output.write(titleValue.getBytes()); // TODO - charset?
+            output.write(titleValue.getBytes(Charset.defaultCharset())); // TODO - charset?
         }
         output.write(CRLF);
         output.write(DASH_DASH);
@@ -1479,7 +1480,7 @@ public class TestHTTPSamplersAgainstHttpMirrorServer extends JMeterTestCaseJUnit
         if (contentEncoding != null) {
             output.write(descriptionValue.getBytes(contentEncoding));
         } else {
-            output.write(descriptionValue.getBytes()); // TODO - charset?
+            output.write(descriptionValue.getBytes(Charset.defaultCharset())); // TODO - charset?
         }
         output.write(CRLF);
         output.write(DASH_DASH);