public void testMillisProviderSecurity() { if (OLD_JDK) { return; } try { try { Policy.setPolicy(RESTRICT); System.setSecurityManager(new SecurityManager()); DateTimeUtils.setCurrentMillisProvider(new MillisProvider() { public long getMillis() { return 0L; } }); fail(); } catch (SecurityException ex) { // ok } finally { System.setSecurityManager(null); Policy.setPolicy(ALLOW); } } finally { DateTimeUtils.setCurrentMillisSystem(); } }
public void testMillisProvider() { try { DateTimeUtils.setCurrentMillisProvider(new MillisProvider() { public long getMillis() { return 1L; } }); assertEquals(1L, DateTimeUtils.currentTimeMillis()); } finally { DateTimeUtils.setCurrentMillisSystem(); } }
/** * Runs the given {@link Block} in the date and time given when constructing * the TimeMachine. A {@link DateTime} created inside the given code will * have always the same value, equal to the DateTime given when constructing * the TimeMachine. * * @param code * The code to be run in a different date and time * @return The result of running the given code */ public <T> T andExecute(Block<T> code) { MillisProvider provider = getCurrentTimeMillisProvider(); DateTimeUtils.setCurrentMillisFixed(somewhereInTime.getMillis()); try { return code.run(); } finally { DateTimeUtils.setCurrentMillisProvider(provider); } }