Skip to content

Commit

Permalink
(yegor256#951) Use 00 matchers and Assertion in time classes
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Mar 2, 2019
1 parent bbe889b commit 52e0c3d
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 152 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ The MIT License (MIT)
<plugin>
<groupId>de.thetaphi</groupId>
<artifactId>forbiddenapis</artifactId>
<version>2.5</version>
<version>2.6</version>
<configuration>
<signaturesFiles>
<signaturesFile>./src/test/resources/forbidden-apis.txt</signaturesFile>
</signaturesFiles>
<!--
@todo #903:30min In the continuation of #588, all the calls
@todo #951:30min In the continuation of #588, all the calls
to Matchers should be replaced with their OO counterparts.
This todo should be updated with a new one until everything is
done. The newly covered classes should be added to the include
Expand All @@ -171,6 +171,7 @@ The MIT License (MIT)
<includes>
<include>org/cactoos/bytes/*.class</include>
<include>org/cactoos/map/*.class</include>
<include>org/cactoos/time/*.class</include>
</includes>
</configuration>
<executions>
Expand Down
70 changes: 36 additions & 34 deletions src/test/java/org/cactoos/time/DateAsTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsNot;
import org.hamcrest.core.IsNull;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.TextIs;

/**
* Tests for DateAsText.
Expand All @@ -42,11 +44,11 @@ public final class DateAsTextTest {

@Test
public void formatsCurrentTime() throws IOException {
MatcherAssert.assertThat(
new Assertion<>(
"Can't format current time",
new DateAsText().asString(),
Matchers.notNullValue()
);
new DateAsText()::asString,
new IsNot<>(new IsNull<>())
).affirm();
}

@Test
Expand All @@ -55,25 +57,25 @@ public void dateFormattedUsingIsoFormatter() throws IOException {
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
calendar.set(Calendar.MILLISECOND, 17);
MatcherAssert.assertThat(
new Assertion<>(
"Can't format a java.util.Date with ISO format.",
new DateAsText(calendar.getTime()).asString(),
Matchers.is("2017-12-13T14:15:16.017Z")
);
() -> new DateAsText(calendar.getTime()),
new TextIs("2017-12-13T14:15:16.017Z")
).affirm();
}

@Test
public void dateFormattedUsingCustomFormat() throws IOException {
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
MatcherAssert.assertThat(
new Assertion<>(
"Can't format a java.util.Date with custom format.",
new DateAsText(
() -> new DateAsText(
calendar.getTime(), "yyyy MM dd hh:mm:ss"
).asString(),
Matchers.is("2017 12 13 02:15:16")
);
),
new TextIs("2017 12 13 02:15:16")
).affirm();
}

@Test
Expand All @@ -82,13 +84,13 @@ public void dateFormattedUsingCustomFormatDifferentLocale()
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
MatcherAssert.assertThat(
new Assertion<>(
"Can't format a java.util.Date with custom format.",
new DateAsText(
() -> new DateAsText(
calendar.getTime(), "yyyy MMM dd hh:mm:ss", Locale.ITALIAN
).asString(),
Matchers.is("2017 dic 13 02:15:16")
);
),
new TextIs("2017 dic 13 02:15:16")
).affirm();
}

@Test
Expand All @@ -97,26 +99,26 @@ public void millisFormattedUsingIsoFormatter() throws IOException {
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
calendar.set(Calendar.MILLISECOND, 17);
MatcherAssert.assertThat(
new Assertion<>(
"Can't format a java.util.Date with ISO format.",
new DateAsText(calendar.getTime().getTime()).asString(),
Matchers.is("2017-12-13T14:15:16.017Z")
);
() -> new DateAsText(calendar.getTime().getTime()),
new TextIs("2017-12-13T14:15:16.017Z")
).affirm();
}

@Test
public void millisFormattedUsingCustomFormat() throws IOException {
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
MatcherAssert.assertThat(
new Assertion<>(
"Can't format a java.util.Date with custom format.",
new DateAsText(
() -> new DateAsText(
calendar.getTime().getTime(),
"yyyy MM dd hh:mm:ss"
).asString(),
Matchers.is("2017 12 13 02:15:16")
);
),
new TextIs("2017 12 13 02:15:16")
).affirm();
}

@Test
Expand All @@ -125,15 +127,15 @@ public void millisFormattedUsingCustomFormatDifferentLocale()
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
MatcherAssert.assertThat(
new Assertion<>(
"Can't format a java.util.Date with custom format.",
new DateAsText(
() -> new DateAsText(
calendar.getTime().getTime(),
"yyyy MMMM dd hh:mm:ss",
Locale.US
).asString(),
Matchers.is("2017 December 13 02:15:16")
);
),
new TextIs("2017 December 13 02:15:16")
).affirm();
}

}
32 changes: 16 additions & 16 deletions src/test/java/org/cactoos/time/DateOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.ScalarHasValue;

/**
* Tests for DateOf.
Expand All @@ -42,53 +42,53 @@ public class DateOfTest {

@Test
public final void testParsingIsoFormattedStringToDate() {
MatcherAssert.assertThat(
new Assertion<>(
"Can't parse a Date with default/ISO format.",
new DateOf("2017-12-13T14:15:16.000000017Z").value(),
Matchers.is(
() -> new DateOf("2017-12-13T14:15:16.000000017Z"),
new ScalarHasValue<>(
Date.from(
LocalDateTime.of(
2017, 12, 13, 14, 15, 16, 17
).toInstant(ZoneOffset.UTC)
)
)
);
).affirm();
}

@Test
public final void testParsingCustomFormattedStringToDate() {
MatcherAssert.assertThat(
new Assertion<>(
"Can't parse a Date with custom format.",
new DateOf(
() -> new DateOf(
"2017-12-13 14:15:16.000000017",
"yyyy-MM-dd HH:mm:ss.n"
).value(),
Matchers.is(
),
new ScalarHasValue<>(
Date.from(
LocalDateTime.of(
2017, 12, 13, 14, 15, 16, 17
).toInstant(ZoneOffset.UTC)
)
)
);
).affirm();
}

@Test
public final void testParsingCustomFormatterStringToDate() {
MatcherAssert.assertThat(
new Assertion<>(
"Can't parse a Date with custom format.",
new DateOf(
() -> new DateOf(
"2017-12-13 14:15:16.000000017",
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.n")
).value(),
Matchers.is(
),
new ScalarHasValue<>(
Date.from(
LocalDateTime.of(
2017, 12, 13, 14, 15, 16, 17
).toInstant(ZoneOffset.UTC)
)
)
);
).affirm();
}

}
40 changes: 21 additions & 19 deletions src/test/java/org/cactoos/time/LocalDateTimeAsTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Locale;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsNot;
import org.hamcrest.core.IsNull;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.TextIs;

/**
* Tests for LocalDateTimeAsText.
Expand All @@ -46,28 +48,28 @@ public void localDateTimeFormattedAsIsoDateTime() throws IOException {
final LocalDateTime date = LocalDateTime.of(
2017, 12, 13, 14, 15, 16, 17
);
MatcherAssert.assertThat(
new Assertion<>(
"Can't format a LocalDateTime with default/ISO format.",
new LocalDateTimeAsText(date).asString(),
Matchers.is(
() -> new LocalDateTimeAsText(date),
new TextIs(
MessageFormat.format(
"2017-12-13T14:15:16.000000017{0}",
date.atZone(ZoneId.systemDefault()).getOffset().toString()
)
)
);
).affirm();
}

@Test
public void localDateTimeFormattedWithFormatString() throws IOException {
final LocalDateTime date = LocalDateTime.of(
2017, 12, 13, 14, 15, 16, 17
);
MatcherAssert.assertThat(
new Assertion<>(
"Can't format a LocalDateTime with format.",
new LocalDateTimeAsText(date, "yyyy-MM-dd HH:mm:ss").asString(),
Matchers.is("2017-12-13 14:15:16")
);
() -> new LocalDateTimeAsText(date, "yyyy-MM-dd HH:mm:ss"),
new TextIs("2017-12-13 14:15:16")
).affirm();
}

@Test
Expand All @@ -76,22 +78,22 @@ public void localDateTimeFormattedWithFormatStringWithLocale()
final LocalDateTime date = LocalDateTime.of(
2017, 12, 13, 14, 15, 16, 17
);
MatcherAssert.assertThat(
new Assertion<>(
"Can't format a LocalDateTime with format using locale.",
new LocalDateTimeAsText(
() -> new LocalDateTimeAsText(
date, "yyyy MMM dd. HH.mm.ss", Locale.FRENCH
).asString(),
Matchers.is("2017 déc. 13. 14.15.16")
);
),
new TextIs("2017 déc. 13. 14.15.16")
).affirm();
}

@Test
public void currentLocalDateTimeAsText() throws IOException {
MatcherAssert.assertThat(
new Assertion<>(
"Can't format a LocalDateTime with ISO format.",
new LocalDateTimeAsText(LocalDateTime.now()).asString(),
Matchers.notNullValue()
);
() -> new LocalDateTimeAsText(LocalDateTime.now()).asString(),
new IsNot<>(new IsNull<>())
).affirm();
}

}
32 changes: 16 additions & 16 deletions src/test/java/org/cactoos/time/LocalDateTimeOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.ScalarHasValue;

/**
* Tests for {@link LocalDateTimeOf}.
Expand All @@ -40,35 +40,35 @@ public class LocalDateTimeOfTest {

@Test
public final void testParsingIsoFormattedStringToLocalDateTime() {
MatcherAssert.assertThat(
new Assertion<>(
"Can't parse a LocalDateTime with default/ISO format.",
new LocalDateTimeOf("2017-12-13T14:15:16.000000017+01:00").value(),
Matchers.is(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17))
);
() -> new LocalDateTimeOf("2017-12-13T14:15:16.000000017+01:00"),
new ScalarHasValue<>(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17))
).affirm();
}

@Test
public final void testParsingFormattedStringWithFormatToLocalDateTime() {
MatcherAssert.assertThat(
new Assertion<>(
"Can't parse a LocalDateTime with custom format.",
new LocalDateTimeOf(
() -> new LocalDateTimeOf(
"2017-12-13 14:15:16.000000017",
"yyyy-MM-dd HH:mm:ss.n"
).value(),
Matchers.is(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17))
);
),
new ScalarHasValue<>(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17))
).affirm();
}

@Test
public final void testParsingFormattedStringWithFormatterToLocalDateTime() {
MatcherAssert.assertThat(
new Assertion<>(
"Can't parse a LocalDateTime with custom formatter.",
new LocalDateTimeOf(
() -> new LocalDateTimeOf(
"2017-12-13 14:15:16.000000017",
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.n")
).value(),
Matchers.is(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17))
);
),
new ScalarHasValue<>(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17))
).affirm();
}

}
Loading

0 comments on commit 52e0c3d

Please sign in to comment.