Permalink
Browse files

Static initializer in a few tests to reduce/remove logging. Cleaned u…

…p steps file and also changed feature a bit.
  • Loading branch information...
1 parent 035ba33 commit 5965833d09b595633b3cc0e95eaa2549286f8640 @schuchert committed Mar 27, 2013
@@ -1,5 +1,7 @@
package shoe.example.schedule;
+import org.joda.time.DateTime;
+
import java.util.LinkedList;
import java.util.List;
@@ -24,16 +26,12 @@ public Resource findOrAddResourceNamed(String resourceName) {
return resource;
}
- public void add(WorkItem workItem) {
- workItems.add(workItem);
- }
-
public void setConflictResolutionTo(ConflictResolutionApproach conflictResolutionApproach) {
this.conflictResolutionApproach = conflictResolutionApproach;
}
public boolean workItemIs(String workItemName, Class<? extends WorkItemState> workItemState) {
- WorkItem item = itemNamed(workItemName);
+ WorkItem item = workItemNamed(workItemName);
return item.stateIs(workItemState);
}
@@ -46,7 +44,7 @@ private Resource resourceNamed(String resourceName) {
return null;
}
- private WorkItem itemNamed(String workItemName) {
+ public WorkItem workItemNamed(String workItemName) {
for (WorkItem current : workItems) {
if (current.nameEquals(workItemName)) {
return current;
@@ -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);
+ }
}
@@ -3,17 +3,21 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import shoe.example.log.SystemLoggerFactory;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
+import java.util.logging.Level;
public class PersonTest {
private EntityManagerFactory emf;
private EntityManager em;
+ static {
+ SystemLoggerFactory.setLevel("org", Level.SEVERE);
+ }
+
@Before
public void initEmfAndEm() {
emf = Persistence.createEntityManagerFactory("examplePersistenceUnit");
@@ -4,14 +4,17 @@
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
-import cucumber.api.java.en.When;
import org.joda.time.DateTime;
import shoe.example.schedule.*;
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 static final String WORD = "([^ ]+)";
+ public static final String NUMBER = "(\\d+)";
+ public static final String TIME = NUMBER + ":" + NUMBER;
ScheduleSystemExample scheduleSystemExample;
@Before
@@ -24,66 +27,74 @@ public void restoreCurrentTime() {
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 {
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 {
- Resource r = scheduleSystemExample.findOrAddResourceNamed(resourceName);
DateTime startDateTime = BusinessDateTimeFactory.todayAt(startHour, startMinutes);
- WorkItem workItem = new WorkItem(itemName, startDateTime, durationMinutes, r);
- scheduleSystemExample.add(workItem);
+ scheduleSystemExample.scheduleNewWorkItem(itemName, startDateTime, durationMinutes, resourceName);
}
@Given("^a first one wins conflict resolution approach$")
public void a_first_one_wins_confilict_resolution_approach() {
scheduleSystemExample.setConflictResolutionTo(new FirstOneWins());
}
- @When("the time becomes (\\d+):(\\d+)$")
- public void the_time_becomes(int hour, int minute) {
+ @Given("^the business time is " + TIME + "$")
+ 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 endDateTime = BusinessDateTimeFactory.todayAt(hour, minute);
- while(endDateTime.isAfter(currentTime)) {
+ while (endDateTime.isAfter(currentTime)) {
currentTime = currentTime.plusMinutes(1);
- BusinessDateTimeFactory.setTimeTo(currentTime.getHourOfDay(), currentTime.getMinuteOfHour());
- scheduleSystemExample.recalculate();
+ setTimeTo(currentTime.getHourOfDay(), currentTime.getMinuteOfHour());
}
}
- @When("^now is (\\d+):(\\d+)$")
- public void now_is(int hour, int minute) {
+ private void setTimeTo(int hour, int 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(0));
- }
-
- @Then("^([^ ]+) should be ([^ ]+)$")
- public void should_be_stateX(String workItemName, String state) throws Throwable {
-
- Class<? extends WorkItemState> clazz = null;
+ private Class<? extends WorkItemState> classForStateNamed(String state) {
if ("active".equals(state)) {
- clazz = Active.class;
+ return Active.class;
} else if ("pending".equals(state)) {
- clazz = Pending.class;
+ return Pending.class;
} else if ("blocked".equals(state)) {
- clazz = Blocked.class;
+ return Blocked.class;
} else if ("completed".equals(state)) {
- clazz = Completed.class;
+ return Completed.class;
} else if ("unshceduled".equals(state)) {
- clazz = Unscheduled.class;
- } else {
- fail("Unhandled state: " + state);
+ return Unscheduled.class;
}
- boolean stateMatches = scheduleSystemExample.workItemIs(workItemName, clazz);
- assertTrue(stateMatches);
+ throw new RuntimeException("Unhandled state: " + state);
}
-
}
@@ -2,32 +2,25 @@ Feature: Handling Scheduling Conflicts
As an operator I want to make sure feature conflicts are managed by an appropriate policy.
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_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 the business time is 9:59
Scenario: Nothing going on
- When now is 9:59
- Then there should be no active items
+ Then there should be no active items at 9:59
Scenario: One item active
- Given now is 9:59
- When the time becomes 10:01
- Then Megatron_Torso should be active
+ Then Megatron_Torso should be active at 10:01
Scenario: Conflict Resolved
- When now is 10:10
- Then Megatron_Torso should be active
+ Then Megatron_Torso should be active at 10:10
And Megatron_Head should be blocked
Scenario: Delayed Start
- Given now is 10:00
- When the time becomes 10:16
- Then Megatron_Torso should be completed
+ Then Megatron_Torso should be completed at 10:16
And Megatron_Head should be active
Scenario: Delayed work item finishes late
- Given now is 9:59
- When the time becomes 10:21
- Then Megatron_Head should be completed
+ Then Megatron_Head should be completed at 10:21

0 comments on commit 5965833

Please sign in to comment.