Skip to content

Commit

Permalink
Static initializer in a few tests to reduce/remove logging. Cleaned u…
Browse files Browse the repository at this point in the history
…p steps file and also changed feature a bit.
  • Loading branch information
schuchert committed Mar 27, 2013
1 parent 035ba33 commit 5965833
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 55 deletions.
16 changes: 10 additions & 6 deletions src/main/java/shoe/example/schedule/ScheduleSystemExample.java
@@ -1,5 +1,7 @@
package shoe.example.schedule; package shoe.example.schedule;


import org.joda.time.DateTime;

import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;


Expand All @@ -24,16 +26,12 @@ public Resource findOrAddResourceNamed(String resourceName) {
return resource; return resource;
} }


public void add(WorkItem workItem) {
workItems.add(workItem);
}

public void setConflictResolutionTo(ConflictResolutionApproach conflictResolutionApproach) { public void setConflictResolutionTo(ConflictResolutionApproach conflictResolutionApproach) {
this.conflictResolutionApproach = conflictResolutionApproach; this.conflictResolutionApproach = conflictResolutionApproach;
} }


public boolean workItemIs(String workItemName, Class<? extends WorkItemState> workItemState) { public boolean workItemIs(String workItemName, Class<? extends WorkItemState> workItemState) {
WorkItem item = itemNamed(workItemName); WorkItem item = workItemNamed(workItemName);
return item.stateIs(workItemState); return item.stateIs(workItemState);
} }


Expand All @@ -46,7 +44,7 @@ private Resource resourceNamed(String resourceName) {
return null; return null;
} }


private WorkItem itemNamed(String workItemName) { public WorkItem workItemNamed(String workItemName) {
for (WorkItem current : workItems) { for (WorkItem current : workItems) {
if (current.nameEquals(workItemName)) { if (current.nameEquals(workItemName)) {
return current; return current;
Expand Down Expand Up @@ -74,4 +72,10 @@ public void recalculate() {
} }
} }
} }

public void scheduleNewWorkItem(String itemName, DateTime startDateTime, int durationMinutes, String resourceName) {
Resource resource = findOrAddResourceNamed(resourceName);
WorkItem workItem = new WorkItem(itemName, startDateTime, durationMinutes, resource);
workItems.add(workItem);
}
} }
8 changes: 6 additions & 2 deletions src/test/java/shoe/example/db/PersonTest.java
Expand Up @@ -3,17 +3,21 @@
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import shoe.example.log.SystemLoggerFactory;
import org.slf4j.LoggerFactory;


import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence; import javax.persistence.Persistence;
import java.util.logging.Level;


