• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fslev / cucumber-jutils / #86

pending completion
#86

push

web-flow
<a href="https://github.com/fslev/cucumber-jutils/commit/<a class=hub.com/fslev/cucumber-jutils/commit/<a class="double-link" href="https://git"><a class=hub.com/fslev/cucumber-jutils/commit/<a class="double-link" href="https://git"><a class=hub.com/fslev/cucumber-jutils/commit/<a class="double-link" href="https://git"><a class=hub.com/fslev/cucumber-jutils/commit/fb8c760bcaa588360f0c212163bbd959e20b6320">fb8c760bc">&lt;a href=&quot;https://github.com/fslev/cucumber-jutils/commit/</a><a class="double-link" href="https://github.com/fslev/cucumber-jutils/commit/&lt;a class=&quot;double-link&quot; href=&quot;https://git">&lt;a class=</a>hub.com/fslev/cucumber-jutils/commit/&lt;a class=&quot;double-link&quot; href=&quot;https://git">&lt;a class=</a>hub.com/fslev/cucumber-jutils/commit/&lt;a class=&quot;double-link&quot; href=&quot;https://git">&lt;a class=</a>hub.com/fslev/cucumber-jutils/commit/&lt;a class=&quot;double-link&quot; href=&quot;https://git">&lt;a class=</a>hub.com/fslev/cucumber-jutils/commit/fb8c760bcaa588360f0c212163bbd959e20b6320">fb8c760bc</a><a href="https://github.com/fslev/cucumber-jutils/commit/fb8c760bcaa588360f0c212163bbd959e20b6320">&quot;&gt;&amp;lt;a href=&amp;quot;https://github.com/fslev/cucumber-jutils/commit/&lt;/a&gt;&lt;a class=&quot;double-link&quot; href=&quot;https://github.com/fslev/cucumber-jutils/commit/&amp;lt;a class=&amp;quot;double-link&amp;quot; href=&amp;quot;https://git&quot;&gt;&amp;lt;a class=&lt;/a&gt;hub.com/fslev/cucumber-jutils/commit/&amp;lt;a class=&amp;quot;double-link&amp;quot; href=&amp;quot;https://git&quot;&gt;&amp;lt;a class=&lt;/a&gt;hub.com/fslev/cucumber-jutils/commit/&amp;lt;a class=&amp;quot;double-link&amp;quot; href=&amp;quot;https://git&quot;&gt;&amp;lt;a class=&lt;/a&gt;hub.com/fslev/cucumber-jutils/commit/fb8c760bcaa588360f0c212163bbd959e20b6320&quot;&gt;fb8c760bc&lt;/a&gt;&lt;a href=&quot;https://github.com/fslev/cucumber-jutils/commit/fb8c760bcaa588360f0c212163bbd959e20b6320&quot;&gt;&amp;quot;&amp;gt;&amp;amp;lt;a href=&amp;amp;quot;https://github.com/fslev/cucumber-jutils/commit/&amp;lt;/a&amp;gt;&amp;lt;a class=&amp;quot;double-link&amp;quot; href=&amp;quot;https://github.com/fslev/cucumber-jutils/commit/&amp;amp;lt;a class=&amp;amp;quot;double-link&amp;amp;quot; href=&amp;amp;quot;https://git&am... (continued)

