From d27f52d57bbbe5e2d67bd092aaaeb9acf25eacd6 Mon Sep 17 00:00:00 2001 From: Vadim Tkachenko Date: Tue, 27 Feb 2024 19:35:47 -0700 Subject: [PATCH] Fix and test case for #306 --- .../net/sf/dz3r/common/DurationParser.java | 34 +++++++++++++++ .../sf/dz3r/counter/FileTimeUsageCounter.java | 4 +- .../sf/dz3r/common/DurationParserTest.java | 42 +++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 modules/hcc-common/src/main/java/net/sf/dz3r/common/DurationParser.java create mode 100644 modules/hcc-common/src/test/java/net/sf/dz3r/common/DurationParserTest.java diff --git a/modules/hcc-common/src/main/java/net/sf/dz3r/common/DurationParser.java b/modules/hcc-common/src/main/java/net/sf/dz3r/common/DurationParser.java new file mode 100644 index 000000000..fd336c537 --- /dev/null +++ b/modules/hcc-common/src/main/java/net/sf/dz3r/common/DurationParser.java @@ -0,0 +1,34 @@ +package net.sf.dz3r.common; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.time.Duration; +import java.time.format.DateTimeParseException; + +public class DurationParser { + + private final Logger logger = LogManager.getLogger(); + + public Duration parse(String source) { + + try { + return Duration.parse(source); + } catch (DateTimeParseException ex) { + try { + logger.warn("'{}' doesn't parse, trying PT{}", source, source); + return Duration.parse("PT" + source); + } catch (DateTimeParseException ex2) { + try { + logger.warn("'PT{}' doesn't parse, trying {} hours", source, source); + return Duration.ofHours(Long.parseLong(source)); + } catch (NumberFormatException ex3) { + + // Enough is enough + logger.warn("Failed to parse '{}' as a) Duration, b) PT$duration, c) hours; giving up and returning 0", source); + return Duration.ZERO; + } + } + } + } +} diff --git a/modules/hcc-common/src/main/java/net/sf/dz3r/counter/FileTimeUsageCounter.java b/modules/hcc-common/src/main/java/net/sf/dz3r/counter/FileTimeUsageCounter.java index c1b1e3c60..821b8b06e 100644 --- a/modules/hcc-common/src/main/java/net/sf/dz3r/counter/FileTimeUsageCounter.java +++ b/modules/hcc-common/src/main/java/net/sf/dz3r/counter/FileTimeUsageCounter.java @@ -1,5 +1,6 @@ package net.sf.dz3r.counter; +import net.sf.dz3r.common.DurationParser; import net.sf.dz3r.common.HCCObjects; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -27,6 +28,7 @@ public class FileTimeUsageCounter implements ResourceUsageCounter, AutoCloseable { private final Logger logger = LogManager.getLogger(); + private final DurationParser durationParser = new DurationParser(); private static final String CF_THRESHOLD = "threshold"; private static final String CF_CURRENT = "current"; @@ -123,7 +125,7 @@ private TimeUsageCounter load(File source) throws IOException { } var threshold = Optional.ofNullable(thresholdString).map(Duration::parse).orElse(Duration.ZERO); - var current = Duration.parse(currentString); + var current = durationParser.parse(currentString); return new TimeUsageCounter(current, threshold); } diff --git a/modules/hcc-common/src/test/java/net/sf/dz3r/common/DurationParserTest.java b/modules/hcc-common/src/test/java/net/sf/dz3r/common/DurationParserTest.java new file mode 100644 index 000000000..6966713fb --- /dev/null +++ b/modules/hcc-common/src/test/java/net/sf/dz3r/common/DurationParserTest.java @@ -0,0 +1,42 @@ +package net.sf.dz3r.common; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import java.time.Duration; +import java.util.stream.Stream; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +class DurationParserTest { + + private final DurationParser dp = new DurationParser(); + + @ParameterizedTest + @MethodSource("durationProvider") + void parse(String2Duration source) { + + var actual = dp.parse(source.s); + + assertThat(actual).isEqualTo(source.d); + } + + private static Stream durationProvider() { + return Stream.of( + new String2Duration("PT200H", Duration.ofHours(200)), + new String2Duration("PT200", Duration.ofHours(0)), + new String2Duration("200H", Duration.ofHours(200)), + new String2Duration("200M", Duration.ofMinutes(200)), + new String2Duration("200S", Duration.ofSeconds(200)), + new String2Duration("40H3M4S", Duration.parse("PT40H3M4S")), + new String2Duration("200", Duration.ofHours(200)), + new String2Duration("0", Duration.ofHours(0)), + new String2Duration("huh?", Duration.ofHours(0)) + ); + } + + private record String2Duration( + String s, + Duration d + ) {} +}