From aa13ae939f681590d4d2a8b99b3163b53cefdc0f Mon Sep 17 00:00:00 2001 From: Felix Schumacher Date: Wed, 13 Apr 2022 12:35:10 +0200 Subject: [PATCH] Further silence error prone warnings Replace JdkObsolete with JavaUtilDate where we can't replace usage of new Date() Use Instant and other newer APIs from java.time, where we can replace Date() without changing our API. Part of #708 --- .../jmeter/engine/DistributedRunner.java | 20 ++++++++++--- .../jmeter/engine/StandardJMeterEngine.java | 28 ++++++++++++++----- .../apache/jmeter/gui/util/JDateField.java | 4 +-- .../org/apache/jmeter/visualizers/Sample.java | 2 +- .../jmeter/visualizers/TableSample.java | 2 +- 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/core/src/main/java/org/apache/jmeter/engine/DistributedRunner.java b/src/core/src/main/java/org/apache/jmeter/engine/DistributedRunner.java index 5ed86cbfc90..1ea5bca7bb1 100644 --- a/src/core/src/main/java/org/apache/jmeter/engine/DistributedRunner.java +++ b/src/core/src/main/java/org/apache/jmeter/engine/DistributedRunner.java @@ -22,11 +22,15 @@ import java.io.PrintStream; import java.rmi.NotBoundException; import java.rmi.RemoteException; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; import java.util.ArrayList; import java.util.Collection; -import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Properties; @@ -116,15 +120,23 @@ public void init(List addresses, HashTree tree) { } } + private static String formatLikeDate(Instant instant) { + return DateTimeFormatter + .ofLocalizedDateTime(FormatStyle.LONG) + .withLocale(Locale.ROOT) + .withZone(ZoneId.systemDefault()) + .format(instant); + } + /** * Starts a remote testing engines * * @param addresses list of the DNS names or IP addresses of the remote testing engines */ - @SuppressWarnings("JdkObsolete") public void start(List addresses) { - long now = System.currentTimeMillis(); - println("Starting distributed test with remote engines: " + addresses + " @ " + new Date(now) + " (" + now + ")"); + Instant now = Instant.now(); + println("Starting distributed test with remote engines: " + + addresses + " @ " + formatLikeDate(now) + " (" + now.toEpochMilli() + ')'); List startedEngines = new ArrayList<>(addresses.size()); List failedEngines = new ArrayList<>(addresses.size()); for (String address : addresses) { diff --git a/src/core/src/main/java/org/apache/jmeter/engine/StandardJMeterEngine.java b/src/core/src/main/java/org/apache/jmeter/engine/StandardJMeterEngine.java index aa1aae664bd..a2a878341b2 100644 --- a/src/core/src/main/java/org/apache/jmeter/engine/StandardJMeterEngine.java +++ b/src/core/src/main/java/org/apache/jmeter/engine/StandardJMeterEngine.java @@ -17,10 +17,14 @@ package org.apache.jmeter.engine; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; import java.util.ArrayList; -import java.util.Date; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Properties; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.TimeUnit; @@ -171,11 +175,12 @@ public void configure(HashTree testTree) { } @Override - @SuppressWarnings("JdkObsolete") public void runTest() throws JMeterEngineException { if (host != null){ - long now=System.currentTimeMillis(); - System.out.println("Starting the test on host " + host + " @ "+new Date(now)+" ("+now+")"); // NOSONAR Intentional + Instant now = Instant.now(); + String nowAsString = formatLikeDate(now); + System.out.println("Starting the test on host " // NOSONAR Intentional + + host + " @ " + nowAsString + " (" + now.toEpochMilli() + ')'); } try { Thread runningThread = new Thread(this, "StandardJMeterEngine"); @@ -186,6 +191,14 @@ public void runTest() throws JMeterEngineException { } } + private String formatLikeDate(Instant instant) { + return DateTimeFormatter + .ofLocalizedDateTime(FormatStyle.LONG) + .withLocale(Locale.ROOT) + .withZone(ZoneId.systemDefault()) + .format(instant); + } + private void removeThreadGroups(List elements) { Iterator iter = elements.iterator(); while (iter.hasNext()) { // Can't use for loop here because we remove elements @@ -209,7 +222,6 @@ private void notifyTestListenersOfStart(SearchByClass testLis } } - @SuppressWarnings("JdkObsolete") private void notifyTestListenersOfEnd(SearchByClass testListeners) { log.info("Notifying test listeners of end of test"); for (TestStateListener tl : testListeners.getSearchResults()) { @@ -225,8 +237,10 @@ private void notifyTestListenersOfEnd(SearchByClass testListe } if (host != null) { log.info("Test has ended on host {} ", host); - long now=System.currentTimeMillis(); - System.out.println("Finished the test on host " + host + " @ "+new Date(now)+" ("+now+")" // NOSONAR Intentional + Instant now = Instant.now(); + String nowAsString = formatLikeDate(now); + System.out.println("Finished the test on host " // NOSONAR Intentional + + host + " @ " + nowAsString + " (" + now.toEpochMilli() + ')' +(EXIT_AFTER_TEST ? " - exit requested." : "")); if (EXIT_AFTER_TEST){ exit(); diff --git a/src/core/src/main/java/org/apache/jmeter/gui/util/JDateField.java b/src/core/src/main/java/org/apache/jmeter/gui/util/JDateField.java index 813310b1a91..27b15538eae 100644 --- a/src/core/src/main/java/org/apache/jmeter/gui/util/JDateField.java +++ b/src/core/src/main/java/org/apache/jmeter/gui/util/JDateField.java @@ -93,7 +93,7 @@ public JDateField(Date date) { } // Dummy constructor to allow JUnit tests to work - @SuppressWarnings("JdkObsolete") + @SuppressWarnings("JavaUtilDate") public JDateField() { this(new Date()); } @@ -113,7 +113,7 @@ public void setDate(Date date) { * * @return The currently set date */ - @SuppressWarnings("JdkObsolete") + @SuppressWarnings("JavaUtilDate") public Date getDate() { try { return dateFormat.parse(getText()); diff --git a/src/core/src/main/java/org/apache/jmeter/visualizers/Sample.java b/src/core/src/main/java/org/apache/jmeter/visualizers/Sample.java index bb0fec28456..c3ce2d3ed21 100644 --- a/src/core/src/main/java/org/apache/jmeter/visualizers/Sample.java +++ b/src/core/src/main/java/org/apache/jmeter/visualizers/Sample.java @@ -208,7 +208,7 @@ public long getStartTime() { * @return the start time using the specified format * Intended for use from Functors */ - @SuppressWarnings("JdkObsolete") + @SuppressWarnings("JavaUtilDate") public String getStartTimeFormatted(Format format) { return format.format(new Date(getStartTime())); } diff --git a/src/core/src/main/java/org/apache/jmeter/visualizers/TableSample.java b/src/core/src/main/java/org/apache/jmeter/visualizers/TableSample.java index 9c7f62efc42..c8623157463 100644 --- a/src/core/src/main/java/org/apache/jmeter/visualizers/TableSample.java +++ b/src/core/src/main/java/org/apache/jmeter/visualizers/TableSample.java @@ -107,7 +107,7 @@ public long getStartTime() { * @return the start time using the specified format * Intended for use from Functors */ - @SuppressWarnings("JdkObsolete") + @SuppressWarnings("JavaUtilDate") public String getStartTimeFormatted(Format format) { return format.format(new Date(getStartTime())); }