// -----------------------------------------------------------------------
 // getRules(String, boolean)
 // -----------------------------------------------------------------------
 @Test
 public void test_getRules_StringBoolean() {
   ZoneRules rules = ZoneRulesProvider.getRules("Europe/London", false);
   assertNotNull(rules);
   ZoneRules rules2 = ZoneRulesProvider.getRules("Europe/London", false);
   assertEquals(rules2, rules);
 }
Example #2
0
 /**
  * Refreshes the rules from the underlying data provider.
  *
  * <p>This method allows an application to request that the providers check for any updates to the
  * provided rules. After calling this method, the offset stored in any {@link ZonedDateTime} may
  * be invalid for the zone ID.
  *
  * <p>Dynamic update of rules is a complex problem and most applications should not use this
  * method or dynamic rules. To achieve dynamic rules, a provider implementation will have to be
  * written as per the specification of this class. In addition, instances of {@code ZoneRules}
  * must not be cached in the application as they will become stale. However, the boolean flag on
  * {@link #provideRules(String, boolean)} allows provider implementations to control the caching
  * of {@code ZoneId}, potentially ensuring that all objects in the system see the new rules. Note
  * that there is likely to be a cost in performance of a dynamic rules provider. Note also that no
  * dynamic rules provider is in this specification.
  *
  * @return true if the rules were updated
  * @throws ZoneRulesException if an error occurs during the refresh
  */
 public static boolean refresh() {
   boolean changed = false;
   for (ZoneRulesProvider provider : PROVIDERS) {
     changed |= provider.provideRefresh();
   }
   return changed;
 }
 // -----------------------------------------------------------------------
 // getAvailableZoneIds()
 // -----------------------------------------------------------------------
 @Test
 public void test_getAvailableGroupIds() {
   Set<String> zoneIds = ZoneRulesProvider.getAvailableZoneIds();
   assertEquals(zoneIds.contains("Europe/London"), true);
   zoneIds.clear();
   assertEquals(zoneIds.size(), 0);
   Set<String> zoneIds2 = ZoneRulesProvider.getAvailableZoneIds();
   assertEquals(zoneIds2.contains("Europe/London"), true);
 }
 // -----------------------------------------------------------------------
 // registerProvider()
 // -----------------------------------------------------------------------
 @Test(groups = {"tck"})
 public void test_registerProvider() {
   Set<String> pre = ZoneRulesProvider.getAvailableZoneIds();
   assertEquals(pre.contains("FooLocation"), false);
   ZoneRulesProvider.registerProvider(new MockTempProvider());
   assertEquals(pre.contains("FooLocation"), false);
   Set<String> post = ZoneRulesProvider.getAvailableZoneIds();
   assertEquals(post.contains("FooLocation"), true);
   assertEquals(
       ZoneRulesProvider.getRules("FooLocation", false), ZoneOffset.of("+01:45").getRules());
 }
