You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2017/12/07 15:01:06 UTC

[isis] 10/18: ISIS-1791: adds TickingFixtureClock

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 44f70d586f90350f49b6f66449b1518bf5f97134
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Wed Dec 6 06:14:27 2017 +0000

    ISIS-1791: adds TickingFixtureClock
    
    also TickingClockFixture
---
 .../java/org/apache/isis/applib/clock/Clock.java   |   6 +-
 .../isis/applib/clock/TickingFixtureClock.java     | 172 +++++++++++++++++++++
 .../fixturescripts/clock/TickingClockFixture.java  |  51 ++++++
 .../fixture/teardown/SimpleModuleTearDown.java     |  36 -----
 4 files changed, 225 insertions(+), 40 deletions(-)

diff --git a/core/applib/src/main/java/org/apache/isis/applib/clock/Clock.java b/core/applib/src/main/java/org/apache/isis/applib/clock/Clock.java
index 48ac2f3..4ade764 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/clock/Clock.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/clock/Clock.java
@@ -16,7 +16,6 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
-
 package org.apache.isis.applib.clock;
 
 import java.sql.Timestamp;
@@ -29,9 +28,8 @@ import org.joda.time.DateTimeZone;
 import org.joda.time.LocalDate;
 import org.joda.time.LocalDateTime;
 
-import org.apache.isis.applib.FatalException;
-import org.apache.isis.applib.RecoverableException;
 import org.apache.isis.applib.Defaults;
