com.emarsys.ecommon.time
Class MockTime

java.lang.Object
  extended by com.emarsys.ecommon.time.MockTime
All Implemented Interfaces:
Time

public final class MockTime
extends java.lang.Object
implements Time

MockTime is a singleton to mock the Time in (unit) test cases.

If you want to use the MockTime instead of the default SystemTime to test your ecommon classes you need to set to corresponding MockTimeBuilder for Time instances in the Builders registry: Builders.set( Time.class, new MockTimeBuilder() ); Then every instance created by TimeBuilder, DateBuilder or CalendarBuilder will use MockTime instead of the SystemTime.

NOTE that Builders.set(Class, com.emarsys.ecommon.builder.Builder) will throw a BuilderException if another Builder is already registered with Time!

A short cut for installing the MockTime instead of the default SystemTime in ecommon's Builders is to call install().

Author:
Michael "kULO" Kulovits

Method Summary
 long currentTimeMillis()
           Returns the current time in milliseconds.
static MockTime getInstance()
           Returns the singleton instance of MockTime.
static void install()
           Installs the MockTime for system wide (classloader) usage through ecommon's Builders.
 void setTime(long currentTimeMillis)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static final MockTime getInstance()

Returns the singleton instance of MockTime.

If not already present a new instance will be created.

Returns:

install

public static final void install()
                          throws BuilderException

Installs the MockTime for system wide (classloader) usage through ecommon's Builders.

After a successfull call MockTime will be used instead of SystemTime in the TimeBuilder (as well as DateBuilder and CalendarBuilder) in order to be able to change the current time "systemwide" (at least in every class that uses the ecommon builder framework). This is a very usefull feature to "control" the system time in test cases.

NOTE that this is only possible before any other Builder has been registered with for creating Time instances, otherwise a BuilderException will be raised.
This also means that there must not have been a call to TimeBuilder.getInstance() before installing the MockTime.

Throws:
BuilderException - - if another Builder instance has already been registered for building Time objects.
See Also:
Builders.set(Class, Builder), MockTimeBuilder

setTime

public void setTime(long currentTimeMillis)
Parameters:
currentTimeMillis -

currentTimeMillis

public long currentTimeMillis()
Description copied from interface: Time

Returns the current time in milliseconds.

Subsequent calls to Time.currentTimeMillis() must not return values that are smaller then previous ones.

Specified by:
currentTimeMillis in interface Time
Returns:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
See Also:
Time.currentTimeMillis()


Copyright © 2010 emarsys AG. All Rights Reserved.