public class PersonTest { public class PersonTest {
private EntityManagerFactory emf; private EntityManagerFactory emf;
private EntityManager em; private EntityManager em;


static {
SystemLoggerFactory.setLevel("org", Level.SEVERE);
}

@Before @Before
public void initEmfAndEm() { public void initEmfAndEm() {
emf = Persistence.createEntityManagerFactory("examplePersistenceUnit"); emf = Persistence.createEntityManagerFactory("examplePersistenceUnit");
Expand Down
Expand Up @@ -4,14 +4,17 @@
import cucumber.api.java.Before; import cucumber.api.java.Before;
import cucumber.api.java.en.Given; import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then; import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import shoe.example.schedule.*; import shoe.example.schedule.*;


import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;


public class ScheduleSteps { public class ScheduleSteps {
public static final String WORD = "([^ ]+)";
public static final String NUMBER = "(\\d+)";
public static final String TIME = NUMBER + ":" + NUMBER;
ScheduleSystemExample scheduleSystemExample; ScheduleSystemExample scheduleSystemExample;


@Before @Before
Expand All @@ -24,66 +27,74 @@ public void restoreCurrentTime() {
BusinessDateTimeFactory.restoreSystemTime(); BusinessDateTimeFactory.restoreSystemTime();
} }


@Given("^a system with no active work items$") @Given("^an empty schedule$")
public void a_system_with_no_active_work_items() throws Throwable { public void a_system_with_no_active_work_items() throws Throwable {
scheduleSystemExample = new ScheduleSystemExample(); scheduleSystemExample = new ScheduleSystemExample();
} }


@Given("^a work item named ([^ ]+) scheduled to start at (\\d+):(\\d+), last for (\\d+) minutes, and use ([^ ]+)$") @Given("^a work item named ([^ ]+) scheduled to start at " + TIME + ", last for " + NUMBER + " minutes, and use " + WORD + "$")
public void a_work_item(String itemName, int startHour, int startMinutes, int durationMinutes, String resourceName) throws Throwable { public void a_work_item(String itemName, int startHour, int startMinutes, int durationMinutes, String resourceName) throws Throwable {
Resource r = scheduleSystemExample.findOrAddResourceNamed(resourceName);
DateTime startDateTime = BusinessDateTimeFactory.todayAt(startHour, startMinutes); DateTime startDateTime = BusinessDateTimeFactory.todayAt(startHour, startMinutes);
WorkItem workItem = new WorkItem(itemName, startDateTime, durationMinutes, r); scheduleSystemExample.scheduleNewWorkItem(itemName, startDateTime, durationMinutes, resourceName);
scheduleSystemExample.add(workItem);
} }


@Given("^a first one wins conflict resolution approach$") @Given("^a first one wins conflict resolution approach$")
public void a_first_one_wins_confilict_resolution_approach() { public void a_first_one_wins_confilict_resolution_approach() {
scheduleSystemExample.setConflictResolutionTo(new FirstOneWins()); scheduleSystemExample.setConflictResolutionTo(new FirstOneWins());
} }


@When("the time becomes (\\d+):(\\d+)$") @Given("^the business time is " + TIME + "$")
public void the_time_becomes(int hour, int minute) { public void the_business_time_is(int hour, int minute) {
setTimeTo(hour, minute);
}

@Then("there should be no active items at " + TIME + "$")
public void there_should_be_no_active_items_at(int hour, int minute) {
moveTimeForwardTo(hour, minute);
assertThat(scheduleSystemExample.workItemsIn(Active.class).size(), is(0));
}

@Then("^" + WORD + " should be " + WORD + " at " + TIME + "$")
public void workItem_should_be_state_at(String workItemName, String state, int hour, int minute) {
moveTimeForwardTo(hour, minute);
boolean stateMatches = scheduleSystemExample.workItemIs(workItemName, classForStateNamed(state));
WorkItem item = scheduleSystemExample.workItemNamed(workItemName);
assertTrue(String.format("Expected state: %s - actual: %s", state, item.getState().getClass().getSimpleName()), stateMatches);
}

@Then("^([^ ]+) should be ([^ ]+)$")
public void should_be_stateX(String workItemName, String state) throws Throwable {
boolean stateMatches = scheduleSystemExample.workItemIs(workItemName, classForStateNamed(state));
assertTrue(stateMatches);
}

private void moveTimeForwardTo(int hour, int minute) {
DateTime currentTime = BusinessDateTimeFactory.now(); DateTime currentTime = BusinessDateTimeFactory.now();
DateTime endDateTime = BusinessDateTimeFactory.todayAt(hour, minute); DateTime endDateTime = BusinessDateTimeFactory.todayAt(hour, minute);
while(endDateTime.isAfter(currentTime)) { while (endDateTime.isAfter(currentTime)) {
currentTime = currentTime.plusMinutes(1); currentTime = currentTime.plusMinutes(1);
BusinessDateTimeFactory.setTimeTo(currentTime.getHourOfDay(), currentTime.getMinuteOfHour()); setTimeTo(currentTime.getHourOfDay(), currentTime.getMinuteOfHour());
scheduleSystemExample.recalculate();
} }
} }


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


@Then("^there should be no active items$") private Class<? extends WorkItemState> classForStateNamed(String state) {
public void there_should_be_no_active_items() throws Throwable {
assertThat(scheduleSystemExample.workItemsIn(Active.class).size(), is(0));
}

@Then("^([^ ]+) should be ([^ ]+)$")
public void should_be_stateX(String workItemName, String state) throws Throwable {

Class<? extends WorkItemState> clazz = null;
if ("active".equals(state)) { if ("active".equals(state)) {
clazz = Active.class; return Active.class;
} else if ("pending".equals(state)) { } else if ("pending".equals(state)) {
clazz = Pending.class; return Pending.class;
} else if ("blocked".equals(state)) { } else if ("blocked".equals(state)) {
clazz = Blocked.class; return Blocked.class;
} else if ("completed".equals(state)) { } else if ("completed".equals(state)) {
clazz = Completed.class; return Completed.class;
} else if ("unshceduled".equals(state)) { } else if ("unshceduled".equals(state)) {
clazz = Unscheduled.class; return Unscheduled.class;
} else {
fail("Unhandled state: " + state);
} }


boolean stateMatches = scheduleSystemExample.workItemIs(workItemName, clazz); throw new RuntimeException("Unhandled state: " + state);
assertTrue(stateMatches);
} }

} }
21 changes: 7 additions & 14 deletions src/test/resources/features/SchedulingConflicts.feature
Expand Up @@ -2,32 +2,25 @@ Feature: Handling Scheduling Conflicts
As an operator I want to make sure feature conflicts are managed by an appropriate policy. As an operator I want to make sure feature conflicts are managed by an appropriate policy.


Background: Background:
Given a system with no active work items Given an empty schedule
And a work item named Megatron_Torso scheduled to start at 10:00, last for 15 minutes, and use 3d_printer_1 And a work item named Megatron_Torso scheduled to start at 10:00, last for 15 minutes, and use 3d_printer_1
And a work item named Megatron_Head scheduled to start at 10:10, last for 5 minutes, and use 3d_printer_1 And a work item named Megatron_Head scheduled to start at 10:10, last for 5 minutes, and use 3d_printer_1
And a first one wins conflict resolution approach And a first one wins conflict resolution approach
And the business time is 9:59


Scenario: Nothing going on Scenario: Nothing going on
When now is 9:59 Then there should be no active items at 9:59
Then there should be no active items


Scenario: One item active Scenario: One item active
Given now is 9:59 Then Megatron_Torso should be active at 10:01
When the time becomes 10:01
Then Megatron_Torso should be active


Scenario: Conflict Resolved Scenario: Conflict Resolved
When now is 10:10 Then Megatron_Torso should be active at 10:10
Then Megatron_Torso should be active
And Megatron_Head should be blocked And Megatron_Head should be blocked


Scenario: Delayed Start Scenario: Delayed Start
Given now is 10:00 Then Megatron_Torso should be completed at 10:16
When the time becomes 10:16
Then Megatron_Torso should be completed
And Megatron_Head should be active And Megatron_Head should be active


Scenario: Delayed work item finishes late Scenario: Delayed work item finishes late
Given now is 9:59 Then Megatron_Head should be completed at 10:21
When the time becomes 10:21
Then Megatron_Head should be completed

0 comments on commit 5965833

Please sign in to comment.