diff --git a/pom.xml b/pom.xml index 7417303..7df9209 100644 --- a/pom.xml +++ b/pom.xml @@ -179,10 +179,10 @@ test - org.hamcrest - hamcrest-library - [1.3,) + org.assertj + assertj-core + 2.8.0 test - + diff --git a/src/test/java/org/shredzone/commons/suncalc/DateMatcher.java b/src/test/java/org/shredzone/commons/suncalc/DateMatcher.java deleted file mode 100644 index 23f3c2b..0000000 --- a/src/test/java/org/shredzone/commons/suncalc/DateMatcher.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Shredzone Commons - suncalc - * - * Copyright (C) 2017 Richard "Shred" Körber - * http://commons.shredzone.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - */ -package org.shredzone.commons.suncalc; - -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimeZone; - -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -import org.hamcrest.Matcher; - -/** - * A {@link Matcher} for comparing a {@link Date} object with a string representation. - */ -public class DateMatcher extends BaseMatcher { - - private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); - - private final String expected; - private final TimeZone timeZone; - - /** - * Creates a new matcher for matching {@link Date} objects. - * - * @param expected - * Expected date, as string. Format "yyyy-MM-dd'T'HH:mm:ss'Z'". The - * expected date must be UTC. Compares date and time, but does not compare - * milliseconds. - */ - public static DateMatcher is(String expected) { - return new DateMatcher(expected, UTC); - } - - /** - * Creates a new matcher for matching {@link Date} objects. - * - * @param expected - * Expected date, as string. Format "yyyy-MM-dd'T'HH:mm:ssZ". The expected - * date must use the correct offset of the given time zone. Compares date - * and time, but does not compare milliseconds. - * @param tz - * Name of the time zone to be used for matching - */ - public static DateMatcher is(String expected, String tz) { - return new DateMatcher(expected, TimeZone.getTimeZone(tz)); - } - - private DateMatcher(String expected, TimeZone timeZone) { - this.expected = expected; - this.timeZone = timeZone; - } - - @Override - public boolean matches(Object item) { - if (item == null || !(item instanceof Date)) { - return false; - } - - String fmtDate = dateToString((Date) item, timeZone); - return fmtDate.equals(expected); - } - - @Override - public void describeTo(Description description) { - description.appendValue(expected); - } - - @Override - public void describeMismatch(Object item, Description description) { - if (item == null) { - description.appendText("is null"); - return; - } - - if (!(item instanceof Date)) { - description.appendText("is not a Date"); - return; - } - - description.appendText("was ").appendValue(dateToString((Date) item, timeZone)); - } - - /** - * Formats date as String. - * - * @param date - * {@link Date} to format - * @return String representation to compare with - */ - private static String dateToString(Date date, TimeZone tz) { - SimpleDateFormat fmt = new SimpleDateFormat( - tz.equals(UTC) - ? "yyyy-MM-dd'T'HH:mm:ss'Z'" - : "yyyy-MM-dd'T'HH:mm:ssZ" - ); - fmt.setTimeZone(tz); - return fmt.format(date); - } - -} diff --git a/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java b/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java index cd6e925..a881db8 100644 --- a/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java @@ -13,10 +13,10 @@ */ package org.shredzone.commons.suncalc; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.shredzone.commons.suncalc.Locations.COLOGNE_TZ; +import org.assertj.core.data.Offset; import org.junit.Test; /** @@ -24,7 +24,7 @@ */ public class MoonIlluminationTest { - private static final double ERROR = 0.1; + private static final Offset ERROR = Offset.offset(0.1); @Test public void testNewMoon() { @@ -32,9 +32,9 @@ public void testNewMoon() { .on(2017, 6, 24, 4, 30, 0) .timezone(COLOGNE_TZ) .execute(); - assertThat("fraction", mi.getFraction(), is(closeTo(0.0, ERROR))); - assertThat("phase", mi.getPhase(), is(closeTo(175.9, ERROR))); // -180.0 - assertThat("angle", mi.getAngle(), is(closeTo(2.0, ERROR))); + assertThat(mi.getFraction()).as("fraction").isCloseTo(0.0, ERROR); + assertThat(mi.getPhase()).as("phase").isCloseTo(175.9, ERROR); // -180.0 + assertThat(mi.getAngle()).as("angle").isCloseTo(2.0, ERROR); } @Test @@ -43,9 +43,9 @@ public void testWaxingHalfMoon() { .on(2017, 7, 1, 2, 51, 0) .timezone(COLOGNE_TZ) .execute(); - assertThat("fraction", mi.getFraction(), is(closeTo(0.5, ERROR))); - assertThat("phase", mi.getPhase(), is(closeTo(-89.9, ERROR))); // -90.0 - assertThat("angle", mi.getAngle(), is(closeTo(-66.9, ERROR))); + assertThat(mi.getFraction()).as("fraction").isCloseTo(0.5, ERROR); + assertThat(mi.getPhase()).as("phase").isCloseTo(-89.9, ERROR); // -90.0 + assertThat(mi.getAngle()).as("angle").isCloseTo(-66.9, ERROR); } @Test @@ -54,9 +54,9 @@ public void testFullMoon() { .on(2017, 7, 9, 6, 6, 0) .timezone(COLOGNE_TZ) .execute(); - assertThat("fraction", mi.getFraction(), is(closeTo(1.0, ERROR))); - assertThat("phase", mi.getPhase(), is(closeTo(-3.1, ERROR))); // 0.0 - assertThat("angle", mi.getAngle(), is(closeTo(-7.4, ERROR))); + assertThat(mi.getFraction()).as("fraction").isCloseTo(1.0, ERROR); + assertThat(mi.getPhase()).as("phase").isCloseTo(-3.1, ERROR); // 0.0 + assertThat(mi.getAngle()).as("angle").isCloseTo(-7.4, ERROR); } @Test @@ -65,9 +65,9 @@ public void testWaningHalfMoon() { .on(2017, 7, 16, 21, 25, 0) .timezone(COLOGNE_TZ) .execute(); - assertThat("fraction", mi.getFraction(), is(closeTo(0.5, ERROR))); - assertThat("phase", mi.getPhase(), is(closeTo(89.8, ERROR))); // 90.0 - assertThat("angle", mi.getAngle(), is(closeTo(68.7, ERROR))); + assertThat(mi.getFraction()).as("fraction").isCloseTo(0.5, ERROR); + assertThat(mi.getPhase()).as("phase").isCloseTo(89.8, ERROR); // 90.0 + assertThat(mi.getAngle()).as("angle").isCloseTo(68.7, ERROR); } } diff --git a/src/test/java/org/shredzone/commons/suncalc/MoonPositionTest.java b/src/test/java/org/shredzone/commons/suncalc/MoonPositionTest.java index 71730a0..66f0eac 100644 --- a/src/test/java/org/shredzone/commons/suncalc/MoonPositionTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/MoonPositionTest.java @@ -13,10 +13,10 @@ */ package org.shredzone.commons.suncalc; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.shredzone.commons.suncalc.Locations.*; +import org.assertj.core.data.Offset; import org.junit.Test; /** @@ -24,7 +24,7 @@ */ public class MoonPositionTest { - private static final double ERROR = 0.1; + private static final Offset ERROR = Offset.offset(0.1); @Test public void testCologne() { @@ -33,17 +33,17 @@ public void testCologne() { .at(COLOGNE) .timezone(COLOGNE_TZ) .execute(); - assertThat("azimuth", mp1.getAzimuth(), is(closeTo(304.8, ERROR))); - assertThat("altitude", mp1.getAltitude(), is(closeTo(-39.6, ERROR))); + assertThat(mp1.getAzimuth()).as("azimuth").isCloseTo(304.8, ERROR); + assertThat(mp1.getAltitude()).as("altitude").isCloseTo(-39.6, ERROR); MoonPosition mp2 = MoonPosition.compute() .on(2017, 7, 12, 3, 51, 0) .at(COLOGNE) .timezone(COLOGNE_TZ) .execute(); - assertThat("azimuth", mp2.getAzimuth(), is(closeTo(179.9, ERROR))); - assertThat("altitude", mp2.getAltitude(), is(closeTo(25.3, ERROR))); - assertThat("distance", mp2.getDistance(), is(closeTo(391626.1, ERROR))); + assertThat(mp2.getAzimuth()).as("azimuth").isCloseTo(179.9, ERROR); + assertThat(mp2.getAltitude()).as("altitude").isCloseTo(25.3, ERROR); + assertThat(mp2.getDistance()).as("distance").isCloseTo(391626.1, ERROR); } @Test @@ -53,17 +53,17 @@ public void testAlert() { .at(ALERT) .timezone(ALERT_TZ) .execute(); - assertThat("azimuth", mp1.getAzimuth(), is(closeTo(257.5, ERROR))); - assertThat("altitude", mp1.getAltitude(), is(closeTo(-10.9, ERROR))); + assertThat(mp1.getAzimuth()).as("azimuth").isCloseTo(257.5, ERROR); + assertThat(mp1.getAltitude()).as("altitude").isCloseTo(-10.9, ERROR); MoonPosition mp2 = MoonPosition.compute() .on(2017, 7, 12, 2, 37, 0) .at(ALERT) .timezone(ALERT_TZ) .execute(); - assertThat("azimuth", mp2.getAzimuth(), is(closeTo(179.8, ERROR))); - assertThat("altitude", mp2.getAltitude(), is(closeTo(-5.7, ERROR))); - assertThat("distance", mp2.getDistance(), is(closeTo(390721.7, ERROR))); + assertThat(mp2.getAzimuth()).as("azimuth").isCloseTo(179.8, ERROR); + assertThat(mp2.getAltitude()).as("altitude").isCloseTo(-5.7, ERROR); + assertThat(mp2.getDistance()).as("distance").isCloseTo(390721.7, ERROR); } @Test @@ -73,17 +73,17 @@ public void testWellington() { .at(WELLINGTON) .timezone(WELLINGTON_TZ) .execute(); - assertThat("azimuth", mp1.getAzimuth(), is(closeTo(311.3, ERROR))); - assertThat("altitude", mp1.getAltitude(), is(closeTo(55.1, ERROR))); + assertThat(mp1.getAzimuth()).as("azimuth").isCloseTo(311.3, ERROR); + assertThat(mp1.getAltitude()).as("altitude").isCloseTo(55.1, ERROR); MoonPosition mp2 = MoonPosition.compute() .on(2017, 7, 12, 2, 17, 0) .at(WELLINGTON) .timezone(WELLINGTON_TZ) .execute(); - assertThat("azimuth", mp2.getAzimuth(), is(closeTo(0.5, ERROR))); - assertThat("altitude", mp2.getAltitude(), is(closeTo(63.9, ERROR))); - assertThat("distance", mp2.getDistance(), is(closeTo(393760.7, ERROR))); + assertThat(mp2.getAzimuth()).as("azimuth").isCloseTo(0.5, ERROR); + assertThat(mp2.getAltitude()).as("altitude").isCloseTo(63.9, ERROR); + assertThat(mp2.getDistance()).as("distance").isCloseTo(393760.7, ERROR); } @Test @@ -93,17 +93,17 @@ public void testPuertoWilliams() { .at(PUERTO_WILLIAMS) .timezone(PUERTO_WILLIAMS_TZ) .execute(); - assertThat("azimuth", mp1.getAzimuth(), is(closeTo(199.4, ERROR))); - assertThat("altitude", mp1.getAltitude(), is(closeTo(-52.7, ERROR))); + assertThat(mp1.getAzimuth()).as("azimuth").isCloseTo(199.4, ERROR); + assertThat(mp1.getAltitude()).as("altitude").isCloseTo(-52.7, ERROR); MoonPosition mp2 = MoonPosition.compute() .on(2017, 2, 7, 23, 4, 0) .at(PUERTO_WILLIAMS) .timezone(PUERTO_WILLIAMS_TZ) .execute(); - assertThat("azimuth", mp2.getAzimuth(), is(closeTo(0.1, ERROR))); - assertThat("altitude", mp2.getAltitude(), is(closeTo(16.3, ERROR))); - assertThat("distance", mp2.getDistance(), is(closeTo(368900.3, ERROR))); + assertThat(mp2.getAzimuth()).as("azimuth").isCloseTo(0.1, ERROR); + assertThat(mp2.getAltitude()).as("altitude").isCloseTo(16.3, ERROR); + assertThat(mp2.getDistance()).as("distance").isCloseTo(368900.3, ERROR); } @Test @@ -113,17 +113,17 @@ public void testSingapore() { .at(SINGAPORE) .timezone(SINGAPORE_TZ) .execute(); - assertThat("azimuth", mp1.getAzimuth(), is(closeTo(240.6, ERROR))); - assertThat("altitude", mp1.getAltitude(), is(closeTo(57.1, ERROR))); + assertThat(mp1.getAzimuth()).as("azimuth").isCloseTo(240.6, ERROR); + assertThat(mp1.getAltitude()).as("altitude").isCloseTo(57.1, ERROR); MoonPosition mp2 = MoonPosition.compute() .on(2017, 7, 12, 3, 11, 0) .at(SINGAPORE) .timezone(SINGAPORE_TZ) .execute(); - assertThat("azimuth", mp2.getAzimuth(), is(closeTo(180.0, ERROR))); - assertThat("altitude", mp2.getAltitude(), is(closeTo(74.1, ERROR))); - assertThat("distance", mp2.getDistance(), is(closeTo(392867.9, ERROR))); + assertThat(mp2.getAzimuth()).as("azimuth").isCloseTo(180.0, ERROR); + assertThat(mp2.getAltitude()).as("altitude").isCloseTo(74.1, ERROR); + assertThat(mp2.getDistance()).as("distance").isCloseTo(392867.9, ERROR); } } diff --git a/src/test/java/org/shredzone/commons/suncalc/MoonTimesTest.java b/src/test/java/org/shredzone/commons/suncalc/MoonTimesTest.java index cf08257..16c7ff3 100644 --- a/src/test/java/org/shredzone/commons/suncalc/MoonTimesTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/MoonTimesTest.java @@ -13,14 +13,15 @@ */ package org.shredzone.commons.suncalc; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.shredzone.commons.suncalc.Locations.*; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; +import org.assertj.core.api.AbstractDateAssert; +import org.junit.BeforeClass; import org.junit.Test; /** @@ -28,67 +29,72 @@ */ public class MoonTimesTest { + @BeforeClass + public static void init() { + AbstractDateAssert.registerCustomDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); + } + @Test public void testCologne() { MoonTimes mt = MoonTimes.compute().on(2017, 7, 12).utc().at(COLOGNE).execute(); - assertThat("rise", mt.getRise(), DateMatcher.is("2017-07-12T21:25:58Z")); - assertThat("set", mt.getSet(), DateMatcher.is("2017-07-12T06:53:19Z")); - assertThat("alwaysup", mt.isAlwaysUp(), is(false)); - assertThat("alwaysdown", mt.isAlwaysDown(), is(false)); + assertThat(mt.getRise()).as("rise").isEqualTo("2017-07-12T21:25:58Z"); + assertThat(mt.getSet()).as("set").isEqualTo("2017-07-12T06:53:19Z"); + assertThat(mt.isAlwaysUp()).as("alwaysup").isFalse(); + assertThat(mt.isAlwaysDown()).as("alwaysdown").isFalse(); } @Test public void testAlert() { MoonTimes mt1 = MoonTimes.compute().on(2017, 7, 12).utc().at(ALERT).execute(); - assertThat("alwaysup", mt1.isAlwaysUp(), is(false)); - assertThat("alwaysdown", mt1.isAlwaysDown(), is(true)); + assertThat(mt1.isAlwaysUp()).as("alwaysup").isFalse(); + assertThat(mt1.isAlwaysDown()).as("alwaysdown").isTrue(); MoonTimes mt2 = MoonTimes.compute().on(2017, 7, 12).utc().at(ALERT).fullCycle().execute(); - assertThat("rise", mt2.getRise(), DateMatcher.is("2017-07-14T05:45:33Z")); - assertThat("set", mt2.getSet(), DateMatcher.is("2017-07-14T11:26:12Z")); - assertThat("alwaysup", mt2.isAlwaysUp(), is(false)); - assertThat("alwaysdown", mt2.isAlwaysDown(), is(true)); + assertThat(mt2.getRise()).as("rise").isEqualTo("2017-07-14T05:45:33Z"); + assertThat(mt2.getSet()).as("set").isEqualTo("2017-07-14T11:26:12Z"); + assertThat(mt2.isAlwaysUp()).as("alwaysup").isFalse(); + assertThat(mt2.isAlwaysDown()).as("alwaysdown").isTrue(); MoonTimes mt3 = MoonTimes.compute().on(2017, 7, 14).utc().at(ALERT).execute(); - assertThat("rise", mt3.getRise(), DateMatcher.is("2017-07-14T05:45:33Z")); - assertThat("set", mt3.getSet(), DateMatcher.is("2017-07-14T11:26:12Z")); - assertThat("alwaysup", mt3.isAlwaysUp(), is(false)); - assertThat("alwaysdown", mt3.isAlwaysDown(), is(false)); + assertThat(mt3.getRise()).as("rise").isEqualTo("2017-07-14T05:45:33Z"); + assertThat(mt3.getSet()).as("set").isEqualTo("2017-07-14T11:26:12Z"); + assertThat(mt3.isAlwaysUp()).as("alwaysup").isFalse(); + assertThat(mt3.isAlwaysDown()).as("alwaysdown").isFalse(); MoonTimes mt4 = MoonTimes.compute().on(2017, 7, 18).utc().at(ALERT).oneDay().execute(); - assertThat("alwaysup", mt4.isAlwaysUp(), is(true)); - assertThat("alwaysdown", mt4.isAlwaysDown(), is(false)); + assertThat(mt4.isAlwaysUp()).as("alwaysup").isTrue(); + assertThat(mt4.isAlwaysDown()).as("alwaysdown").isFalse(); MoonTimes mt5 = MoonTimes.compute().on(2017, 7, 18).utc().at(ALERT).fullCycle().execute(); - assertThat("rise", mt5.getRise(), DateMatcher.is("2017-07-27T11:59:07Z")); - assertThat("set", mt5.getSet(), DateMatcher.is("2017-07-27T04:07:14Z")); - assertThat("alwaysup", mt5.isAlwaysUp(), is(true)); - assertThat("alwaysdown", mt5.isAlwaysDown(), is(false)); + assertThat(mt5.getRise()).as("rise").isEqualTo("2017-07-27T11:59:07Z"); + assertThat(mt5.getSet()).as("set").isEqualTo("2017-07-27T04:07:14Z"); + assertThat(mt5.isAlwaysUp()).as("alwaysup").isTrue(); + assertThat(mt5.isAlwaysDown()).as("alwaysdown").isFalse(); } @Test public void testWellington() { MoonTimes mt1 = MoonTimes.compute().on(2017, 7, 12).utc().at(WELLINGTON).execute(); - assertThat("rise", mt1.getRise(), DateMatcher.is("2017-07-12T08:05:50Z")); - assertThat("set", mt1.getSet(), DateMatcher.is("2017-07-12T21:57:35Z")); + assertThat(mt1.getRise()).as("rise").isEqualTo("2017-07-12T08:05:50Z"); + assertThat(mt1.getSet()).as("set").isEqualTo("2017-07-12T21:57:35Z"); MoonTimes mt2 = MoonTimes.compute().on(2017, 7, 12).timezone("NZ").at(WELLINGTON).execute(); - assertThat("rise", mt2.getRise(), DateMatcher.is("2017-07-12T20:05:50+1200", "NZ")); - assertThat("set", mt2.getSet(), DateMatcher.is("2017-07-12T09:22:59+1200", "NZ")); + assertThat(mt2.getRise()).as("rise").isEqualTo("2017-07-12T20:05:50+12:00"); + assertThat(mt2.getSet()).as("set").isEqualTo("2017-07-12T09:22:59+12:00"); } @Test public void testPuertoWilliams() { MoonTimes mt = MoonTimes.compute().on(2017, 7, 13).utc().at(PUERTO_WILLIAMS).execute(); - assertThat("rise", mt.getRise(), DateMatcher.is("2017-07-13T00:31:12Z")); - assertThat("set", mt.getSet(), DateMatcher.is("2017-07-13T14:48:21Z")); + assertThat(mt.getRise()).as("rise").isEqualTo("2017-07-13T00:31:12Z"); + assertThat(mt.getSet()).as("set").isEqualTo("2017-07-13T14:48:21Z"); } @Test public void testSingapore() { MoonTimes mt = MoonTimes.compute().on(2017, 7, 13).utc().at(SINGAPORE).execute(); - assertThat("rise", mt.getRise(), DateMatcher.is("2017-07-13T14:35:11Z")); - assertThat("set", mt.getSet(), DateMatcher.is("2017-07-13T02:08:54Z")); + assertThat(mt.getRise()).as("rise").isEqualTo("2017-07-13T14:35:11Z"); + assertThat(mt.getSet()).as("set").isEqualTo("2017-07-13T02:08:54Z"); } @Test @@ -110,18 +116,18 @@ public void testSequence() { if (hour < 12 || (hour == 12 && minute == 0)) { long diff = Math.abs(times.getRise().getTime() - riseBefore.getTime()); - assertThat("rise @" + hour + ":" + minute, diff, is(lessThan(acceptableError))); + assertThat(diff).as("rise @%02d:%02d", hour, minute).isLessThan(acceptableError); } else { long diff = Math.abs(times.getRise().getTime() - riseAfter.getTime()); - assertThat("rise @" + hour + ":" + minute, diff, is(lessThan(acceptableError))); + assertThat(diff).as("rise @%02d:%02d", hour, minute).isLessThan(acceptableError); } if (hour < 21 || (hour == 21 && minute <= 49)) { long diff = Math.abs(times.getSet().getTime() - setBefore.getTime()); - assertThat("set @" + hour + ":" + minute, diff, is(lessThan(acceptableError))); + assertThat(diff).as("set @%02d:%02d", hour, minute).isLessThan(acceptableError); } else { long diff = Math.abs(times.getSet().getTime() - setAfter.getTime()); - assertThat("set @" + hour + ":" + minute, diff, is(lessThan(acceptableError))); + assertThat(diff).as("set @%02d:%02d", hour, minute).isLessThan(acceptableError); } } } diff --git a/src/test/java/org/shredzone/commons/suncalc/SunPositionTest.java b/src/test/java/org/shredzone/commons/suncalc/SunPositionTest.java index a31d2ce..7165178 100644 --- a/src/test/java/org/shredzone/commons/suncalc/SunPositionTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/SunPositionTest.java @@ -13,10 +13,10 @@ */ package org.shredzone.commons.suncalc; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.shredzone.commons.suncalc.Locations.*; +import org.assertj.core.data.Offset; import org.junit.Test; /** @@ -24,7 +24,7 @@ */ public class SunPositionTest { - private static final double ERROR = 0.1; + private static final Offset ERROR = Offset.offset(0.1); @Test public void testCologne() { @@ -33,16 +33,16 @@ public void testCologne() { .at(COLOGNE) .timezone(COLOGNE_TZ) .execute(); - assertThat("azimuth", sp1.getAzimuth(), is(closeTo(239.8, ERROR))); - assertThat("altitude", sp1.getAltitude(), is(closeTo(48.6, ERROR))); + assertThat(sp1.getAzimuth()).as("azimuth").isCloseTo(239.8, ERROR); + assertThat(sp1.getAltitude()).as("altitude").isCloseTo(48.6, ERROR); SunPosition sp2 = SunPosition.compute() .on(2017, 7, 12, 13, 37, 0) .at(COLOGNE) .timezone(COLOGNE_TZ) .execute(); - assertThat("azimuth", sp2.getAzimuth(), is(closeTo(179.6, ERROR))); - assertThat("altitude", sp2.getAltitude(), is(closeTo(61.0, ERROR))); + assertThat(sp2.getAzimuth()).as("azimuth").isCloseTo(179.6, ERROR); + assertThat(sp2.getAltitude()).as("altitude").isCloseTo(61.0, ERROR); } @Test @@ -52,16 +52,16 @@ public void testAlert() { .at(ALERT) .timezone(ALERT_TZ) .execute(); - assertThat("azimuth", sp1.getAzimuth(), is(closeTo(87.5, ERROR))); - assertThat("altitude", sp1.getAltitude(), is(closeTo(21.8, ERROR))); + assertThat(sp1.getAzimuth()).as("azimuth").isCloseTo(87.5, ERROR); + assertThat(sp1.getAltitude()).as("altitude").isCloseTo(21.8, ERROR); SunPosition sp2 = SunPosition.compute() .on(2017, 7, 12, 12, 14, 0) .at(ALERT) .timezone(ALERT_TZ) .execute(); - assertThat("azimuth", sp2.getAzimuth(), is(closeTo(179.7, ERROR))); - assertThat("altitude", sp2.getAltitude(), is(closeTo(29.4, ERROR))); + assertThat(sp2.getAzimuth()).as("azimuth").isCloseTo(179.7, ERROR); + assertThat(sp2.getAltitude()).as("altitude").isCloseTo(29.4, ERROR); } @Test @@ -71,16 +71,16 @@ public void testWellington() { .at(WELLINGTON) .timezone(WELLINGTON_TZ) .execute(); - assertThat("azimuth", sp1.getAzimuth(), is(closeTo(107.3, ERROR))); - assertThat("altitude", sp1.getAltitude(), is(closeTo(-51.3, ERROR))); + assertThat(sp1.getAzimuth()).as("azimuth").isCloseTo(107.3, ERROR); + assertThat(sp1.getAltitude()).as("altitude").isCloseTo(-51.3, ERROR); SunPosition sp2 = SunPosition.compute() .on(2017, 7, 12, 12, 26, 0) .at(WELLINGTON) .timezone(WELLINGTON_TZ) .execute(); - assertThat("azimuth", sp2.getAzimuth(), is(closeTo(0.1, ERROR))); - assertThat("altitude", sp2.getAltitude(), is(closeTo(26.8, ERROR))); + assertThat(sp2.getAzimuth()).as("azimuth").isCloseTo(0.1, ERROR); + assertThat(sp2.getAltitude()).as("altitude").isCloseTo(26.8, ERROR); } @Test @@ -90,16 +90,16 @@ public void testPuertoWilliams() { .at(PUERTO_WILLIAMS) .timezone(PUERTO_WILLIAMS_TZ) .execute(); - assertThat("azimuth", sp1.getAzimuth(), is(closeTo(280.1, ERROR))); - assertThat("altitude", sp1.getAltitude(), is(closeTo(25.4, ERROR))); + assertThat(sp1.getAzimuth()).as("azimuth").isCloseTo(280.1, ERROR); + assertThat(sp1.getAltitude()).as("altitude").isCloseTo(25.4, ERROR); SunPosition sp2 = SunPosition.compute() .on(2017, 2, 7, 13, 44, 0) .at(PUERTO_WILLIAMS) .timezone(PUERTO_WILLIAMS_TZ) .execute(); - assertThat("azimuth", sp2.getAzimuth(), is(closeTo(0.2, ERROR))); - assertThat("altitude", sp2.getAltitude(), is(closeTo(50.2, ERROR))); + assertThat(sp2.getAzimuth()).as("azimuth").isCloseTo(0.2, ERROR); + assertThat(sp2.getAltitude()).as("altitude").isCloseTo(50.2, ERROR); } @Test @@ -109,16 +109,16 @@ public void testSingapore() { .at(SINGAPORE) .timezone(SINGAPORE_TZ) .execute(); - assertThat("azimuth", sp1.getAzimuth(), is(closeTo(60.4, ERROR))); - assertThat("altitude", sp1.getAltitude(), is(closeTo(43.5, ERROR))); + assertThat(sp1.getAzimuth()).as("azimuth").isCloseTo(60.4, ERROR); + assertThat(sp1.getAltitude()).as("altitude").isCloseTo(43.5, ERROR); SunPosition sp2 = SunPosition.compute() .on(2017, 7, 12, 13, 10, 0) .at(SINGAPORE) .timezone(SINGAPORE_TZ) .execute(); - assertThat("azimuth", sp2.getAzimuth(), is(closeTo(0.2, ERROR))); - assertThat("altitude", sp2.getAltitude(), is(closeTo(69.4, ERROR))); + assertThat(sp2.getAzimuth()).as("azimuth").isCloseTo(0.2, ERROR); + assertThat(sp2.getAltitude()).as("altitude").isCloseTo(69.4, ERROR); } @Test @@ -128,28 +128,28 @@ public void testDistance() { .at(COLOGNE) .timezone(COLOGNE_TZ) .execute(); - assertThat(sp1.getDistance(), is(closeTo(147097390.6, ERROR))); + assertThat(sp1.getDistance()).as("distance").isCloseTo(147097390.6, ERROR); SunPosition sp2 = SunPosition.compute() .on(2017, 4, 20, 13, 31, 0) .at(COLOGNE) .timezone(COLOGNE_TZ) .execute(); - assertThat(sp2.getDistance(), is(closeTo(150181373.3, ERROR))); + assertThat(sp2.getDistance()).as("distance").isCloseTo(150181373.3, ERROR); SunPosition sp3 = SunPosition.compute() .on(2017, 7, 12, 13, 37, 0) .at(COLOGNE) .timezone(COLOGNE_TZ) .execute(); - assertThat(sp3.getDistance(), is(closeTo(152088309.0, ERROR))); + assertThat(sp3.getDistance()).as("distance").isCloseTo(152088309.0, ERROR); SunPosition sp4 = SunPosition.compute() .on(2017, 10, 11, 13, 18, 0) .at(COLOGNE) .timezone(COLOGNE_TZ) .execute(); - assertThat(sp4.getDistance(), is(closeTo(149380680.0, ERROR))); + assertThat(sp4.getDistance()).as("distance").isCloseTo(149380680.0, ERROR); } } diff --git a/src/test/java/org/shredzone/commons/suncalc/SunTimesTest.java b/src/test/java/org/shredzone/commons/suncalc/SunTimesTest.java index d17ca92..635ffc2 100644 --- a/src/test/java/org/shredzone/commons/suncalc/SunTimesTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/SunTimesTest.java @@ -13,8 +13,7 @@ */ package org.shredzone.commons.suncalc; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.shredzone.commons.suncalc.Locations.*; import java.util.Calendar; @@ -23,7 +22,8 @@ import java.util.Map; import java.util.TimeZone; -import org.hamcrest.Matcher; +import org.assertj.core.api.AbstractDateAssert; +import org.junit.BeforeClass; import org.junit.Test; import org.shredzone.commons.suncalc.SunTimes.Twilight; @@ -32,47 +32,52 @@ */ public class SunTimesTest { + @BeforeClass + public static void init() { + AbstractDateAssert.registerCustomDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); + } + @Test public void testCologne() { - Map> riseTimes = new EnumMap<>(Twilight.class); - riseTimes.put(Twilight.ASTRONOMICAL, DateMatcher.is("2017-08-10T01:44:18Z")); - riseTimes.put(Twilight.NAUTICAL, DateMatcher.is("2017-08-10T02:44:33Z")); - riseTimes.put(Twilight.CIVIL, DateMatcher.is("2017-08-10T03:34:01Z")); - riseTimes.put(Twilight.BLUE_HOUR, DateMatcher.is("2017-08-10T03:48:59Z")); - riseTimes.put(Twilight.VISUAL, DateMatcher.is("2017-08-10T04:11:36Z")); - riseTimes.put(Twilight.VISUAL_LOWER, DateMatcher.is("2017-08-10T04:15:17Z")); - riseTimes.put(Twilight.HORIZON, DateMatcher.is("2017-08-10T04:17:27Z")); - riseTimes.put(Twilight.GOLDEN_HOUR, DateMatcher.is("2017-08-10T04:58:31Z")); - - Map> setTimes = new EnumMap<>(Twilight.class); - setTimes.put(Twilight.GOLDEN_HOUR, DateMatcher.is("2017-08-10T18:15:34Z")); - setTimes.put(Twilight.HORIZON, DateMatcher.is("2017-08-10T18:56:26Z")); - setTimes.put(Twilight.VISUAL_LOWER, DateMatcher.is("2017-08-10T18:58:37Z")); - setTimes.put(Twilight.VISUAL, DateMatcher.is("2017-08-10T19:02:20Z")); - setTimes.put(Twilight.BLUE_HOUR, DateMatcher.is("2017-08-10T19:25:16Z")); - setTimes.put(Twilight.CIVIL, DateMatcher.is("2017-08-10T19:40:13Z")); - setTimes.put(Twilight.NAUTICAL, DateMatcher.is("2017-08-10T20:28:24Z")); - setTimes.put(Twilight.ASTRONOMICAL, DateMatcher.is("2017-08-10T21:28:43Z")); + Map riseTimes = new EnumMap<>(Twilight.class); + riseTimes.put(Twilight.ASTRONOMICAL, "2017-08-10T01:44:18Z"); + riseTimes.put(Twilight.NAUTICAL, "2017-08-10T02:44:33Z"); + riseTimes.put(Twilight.CIVIL, "2017-08-10T03:34:01Z"); + riseTimes.put(Twilight.BLUE_HOUR, "2017-08-10T03:48:59Z"); + riseTimes.put(Twilight.VISUAL, "2017-08-10T04:11:36Z"); + riseTimes.put(Twilight.VISUAL_LOWER, "2017-08-10T04:15:17Z"); + riseTimes.put(Twilight.HORIZON, "2017-08-10T04:17:27Z"); + riseTimes.put(Twilight.GOLDEN_HOUR, "2017-08-10T04:58:31Z"); + + Map setTimes = new EnumMap<>(Twilight.class); + setTimes.put(Twilight.GOLDEN_HOUR, "2017-08-10T18:15:34Z"); + setTimes.put(Twilight.HORIZON, "2017-08-10T18:56:26Z"); + setTimes.put(Twilight.VISUAL_LOWER, "2017-08-10T18:58:37Z"); + setTimes.put(Twilight.VISUAL, "2017-08-10T19:02:20Z"); + setTimes.put(Twilight.BLUE_HOUR, "2017-08-10T19:25:16Z"); + setTimes.put(Twilight.CIVIL, "2017-08-10T19:40:13Z"); + setTimes.put(Twilight.NAUTICAL, "2017-08-10T20:28:24Z"); + setTimes.put(Twilight.ASTRONOMICAL, "2017-08-10T21:28:43Z"); for (Twilight angle : Twilight.values()) { SunTimes times = SunTimes.compute().at(COLOGNE).on(2017, 8, 10).utc() .twilight(angle).execute(); - assertThat(angle.name() + "-rise", times.getRise(), riseTimes.get(angle)); - assertThat(angle.name() + "-set", times.getSet(), setTimes.get(angle)); - assertThat("noon", times.getNoon(), DateMatcher.is("2017-08-10T11:37:57Z")); - assertThat("nadir", times.getNadir(), DateMatcher.is("2017-08-10T23:37:59Z")); - assertThat("always-down", times.isAlwaysDown(), is(false)); - assertThat("always-up", times.isAlwaysUp(), is(false)); + assertThat(times.getRise()).as("%s-rise", angle.name()).isEqualTo(riseTimes.get(angle)); + assertThat(times.getSet()).as("%s-set", angle.name()).isEqualTo(setTimes.get(angle)); + assertThat(times.getNoon()).as("noon").isEqualTo("2017-08-10T11:37:57Z"); + assertThat(times.getNadir()).as("nadir").isEqualTo("2017-08-10T23:37:59Z"); + assertThat(times.isAlwaysDown()).as("always-down").isFalse(); + assertThat(times.isAlwaysUp()).as("always-up").isFalse(); } SunTimes times = SunTimes.compute().at(COLOGNE).on(2017, 8, 10).utc() .twilight(-4.0).execute(); - assertThat("rise", times.getRise(), DateMatcher.is("2017-08-10T03:48:59Z")); - assertThat("set", times.getSet(), DateMatcher.is("2017-08-10T19:25:16Z")); - assertThat("noon", times.getNoon(), DateMatcher.is("2017-08-10T11:37:57Z")); - assertThat("nadir", times.getNadir(), DateMatcher.is("2017-08-10T23:37:59Z")); - assertThat("always-down", times.isAlwaysDown(), is(false)); - assertThat("always-up", times.isAlwaysUp(), is(false)); + assertThat(times.getRise()).as("rise").isEqualTo("2017-08-10T03:48:59Z"); + assertThat(times.getSet()).as("set").isEqualTo("2017-08-10T19:25:16Z"); + assertThat(times.getNoon()).as("noon").isEqualTo("2017-08-10T11:37:57Z"); + assertThat(times.getNadir()).as("nadir").isEqualTo("2017-08-10T23:37:59Z"); + assertThat(times.isAlwaysDown()).as("always-down").isFalse(); + assertThat(times.isAlwaysUp()).as("always-up").isFalse(); } @Test @@ -133,18 +138,18 @@ public void testSequence() { if (hour < 7 || (hour == 7 && minute <= 4)) { long diff = Math.abs(times.getRise().getTime() - riseBefore.getTime()); - assertThat("rise @" + hour + ":" + minute, diff, is(lessThan(acceptableError))); + assertThat(diff).as("rise @%02d:%02d", hour, minute).isLessThan(acceptableError); } else { long diff = Math.abs(times.getRise().getTime() - riseAfter.getTime()); - assertThat("rise @" + hour + ":" + minute, diff, is(lessThan(acceptableError))); + assertThat(diff).as("rise @%02d:%02d", hour, minute).isLessThan(acceptableError); } if (hour < 15 || (hour == 15 && minute <= 33)) { long diff = Math.abs(times.getSet().getTime() - setBefore.getTime()); - assertThat("set @" + hour + ":" + minute, diff, is(lessThan(acceptableError))); + assertThat(diff).as("set @%02d:%02d", hour, minute).isLessThan(acceptableError); } else { long diff = Math.abs(times.getSet().getTime() - setAfter.getTime()); - assertThat("set @" + hour + ":" + minute, diff, is(lessThan(acceptableError))); + assertThat(diff).as("set @%02d:%02d", hour, minute).isLessThan(acceptableError); } } } @@ -163,25 +168,25 @@ private void assertTimes(SunTimes t, String rise, String set, String noon) { private void assertTimes(SunTimes t, String rise, String set, String noon, Boolean alwaysUp) { if (rise != null) { - assertThat("sunrise", t.getRise(), DateMatcher.is(rise)); + assertThat(t.getRise()).as("sunrise").isEqualTo(rise); } else { - assertThat("sunrise", t.getRise(), is(nullValue())); + assertThat(t.getRise()).as("sunrise").isNull(); } if (set != null) { - assertThat("sunset", t.getSet(), DateMatcher.is(set)); + assertThat(t.getSet()).as("sunset").isEqualTo(set); } else { - assertThat("sunset", t.getSet(), is(nullValue())); + assertThat(t.getSet()).as("sunset").isNull(); } - assertThat("noon", t.getNoon(), DateMatcher.is(noon)); + assertThat(t.getNoon()).as("noon").isEqualTo(noon); if (alwaysUp != null) { - assertThat("always-down", t.isAlwaysDown(), is(!alwaysUp)); - assertThat("always-up", t.isAlwaysUp(), is(alwaysUp)); + assertThat(t.isAlwaysDown()).as("always-down").isNotEqualTo(alwaysUp); + assertThat(t.isAlwaysUp()).as("always-up").isEqualTo(alwaysUp); } else { - assertThat("always-down", t.isAlwaysDown(), is(false)); - assertThat("always-up", t.isAlwaysUp(), is(false)); + assertThat(t.isAlwaysDown()).as("always-down").isFalse(); + assertThat(t.isAlwaysUp()).as("always-up").isFalse(); } } diff --git a/src/test/java/org/shredzone/commons/suncalc/util/BaseBuilderTest.java b/src/test/java/org/shredzone/commons/suncalc/util/BaseBuilderTest.java index a618839..d0ff227 100644 --- a/src/test/java/org/shredzone/commons/suncalc/util/BaseBuilderTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/util/BaseBuilderTest.java @@ -14,13 +14,15 @@ package org.shredzone.commons.suncalc.util; import static java.lang.Math.toRadians; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.*; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; +import org.assertj.core.api.AbstractDateAssert; +import org.assertj.core.data.Offset; +import org.junit.BeforeClass; import org.junit.Test; /** @@ -30,9 +32,14 @@ */ public class BaseBuilderTest { - private static final double ERROR = 0.001; + private static final Offset ERROR = Offset.offset(0.001); private static final Calendar NOW = Calendar.getInstance(); + @BeforeClass + public static void init() { + AbstractDateAssert.registerCustomDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); + } + @Test public void testLocationParameters() { TestBuilder p = new TestBuilder(); @@ -42,35 +49,35 @@ public void testLocationParameters() { r = p.at(12.34, 34.56); assertLatLng(p, 12.34, 34.56, 0.0); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.at(new double[] { 13.43, 51.23 }); assertLatLng(p, 13.43, 51.23, 0.0); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.latitude(-11.22); assertLatLng(p, -11.22, 51.23, 0.0); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.longitude(-8.23); assertLatLng(p, -11.22, -8.23, 0.0); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.latitude(5, 7, 37.2); assertLatLng(p, 5.127, -8.23, 0.0); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.longitude(-12, 43, 22.8); assertLatLng(p, 5.127, -12.723, 0.0); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.height(18267.3); assertLatLng(p, 5.127, -12.723, 18267.3); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.at(new double[] { 1.22, -3.44, 323.0 }); assertLatLng(p, 1.22, -3.44, 323.0); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); } @Test @@ -79,42 +86,42 @@ public void testBadLocations() { try { p.at(new double[] { 12.0 }); - fail(); + failBecauseExceptionWasNotThrown(IllegalAccessException.class); } catch (IllegalArgumentException ex) { // expected } try { p.at(new double[] { 12.0, 34.0, 56.0, 78.0 }); - fail(); + failBecauseExceptionWasNotThrown(IllegalAccessException.class); } catch (IllegalArgumentException ex) { // expected } try { p.latitude(-90.1); - fail(); + failBecauseExceptionWasNotThrown(IllegalAccessException.class); } catch (IllegalArgumentException ex) { // expected } try { p.latitude(90.1); - fail(); + failBecauseExceptionWasNotThrown(IllegalAccessException.class); } catch (IllegalArgumentException ex) { // expected } try { p.longitude(-180.1); - fail(); + failBecauseExceptionWasNotThrown(IllegalAccessException.class); } catch (IllegalArgumentException ex) { // expected } try { p.longitude(180.1); - fail(); + failBecauseExceptionWasNotThrown(IllegalAccessException.class); } catch (IllegalArgumentException ex) { // expected } @@ -136,31 +143,31 @@ public void testTimeParameters() { r = p.on(2017, 8, 12); assertDate(p, 2017, 8, 12, 0, 0, 0, TimeZone.getDefault()); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.on(2012, 3, 11, 8, 1, 12).midnight(); assertDate(p, 2012, 3, 11, 0, 0, 0, TimeZone.getDefault()); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.on(2016, 4, 10, 14, 11, 59); assertDate(p, 2016, 4, 10, 14, 11, 59, TimeZone.getDefault()); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.timezone("Europe/Berlin"); assertDate(p, 2016, 4, 10, 14, 11, 59, TimeZone.getTimeZone("Europe/Berlin")); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.timezone(TimeZone.getTimeZone("JST")); assertDate(p, 2016, 4, 10, 14, 11, 59, TimeZone.getTimeZone("JST")); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.utc(); assertDate(p, 2016, 4, 10, 14, 11, 59, TimeZone.getTimeZone("UTC")); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.localTime(); assertDate(p, 2016, 4, 10, 14, 11, 59, TimeZone.getDefault()); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.on(2000, 1, 1, 2, 3, 4).now(); assertDate(p, @@ -171,7 +178,7 @@ public void testTimeParameters() { NOW.get(Calendar.MINUTE), NOW.get(Calendar.SECOND), NOW.getTimeZone()); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.on(2000, 2, 2, 3, 4, 5).today(); assertDate(p, @@ -180,7 +187,7 @@ public void testTimeParameters() { NOW.get(Calendar.DAY_OF_MONTH), 0, 0, 0, NOW.getTimeZone()); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.on(2000, 3, 3, 4, 5, 6).on(NOW); assertDate(p, @@ -191,7 +198,7 @@ public void testTimeParameters() { NOW.get(Calendar.MINUTE), NOW.get(Calendar.SECOND), NOW.getTimeZone()); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); r = p.on(2000, 4, 4, 5, 6, 7).on(new Date(NOW.getTimeInMillis())); assertDate(p, @@ -202,28 +209,28 @@ public void testTimeParameters() { NOW.get(Calendar.MINUTE), NOW.get(Calendar.SECOND), NOW.getTimeZone()); - assertThat(r, is(sameInstance(p))); + assertThat(r).isSameAs(p); } private void assertLatLng(TestBuilder p, double lat, double lng, double height) { - assertThat("latitude", p.getLatitude(), is(closeTo(lat, ERROR))); - assertThat("longitude", p.getLongitude(), is(closeTo(lng, ERROR))); - assertThat("latitude-rad", p.getLatitudeRad(), is(closeTo(toRadians(lat), ERROR))); - assertThat("longitude-rad", p.getLongitudeRad(), is(closeTo(toRadians(lng), ERROR))); - assertThat("height", p.getHeight(), is(closeTo(height, ERROR))); + assertThat(p.getLatitude()).as("latitude").isCloseTo(lat, ERROR); + assertThat(p.getLongitude()).as("longitude").isCloseTo(lng, ERROR); + assertThat(p.getLatitudeRad()).as("latitude-rad").isCloseTo(toRadians(lat), ERROR); + assertThat(p.getLongitudeRad()).as("longitude-rad").isCloseTo(toRadians(lng), ERROR); + assertThat(p.getHeight()).as("height").isCloseTo(height, ERROR); } private void assertDate(TestBuilder p, int year, int month, int day, int hour, int minute, int second, TimeZone tz) { Calendar cal = p.getJulianDate().getCalendar(); - assertThat("year", cal.get(Calendar.YEAR), is(year)); - assertThat("month", cal.get(Calendar.MONTH), is(month - 1)); - assertThat("day", cal.get(Calendar.DAY_OF_MONTH), is(day)); - assertThat("hour", cal.get(Calendar.HOUR_OF_DAY), is(hour)); - assertThat("minute", cal.get(Calendar.MINUTE), is(minute)); - assertThat("second", cal.get(Calendar.SECOND), is(second)); - assertThat("timezone", cal.getTimeZone(), is(tz)); + assertThat(cal.get(Calendar.YEAR)).as("year").isEqualTo(year); + assertThat(cal.get(Calendar.MONTH)).as("month").isEqualTo(month - 1); + assertThat(cal.get(Calendar.DAY_OF_MONTH)).as("day").isEqualTo(day); + assertThat(cal.get(Calendar.HOUR_OF_DAY)).as("hour").isEqualTo(hour); + assertThat(cal.get(Calendar.MINUTE)).as("minute").isEqualTo(minute); + assertThat(cal.get(Calendar.SECOND)).as("second").isEqualTo(second); + assertThat(cal.getTimeZone()).as("timezone").isEqualTo(tz); } private static class TestBuilder extends BaseBuilder { diff --git a/src/test/java/org/shredzone/commons/suncalc/util/ExtendedMathTest.java b/src/test/java/org/shredzone/commons/suncalc/util/ExtendedMathTest.java index 840c589..7959a1e 100644 --- a/src/test/java/org/shredzone/commons/suncalc/util/ExtendedMathTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/util/ExtendedMathTest.java @@ -13,10 +13,10 @@ */ package org.shredzone.commons.suncalc.util; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.shredzone.commons.suncalc.util.ExtendedMath.*; +import org.assertj.core.data.Offset; import org.junit.Test; /** @@ -24,31 +24,31 @@ */ public class ExtendedMathTest { - private static final double ERROR = 0.001; + private static final Offset ERROR = Offset.offset(0.001); @Test public void testFrac() { - assertThat(frac( 1.0 ), is(closeTo( 0.0 , ERROR))); - assertThat(frac( 0.5 ), is(closeTo( 0.5 , ERROR))); - assertThat(frac( 123.25), is(closeTo( 0.25, ERROR))); - assertThat(frac( 0.0 ), is(closeTo( 0.0 , ERROR))); - assertThat(frac( -1.0 ), is(closeTo( 0.0 , ERROR))); - assertThat(frac( -0.5 ), is(closeTo(-0.5 , ERROR))); - assertThat(frac(-123.25), is(closeTo(-0.25, ERROR))); + assertThat(frac( 1.0 )).isCloseTo( 0.0 , ERROR); + assertThat(frac( 0.5 )).isCloseTo( 0.5 , ERROR); + assertThat(frac( 123.25)).isCloseTo( 0.25, ERROR); + assertThat(frac( 0.0 )).isCloseTo( 0.0 , ERROR); + assertThat(frac( -1.0 )).isCloseTo( 0.0 , ERROR); + assertThat(frac( -0.5 )).isCloseTo(-0.5 , ERROR); + assertThat(frac(-123.25)).isCloseTo(-0.25, ERROR); } @Test public void testIsZero() { - assertThat(isZero( 1.0 ), is(false)); - assertThat(isZero( 0.0001), is(false)); - assertThat(isZero( 0.0 ), is(true)); - assertThat(isZero(-0.0 ), is(true)); - assertThat(isZero(-0.0001), is(false)); - assertThat(isZero(-1.0 ), is(false)); - assertThat(isZero( Double.NaN), is(false)); - assertThat(isZero(-Double.NaN), is(false)); - assertThat(isZero( Double.POSITIVE_INFINITY), is(false)); - assertThat(isZero( Double.NEGATIVE_INFINITY), is(false)); + assertThat(isZero( 1.0 )).isFalse(); + assertThat(isZero( 0.0001)).isFalse(); + assertThat(isZero( 0.0 )).isTrue(); + assertThat(isZero(-0.0 )).isTrue(); + assertThat(isZero(-0.0001)).isFalse(); + assertThat(isZero(-1.0 )).isFalse(); + assertThat(isZero( Double.NaN)).isFalse(); + assertThat(isZero(-Double.NaN)).isFalse(); + assertThat(isZero( Double.POSITIVE_INFINITY)).isFalse(); + assertThat(isZero( Double.NEGATIVE_INFINITY)).isFalse(); } } diff --git a/src/test/java/org/shredzone/commons/suncalc/util/JulianDateTest.java b/src/test/java/org/shredzone/commons/suncalc/util/JulianDateTest.java index d1ebb98..50a3207 100644 --- a/src/test/java/org/shredzone/commons/suncalc/util/JulianDateTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/util/JulianDateTest.java @@ -14,35 +14,42 @@ package org.shredzone.commons.suncalc.util; import static java.lang.Math.PI; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.data.Offset.offset; import java.util.Calendar; import java.util.TimeZone; +import org.assertj.core.api.AbstractDateAssert; +import org.assertj.core.data.Offset; +import org.junit.BeforeClass; import org.junit.Test; -import org.shredzone.commons.suncalc.DateMatcher; /** * Unit tests for {@link JulianDate}. */ public class JulianDateTest { - private static final double ERROR = 0.001; + private static final Offset ERROR = Offset.offset(0.001); + + @BeforeClass + public static void init() { + AbstractDateAssert.registerCustomDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); + } @Test public void testAtHour() { JulianDate jd = new JulianDate(of(2017, 8, 19, 0, 0, 0, "UTC")); - assertDate(jd, "2017-08-19T00:00:00Z", "UTC"); + assertDate(jd, "2017-08-19T00:00:00Z"); JulianDate jd2 = jd.atHour(8.5); - assertDate(jd2, "2017-08-19T08:30:00Z", "UTC"); + assertDate(jd2, "2017-08-19T08:30:00Z"); JulianDate jd3 = new JulianDate(of(2017, 8, 19, 0, 0, 0, "Europe/Berlin")); - assertDate(jd3, "2017-08-19T00:00:00+0200", "Europe/Berlin"); + assertDate(jd3, "2017-08-19T00:00:00+02:00"); JulianDate jd4 = jd3.atHour(8.5); - assertDate(jd4, "2017-08-19T08:30:00+0200", "Europe/Berlin"); + assertDate(jd4, "2017-08-19T08:30:00+02:00"); } @Test @@ -50,52 +57,52 @@ public void testDateGetters() { Calendar cal = of(2017, 8, 19, 0, 0, 0, "UTC"); JulianDate jd = new JulianDate(cal); - assertThat(jd.getCalendar(), is(cal)); - assertThat(jd.getDate().getTime(), is(cal.getTimeInMillis())); + assertThat(jd.getCalendar()).isNotSameAs(cal); + assertThat(jd.getCalendar()).isEqualTo(cal); } @Test public void testModifiedJulianDate() { // MJD epoch is midnight of November 17th, 1858. JulianDate jd1 = new JulianDate(of(1858, 11, 17, 0, 0, 0, "UTC")); - assertThat(jd1.getModifiedJulianDate(), is(0.0)); - assertThat(jd1.toString(), is("0d 00h 00m 00s")); + assertThat(jd1.getModifiedJulianDate()).isZero(); + assertThat(jd1.toString()).isEqualTo("0d 00h 00m 00s"); JulianDate jd2 = new JulianDate(of(2017, 8, 19, 15, 6, 16, "UTC")); - assertThat(jd2.getModifiedJulianDate(), is(closeTo(57984.629, ERROR))); - assertThat(jd2.toString(), is("57984d 15h 06m 16s")); + assertThat(jd2.getModifiedJulianDate()).isCloseTo(57984.629, ERROR); + assertThat(jd2.toString()).isEqualTo("57984d 15h 06m 16s"); JulianDate jd3 = new JulianDate(of(2017, 8, 19, 15, 6, 16, "GMT+2")); - assertThat(jd3.getModifiedJulianDate(), is(closeTo(57984.546, ERROR))); - assertThat(jd3.toString(), is("57984d 13h 06m 16s")); + assertThat(jd3.getModifiedJulianDate()).isCloseTo(57984.546, ERROR); + assertThat(jd3.toString()).isEqualTo("57984d 13h 06m 16s"); } @Test public void testJulianCentury() { JulianDate jd1 = new JulianDate(of(2000, 1, 1, 0, 0, 0, "UTC")); - assertThat(jd1.getJulianCentury(), is(closeTo(0.0, ERROR))); + assertThat(jd1.getJulianCentury()).isCloseTo(0.0, ERROR); JulianDate jd2 = new JulianDate(of(2017, 1, 1, 0, 0, 0, "UTC")); - assertThat(jd2.getJulianCentury(), is(closeTo(0.17, ERROR))); + assertThat(jd2.getJulianCentury()).isCloseTo(0.17, ERROR); JulianDate jd3 = new JulianDate(of(2050, 7, 1, 0, 0, 0, "UTC")); - assertThat(jd3.getJulianCentury(), is(closeTo(0.505, ERROR))); + assertThat(jd3.getJulianCentury()).isCloseTo(0.505, ERROR); } @Test public void testGreenwichMeanSiderealTime() { JulianDate jd1 = new JulianDate(of(2017, 9, 3, 19, 5, 15, "UTC")); - assertThat(jd1.getGreenwichMeanSiderealTime(), is(closeTo(4.702, ERROR))); + assertThat(jd1.getGreenwichMeanSiderealTime()).isCloseTo(4.702, ERROR); } @Test public void testTrueAnomaly() { JulianDate jd1 = new JulianDate(of(2017, 1, 4, 0, 0, 0, "UTC")); - assertThat(jd1.getTrueAnomaly(), is(closeTo(0.0, 0.1))); + assertThat(jd1.getTrueAnomaly()).isCloseTo(0.0, offset(0.1)); JulianDate jd2 = new JulianDate(of(2017, 7, 4, 0, 0, 0, "UTC")); - assertThat(jd2.getTrueAnomaly(), is(closeTo(PI, 0.1))); + assertThat(jd2.getTrueAnomaly()).isCloseTo(PI, offset(0.1)); } private Calendar of(int year, int month, int day, int hour, int minute, int second, String zone) { @@ -105,8 +112,8 @@ private Calendar of(int year, int month, int day, int hour, int minute, int seco return cal; } - private void assertDate(JulianDate jd, String date, String tz) { - assertThat(jd.getCalendar().getTime(), DateMatcher.is(date, tz)); + private void assertDate(JulianDate jd, String date) { + assertThat(jd.getCalendar().getTime()).isEqualTo(date); } } diff --git a/src/test/java/org/shredzone/commons/suncalc/util/MatrixTest.java b/src/test/java/org/shredzone/commons/suncalc/util/MatrixTest.java index 63c1a90..8399862 100644 --- a/src/test/java/org/shredzone/commons/suncalc/util/MatrixTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/util/MatrixTest.java @@ -14,9 +14,9 @@ package org.shredzone.commons.suncalc.util; import static java.lang.Math.PI; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; +import org.assertj.core.data.Offset; import org.junit.Test; /** @@ -24,7 +24,7 @@ */ public class MatrixTest { - private static final double ERROR = 0.001; + private static final Offset ERROR = Offset.offset(0.001); private static final double PI_HALF = PI / 2.0; @Test @@ -143,9 +143,9 @@ public void testVectorMultiply() { Matrix mx = Matrix.rotateX(PI_HALF); Vector vc = new Vector(5.0, 8.0, -3.0); Vector result = mx.multiply(vc); - assertThat(result.getX(), is(closeTo( 5.0, ERROR))); - assertThat(result.getY(), is(closeTo(-3.0, ERROR))); - assertThat(result.getZ(), is(closeTo(-8.0, ERROR))); + assertThat(result.getX()).isCloseTo( 5.0, ERROR); + assertThat(result.getY()).isCloseTo(-3.0, ERROR); + assertThat(result.getZ()).isCloseTo(-8.0, ERROR); } @Test @@ -154,12 +154,12 @@ public void testEquals() { Matrix mx2 = Matrix.rotateX(PI_HALF); Matrix mx3 = Matrix.identity(); - assertThat(mx1.equals(mx2), is(false)); - assertThat(mx1.equals(mx3), is(true)); - assertThat(mx2.equals(mx3), is(false)); - assertThat(mx3.equals(mx1), is(true)); - assertThat(mx1.equals(null), is(false)); - assertThat(mx1.equals(new Object()), is(false)); + assertThat(mx1.equals(mx2)).isFalse(); + assertThat(mx1.equals(mx3)).isTrue(); + assertThat(mx2.equals(mx3)).isFalse(); + assertThat(mx3.equals(mx1)).isTrue(); + assertThat(mx1.equals(null)).isFalse(); + assertThat(mx1.equals(new Object())).isFalse(); } @Test @@ -168,26 +168,27 @@ public void testHashCode() { int mx2 = Matrix.rotateX(PI_HALF).hashCode(); int mx3 = Matrix.identity().hashCode(); - assertThat(mx1, not(equalTo(0))); - assertThat(mx2, not(equalTo(0))); - assertThat(mx3, not(equalTo(0))); - assertThat(mx1, not(equalTo(mx2))); - assertThat(mx1, is(equalTo(mx3))); + assertThat(mx1).isNotEqualTo(0); + assertThat(mx2).isNotEqualTo(0); + assertThat(mx3).isNotEqualTo(0); + assertThat(mx1).isNotEqualTo(mx2); + assertThat(mx1).isEqualTo(mx3); } @Test public void testToString() { Matrix mx = Matrix.identity(); - assertThat(mx.toString(), is("[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]")); + assertThat(mx.toString()).isEqualTo("[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]"); } private void assertValues(Matrix mx, double... values) { for (int ix = 0; ix < values.length; ix++) { int r = ix / 3; int c = ix % 3; - assertThat("r=" + r +", c=" + c, - mx.get(r, c), is(closeTo(values[ix], ERROR))); + assertThat(mx.get(r, c)) + .as("r=%d, c=%d", r, c) + .isCloseTo(values[ix], ERROR); } } diff --git a/src/test/java/org/shredzone/commons/suncalc/util/QuadraticInterpolationTest.java b/src/test/java/org/shredzone/commons/suncalc/util/QuadraticInterpolationTest.java index 24143a8..6bc1062 100644 --- a/src/test/java/org/shredzone/commons/suncalc/util/QuadraticInterpolationTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/util/QuadraticInterpolationTest.java @@ -13,9 +13,9 @@ */ package org.shredzone.commons.suncalc.util; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; +import org.assertj.core.data.Offset; import org.junit.Test; /** @@ -23,48 +23,48 @@ */ public class QuadraticInterpolationTest { - private static final double ERROR = 0.001; + private static final Offset ERROR = Offset.offset(0.001); @Test public void testTwoRootsAndMinimum() { QuadraticInterpolation qi = new QuadraticInterpolation(1.0, -1.0, 1.0); - assertThat(qi.getNumberOfRoots(), is(2)); - assertThat(qi.getRoot1(), is(closeTo(-0.707, ERROR))); - assertThat(qi.getRoot2(), is(closeTo( 0.707, ERROR))); - assertThat(qi.getXe(), is(closeTo( 0.0, ERROR))); - assertThat(qi.getYe(), is(closeTo(-1.0, ERROR))); - assertThat(qi.isMaximum(), is(false)); + assertThat(qi.getNumberOfRoots()).isEqualTo(2); + assertThat(qi.getRoot1()).isCloseTo(-0.707, ERROR); + assertThat(qi.getRoot2()).isCloseTo( 0.707, ERROR); + assertThat(qi.getXe()).isCloseTo( 0.0, ERROR); + assertThat(qi.getYe()).isCloseTo(-1.0, ERROR); + assertThat(qi.isMaximum()).isFalse(); } @Test public void testTwoRootsAndMaximum() { QuadraticInterpolation qi = new QuadraticInterpolation(-1.0, 1.0, -1.0); - assertThat(qi.getNumberOfRoots(), is(2)); - assertThat(qi.getRoot1(), is(closeTo(-0.707, ERROR))); - assertThat(qi.getRoot2(), is(closeTo( 0.707, ERROR))); - assertThat(qi.getXe(), is(closeTo(0.0, ERROR))); - assertThat(qi.getYe(), is(closeTo(1.0, ERROR))); - assertThat(qi.isMaximum(), is(true)); + assertThat(qi.getNumberOfRoots()).isEqualTo(2); + assertThat(qi.getRoot1()).isCloseTo(-0.707, ERROR); + assertThat(qi.getRoot2()).isCloseTo( 0.707, ERROR); + assertThat(qi.getXe()).isCloseTo(0.0, ERROR); + assertThat(qi.getYe()).isCloseTo(1.0, ERROR); + assertThat(qi.isMaximum()).isTrue(); } @Test public void testOneRoot() { QuadraticInterpolation qi = new QuadraticInterpolation(2.0, 0.0, -1.0); - assertThat(qi.getNumberOfRoots(), is(1)); - assertThat(qi.getRoot1(), is(closeTo( 0.0, ERROR))); - assertThat(qi.getXe(), is(closeTo( 1.5, ERROR))); - assertThat(qi.getYe(), is(closeTo(-1.125, ERROR))); - assertThat(qi.isMaximum(), is(false)); + assertThat(qi.getNumberOfRoots()).isEqualTo(1); + assertThat(qi.getRoot1()).isCloseTo( 0.0, ERROR); + assertThat(qi.getXe()).isCloseTo( 1.5, ERROR); + assertThat(qi.getYe()).isCloseTo(-1.125, ERROR); + assertThat(qi.isMaximum()).isFalse(); } @Test public void testNoRoot() { QuadraticInterpolation qi = new QuadraticInterpolation(3.0, 2.0, 1.0); - assertThat(qi.getNumberOfRoots(), is(0)); + assertThat(qi.getNumberOfRoots()).isZero(); } } diff --git a/src/test/java/org/shredzone/commons/suncalc/util/VectorTest.java b/src/test/java/org/shredzone/commons/suncalc/util/VectorTest.java index f081e85..e558c6f 100644 --- a/src/test/java/org/shredzone/commons/suncalc/util/VectorTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/util/VectorTest.java @@ -14,9 +14,9 @@ package org.shredzone.commons.suncalc.util; import static java.lang.Math.PI; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; +import org.assertj.core.data.Offset; import org.junit.Test; /** @@ -24,30 +24,30 @@ */ public class VectorTest { - private static final double ERROR = 0.001; + private static final Offset ERROR = Offset.offset(0.001); private static final double PI_HALF = PI / 2.0; @Test public void testConstructors() { Vector v1 = new Vector(20.0, 10.0, 5.0); - assertThat(v1.getX(), is(20.0)); - assertThat(v1.getY(), is(10.0)); - assertThat(v1.getZ(), is(5.0)); + assertThat(v1.getX()).isEqualTo(20.0); + assertThat(v1.getY()).isEqualTo(10.0); + assertThat(v1.getZ()).isEqualTo(5.0); Vector v2 = new Vector(new double[] { 20.0, 10.0, 5.0 }); - assertThat(v2.getX(), is(20.0)); - assertThat(v2.getY(), is(10.0)); - assertThat(v2.getZ(), is(5.0)); + assertThat(v2.getX()).isEqualTo(20.0); + assertThat(v2.getY()).isEqualTo(10.0); + assertThat(v2.getZ()).isEqualTo(5.0); Vector v3 = Vector.ofPolar(0.5, 0.25); - assertThat(v3.getPhi(), is(0.5)); - assertThat(v3.getTheta(), is(0.25)); - assertThat(v3.getR(), is(1.0)); + assertThat(v3.getPhi()).isEqualTo(0.5); + assertThat(v3.getTheta()).isEqualTo(0.25); + assertThat(v3.getR()).isEqualTo(1.0); Vector v4 = Vector.ofPolar(0.5, 0.25, 50.0); - assertThat(v4.getPhi(), is(0.5)); - assertThat(v4.getTheta(), is(0.25)); - assertThat(v4.getR(), is(50.0)); + assertThat(v4.getPhi()).isEqualTo(0.5); + assertThat(v4.getTheta()).isEqualTo(0.25); + assertThat(v4.getR()).isEqualTo(50.0); } @Test(expected = IllegalArgumentException.class) @@ -61,14 +61,14 @@ public void testAdd() { Vector v2 = new Vector(10.0, 25.0, 15.0); Vector r1 = v1.add(v2); - assertThat(r1.getX(), is(30.0)); - assertThat(r1.getY(), is(35.0)); - assertThat(r1.getZ(), is(20.0)); + assertThat(r1.getX()).isEqualTo(30.0); + assertThat(r1.getY()).isEqualTo(35.0); + assertThat(r1.getZ()).isEqualTo(20.0); Vector r2 = v2.add(v1); - assertThat(r2.getX(), is(30.0)); - assertThat(r2.getY(), is(35.0)); - assertThat(r2.getZ(), is(20.0)); + assertThat(r2.getX()).isEqualTo(30.0); + assertThat(r2.getY()).isEqualTo(35.0); + assertThat(r2.getZ()).isEqualTo(20.0); } @Test @@ -77,14 +77,14 @@ public void testSubtract() { Vector v2 = new Vector(10.0, 25.0, 15.0); Vector r1 = v1.subtract(v2); - assertThat(r1.getX(), is(10.0)); - assertThat(r1.getY(), is(-15.0)); - assertThat(r1.getZ(), is(-10.0)); + assertThat(r1.getX()).isEqualTo(10.0); + assertThat(r1.getY()).isEqualTo(-15.0); + assertThat(r1.getZ()).isEqualTo(-10.0); Vector r2 = v2.subtract(v1); - assertThat(r2.getX(), is(-10.0)); - assertThat(r2.getY(), is(15.0)); - assertThat(r2.getZ(), is(10.0)); + assertThat(r2.getX()).isEqualTo(-10.0); + assertThat(r2.getY()).isEqualTo(15.0); + assertThat(r2.getZ()).isEqualTo(10.0); } @Test @@ -92,9 +92,9 @@ public void testMultiply() { Vector v1 = new Vector(20.0, 10.0, 5.0); Vector r1 = v1.multiply(5.0); - assertThat(r1.getX(), is(100.0)); - assertThat(r1.getY(), is(50.0)); - assertThat(r1.getZ(), is(25.0)); + assertThat(r1.getX()).isEqualTo(100.0); + assertThat(r1.getY()).isEqualTo(50.0); + assertThat(r1.getZ()).isEqualTo(25.0); } @Test @@ -102,9 +102,9 @@ public void testNegate() { Vector v1 = new Vector(20.0, 10.0, 5.0); Vector r1 = v1.negate(); - assertThat(r1.getX(), is(-20.0)); - assertThat(r1.getY(), is(-10.0)); - assertThat(r1.getZ(), is(-5.0)); + assertThat(r1.getX()).isEqualTo(-20.0); + assertThat(r1.getY()).isEqualTo(-10.0); + assertThat(r1.getZ()).isEqualTo(-5.0); } @Test @@ -113,9 +113,9 @@ public void testCross() { Vector v2 = new Vector(4.0, 9.0, 2.0); Vector r1 = v1.cross(v2); - assertThat(r1.getX(), is(-15.0)); - assertThat(r1.getY(), is(-2.0)); - assertThat(r1.getZ(), is(39.0)); + assertThat(r1.getX()).isEqualTo(-15.0); + assertThat(r1.getY()).isEqualTo(-2.0); + assertThat(r1.getZ()).isEqualTo(39.0); } @Test @@ -124,7 +124,7 @@ public void testDot() { Vector v2 = new Vector(4.0, -5.0, 6.0); double r1 = v1.dot(v2); - assertThat(r1, is(closeTo(12.0, ERROR))); + assertThat(r1).isCloseTo(12.0, ERROR); } @Test @@ -132,7 +132,7 @@ public void testNorm() { Vector v1 = new Vector(5.0, -6.0, 7.0); double r1 = v1.norm(); - assertThat(r1, is(closeTo(10.488, ERROR))); + assertThat(r1).isCloseTo(10.488, ERROR); } @Test @@ -141,12 +141,12 @@ public void testEquals() { Vector v2 = new Vector(4.0, 9.0, 2.0); Vector v3 = new Vector(3.0, -3.0, 1.0); - assertThat(v1.equals(v2), is(false)); - assertThat(v1.equals(v3), is(true)); - assertThat(v2.equals(v3), is(false)); - assertThat(v3.equals(v1), is(true)); - assertThat(v1.equals(null), is(false)); - assertThat(v1.equals(new Object()), is(false)); + assertThat(v1.equals(v2)).isFalse(); + assertThat(v1.equals(v3)).isTrue(); + assertThat(v2.equals(v3)).isFalse(); + assertThat(v3.equals(v1)).isTrue(); + assertThat(v1.equals(null)).isFalse(); + assertThat(v1.equals(new Object())).isFalse(); } @Test @@ -155,109 +155,109 @@ public void testHashCode() { int h2 = new Vector(4.0, 9.0, 2.0).hashCode(); int h3 = new Vector(3.0, -3.0, 1.0).hashCode(); - assertThat(h1, not(equalTo(0))); - assertThat(h2, not(equalTo(0))); - assertThat(h3, not(equalTo(0))); - assertThat(h1, not(equalTo(h2))); - assertThat(h1, is(equalTo(h3))); + assertThat(h1).isNotZero(); + assertThat(h2).isNotZero(); + assertThat(h3).isNotZero(); + assertThat(h1).isNotEqualTo(h2); + assertThat(h1).isEqualTo(h3); } @Test public void testToString() { Vector v1 = new Vector(3.0, -3.0, 1.0); - assertThat(v1.toString(), is("(x=3.0, y=-3.0, z=1.0)")); + assertThat(v1.toString()).isEqualTo("(x=3.0, y=-3.0, z=1.0)"); } @Test public void testToCartesian() { Vector v1 = Vector.ofPolar(0.0, 0.0); - assertThat(v1.getX(), is(closeTo(1.0, ERROR))); - assertThat(v1.getY(), is(closeTo(0.0, ERROR))); - assertThat(v1.getZ(), is(closeTo(0.0, ERROR))); + assertThat(v1.getX()).isCloseTo(1.0, ERROR); + assertThat(v1.getY()).isCloseTo(0.0, ERROR); + assertThat(v1.getZ()).isCloseTo(0.0, ERROR); Vector v2 = Vector.ofPolar(PI_HALF, 0.0); - assertThat(v2.getX(), is(closeTo(0.0, ERROR))); - assertThat(v2.getY(), is(closeTo(1.0, ERROR))); - assertThat(v2.getZ(), is(closeTo(0.0, ERROR))); + assertThat(v2.getX()).isCloseTo(0.0, ERROR); + assertThat(v2.getY()).isCloseTo(1.0, ERROR); + assertThat(v2.getZ()).isCloseTo(0.0, ERROR); Vector v3 = Vector.ofPolar(0.0, PI_HALF); - assertThat(v3.getX(), is(closeTo(0.0, ERROR))); - assertThat(v3.getY(), is(closeTo(0.0, ERROR))); - assertThat(v3.getZ(), is(closeTo(1.0, ERROR))); + assertThat(v3.getX()).isCloseTo(0.0, ERROR); + assertThat(v3.getY()).isCloseTo(0.0, ERROR); + assertThat(v3.getZ()).isCloseTo(1.0, ERROR); Vector v4 = Vector.ofPolar(PI_HALF, PI_HALF); - assertThat(v4.getX(), is(closeTo(0.0, ERROR))); - assertThat(v4.getY(), is(closeTo(0.0, ERROR))); - assertThat(v4.getZ(), is(closeTo(1.0, ERROR))); + assertThat(v4.getX()).isCloseTo(0.0, ERROR); + assertThat(v4.getY()).isCloseTo(0.0, ERROR); + assertThat(v4.getZ()).isCloseTo(1.0, ERROR); Vector v5 = Vector.ofPolar(PI_HALF, -PI_HALF); - assertThat(v5.getX(), is(closeTo(0.0, ERROR))); - assertThat(v5.getY(), is(closeTo(0.0, ERROR))); - assertThat(v5.getZ(), is(closeTo(-1.0, ERROR))); + assertThat(v5.getX()).isCloseTo(0.0, ERROR); + assertThat(v5.getY()).isCloseTo(0.0, ERROR); + assertThat(v5.getZ()).isCloseTo(-1.0, ERROR); Vector v6 = Vector.ofPolar(0.0, 0.0, 5.0); - assertThat(v6.getX(), is(closeTo(5.0, ERROR))); - assertThat(v6.getY(), is(closeTo(0.0, ERROR))); - assertThat(v6.getZ(), is(closeTo(0.0, ERROR))); + assertThat(v6.getX()).isCloseTo(5.0, ERROR); + assertThat(v6.getY()).isCloseTo(0.0, ERROR); + assertThat(v6.getZ()).isCloseTo(0.0, ERROR); Vector v7 = Vector.ofPolar(PI_HALF, 0.0, 5.0); - assertThat(v7.getX(), is(closeTo(0.0, ERROR))); - assertThat(v7.getY(), is(closeTo(5.0, ERROR))); - assertThat(v7.getZ(), is(closeTo(0.0, ERROR))); + assertThat(v7.getX()).isCloseTo(0.0, ERROR); + assertThat(v7.getY()).isCloseTo(5.0, ERROR); + assertThat(v7.getZ()).isCloseTo(0.0, ERROR); Vector v8 = Vector.ofPolar(0.0, PI_HALF, 5.0); - assertThat(v8.getX(), is(closeTo(0.0, ERROR))); - assertThat(v8.getY(), is(closeTo(0.0, ERROR))); - assertThat(v8.getZ(), is(closeTo(5.0, ERROR))); + assertThat(v8.getX()).isCloseTo(0.0, ERROR); + assertThat(v8.getY()).isCloseTo(0.0, ERROR); + assertThat(v8.getZ()).isCloseTo(5.0, ERROR); Vector v9 = Vector.ofPolar(PI_HALF, PI_HALF, 5.0); - assertThat(v9.getX(), is(closeTo(0.0, ERROR))); - assertThat(v9.getY(), is(closeTo(0.0, ERROR))); - assertThat(v9.getZ(), is(closeTo(5.0, ERROR))); + assertThat(v9.getX()).isCloseTo(0.0, ERROR); + assertThat(v9.getY()).isCloseTo(0.0, ERROR); + assertThat(v9.getZ()).isCloseTo(5.0, ERROR); Vector v10 = Vector.ofPolar(PI_HALF, -PI_HALF, 5.0); - assertThat(v10.getX(), is(closeTo(0.0, ERROR))); - assertThat(v10.getY(), is(closeTo(0.0, ERROR))); - assertThat(v10.getZ(), is(closeTo(-5.0, ERROR))); + assertThat(v10.getX()).isCloseTo(0.0, ERROR); + assertThat(v10.getY()).isCloseTo(0.0, ERROR); + assertThat(v10.getZ()).isCloseTo(-5.0, ERROR); } @Test public void testToPolar() { Vector v1 = new Vector(20.0, 0.0, 0.0); - assertThat(v1.getPhi(), is(0.0)); - assertThat(v1.getTheta(), is(0.0)); - assertThat(v1.getR(), is(20.0)); + assertThat(v1.getPhi()).isEqualTo(0.0); + assertThat(v1.getTheta()).isEqualTo(0.0); + assertThat(v1.getR()).isEqualTo(20.0); Vector v2 = new Vector(0.0, 20.0, 0.0); - assertThat(v2.getPhi(), is(PI_HALF)); - assertThat(v2.getTheta(), is(0.0)); - assertThat(v2.getR(), is(20.0)); + assertThat(v2.getPhi()).isEqualTo(PI_HALF); + assertThat(v2.getTheta()).isEqualTo(0.0); + assertThat(v2.getR()).isEqualTo(20.0); Vector v3 = new Vector(0.0, 0.0, 20.0); - assertThat(v3.getPhi(), is(0.0)); - assertThat(v3.getTheta(), is(PI_HALF)); - assertThat(v3.getR(), is(20.0)); + assertThat(v3.getPhi()).isEqualTo(0.0); + assertThat(v3.getTheta()).isEqualTo(PI_HALF); + assertThat(v3.getR()).isEqualTo(20.0); Vector v4 = new Vector(-20.0, 0.0, 0.0); - assertThat(v4.getPhi(), is(PI)); - assertThat(v4.getTheta(), is(0.0)); - assertThat(v4.getR(), is(20.0)); + assertThat(v4.getPhi()).isEqualTo(PI); + assertThat(v4.getTheta()).isEqualTo(0.0); + assertThat(v4.getR()).isEqualTo(20.0); Vector v5 = new Vector(0.0, -20.0, 0.0); - assertThat(v5.getPhi(), is(PI + PI_HALF)); - assertThat(v5.getTheta(), is(0.0)); - assertThat(v5.getR(), is(20.0)); + assertThat(v5.getPhi()).isEqualTo(PI + PI_HALF); + assertThat(v5.getTheta()).isEqualTo(0.0); + assertThat(v5.getR()).isEqualTo(20.0); Vector v6 = new Vector(0.0, 0.0, -20.0); - assertThat(v6.getPhi(), is(0.0)); - assertThat(v6.getTheta(), is(-PI_HALF)); - assertThat(v6.getR(), is(20.0)); + assertThat(v6.getPhi()).isEqualTo(0.0); + assertThat(v6.getTheta()).isEqualTo(-PI_HALF); + assertThat(v6.getR()).isEqualTo(20.0); Vector v7 = new Vector(0.0, 0.0, 0.0); - assertThat(v7.getPhi(), is(0.0)); - assertThat(v7.getTheta(), is(0.0)); - assertThat(v7.getR(), is(0.0)); + assertThat(v7.getPhi()).isEqualTo(0.0); + assertThat(v7.getTheta()).isEqualTo(0.0); + assertThat(v7.getR()).isEqualTo(0.0); } }