Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.eternalcode.commons.time;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class SimpleDurationUtil {

private static final Duration ONE_SECOND = Duration.ofSeconds(1);
private static final String ZERO_SECONDS = "0s";

private static final TemporalAmountParser<Duration> WITHOUT_MILLIS_FORMAT = new DurationParser()
.withUnit("s", ChronoUnit.SECONDS)
.withUnit("m", ChronoUnit.MINUTES)
.withUnit("h", ChronoUnit.HOURS)
.withUnit("d", ChronoUnit.DAYS)
.withUnit("w", ChronoUnit.WEEKS)
.withUnit("mo", ChronoUnit.MONTHS)
.withUnit("y", ChronoUnit.YEARS)
.roundOff(ChronoUnit.MILLIS);

private static final TemporalAmountParser<Duration> STANDARD_FORMAT = new DurationParser()
.withUnit("ms", ChronoUnit.MILLIS)
.withUnit("s", ChronoUnit.SECONDS)
.withUnit("m", ChronoUnit.MINUTES)
.withUnit("h", ChronoUnit.HOURS)
.withUnit("d", ChronoUnit.DAYS)
.withUnit("w", ChronoUnit.WEEKS)
.withUnit("mo", ChronoUnit.MONTHS)
.withUnit("y", ChronoUnit.YEARS);

public static String format(Duration duration, boolean removeMillis) {
if (removeMillis) {
if (duration.toMillis() < ONE_SECOND.toMillis()) {
return "0s";
}

return WITHOUT_MILLIS_FORMAT.format(duration);
}

return STANDARD_FORMAT.format(duration);
}

public static String format(Duration duration) {
return format(duration, false);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.eternalcode.commons.time;

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Duration;
import org.junit.jupiter.api.Test;

public class SimpleDurationUtilTest {

@Test
public void testFormatWithoutMillis() {
Duration duration = Duration.ofMillis(500);
String result = SimpleDurationUtil.format(duration, true);
assertEquals("0s", result);

duration = Duration.ofSeconds(30);
result = SimpleDurationUtil.format(duration, true);
assertEquals("30s", result);

duration = Duration.ofMinutes(5);
result = SimpleDurationUtil.format(duration, true);
assertEquals("5m", result);

duration = Duration.ofHours(2);
result = SimpleDurationUtil.format(duration, true);
assertEquals("2h", result);

duration = Duration.ofDays(1);
result = SimpleDurationUtil.format(duration, true);
assertEquals("1d", result);

duration = Duration.ofDays(14);
result = SimpleDurationUtil.format(duration, true);
assertEquals("2w", result);

duration = Duration.ofDays(60);
result = SimpleDurationUtil.format(duration, true);
assertEquals("2mo", result);

duration = Duration.ofDays(365 * 3);
result = SimpleDurationUtil.format(duration, true);
assertEquals("3y", result);
}
Comment on lines +9 to +42
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve test method clarity and add assertion messages

The test method testFormatWithoutMillis has a few areas for improvement:

  1. The method name suggests testing without milliseconds, but the includeMillis parameter is set to true. This is counterintuitive and may lead to confusion.
  2. The test cases lack assertion messages, which could make debugging failures more difficult.

Consider the following improvements:

  1. Rename the method to testFormatWithoutMillis to accurately reflect its purpose.
  2. Change the SimpleDurationUtil.format(duration, true) calls to SimpleDurationUtil.format(duration, false) to match the method name.
  3. Add descriptive assertion messages to each assertion. For example:
-        assertEquals("0s", result);
+        assertEquals("0s", result, "Duration of 500ms should be formatted as '0s' without milliseconds");

Apply this pattern to all assertions in the method.

Committable suggestion was skipped due to low confidence.


@Test
public void testFormatWithMillis() {
Duration duration = Duration.ofMillis(500);
String result = SimpleDurationUtil.format(duration, false);
assertEquals("500ms", result);

duration = Duration.ofSeconds(30);
result = SimpleDurationUtil.format(duration, false);
assertEquals("30s", result);

duration = Duration.ofMinutes(5);
result = SimpleDurationUtil.format(duration, false);
assertEquals("5m", result);

duration = Duration.ofHours(2);
result = SimpleDurationUtil.format(duration, false);
assertEquals("2h", result);

duration = Duration.ofDays(1);
result = SimpleDurationUtil.format(duration, false);
assertEquals("1d", result);

duration = Duration.ofDays(14);
result = SimpleDurationUtil.format(duration, false);
assertEquals("2w", result);

duration = Duration.ofDays(60);
result = SimpleDurationUtil.format(duration, false);
assertEquals("2mo", result);

duration = Duration.ofDays(365 * 3);
result = SimpleDurationUtil.format(duration, false);
assertEquals("3y", result);
}
Comment on lines +44 to +77
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve test method clarity and add assertion messages

The test method testFormatWithMillis has similar issues to the previous method:

  1. The method name suggests testing with milliseconds, but the includeMillis parameter is set to false. This is counterintuitive and may lead to confusion.
  2. The test cases lack assertion messages, which could make debugging failures more difficult.

Consider the following improvements:

  1. Rename the method to testFormatWithMillis to accurately reflect its purpose.
  2. Change the SimpleDurationUtil.format(duration, false) calls to SimpleDurationUtil.format(duration, true) to match the method name.
  3. Add descriptive assertion messages to each assertion. For example:
-        assertEquals("500ms", result);
+        assertEquals("500ms", result, "Duration of 500ms should be formatted as '500ms' with milliseconds");

Apply this pattern to all assertions in the method.

Committable suggestion was skipped due to low confidence.


@Test
public void testFormatDefault() {
Duration duration = Duration.ofSeconds(610);
String result = SimpleDurationUtil.format(duration);
assertEquals("10m10s", result);

duration = Duration.ofMillis(120);
result = SimpleDurationUtil.format(duration);
assertEquals("120ms", result);
}
}
Comment on lines +1 to +89
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance overall test class structure and coverage

While the test class covers the main functionality of SimpleDurationUtil, there are several areas where it can be improved:

  1. Add a class-level Javadoc comment explaining the purpose of the test class.
  2. Include tests for edge cases and error scenarios.
  3. Improve the organization of test methods.

Consider the following improvements:

  1. Add a class-level Javadoc comment:
/**
 * Test class for SimpleDurationUtil.
 * This class contains unit tests to verify the correct functioning
 * of the SimpleDurationUtil.format method under various scenarios.
 */
public class SimpleDurationUtilTest {
    // ... existing code ...
}
  1. Add tests for edge cases and error scenarios:
@Test
public void testFormatZeroDuration() {
    Duration duration = Duration.ZERO;
    String result = SimpleDurationUtil.format(duration);
    assertEquals("0s", result, "Zero duration should be formatted as '0s'");
}

@Test
public void testFormatNegativeDuration() {
    Duration duration = Duration.ofSeconds(-60);
    String result = SimpleDurationUtil.format(duration);
    assertEquals("-1m", result, "Negative duration should be formatted with a minus sign");
}

@Test
public void testFormatMaxDuration() {
    Duration duration = Duration.ofSeconds(Long.MAX_VALUE);
    String result = SimpleDurationUtil.format(duration);
    // Assert the expected result for maximum duration
}
  1. Improve organization using nested classes or descriptive naming:
class SimpleDurationUtilTest {

    @Nested
    class FormatWithoutMillis {
        @Test
        void shouldFormatSecondsWithoutMillis() {
            // ... test case for seconds ...
        }

        @Test
        void shouldFormatMinutesWithoutMillis() {
            // ... test case for minutes ...
        }

        // ... other test methods ...
    }

    @Nested
    class FormatWithMillis {
        // ... similar structure for with-millis tests ...
    }

    @Nested
    class FormatDefault {
        // ... default format test methods ...
    }

    @Nested
    class EdgeCases {
        // ... edge case test methods ...
    }
}

These improvements will enhance the overall structure, readability, and coverage of the test class.