diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 4538e94..f19335f 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -17,8 +17,8 @@ on: pull_request: # The branches below must be a subset of the branches above branches: [ master ] - schedule: - - cron: '20 7 * * 0' + #schedule: + # - cron: '20 7 * * 0' jobs: analyze: diff --git a/README.md b/README.md index bbfa0cc..70efa8a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ [![Release](https://jitpack.io/v/umjammer/vavi-commons-sandbox.svg)](https://jitpack.io/#umjammer/vavi-commons-sandbox) +[![Java CI with Maven](https://github.com/umjammer/vavi-commons-sandbox/actions/workflows/maven.yml/badge.svg)](https://github.com/umjammer/vavi-commons-sandbox/actions/workflows/maven.yml) +[![CodeQL](https://github.com/umjammer/vavi-commons-sandbox/actions/workflows/codeql.yml/badge.svg)](https://github.com/umjammer/vavi-commons-sandbox/actions/workflows/codeql.yml) +![Java](https://img.shields.io/badge/Java-8-b07219) # vavi-commons-sandbox @@ -25,7 +28,7 @@ to be deprecated ### 🔧 Utility - * Japanese Holiday + * Holiday Provider * iCal version * google calendar cloud api version * calculated version @@ -44,6 +47,8 @@ to be deprecated ### 🔧 Dynamic Morphism +wip + ### TODO * regex find group iteration diff --git a/pom.xml b/pom.xml index 5db8c14..5a56e81 100644 --- a/pom.xml +++ b/pom.xml @@ -8,11 +8,11 @@ vavi vavi-commons-sandbox - 0.4.1 + 0.4.2 Vavi Commons Sandbox API - + vavi https://github.com/umjammer/vavi-commons-sandbox @@ -81,7 +81,9 @@ TODO + org.apache.maven.plugins maven-compiler-plugin + 3.10.1 1.8 1.8 @@ -159,7 +161,7 @@ TODO org.junit junit-bom - 5.8.1 + 5.9.0 pom import @@ -168,9 +170,9 @@ TODO - com.github.umjammer + com.github.umjammer vavi-commons - 1.1.6 + 1.1.7 commons-beanutils @@ -180,7 +182,7 @@ TODO org.apache.ant ant - 1.10.11 + 1.10.12 com.github.umjammer @@ -209,6 +211,11 @@ TODO junit-platform-commons test + + org.junit.jupiter + junit-jupiter-params + test + com.github.umjammer.vavi-net-auth @@ -219,7 +226,7 @@ TODO com.google.apis google-api-services-calendar - v3-rev409-1.25.0 + v3-rev20220715-2.0.0 test diff --git a/src/main/java/vavi/util/Locales.java b/src/main/java/vavi/util/Locales.java deleted file mode 100644 index 7465fc2..0000000 --- a/src/main/java/vavi/util/Locales.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2022 by Naohide Sano, All rights reserved. - * - * Programmed by Naohide Sano - */ - -package vavi.util; - - -/** - * Locales. - * - * @author Naohide Sano (umjammer) - * @version 0.00 2022/02/23 umjammer initial version
- */ -public @interface Locales { - - String countries() default ""; - - String languages() default ""; -} - -/* */ diff --git a/src/main/java/vavi/util/holiday/CalculatedHolidaysJaProvider.java b/src/main/java/vavi/util/holiday/CalculatedHolidaysJaProvider.java index 204c3b3..59f6a68 100644 --- a/src/main/java/vavi/util/holiday/CalculatedHolidaysJaProvider.java +++ b/src/main/java/vavi/util/holiday/CalculatedHolidaysJaProvider.java @@ -18,7 +18,7 @@ /** - * CalclatedHolidaysJaProvider. + * CalculatedHolidaysJaProvider. * * @author Naohide Sano (nsano) * @version 0.00 021119 nsano port
@@ -26,7 +26,7 @@ @Locales(countries = "Japan", languages = "Japanese") public class CalculatedHolidaysJaProvider implements HolidaysProvider { - /** this cause Y10K problem lol */ + /** this causes the Y10K problem lol */ private static final int MoY = 9999; enum Holidays { @@ -101,14 +101,14 @@ private static int[] equinoxes(int year) { } @Override - public List holidays(int year) { - List holydays = new ArrayList<>(); + public List holidays(int year) { + List holydays = new ArrayList<>(); Arrays.stream(Holidays.values()).forEach(e -> { LocalDate date = e.getDate(year); if (date != null) { - holydays.add(new Holyday(date, e.summary)); + holydays.add(new Holiday(date, e.summary)); if (year >= 1973 && date.getDayOfWeek() == DayOfWeek.SUNDAY) { - holydays.add(new Holyday(date.withDayOfMonth(date.getDayOfMonth() + 1), "振替休日")); + holydays.add(new Holiday(date.withDayOfMonth(date.getDayOfMonth() + 1), "振替休日")); } } }); diff --git a/src/main/java/vavi/util/holiday/GoogleICalHolidaysJaProvider.java b/src/main/java/vavi/util/holiday/GoogleICalHolidaysJaProvider.java index 198976c..98782b4 100644 --- a/src/main/java/vavi/util/holiday/GoogleICalHolidaysJaProvider.java +++ b/src/main/java/vavi/util/holiday/GoogleICalHolidaysJaProvider.java @@ -31,16 +31,16 @@ * @author Naohide Sano (umjammer) * @version 0.00 2022/02/22 umjammer initial version
*/ -@Locales(countries = "Japan", languages = "Japanese, English") +@Locales(countries = "Japan", languages = {"Japanese", "English"}) public class GoogleICalHolidaysJaProvider implements HolidaysProvider { - private static final String url_jp = "https://www.google.com/calendar/ical/ja.japanese%23holiday%40group.v.calendar.google.com/public/basic.ics"; + private static final String url_ja = "https://www.google.com/calendar/ical/ja.japanese%23holiday%40group.v.calendar.google.com/public/basic.ics"; private static final String url_en = "https://www.google.com/calendar/ical/japanese%40holiday.calendar.google.com/public/basic.ics"; @Override - public List holidays(int year) { + public List holidays(int year) { try { - String url = Locale.getDefault().getLanguage().equals(Locale.JAPAN.getLanguage()) ? url_jp : url_en; + String url = Locale.getDefault().getLanguage().equals(Locale.JAPAN.getLanguage()) ? url_ja : url_en; Debug.println("country: " + Locale.getDefault().getCountry()); String userDefinedUrl = System.getProperty("vavix.util.holiday.ICalProvider.url"); if (userDefinedUrl != null) { @@ -54,7 +54,7 @@ public List holidays(int year) { return calendar.events.stream() .filter(e -> e.getDate().getYear() == year) - .map(e -> new Holyday(e.getDate(), e.summary)) + .map(e -> new Holiday(e.getDate(), e.summary)) .sorted() .collect(Collectors.toList()); } catch (IOException e) { diff --git a/src/main/java/vavi/util/holiday/HolidaysProvider.java b/src/main/java/vavi/util/holiday/HolidaysProvider.java index f252042..a03f022 100644 --- a/src/main/java/vavi/util/holiday/HolidaysProvider.java +++ b/src/main/java/vavi/util/holiday/HolidaysProvider.java @@ -7,7 +7,13 @@ package vavi.util.holiday; import java.time.LocalDate; +import java.util.Arrays; import java.util.List; +import java.util.Locale; +import java.util.NoSuchElementException; +import java.util.ServiceLoader; + +import vavi.util.Locales; /** @@ -19,10 +25,10 @@ public interface HolidaysProvider { /** common */ - class Holyday implements Comparable { + class Holiday implements Comparable { LocalDate date; String desc; - public Holyday(LocalDate date, String desc) { + public Holiday(LocalDate date, String desc) { this.date = date; this.desc = desc; } @@ -31,17 +37,40 @@ public String toString() { return date + " " + desc; } @Override - public int compareTo(Holyday o) { + public int compareTo(Holiday o) { return this.date.compareTo(o.date); } } /** sorted */ - List holidays(int year); + List holidays(int year); - /** factory */ + /** + * factory + *

+ * TODO use ServiceProvider, consider @Locales + */ static HolidaysProvider defaultProvider() { - return new GoogleICalHolidaysJaProvider(); + for (HolidaysProvider p : ServiceLoader.load(HolidaysProvider.class)) { + Locales a = p.getClass().getAnnotation(Locales.class); + if (Arrays.asList(a.countries()).contains(Locale.getDefault().getCountry()) && + Arrays.asList(a.languages()).contains(Locale.getDefault().getCountry())) { + return p; + } + } + for (HolidaysProvider p : ServiceLoader.load(HolidaysProvider.class)) { + Locales a = p.getClass().getAnnotation(Locales.class); + if (Arrays.asList(a.countries()).contains(Locale.getDefault().getCountry())) { + return p; + } + } + for (HolidaysProvider p : ServiceLoader.load(HolidaysProvider.class)) { + Locales a = p.getClass().getAnnotation(Locales.class); + if (Arrays.asList(a.languages()).contains(Locale.getDefault().getCountry())) { + return p; + } + } + return ServiceLoader.load(HolidaysProvider.class).iterator().next(); } } diff --git a/src/main/resources/META-INF/services/vavi.util.holiday.HolidaysProvider b/src/main/resources/META-INF/services/vavi.util.holiday.HolidaysProvider new file mode 100644 index 0000000..43f62df --- /dev/null +++ b/src/main/resources/META-INF/services/vavi.util.holiday.HolidaysProvider @@ -0,0 +1,2 @@ +vavi.util.holiday.CalculatedHolidaysJaProvider +vavi.util.holiday.GoogleICalHolidaysJaProvider \ No newline at end of file diff --git a/src/test/java/vavi/util/holiday/CalculatedHolidaysJaProviderTest.java b/src/test/java/vavi/util/holiday/CalculatedHolidaysJaProviderTest.java index cb8f65e..20e007a 100644 --- a/src/test/java/vavi/util/holiday/CalculatedHolidaysJaProviderTest.java +++ b/src/test/java/vavi/util/holiday/CalculatedHolidaysJaProviderTest.java @@ -27,11 +27,11 @@ class CalculatedHolidaysJaProviderTest { @Test void test() { CalculatedHolidaysJaProvider provider = new CalculatedHolidaysJaProvider(); - List holidays = provider.holidays(2022); + List holidays = provider.holidays(2022); Debug.println("holidays: " + holidays.size()); assertNotEquals(0, holidays.size()); holidays.forEach(System.err::println); - assertArrayEquals(HolidaysProviderTest.expected, holidays.stream().map(HolidaysProvider.Holyday::toString).toArray()); + assertArrayEquals(HolidaysProviderTest.expected, holidays.stream().map(HolidaysProvider.Holiday::toString).toArray()); } } diff --git a/src/test/java/vavi/util/holiday/GoogleCalendarHolidaysJaProvider.java b/src/test/java/vavi/util/holiday/GoogleCalendarHolidaysJaProvider.java index fc9642c..e1146ce 100644 --- a/src/test/java/vavi/util/holiday/GoogleCalendarHolidaysJaProvider.java +++ b/src/test/java/vavi/util/holiday/GoogleCalendarHolidaysJaProvider.java @@ -60,7 +60,7 @@ public class GoogleCalendarHolidaysJaProvider implements HolidaysProvider { } @Override - public List holidays(int year) { + public List holidays(int year) { try { Events events = service.events().list(calendarId) .setTimeMin(new DateTime(String.format("%04d-01-01T00:00:00.000+09:00", year))) @@ -70,7 +70,7 @@ public List holidays(int year) { .execute(); Debug.println(Level.FINE, "items: " + events.getItems().size()); //events.getItems().forEach(System.err::println); - return events.getItems().stream().map(e -> new Holyday(toLocalDate(e.getStart().getDate()), e.getSummary())).collect(Collectors.toList()); + return events.getItems().stream().map(e -> new Holiday(toLocalDate(e.getStart().getDate()), e.getSummary())).collect(Collectors.toList()); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/src/test/java/vavi/util/holiday/HolidaysProviderTest.java b/src/test/java/vavi/util/holiday/HolidaysProviderTest.java index c66a451..55a4cc5 100644 --- a/src/test/java/vavi/util/holiday/HolidaysProviderTest.java +++ b/src/test/java/vavi/util/holiday/HolidaysProviderTest.java @@ -8,12 +8,17 @@ import java.util.List; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import vavi.util.Debug; +import vavi.util.Locales; import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; /** @@ -47,9 +52,9 @@ class HolidaysProviderTest { void test() { Debug.println("-------- Google Calendar ICal --------"); HolidaysProvider provider = new GoogleICalHolidaysJaProvider(); - List holidays = provider.holidays(2022); + List holidays = provider.holidays(2022); holidays.stream().sorted().forEach(System.err::println); - assertArrayEquals(expected, holidays.stream().map(HolidaysProvider.Holyday::toString).toArray()); + assertArrayEquals(expected, holidays.stream().map(HolidaysProvider.Holiday::toString).toArray()); } @Test @@ -57,9 +62,19 @@ void test() { void test2() { Debug.println("-------- Google Calendar API --------"); HolidaysProvider provider = new GoogleCalendarHolidaysJaProvider(); - List holidays = provider.holidays(2022); + List holidays = provider.holidays(2022); holidays.stream().sorted().forEach(System.err::println); - assertArrayEquals(expected, holidays.stream().map(HolidaysProvider.Holyday::toString).toArray()); + assertArrayEquals(expected, holidays.stream().map(HolidaysProvider.Holiday::toString).toArray()); + } + + /** @see "https://blog1.mammb.com/entry/2017/07/05/223914" */ + @Disabled("wip") + @ParameterizedTest + @ValueSource(strings = {"ja", "en"}) + void test3(String lang) throws Exception { + System.setProperty("user.language", lang); + HolidaysProvider p = HolidaysProvider.defaultProvider(); + assertInstanceOf(GoogleICalHolidaysJaProvider.class, p); } } diff --git a/src/test/resources/META-INF/services/vavi.util.holiday.HolidaysProvider b/src/test/resources/META-INF/services/vavi.util.holiday.HolidaysProvider new file mode 100644 index 0000000..d8acf6c --- /dev/null +++ b/src/test/resources/META-INF/services/vavi.util.holiday.HolidaysProvider @@ -0,0 +1 @@ +vavi.util.holiday.GoogleCalendarHolidaysJaProvider \ No newline at end of file