Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package libcore.libcore.util;
     18 
     19 import org.junit.Test;
     20 
     21 import java.util.Arrays;
     22 import java.util.List;
     23 import java.util.stream.Collectors;
     24 import libcore.util.CountryTimeZones;
     25 import libcore.util.CountryTimeZones.TimeZoneMapping;
     26 import libcore.util.CountryZonesFinder;
     27 
     28 import static org.junit.Assert.assertEquals;
     29 import static org.junit.Assert.assertNull;
     30 import static org.junit.Assert.assertSame;
     31 import static org.junit.Assert.fail;
     32 
     33 public class CountryZonesFinderTest {
     34 
     35     private static final CountryTimeZones GB_ZONES = CountryTimeZones.createValidated(
     36             "gb", "Europe/London", true, timeZoneMappings("Europe/London"), "test");
     37 
     38     private static final CountryTimeZones IM_ZONES = CountryTimeZones.createValidated(
     39             "im", "Europe/London", true, timeZoneMappings("Europe/London"), "test");
     40 
     41     private static final CountryTimeZones FR_ZONES = CountryTimeZones.createValidated(
     42             "fr", "Europe/Paris", true, timeZoneMappings("Europe/Paris"), "test");
     43 
     44     private static final CountryTimeZones US_ZONES = CountryTimeZones.createValidated(
     45             "us", "America/New_York", true,
     46             timeZoneMappings("America/New_York", "America/Los_Angeles"), "test");
     47 
     48     @Test
     49     public void lookupAllCountryIsoCodes() throws Exception {
     50         CountryZonesFinder countryZonesFinder =
     51                 CountryZonesFinder.createForTests(list(GB_ZONES, FR_ZONES));
     52 
     53         List<String> isoList = countryZonesFinder.lookupAllCountryIsoCodes();
     54         assertEqualsAndImmutable(list(GB_ZONES.getCountryIso(), FR_ZONES.getCountryIso()), isoList);
     55     }
     56 
     57     @Test
     58     public void lookupAllCountryIsoCodes_empty() throws Exception {
     59         CountryZonesFinder countryZonesFinder = CountryZonesFinder.createForTests(list());
     60         List<String> isoList = countryZonesFinder.lookupAllCountryIsoCodes();
     61         assertEqualsAndImmutable(list(), isoList);
     62     }
     63 
     64     @Test
     65     public void lookupCountryCodesForZoneId() throws Exception {
     66         CountryZonesFinder countryZonesFinder =
     67                 CountryZonesFinder.createForTests(list(GB_ZONES, IM_ZONES, FR_ZONES, US_ZONES));
     68 
     69         assertEqualsAndImmutable(list(GB_ZONES, IM_ZONES),
     70                 countryZonesFinder.lookupCountryTimeZonesForZoneId("Europe/London"));
     71         assertEqualsAndImmutable(list(US_ZONES),
     72                 countryZonesFinder.lookupCountryTimeZonesForZoneId("America/New_York"));
     73         assertEqualsAndImmutable(list(US_ZONES),
     74                 countryZonesFinder.lookupCountryTimeZonesForZoneId("America/Los_Angeles"));
     75         assertEqualsAndImmutable(list(),
     76                 countryZonesFinder.lookupCountryTimeZonesForZoneId("DOES_NOT_EXIST"));
     77     }
     78 
     79     @Test
     80     public void lookupCountryTimeZones() throws Exception {
     81         CountryZonesFinder countryZonesFinder =
     82                 CountryZonesFinder.createForTests(list(GB_ZONES, IM_ZONES, FR_ZONES, US_ZONES));
     83         assertSame(GB_ZONES, countryZonesFinder.lookupCountryTimeZones(GB_ZONES.getCountryIso()));
     84         assertSame(IM_ZONES, countryZonesFinder.lookupCountryTimeZones(IM_ZONES.getCountryIso()));
     85         assertNull(countryZonesFinder.lookupCountryTimeZones("DOES_NOT_EXIST"));
     86     }
     87 
     88     private static <X> void assertEqualsAndImmutable(List<X> expected, List<X> actual) {
     89         assertEquals(expected, actual);
     90         assertImmutableList(actual);
     91     }
     92 
     93     private static <X> void assertImmutableList(List<X> list) {
     94         try {
     95             list.add(null);
     96             fail();
     97         } catch (UnsupportedOperationException expected) {
     98         }
     99     }
    100 
    101     private static <X> List<X> list(X... values) {
    102         return Arrays.asList(values);
    103     }
    104 
    105     /**
    106      * Creates a list of default {@link TimeZoneMapping} objects with the specified time zone IDs.
    107      */
    108     private static List<TimeZoneMapping> timeZoneMappings(String... timeZoneIds) {
    109         return Arrays.stream(timeZoneIds)
    110                 .map(x -> TimeZoneMapping.createForTests(
    111                         x, true /* picker */, null /* notUsedAfter */))
    112                 .collect(Collectors.toList());
    113     }
    114 }
    115