+import org.apache.isis.applib.RecoverableException;
 import org.apache.isis.applib.fixtures.FixtureClock;
 
 /**
@@ -51,7 +49,7 @@ import org.apache.isis.applib.fixtures.FixtureClock;
  * {@link FixtureClock#getInstance()}.
  */
 public abstract class Clock {
-    private static Clock instance;
+    static Clock instance;
     private static boolean isReplaceable = true;
 
     /**
diff --git a/core/applib/src/main/java/org/apache/isis/applib/clock/TickingFixtureClock.java b/core/applib/src/main/java/org/apache/isis/applib/clock/TickingFixtureClock.java
new file mode 100644
index 0000000..d18e259
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/clock/TickingFixtureClock.java
@@ -0,0 +1,172 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.applib.clock;
+
+import java.util.Calendar;
+import java.util.TimeZone;
+
+public class TickingFixtureClock extends Clock {
+    private static final TimeZone UTC_TIME_ZONE;
+
+    static {
+        TimeZone tempTimeZone = TimeZone.getTimeZone("Etc/UTC");
+        if (tempTimeZone == null) {
+            tempTimeZone = TimeZone.getTimeZone("UTC");
+        }
+        UTC_TIME_ZONE = tempTimeZone;
+    }
+
+
+    static Clock existingInstance;
+
+    /**
+     * 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 TickingFixtureClock replaceExisting() {
+        final Clock instance = getInstance();
+        if (instance instanceof TickingFixtureClock) {
+            return (TickingFixtureClock) instance;
+        }
+
+        final long time = Clock.getTime();
+        existingInstance = Clock.instance;
+
+        // installs as the singleton
+        Clock.remove();
+
+        return new TickingFixtureClock(time);
+    }
+
+    /**
+     * Makes {@link Clock#remove()} visible.
+     */
+    public static boolean reinstateExisting() {
+
+        Clock.instance = existingInstance;
+
+        return true;
+    }
+
+
+
+    private final Calendar calendar = Calendar.getInstance();
+    private long t0 = 0L;
+
+    private TickingFixtureClock(final long time) {
+        calendar.setTimeZone(UTC_TIME_ZONE);
+        calendar.setTimeInMillis(time);
+
+        t0 = System.currentTimeMillis();
+    }
+
+    private long getOffset() {
+        return System.currentTimeMillis() - t0;
+    }
+
+
+    /**
+     * Access via {@link Clock#getTime()}.
+     *
+     * <p>
+     * Will just return the system time until {@link #setDate(int, int, int)} or
+     * {@link #setTime(int, int)} (or one of the overloads) has been called.
+     */
+    @Override
+    protected long time() {
+        return calendar.getTime().getTime() + getOffset();
+    }
+
+    // //////////////////////////////////////////////////
+    // setting/adjusting time
+    // //////////////////////////////////////////////////
+
+    /**
+     * 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) {
+        calendar.set(Calendar.HOUR_OF_DAY, hour);
+        calendar.set(Calendar.MINUTE, min);
+        calendar.set(Calendar.SECOND, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
+        t0 = System.currentTimeMillis();
+    }
+
+    /**
+     * 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) {
+        calendar.set(Calendar.YEAR, year);
+        calendar.set(Calendar.MONTH, month - 1);
+        calendar.set(Calendar.DAY_OF_MONTH, day);
+
+        t0 = System.currentTimeMillis();
+    }
+
+    /**
+     * 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) {
+        calendar.add(Calendar.HOUR_OF_DAY, hours);
+        calendar.add(Calendar.MINUTE, minutes);
+    }
+
+    /**
+     * 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) {
+        calendar.add(Calendar.YEAR, years);
+        calendar.add(Calendar.MONTH, months);
+        calendar.add(Calendar.DAY_OF_MONTH, days);
+    }
+
+
+
+    @Override
+    public String toString() {
+        return Clock.getTimeAsDateTime().toString();
+    }
+
+}
diff --git a/core/applib/src/main/java/org/apache/isis/applib/fixturescripts/clock/TickingClockFixture.java b/core/applib/src/main/java/org/apache/isis/applib/fixturescripts/clock/TickingClockFixture.java
new file mode 100644
index 0000000..2440bf6
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/fixturescripts/clock/TickingClockFixture.java
@@ -0,0 +1,51 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.applib.fixturescripts.clock;
+
+import org.apache.isis.applib.clock.Clock;
+import org.apache.isis.applib.clock.TickingFixtureClock;
+import org.apache.isis.applib.fixtures.FixtureClock;
+import org.apache.isis.applib.fixturescripts.FixtureScript;
+import org.apache.isis.applib.fixturescripts.FixtureScriptWithExecutionStrategy;
+import org.apache.isis.applib.fixturescripts.FixtureScripts;
+
+
+public class TickingClockFixture extends FixtureScript implements FixtureScriptWithExecutionStrategy {
+
+    @Override
+    protected void execute(ExecutionContext executionContext) {
+
+        final Clock instance = Clock.getInstance();
+
+        if(instance instanceof TickingFixtureClock) {
+            TickingFixtureClock.reinstateExisting();
+            executionContext.executeChild(this, ClockFixture.setTo("2014-05-18"));
+            TickingFixtureClock.replaceExisting();
+        }
+
+        if(instance instanceof FixtureClock) {
+            executionContext.executeChild(this, ClockFixture.setTo("2014-05-18"));
+        }
+    }
+
+    @Override
+    public FixtureScripts.MultipleExecutionStrategy getMultipleExecutionStrategy() {
+        return FixtureScripts.MultipleExecutionStrategy.EXECUTE_ONCE_BY_CLASS;
+    }
+}
diff --git a/example/application/simpleapp/module-simple/src/main/java/domainapp/modules/simple/fixture/teardown/SimpleModuleTearDown.java b/example/application/simpleapp/module-simple/src/main/java/domainapp/modules/simple/fixture/teardown/SimpleModuleTearDown.java
deleted file mode 100644
index 66287bf..0000000
--- a/example/application/simpleapp/module-simple/src/main/java/domainapp/modules/simple/fixture/teardown/SimpleModuleTearDown.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one
- *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
- *  to you under the Apache License, Version 2.0 (the
- *  "License"); you may not use this file except in compliance
- *  with the License.  You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing,
- *  software distributed under the License is distributed on an
- *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- *  KIND, either express or implied.  See the License for the
- *  specific language governing permissions and limitations
- *  under the License.
- */
-
-package domainapp.modules.simple.fixture.teardown;
-
-import org.apache.isis.applib.fixturescripts.FixtureScript;
-import org.apache.isis.applib.services.jdosupport.IsisJdoSupport;
-
-public class SimpleModuleTearDown extends FixtureScript {
-
-    @Override
-    protected void execute(ExecutionContext executionContext) {
-        isisJdoSupport.executeUpdate("delete from \"simple\".\"SimpleObject\"");
-    }
-
-
-    @javax.inject.Inject
-    private IsisJdoSupport isisJdoSupport;
-
-}

-- 
To stop receiving notification emails like this one, please contact
"commits@isis.apache.org" <co...@isis.apache.org>.