268 of 280 relevant lines covered (95.71%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.46
/src/main/java/com/cucumber/utils/context/steps/DateTimeSteps.java
1
package com.cucumber.utils.context.steps;
2

3
import com.cucumber.utils.context.ScenarioUtils;
4
import com.cucumber.utils.context.vars.ScenarioVars;
5
import com.google.inject.Inject;
6
import io.cucumber.guice.ScenarioScoped;
7
import io.cucumber.java.en.Then;
8

9
import java.time.Instant;
10
import java.time.LocalDate;
11
import java.time.ZoneId;
12
import java.time.ZonedDateTime;
13
import java.time.format.DateTimeFormatter;
14
import java.time.temporal.ChronoUnit;
15

16
import static org.junit.jupiter.api.Assertions.assertEquals;
17

18

19
@ScenarioScoped
20
public class DateTimeSteps {
1✔
21
    @Inject
22
    private ScenarioUtils logger;
23
    @Inject
24
    private ScenarioVars scenarioVars;
25

26
    public enum Operation {
1✔
27
        PLUS, MINUS
1✔
28
    }
29

30
    @Then("[time-util] Check period from {} to {} is {} {} using date pattern {}")
31
    public void matchDates(String date1, String date2, long value, ChronoUnit chronoUnit, String pattern) {
32
        logger.log("Check date period from '{}' to '{}' is {}{} using date pattern '{}'", date1, date2, value, chronoUnit, pattern);
1✔
33
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
1✔
34
        LocalDate localDate1 = LocalDate.parse(date1, dateTimeFormatter);
1✔
35
        LocalDate localDate2 = LocalDate.parse(date2, dateTimeFormatter);
1✔
36
        assertEquals(value, chronoUnit.between(localDate1, localDate2), chronoUnit + " differ");
1✔
37
    }
1✔
38

39
    @Then("[time-util] Check period from {} to {} is {} {} using date time pattern {}")
40
    public void matchDateTimes(String date1, String date2, long value, ChronoUnit chronoUnit, String pattern) {
41
        logger.log("Check date period from '{}' to '{}' is {}{} using date time pattern '{}'", date1, date2, value, chronoUnit, pattern);
1✔
42
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern).withZone(ZoneId.systemDefault());
1✔
43
        ZonedDateTime zonedDateTime1 = ZonedDateTime.parse(date1, dateTimeFormatter);
1✔
44
        ZonedDateTime zonedDateTime2 = ZonedDateTime.parse(date2, dateTimeFormatter);
1✔
45
        assertEquals(value, chronoUnit.between(zonedDateTime1, zonedDateTime2), chronoUnit + " differ");
1✔
46
    }
1✔
47

48
    @Then("[time-util] Check period from {} to {} doesn't match {} {} using date pattern {}")
49
    public void negativeMatchDates(String date1, String date2, long value, ChronoUnit chronoUnit, String pattern) {
50
        logger.log("Negative check date period from '{}' to '{}' is {}{} using date pattern '{}'", date1, date2, value, chronoUnit, pattern);
1✔
51
        try {
52
            matchDates(date1, date2, value, chronoUnit, pattern);
×
53
        } catch (AssertionError e) {
1✔
54
            logger.log("Negative match passes {}", e.getMessage());
1✔
55
            return;
1✔
56
        }
×
57
        throw new AssertionError("Compared dates match");
×
58
    }
59

60
    @Then("[time-util] Check period from {} to {} doesn't match {} {} using date time pattern {}")
61
    public void negativeMatchDateTimes(String date1, String date2, long value, ChronoUnit chronoUnit, String pattern) {
62
        logger.log("Negative check date period from '{}' to '{}' is {}{} using date time pattern '{}'", date1, date2, value, chronoUnit, pattern);
1✔
63
        try {
64
            matchDateTimes(date1, date2, value, chronoUnit, pattern);
×
65
        } catch (AssertionError e) {
1✔
66
            logger.log("Negative match passes {}", e.getMessage());
1✔
67
            return;
1✔
68
        }
×
69
        throw new AssertionError("Compared dates match");
×
70
    }
71

72
    @Then("[time-util] date var {}=from millis {} {} {} {} with format pattern={}")
73
    public void setDateVar(String param, Long millis, Operation operation, int value, ChronoUnit chronoUnit, String formatPattern) {
74
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatPattern).withZone(ZoneId.systemDefault());
1✔
75
        switch (operation) {
1✔
76
            case MINUS:
77
                scenarioVars.put(param, ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault())
1✔
78
                        .minus(value, chronoUnit).format(dateTimeFormatter));
1✔
79
                break;
1✔
80
            case PLUS:
81
                scenarioVars.put(param, ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault())
1✔
82
                        .plus(value, chronoUnit).format(dateTimeFormatter));
1✔
83
                break;
84
        }
85
        logger.log("Date var {} = {}", param, scenarioVars.get(param));
1✔
86
    }
1✔
87

88
    @Then("[time-util] date var {}=from date {} {} {} {} with format pattern={}")
89
    public void setDateVar(String param, String date, Operation operation, int value, ChronoUnit chronoUnit, String formatPattern) {
90
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatPattern).withZone(ZoneId.systemDefault());
1✔
91
        switch (operation) {
1✔
92
            case MINUS:
93
                scenarioVars.put(param, ZonedDateTime.parse(date, dateTimeFormatter).minus(value, chronoUnit).format(dateTimeFormatter));
1✔
94
                break;
1✔
95
            case PLUS:
96
                scenarioVars.put(param, ZonedDateTime.parse(date, dateTimeFormatter).plus(value, chronoUnit).format(dateTimeFormatter));
1✔
97
                break;
98
        }
99
        logger.log("Date var {} = {}", param, scenarioVars.get(param));
1✔
100
    }
1✔
101

102
    @Then("[time-util] date millis var {}=from date {} {} {} {} with format pattern={}")
103
    public void setDateInMillisParam(String param, String date, Operation operation, int value, ChronoUnit chronoUnit, String formatPattern) {
104
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatPattern).withZone(ZoneId.systemDefault());
1✔
105
        switch (operation) {
1✔
106
            case MINUS:
107
                scenarioVars.put(param, ZonedDateTime.parse(date, dateTimeFormatter).minus(value, chronoUnit).toInstant().toEpochMilli());
1✔
108
                break;
1✔
109
            case PLUS:
110
                scenarioVars.put(param, ZonedDateTime.parse(date, dateTimeFormatter).plus(value, chronoUnit).toInstant().toEpochMilli());
1✔
111
                break;
112
        }
113
        logger.log("Date in millis var {} = {}", param, scenarioVars.get(param));
1✔
114
    }
1✔
115
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc