public AbstractFixture(final FixtureType fixtureType) { this.fixtureType = fixtureType; try { clock = FixtureClock.initialize(); } catch(IllegalStateException ex) { clock = null; System.err.println(ex.getMessage()); System.err.println("calls to change date or time will be ignored"); } }
/** * Configures the system to use a FixtureClock rather than the in-built system clock. * Can be called multiple times. * * <p> * Must call before any other call to {@link Clock#getInstance()}. * * @throws IllegalStateException if Clock singleton already initialized with some other implementation. */ public synchronized static FixtureClock initialize() { if (!isInitialized() || !(getInstance() instanceof FixtureClock)) { // installs the FixtureClock as the Clock singleton via the Clock's constructor // if was initialized, then will replace. // (if non-replaceable, then superclass will throw exception for us. new FixtureClock(); } return (FixtureClock) getInstance(); }
/** * Sets the clock to epoch, that is midnight, 1 Jan 1970 UTC. * * <p> * This is typically called before either {@link #setDate(int, int, int)} * (so that time is set to midnight) and/or {@link #setTime(int, int)} * (so that date is set to a well known value). */ public void clear() { setupCalendarIfRequired(); calendar.clear(); }
/** * Will print warning message and do nothing if {@link FixtureClock} could not be {@link FixtureClock#initialize() initialized}. */ public void earlierDate(final int years, final int months, final int days) { if (clockNotSetup("earlierDate")) return; clock.addDate(-years, -months, -days); }
/** * Will print warning message and do nothing if {@link FixtureClock} could not be {@link FixtureClock#initialize() initialized}. */ public void earlierTime(final int hours, final int minutes) { if (clockNotSetup("earlierDate")) return; clock.addTime(-hours, -minutes); }
/** * Will print warning message and do nothing if {@link FixtureClock} could not be {@link FixtureClock#initialize() initialized}. */ public void setTime(final int hour, final int minute) { if (clockNotSetup("setTime")) return; clock.setTime(hour, minute); }
/** * Will print warning message and do nothing if {@link FixtureClock} could not be {@link FixtureClock#initialize() initialized}. */ public void resetClock() { if (clockNotSetup("laterTime")) return; clock.reset(); }
/** * Will print warning message and do nothing if {@link FixtureClock} could not be {@link FixtureClock#initialize() initialized}. */ public void setDate(final int year, final int month, final int day) { if (clockNotSetup("setDate")) return; clock.setDate(year, month, day); }
/** * Will print warning message and do nothing if {@link FixtureClock} could not be {@link FixtureClock#initialize() initialized}. */ public void laterDate(final int years, final int months, final int days) { if (clockNotSetup("laterDate")) return; clock.addDate(years, months, days); }
/** * Will print warning message and do nothing if {@link FixtureClock} could not be {@link FixtureClock#initialize() initialized}. */ public void laterTime(final int hours, final int minutes) { if (clockNotSetup("laterTime")) return; clock.addTime(hours, minutes); }
public final void init() { // check prereq dependencies injected ensureThatState(specificationLoader, is(notNullValue())); ensureThatState(serviceList, is(notNullValue())); // a bit of a workaround, but required if anything in the metamodel (for example, a // ValueSemanticsProvider for a date value type) needs to use the Clock singleton // we do this after loading the services to allow a service to prime a different clock // implementation (eg to use an NTP time service). if (!deploymentType.isProduction() && !Clock.isInitialized()) { FixtureClock.initialize(); } doInit(); }
/** * Sets the date, but the time portion is left unchanged. * * @see #setTime(int, int) * @see #addDate(int, int, int) */ public void setDate(final int year, final int month, final int day) { setupCalendarIfRequired(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.DAY_OF_MONTH, day); }
/** * Adjusts the time by the specified number of hours and minutes. * * <p> * Typically called after {@link #setTime(int, int)}, to move the clock * forward or perhaps back. * * @see #addDate(int, int, int) */ public void addTime(final int hours, final int minutes) { setupCalendarIfRequired(); calendar.add(Calendar.HOUR_OF_DAY, hours); calendar.add(Calendar.MINUTE, minutes); }
/** * Sets the hours and minutes as specified, and sets the seconds and * milliseconds to zero, but the date portion is left unchanged. * * @see #setDate(int, int, int) * @see #addTime(int, int) */ public void setTime(final int hour, final int min) { setupCalendarIfRequired(); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, min); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); }
/** * Adjusts the time by the specified number of years, months or days. * * <p> * Typically called after {@link #setDate(int, int, int)}, to move the clock * forward or perhaps back. * * @see #addTime(int, int) */ public void addDate(final int years, final int months, final int days) { setupCalendarIfRequired(); calendar.add(Calendar.YEAR, years); calendar.add(Calendar.MONTH, months); calendar.add(Calendar.DAY_OF_MONTH, days); }