Skip to content

Commit

Permalink
Took features from article and made them execute. That's all of this.…
Browse files Browse the repository at this point in the history
… Initial checkin.
  • Loading branch information
schuchert committed Mar 21, 2013
1 parent c370c4d commit b0ae8cc
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 47 deletions.
6 changes: 2 additions & 4 deletions src/main/java/shoe/example/schedule/Active.java
Expand Up @@ -7,14 +7,12 @@ public class Active implements WorkItemState {
DateTime actualStart;

public Active() {
actualStart = DateTimeFactory.now();
actualStart = BusinessDateTimeFactory.now();
}

@Override
public void tick(WorkItem item) {
DateTime now = DateTimeFactory.now();
DateTime end = item.start.plusMinutes(item.durationMinutes);

DateTime now = BusinessDateTimeFactory.now();
int minutesPassed = Minutes.minutesBetween(actualStart, now).getMinutes();

if (minutesPassed >= item.durationMinutes) {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/shoe/example/schedule/Blocked.java
@@ -1,13 +1,9 @@
package shoe.example.schedule;

import org.joda.time.DateTime;

public class Blocked implements WorkItemState {
@Override
public void tick(WorkItem item) {
DateTime now = DateTimeFactory.now();

if(item.resource.available()) {
if (item.resource.available()) {
item.tryToStart();
}
}
Expand Down
@@ -1,16 +1,26 @@
package shoe.example.schedule;

import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.MutableDateTime;
import org.springframework.stereotype.Component;


// <codeFragment name = "BusinessDateTimeAdjuster">
@Component
public class BusinessDateTimeAdjuster {
public void resetToSystemTime() {
DateTimeUtils.setCurrentMillisSystem();
}

public void setTimeTo(int hour, int minute) {
DateTimeUtils.setCurrentMillisFixed(DateTimeFactory.todayAt(hour, minute).getMillis());
DateTimeUtils.setCurrentMillisFixed(todayAt(hour, minute).getMillis());
}

DateTime todayAt(int hour, int minute) {
MutableDateTime dateTime = new MutableDateTime();
dateTime.setTime(hour, minute, 0, 0);
return dateTime.toDateTime();
}
}
// </codeFragment>
Expand Up @@ -2,7 +2,7 @@

import org.joda.time.*;

public class DateTimeFactory {
public class BusinessDateTimeFactory {
public static DateTime now() {
return new DateTime();
}
Expand All @@ -14,8 +14,7 @@ public static void restoreSystemTime() {
public static DateTime todayAt(int hour, int minute) {
MutableDateTime dateTime = now().toMutableDateTime();
dateTime.setTime(hour, minute, 0, 0);
DateTime result = dateTime.toDateTime();
return result;
return dateTime.toDateTime();
}

public static void setTimeTo(int hour, int minute) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/shoe/example/schedule/Pending.java
Expand Up @@ -5,7 +5,7 @@
public class Pending implements WorkItemState {
@Override
public void tick(WorkItem item) {
DateTime now = DateTimeFactory.now();
DateTime now = BusinessDateTimeFactory.now();

if (now.equals(item.start) || now.isAfter(item.start)) {
item.tryToStart();
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/shoe/example/schedule/Unscheduled.java
Expand Up @@ -6,7 +6,7 @@
public class Unscheduled implements WorkItemState {
@Override
public void tick(WorkItem item) {
DateTime now = DateTimeFactory.now();
DateTime now = BusinessDateTimeFactory.now();
DateTime end = item.start.plusMinutes(item.durationMinutes);

if (now.equals(item.start) || now.isAfter(item.start) && end.isAfter(now)) {
Expand All @@ -17,12 +17,4 @@ public void tick(WorkItem item) {
item.setState(new Pending());
}
}

private boolean wholeScheduleInFuture(WorkItem item, DateTime now) {
return now.isAfter(item.start);
}

private boolean wholeScheudleInPast(DateTime now, DateTime end) {
return end.isBefore(now);
}
}
Expand Up @@ -3,10 +3,12 @@
import cucumber.api.junit.Cucumber;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import shoe.example.context.SystemApplicationContext;
import shoe.example.server.EmbeddedJetty;

@Ignore
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"}, features = "src/test/resources", glue = "shoe/example/features/step_definitions")
public class FullyIntegratedSystemCucumberExecutor {
Expand Down
Expand Up @@ -18,12 +18,12 @@ public class ScheduleSteps {

@Before
public void resetToSystemTime() {
DateTimeFactory.restoreSystemTime();
BusinessDateTimeFactory.restoreSystemTime();
}

@After
public void restoreCurrentTime() {
DateTimeFactory.restoreSystemTime();
BusinessDateTimeFactory.restoreSystemTime();
}

@Given("^a system with no active work items$")
Expand All @@ -34,7 +34,7 @@ public void a_system_with_no_active_work_items() throws Throwable {
@Given("^a work item named ([^ ]+) scheduled to start at (\\d+):(\\d+), last for (\\d+) minutes, and use ([^ ]+)$")
public void a_work_item(String itemName, int startHour, int startMinutes, int durationMinutes, String resourceName) throws Throwable {
Resource r = scheduleSystemExample.findOrAddResourceNamed(resourceName);
DateTime startDateTime = DateTimeFactory.todayAt(startHour, startMinutes);
DateTime startDateTime = BusinessDateTimeFactory.todayAt(startHour, startMinutes);
WorkItem workItem = new WorkItem(itemName, startDateTime, durationMinutes, r);
scheduleSystemExample.add(workItem);
}
Expand All @@ -46,13 +46,13 @@ public void a_first_one_wins_confilict_resolution_approach() {

@When("^now is (\\d+):(\\d+)$")
public void now_is(int hour, int minute) {
DateTimeFactory.setTimeTo(hour, minute);
BusinessDateTimeFactory.setTimeTo(hour, minute);
scheduleSystemExample.recalculate();
}

@Then("^there should be no active items$")
public void there_should_be_no_active_items() throws Throwable {
assertThat(scheduleSystemExample.workItemsIn(Active.class).size(), is(new Integer(0)));
assertThat(scheduleSystemExample.workItemsIn(Active.class).size(), is(0));
}

@Then("^([^ ]+) should be ([^ ]+)$")
Expand Down
Expand Up @@ -10,26 +10,17 @@
import static org.junit.Assert.assertThat;

public class BusinessDateTimeAdjusterShould {
private BusinessDateTimeAdjuster adjuster;

@Before
public void setTheTime() {
new BusinessDateTimeAdjuster().setTimeTo(10, 17);
adjuster = new BusinessDateTimeAdjuster();
adjuster.setTimeTo(10, 17);
}

@After
public void resetTheTime() {
new BusinessDateTimeAdjuster().resetToSystemTime();
}

@Test
public void beResettable() throws Exception {
DateTime before = new DateTime();
new BusinessDateTimeAdjuster().resetToSystemTime();
DateTime after = new DateTime();
if (after.equals(before)) {
Thread.sleep(10);
after = new DateTime();
}
assertNotEquals(after, before);
adjuster.resetToSystemTime();
}

@Test
Expand All @@ -40,10 +31,22 @@ public void beAbleToSetCurrentDateTimeDuringCallToNew() {

@Test
public void beAbleToSetCurrentDateTimeWithinFactory() {
DateTime dateTime = DateTimeFactory.now();
DateTime dateTime = BusinessDateTimeFactory.now();
validateTime(dateTime);
}

@Test
public void beResettable() throws Exception {
DateTime before = new DateTime();
adjuster.resetToSystemTime();
DateTime after = new DateTime();
if (after.equals(before)) {
Thread.sleep(10);
after = new DateTime();
}
assertNotEquals(after, before);
}

private void validateTime(DateTime dateTime) {
assertThat(dateTime.toLocalTime().getHourOfDay(), is(10));
assertThat(dateTime.toLocalTime().getMinuteOfHour(), is(17));
Expand Down
Expand Up @@ -18,7 +18,7 @@ public void init() {
adjuster = new BusinessDateTimeAdjuster();

resource = new Resource("foo");
workItem = new WorkItem("Name", DateTimeFactory.todayAt(START_HOUR, START_MIN), DURATION, resource);
workItem = new WorkItem("Name", BusinessDateTimeFactory.todayAt(START_HOUR, START_MIN), DURATION, resource);

}

Expand Down
Expand Up @@ -15,7 +15,7 @@ public void init() {
adjuster = new BusinessDateTimeAdjuster();

resource = new Resource("foo");
workItem = new WorkItem("Name", DateTimeFactory.todayAt(10, 0), 10, resource);
workItem = new WorkItem("Name", BusinessDateTimeFactory.todayAt(10, 0), 10, resource);
workItem.setState(new Pending());
}

Expand All @@ -28,7 +28,7 @@ public void stayInPendingBeforeTime() {

@Test
public void activateAtTime() {
adjuster.setTimeTo(10,00);
adjuster.setTimeTo(10,0);
workItem.tick();
assertEquals(Active.class, workItem.getState().getClass());
}
Expand Down
Expand Up @@ -15,7 +15,7 @@ public void init() {
adjuster = new BusinessDateTimeAdjuster();

resource = new Resource("foo");
workItem = new WorkItem("Name", DateTimeFactory.todayAt(10, 0), 10, resource);
workItem = new WorkItem("Name", BusinessDateTimeFactory.todayAt(10, 0), 10, resource);
}

@Test
Expand Down

0 comments on commit b0ae8cc

Please sign in to comment.