@Test public void test3() throws NoSuchMethodException, SecurityException, SQLException { PropertyMapperManager mapper = new PropertyMapperManager(); LocalDate localDate = LocalDate.now(); java.sql.Date date = java.sql.Date.valueOf(localDate); JapaneseDate japaneseDate = JapaneseDate.of(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth()); assertThat(mapper.getValue(JavaType.of(JapaneseEra.class), newResultSet("getInt", 2), 1), is(JapaneseEra.HEISEI)); assertThat(mapper.getValue(JavaType.of(Era.class), newResultSet("getInt", 2), 1), is(nullValue())); assertThat(mapper.getValue(JavaType.of(JapaneseDate.class), newResultSet("getDate", date), 1), is(japaneseDate)); assertThat(mapper.getValue(JavaType.of(ChronoLocalDate.class), newResultSet("getDate", null), 1), is(nullValue())); assertThat(mapper.getValue(JavaType.of(JapaneseEra.class), newResultSet("getInt", 0, "wasNull", true), 1), is(nullValue())); assertThat(mapper.getValue(JavaType.of(JapaneseDate.class), newResultSet("getDate", null), 1), is(nullValue())); }
@Override public int prolepticYear(Era<JapaneseChrono> era, int yearOfEra) { if (era instanceof JapaneseEra == false) { throw new DateTimeException("Era must be JapaneseEra"); } JapaneseEra jera = (JapaneseEra) era; int gregorianYear = jera.getPrivateEra().getSinceDate().getYear() + yearOfEra - 1; if (yearOfEra == 1) { return gregorianYear; } LocalGregorianCalendar.Date jdate = JCAL.newCalendarDate(null); jdate.setEra(jera.getPrivateEra()).setDate(yearOfEra, 1, 1); JCAL.normalize(jdate); if (jdate.getNormalizedYear() == gregorianYear) { return gregorianYear; } throw new DateTimeException("invalid yearOfEra value"); }
private boolean checkEra(final Class<?> type) { if (!Era.class.isAssignableFrom(type)) { return false; } try { Method method = type.getMethod("of", int.class); return Modifier.isStatic(method.getModifiers()) && Modifier.isPublic(method.getModifiers()) && method.getReturnType().equals(type); } catch (NoSuchMethodException | SecurityException e) { return false; } }
@Test(dataProvider="prolepticYear") public void test_prolepticYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) { Era eraObj = JapaneseChronology.INSTANCE.eraOf(eraValue); assertTrue(JapaneseChronology.INSTANCE.eras().contains(eraObj)); assertEquals(eraObj, era); assertEquals(JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra), expectedProlepticYear); }
@Test(dataProvider="prolepticYear") public void test_isLeapYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) { assertEquals(JapaneseChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear); assertEquals(JapaneseChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear).isLeap()); JapaneseDate jdate = JapaneseDate.now(); jdate = jdate.with(ChronoField.YEAR, expectedProlepticYear).with(ChronoField.MONTH_OF_YEAR, 2); if (isLeapYear) { assertEquals(jdate.lengthOfMonth(), 29); } else { assertEquals(jdate.lengthOfMonth(), 28); } }
@Test(dataProvider="japaneseEras") public void test_Japanese_Eras(Era era, int eraValue, String name) { assertEquals(era.getValue(), eraValue, "EraValue"); assertEquals(era.toString(), name, "Era Name"); assertEquals(era, JapaneseChronology.INSTANCE.eraOf(eraValue), "JapaneseChronology.eraOf()"); List<Era> eras = JapaneseChronology.INSTANCE.eras(); assertTrue(eras.contains(era), "Era is not present in JapaneseChronology.INSTANCE.eras()"); }
@Test public void test_Japanese_badEras() { int badEras[] = {-1000, -998, -997, -2, 3, 4, 1000}; for (int badEra : badEras) { try { Era era = JapaneseChronology.INSTANCE.eraOf(badEra); fail("JapaneseChronology.eraOf returned " + era + " + for invalid eraValue " + badEra); } catch (DateTimeException ex) { // ignore expected exception } } }
@Test(dataProvider="japaneseEras") public void test_JapaneseEra_singletons(Era expectedEra, int eraValue, String name) { JapaneseEra actualEra = JapaneseEra.valueOf(name); assertEquals(actualEra, expectedEra, "JapaneseEra.valueOf(name)"); actualEra = JapaneseEra.of(eraValue); assertEquals(actualEra, expectedEra, "JapaneseEra.of(value)"); String string = actualEra.toString(); assertEquals(string, name, "JapaneseEra.toString()"); }
@Test public void test_JapaneseChronology_eras() { List<Era> actualEras = JapaneseChronology.INSTANCE.eras(); Object[][] erasInfo = data_japanseseEras(); assertEquals(actualEras.size(), erasInfo.length, "Wrong number of Eras"); for (int i = 0; i < erasInfo.length; i++) { Object[] eraInfo = erasInfo[i]; assertEquals(actualEras.get(i), eraInfo[0], "Singleton mismatch"); } }
@Override public int prolepticYear(Era era, int yearOfEra) { if (era instanceof CopticEra == false) { throw new ClassCastException("Era must be CopticEra"); } return (era == CopticEra.AM ? yearOfEra : 1 - yearOfEra); }
@Test(dataProvider="prolepticYear") public void test_prolepticYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) { Era eraObj = MinguoChronology.INSTANCE.eraOf(eraValue); assertTrue(MinguoChronology.INSTANCE.eras().contains(eraObj)); assertEquals(eraObj, era); assertEquals(MinguoChronology.INSTANCE.prolepticYear(era, yearOfEra), expectedProlepticYear); }
@Test(dataProvider="prolepticYear") public void test_isLeapYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) { assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear); assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear + YDIFF).isLeap()); MinguoDate minguo = MinguoDate.now(); minguo = minguo.with(ChronoField.YEAR, expectedProlepticYear).with(ChronoField.MONTH_OF_YEAR, 2); if (isLeapYear) { assertEquals(minguo.lengthOfMonth(), 29); } else { assertEquals(minguo.lengthOfMonth(), 28); } }
@DataProvider(name = "Eras") Era[][] data_of_calendars() { return new Era[][] { {HijrahEra.AH}, {IsoEra.BCE}, {IsoEra.CE}, {MinguoEra.BEFORE_ROC}, {MinguoEra.ROC}, {ThaiBuddhistEra.BEFORE_BE}, {ThaiBuddhistEra.BE}, }; }
@Test(dataProvider="prolepticYear") public void test_prolepticYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) { Era eraObj = ThaiBuddhistChronology.INSTANCE.eraOf(eraValue); assertTrue(ThaiBuddhistChronology.INSTANCE.eras().contains(eraObj)); assertEquals(eraObj, era); assertEquals(ThaiBuddhistChronology.INSTANCE.prolepticYear(era, yearOfEra), expectedProlepticYear); }
@Test(dataProvider="prolepticYear") public void test_isLeapYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) { assertEquals(ThaiBuddhistChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear) ; assertEquals(ThaiBuddhistChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear - YDIFF).isLeap()); ThaiBuddhistDate jdate = ThaiBuddhistDate.now(); jdate = jdate.with(ChronoField.YEAR, expectedProlepticYear).with(ChronoField.MONTH_OF_YEAR, 2); if (isLeapYear) { assertEquals(jdate.lengthOfMonth(), 29); } else { assertEquals(jdate.lengthOfMonth(), 28); } }
@Override public int parse(DateTimeParseContext context, CharSequence parseText, int position) { int length = parseText.length(); if (position < 0 || position > length) { throw new IndexOutOfBoundsException(); } TextStyle style = (context.isStrict() ? textStyle : null); Chronology chrono = context.getEffectiveChronology(); Iterator<Entry<String, Long>> it; if (chrono == null || chrono == IsoChronology.INSTANCE) { it = provider.getTextIterator(field, style, context.getLocale()); } else { it = provider.getTextIterator(chrono, field, style, context.getLocale()); } if (it != null) { while (it.hasNext()) { Entry<String, Long> entry = it.next(); String itText = entry.getKey(); if (context.subSequenceEquals(itText, 0, parseText, position, itText.length())) { return context.setParsedField(field, entry.getValue(), position, position + itText.length()); } } if (field == ERA && !context.isStrict()) { // parse the possible era name from era.toString() List<Era> eras = chrono.eras(); for (Era era : eras) { String name = era.toString(); if (context.subSequenceEquals(name, 0, parseText, position, name.length())) { return context.setParsedField(field, era.getValue(), position, position + name.length()); } } } if (context.isStrict()) { return ~position; } } return numberPrinterParser().parse(context, parseText, position); }
@Test(dataProvider = "era_epochSecond_dataProvider") public void test_epochSecond(Chronology chrono, Era era, int y, int m, int d, int h, int min, int s, ZoneOffset offset) { ChronoLocalDate chronoLd = chrono.date(era, y, m, d); assertEquals(chrono.epochSecond(era, y, m, d, h, min, s, offset), OffsetDateTime.of(LocalDate.from(chronoLd), LocalTime.of(h, min, s), offset) .toEpochSecond()); }