@Override public MonthDay decode( BsonReader reader, DecoderContext decoderContext) { BigDecimal value = reader .readDecimal128() .bigDecimalValue(); int month = value.intValue(); return of( month, value.subtract(new BigDecimal(month)) .scaleByPowerOfTen(2) .intValue() ); }
/** * Returns events that occurred on the day of month specified. * * @param monthDay an object that wraps the month of year and the day of * month * @return the list of objects that represent events that occurred on the * day of month specified * @throws IOException if any I/O error occurs */ public List<Event> getEvents(MonthDay monthDay) throws IOException { String url = getWikipediaURL(monthDay); logger.info("Retrieving web page from {}", url); Document doc = Jsoup.connect(url).timeout(timeout).get(); List<Event> events = new ArrayList<Event>(); Elements elements = doc.select("h2:has(#Events) + ul > li"); logger.info("Found {} event(s)", elements.size()); for (Element element: elements) { logger.debug("Text to be parsed: {}", element.text()); String[] parts = element.text().split(" \u2013 ", 2); if (parts.length != 2) { logger.warn("Skipping a malformed event"); continue; } parts[0] = parts[0].trim(); parts[1] = parts[1].trim(); int year = Year.parse(parts[0], formatter).getValue(); Event event = new Event(monthDay.atYear(year), parts[1]); logger.debug("Event created: {}", event); events.add(event); } return events; }
@Test public void test2() throws NoSuchMethodException, SecurityException, SQLException { PropertyMapperManager mapper = new PropertyMapperManager(); assertThat(mapper.getValue(JavaType.of(Year.class), newResultSet("getInt", 2000), 1), is(Year.of(2000))); assertThat(mapper.getValue(JavaType.of(YearMonth.class), newResultSet("getInt", 200004), 1), is(YearMonth.of(2000, 4))); assertThat(mapper.getValue(JavaType.of(MonthDay.class), newResultSet("getInt", 401), 1), is(MonthDay.of(4, 1))); assertThat(mapper.getValue(JavaType.of(Month.class), newResultSet("getInt", 4), 1), is(Month.APRIL)); assertThat(mapper.getValue(JavaType.of(DayOfWeek.class), newResultSet("getInt", 4), 1), is(DayOfWeek.THURSDAY)); assertThat(mapper.getValue(JavaType.of(Year.class), newResultSet("getInt", 0, "wasNull", true), 1), is(nullValue())); assertThat(mapper.getValue(JavaType.of(YearMonth.class), newResultSet("getInt", 0, "wasNull", true), 1), is(nullValue())); assertThat(mapper.getValue(JavaType.of(MonthDay.class), newResultSet("getInt", 0, "wasNull", true), 1), is(nullValue())); assertThat(mapper.getValue(JavaType.of(Month.class), newResultSet("getInt", 0, "wasNull", true), 1), is(nullValue())); assertThat(mapper.getValue(JavaType.of(DayOfWeek.class), newResultSet("getInt", 0, "wasNull", true), 1), is(nullValue())); }
void doTest_comparisons_MonthDay(MonthDay... localDates) { for (int i = 0; i < localDates.length; i++) { MonthDay a = localDates[i]; for (int j = 0; j < localDates.length; j++) { MonthDay b = localDates[j]; if (i < j) { assertTrue(a.compareTo(b) < 0, a + " <=> " + b); assertEquals(a.isBefore(b), true, a + " <=> " + b); assertEquals(a.isAfter(b), false, a + " <=> " + b); assertEquals(a.equals(b), false, a + " <=> " + b); } else if (i > j) { assertTrue(a.compareTo(b) > 0, a + " <=> " + b); assertEquals(a.isBefore(b), false, a + " <=> " + b); assertEquals(a.isAfter(b), true, a + " <=> " + b); assertEquals(a.equals(b), false, a + " <=> " + b); } else { assertEquals(a.compareTo(b), 0, a + " <=> " + b); assertEquals(a.isBefore(b), false, a + " <=> " + b); assertEquals(a.isAfter(b), false, a + " <=> " + b); assertEquals(a.equals(b), true, a + " <=> " + b); } } } }
@Override public void encode( BsonWriter writer, MonthDay value, EncoderContext encoderContext) { writer.writeDecimal128(parse(format( "%d.%02d", value.getMonthValue(), value.getDayOfMonth() ))); }
@DataProvider(name="isValidMonthDay") Object[][] data_isValidMonthDay() { return new Object[][] { {Year.of(2007), MonthDay.of(6, 30), true}, {Year.of(2008), MonthDay.of(2, 28), true}, {Year.of(2008), MonthDay.of(2, 29), true}, {Year.of(2009), MonthDay.of(2, 28), true}, {Year.of(2009), MonthDay.of(2, 29), false}, {Year.of(2009), null, false}, }; }
@DataProvider(name="atMonthDay") Object[][] data_atMonthDay() { return new Object[][] { {Year.of(2008), MonthDay.of(6, 30), LocalDate.of(2008, 6, 30)}, {Year.of(2008), MonthDay.of(2, 29), LocalDate.of(2008, 2, 29)}, {Year.of(2009), MonthDay.of(2, 29), LocalDate.of(2009, 2, 28)}, }; }
@Test public void test_serialization_format() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (DataOutputStream dos = new DataOutputStream(baos) ) { dos.writeByte(13); // java.time.temporal.Ser.MONTH_DAY_TYPE dos.writeByte(9); dos.writeByte(16); } byte[] bytes = baos.toByteArray(); assertSerializedBySer(MonthDay.of(9, 16), bytes); }
@Test(dataProvider = "fields") public void test_WBY_isSupportedBy(TemporalField weekField, TemporalField yearField) { assertEquals(yearField.isSupportedBy(LocalTime.NOON), false); assertEquals(yearField.isSupportedBy(MonthDay.of(2, 1)), false); assertEquals(yearField.isSupportedBy(LocalDate.MIN), true); assertEquals(yearField.isSupportedBy(OffsetDateTime.MAX), true); }
@Test public void now() { MonthDay expected = MonthDay.now(Clock.systemDefaultZone()); MonthDay test = MonthDay.now(); for (int i = 0; i < 100; i++) { if (expected.equals(test)) { return; } expected = MonthDay.now(Clock.systemDefaultZone()); test = MonthDay.now(); } assertEquals(test, expected); }
@Test public void now_Clock() { Instant instant = LocalDateTime.of(2010, 12, 31, 0, 0).toInstant(ZoneOffset.UTC); Clock clock = Clock.fixed(instant, ZoneOffset.UTC); MonthDay test = MonthDay.now(clock); assertEquals(test.getMonth(), Month.DECEMBER); assertEquals(test.getDayOfMonth(), 31); }
@Test(dataProvider = "fields") public void test_WOWBY_isSupportedBy(TemporalField weekField, TemporalField yearField) { assertEquals(weekField.isSupportedBy(LocalTime.NOON), false); assertEquals(weekField.isSupportedBy(MonthDay.of(2, 1)), false); assertEquals(weekField.isSupportedBy(LocalDate.MIN), true); assertEquals(weekField.isSupportedBy(OffsetDateTime.MAX), true); }
@Test public void test_hashCode_unique() { int leapYear = 2008; Set<Integer> uniques = new HashSet<Integer>(366); for (int i = 1; i <= 12; i++) { for (int j = 1; j <= 31; j++) { if (YearMonth.of(leapYear, i).isValidDay(j)) { assertTrue(uniques.add(MonthDay.of(i, j).hashCode())); } } } }
@Test(expectedExceptions=DateTimeException.class) public void test_atYear_int_invalidYear() { MonthDay test = MonthDay.of(6, 30); test.atYear(Integer.MIN_VALUE); }
@Test public void test_comparisons() { doTest_comparisons_MonthDay( MonthDay.of(1, 1), MonthDay.of(1, 31), MonthDay.of(2, 1), MonthDay.of(2, 29), MonthDay.of(3, 1), MonthDay.of(12, 31) ); }
@Test public void test_equals() { MonthDay a = MonthDay.of(1, 1); MonthDay b = MonthDay.of(1, 1); MonthDay c = MonthDay.of(2, 1); MonthDay d = MonthDay.of(1, 2); assertEquals(a.equals(a), true); assertEquals(a.equals(b), true); assertEquals(a.equals(c), false); assertEquals(a.equals(d), false); assertEquals(b.equals(a), true); assertEquals(b.equals(b), true); assertEquals(b.equals(c), false); assertEquals(b.equals(d), false); assertEquals(c.equals(a), false); assertEquals(c.equals(b), false); assertEquals(c.equals(c), true); assertEquals(c.equals(d), false); assertEquals(d.equals(a), false); assertEquals(d.equals(b), false); assertEquals(d.equals(c), false); assertEquals(d.equals(d), true); }
@Test public void test_parse_errorMessage() throws Exception { assertGoodErrorDate(DayOfWeek::from, "DayOfWeek"); assertGoodErrorDate(Month::from, "Month"); assertGoodErrorDate(YearMonth::from, "YearMonth"); assertGoodErrorDate(MonthDay::from, "MonthDay"); assertGoodErrorDate(LocalDate::from, "LocalDate"); assertGoodErrorDate(LocalTime::from, "LocalTime"); assertGoodErrorDate(LocalDateTime::from, "LocalDateTime"); assertGoodErrorDate(OffsetTime::from, "OffsetTime"); assertGoodErrorDate(OffsetDateTime::from, "OffsetDateTime"); assertGoodErrorDate(ZonedDateTime::from, "ZonedDateTime"); assertGoodErrorDate(Instant::from, "Instant"); assertGoodErrorDate(ZoneOffset::from, "ZoneOffset"); assertGoodErrorDate(ZoneId::from, "ZoneId"); assertGoodErrorDate(ThaiBuddhistChronology.INSTANCE::date, ""); assertGoodErrorTime(DayOfWeek::from, "DayOfWeek"); assertGoodErrorTime(Month::from, "Month"); assertGoodErrorTime(Year::from, "Year"); assertGoodErrorTime(YearMonth::from, "YearMonth"); assertGoodErrorTime(MonthDay::from, "MonthDay"); assertGoodErrorTime(LocalDate::from, "LocalDate"); assertGoodErrorTime(LocalTime::from, "LocalTime"); assertGoodErrorTime(LocalDateTime::from, "LocalDateTime"); assertGoodErrorTime(OffsetTime::from, "OffsetTime"); assertGoodErrorTime(OffsetDateTime::from, "OffsetDateTime"); assertGoodErrorTime(ZonedDateTime::from, "ZonedDateTime"); assertGoodErrorTime(Instant::from, "Instant"); assertGoodErrorTime(ZoneOffset::from, "ZoneOffset"); assertGoodErrorTime(ZoneId::from, "ZoneId"); assertGoodErrorTime(ThaiBuddhistChronology.INSTANCE::date, ""); }
@Override public Class<MonthDay> getEncoderClass() { return MonthDay.class; }
@Test public void test_with_Month_noChangeSame() { MonthDay test = MonthDay.of(6, 30); assertSame(test.with(Month.JUNE), test); }
@Test(expectedExceptions=NullPointerException.class) public void factory_intMonth_nullMonth() { MonthDay.of(null, 15); }
@Test(expectedExceptions=DateTimeParseException.class) public void factory_parse_illegalValue_Month() { MonthDay.parse("--13-25"); }
Holiday(String key, MonthDay start, MonthDay end) { this.key = key; this.start = start; this.end = end; }
@BeforeMethod public void setUp() { TEST_07_15 = MonthDay.of(7, 15); }
@Test(dataProvider="goodParseData") public void factory_parse_success(String text, MonthDay expected) { MonthDay monthDay = MonthDay.parse(text); assertEquals(monthDay, expected); }
@Test(expectedExceptions=NullPointerException.class) public void test_with_Month_null() { MonthDay.of(6, 30).with((Month) null); }
@Test(expectedExceptions=DateTimeException.class) public void test_withMonth_tooLow() { MonthDay.of(6, 30).withMonth(0); }
@Test(dataProvider="isValidMonthDay") public void test_isValidMonthDay(Year year, MonthDay monthDay, boolean expected) { assertEquals(year.isValidMonthDay(monthDay), expected); }
@Test(expectedExceptions=NullPointerException.class) public void test_atMonthDay_nullMonthDay() { Year test = Year.of(2008); test.atMonthDay((MonthDay) null); }
@Test public void test_invalid_serialform() throws Exception { assertNotSerializable(MonthDay.class); }
@Test public void test_withMonth() { assertEquals(MonthDay.of(6, 30).withMonth(1), MonthDay.of(1, 30)); }
void check(MonthDay test, int m, int d) { assertEquals(test.getMonth().getValue(), m); assertEquals(test.getDayOfMonth(), d); }
@Test public void test_with_Month_noChangeEqual() { MonthDay test = MonthDay.of(6, 30); assertEquals(test.with(Month.JUNE), test); }