Example #5
0
  static {
    // if the property java.time.zone.DefaultZoneRulesProvider is
    // set then its value is the class name of the default provider
    final List<ZoneRulesProvider> loaded = new ArrayList<>();
    AccessController.doPrivileged(
        new PrivilegedAction<>() {
          public Object run() {
            String prop = System.getProperty("java.time.zone.DefaultZoneRulesProvider");
            if (prop != null) {
              try {
                Class<?> c = Class.forName(prop, true, ClassLoader.getSystemClassLoader());
                @SuppressWarnings("deprecation")
                ZoneRulesProvider provider = ZoneRulesProvider.class.cast(c.newInstance());
                registerProvider(provider);
                loaded.add(provider);
              } catch (Exception x) {
                throw new Error(x);
              }
            } else {
              registerProvider(new TzdbZoneRulesProvider());
            }
            return null;
          }
        });

    ServiceLoader<ZoneRulesProvider> sl =
        ServiceLoader.load(ZoneRulesProvider.class, ClassLoader.getSystemClassLoader());
    Iterator<ZoneRulesProvider> it = sl.iterator();
    while (it.hasNext()) {
      ZoneRulesProvider provider;
      try {
        provider = it.next();
      } catch (ServiceConfigurationError ex) {
        if (ex.getCause() instanceof SecurityException) {
          continue; // ignore the security exception, try the next provider
        }
        throw ex;
      }
      boolean found = false;
      for (ZoneRulesProvider p : loaded) {
        if (p.getClass() == provider.getClass()) {
          found = true;
        }
      }
      if (!found) {
        registerProvider0(provider);
        loaded.add(provider);
      }
    }
    // CopyOnWriteList could be slow if lots of providers and each added individually
    PROVIDERS.addAll(loaded);
  }
  // -----------------------------------------------------------------------
  // getVersions(String)
  // -----------------------------------------------------------------------
  @Test
  public void test_getVersions_String() {
    NavigableMap<String, ZoneRules> versions = ZoneRulesProvider.getVersions("Europe/London");
    assertTrue(versions.size() >= 1);
    ZoneRules rules = ZoneRulesProvider.getRules("Europe/London", false);
    assertEquals(versions.lastEntry().getValue(), rules);

    NavigableMap<String, ZoneRules> copy = new TreeMap<>(versions);
    versions.clear();
    assertEquals(versions.size(), 0);
    NavigableMap<String, ZoneRules> versions2 = ZoneRulesProvider.getVersions("Europe/London");
    assertEquals(versions2, copy);
  }
  @Test
  public void test_getRules_StringBoolean_dynamic() {
    MockDynamicProvider dynamicProvider = new MockDynamicProvider();
    ZoneRulesProvider.registerProvider(dynamicProvider);

    assertEquals(dynamicProvider.count, 0);
    ZoneRules rules1 = ZoneId.of("DynamicLocation").getRules();
    assertEquals(dynamicProvider.count, 2);
    assertEquals(rules1, dynamicProvider.BASE);
    ZoneRules rules2 = ZoneId.of("DynamicLocation").getRules();
    assertEquals(dynamicProvider.count, 4);
    assertEquals(rules2, dynamicProvider.ALTERNATE);
  }
Example #8
0
 /**
  * Registers the provider.
  *
  * @param provider the provider to register, not null
  * @throws ZoneRulesException if unable to complete the registration
  */
 private static synchronized void registerProvider0(ZoneRulesProvider provider) {
   for (String zoneId : provider.provideZoneIds()) {
     Objects.requireNonNull(zoneId, "zoneId");
     ZoneRulesProvider old = ZONES.putIfAbsent(zoneId, provider);
     if (old != null) {
       throw new ZoneRulesException(
           "Unable to register zone as one already registered with that ID: "
               + zoneId
               + ", currently loading from provider: "
               + provider);
     }
   }
   Set<String> combinedSet = new HashSet<String>(ZONES.keySet());
   ZONE_IDS = Collections.unmodifiableSet(combinedSet);
 }
Example #9
0
  @Override
  public Set<String> getAvailableIDs() {

    return Collections.unmodifiableSet(ZoneRulesProvider.getAvailableZoneIds());
  }
Example #10
0
  public JdkZoneProviderSPI() {
    super();

    this.version = ZoneRulesProvider.getVersions("America/New_York").lastEntry().getKey();
  }
 // -----------------------------------------------------------------------
 // refresh()
 // -----------------------------------------------------------------------
 @Test
 public void test_refresh() {
   assertEquals(ZoneRulesProvider.refresh(), false);
 }
 @Test(expectedExceptions = NullPointerException.class)
 public void test_getVersions_String_null() {
   ZoneRulesProvider.getVersions(null);
 }
 @Test(expectedExceptions = ZoneRulesException.class)
 public void test_getVersions_String_unknownId() {
   ZoneRulesProvider.getVersions("Europe/Lon");
 }
 @Test(expectedExceptions = NullPointerException.class)
 public void test_getRules_StringBoolean_null() {
   ZoneRulesProvider.getRules(null, false);
 }
 @Test(expectedExceptions = ZoneRulesException.class)
 public void test_getRules_StringBoolean_unknownId() {
   ZoneRulesProvider.getRules("Europe/Lon", false);
 }