Browse files

Finished FitNesse.Tutorials.2, Query tables and QueryResultBuilder.

  • Loading branch information...
1 parent 0c75036 commit 8126876f75e9d8ad7b5eadd271b73112ddbe688a Brett L. Schuchert committed Apr 7, 2009
View
18 DVR/src/com/om/example/dvr/domain/Program.java
@@ -15,4 +15,22 @@ public Program(String programName, String episodeName, TimeSlot timeSlot) {
public String getId() {
return String.format("(%s:%d)", programName, timeSlot.channel);
}
+
+ public String getProgramName() {
+ return programName;
+ }
+
+ public String getEpisodeName() {
+ return episodeName;
+ }
+
+ public TimeSlot getTimeSlot() {
+ return timeSlot;
+ }
+
+ public boolean sameEpisodeAs(Program program) {
+ return timeSlot.channel == program.timeSlot.channel
+ && programName.equals(program.programName)
+ && episodeName.equals(program.episodeName);
+ }
}
View
15 DVR/src/com/om/example/dvr/domain/Schedule.java
@@ -29,11 +29,26 @@ public void removeProgramById(String programIdToRemove) {
}
}
+ public List<Program> findProgramsNamedOn(String programName, int channel) {
+ List<Program> result = new LinkedList<Program>();
+
+ for (Program program : scheduledPrograms)
+ if (program.timeSlot.channel == channel
+ && program.programName.equals(programName))
+ result.add(program);
+
+ return result;
+ }
+
private boolean conflictsWithOtherTimeSlots(TimeSlot timeSlot) {
for (Program current : scheduledPrograms)
if (current.timeSlot.conflictsWith(timeSlot))
return true;
return false;
}
+
+ public void clear() {
+ scheduledPrograms.clear();
+ }
}
View
51 DVR/src/com/om/example/dvr/domain/SeasonPassManager.java
@@ -0,0 +1,51 @@
+package com.om.example.dvr.domain;
+
+import java.util.LinkedList;
+import java.util.List;
+
+public class SeasonPassManager {
+ private final Schedule schedule;
+ private List<Program> toDoList = new LinkedList<Program>();
+
+ public SeasonPassManager(Schedule schedule) {
+ this.schedule = schedule;
+ }
+
+ public int sizeOfToDoList() {
+ return toDoList.size();
+ }
+
+ public Program createNewSeasonPass(String programName, int channel) {
+ List<Program> programsFound = schedule.findProgramsNamedOn(programName, channel);
+
+ for (Program current : programsFound)
+ if (!alreadyInToDoList(current))
+ toDoList.add(current);
+
+ if (programsFound.size() > 0)
+ return programsFound.get(0);
+ return null;
+ }
+
+ private boolean alreadyInToDoList(Program candidate) {
+ for (Program current : toDoList)
+ if (current.sameEpisodeAs(candidate))
+ return true;
+
+ return false;
+ }
+
+ public Iterable<?> toDoListIterator() {
+ return toDoList;
+ }
+
+ public List<Program> toDoListContentsFor(String programId) {
+ List<Program> result = new LinkedList<Program>();
+
+ for (Program current : toDoList)
+ if (current.getId().equals(programId))
+ result.add(current);
+
+ return result;
+ }
+}
View
45 DVR/src/com/om/example/dvr/domain/SeasonPassManagerTest.java
@@ -0,0 +1,45 @@
+package com.om.example.dvr.domain;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class SeasonPassManagerTest {
+ private SeasonPassManager seasonPassManager;
+ private Schedule schedule;
+
+ private Date createDate(int year, int month, int day, int hour, int min) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.clear();
+ calendar.set(Calendar.YEAR, year);
+ calendar.set(Calendar.MONTH, month);
+ calendar.set(Calendar.DAY_OF_MONTH, day);
+ calendar.set(Calendar.HOUR, hour);
+ calendar.set(Calendar.MINUTE, min);
+
+ return calendar.getTime();
+ }
+
+ @Before
+ public void init() {
+ schedule = new Schedule();
+ schedule.addProgram("p1", "e1", 7, createDate(2008, 4, 5, 7, 0), 60);
+ schedule.addProgram("p2", "e2", 7, createDate(2008, 4, 5, 8, 0), 60);
+ seasonPassManager = new SeasonPassManager(schedule);
+ }
+
+ @Test
+ public void AssertNewSeasonPassManagerHasEmptyToDoList() {
+ assertEquals(0, seasonPassManager.sizeOfToDoList());
+ }
+
+ @Test
+ public void schduleProgramWithOneEpisodeToDoListIs1() {
+ seasonPassManager.createNewSeasonPass("p1", 7);
+ assertEquals(1, seasonPassManager.sizeOfToDoList());
+ }
+}
View
16 DVR/src/com/om/example/dvr/fixtures/AddProgramsToSchedule.java
@@ -18,6 +18,7 @@
private String programName;
private String episodeName;
private String lastId;
+ private boolean lastCreationSuccessful;
public static Schedule getSchedule() {
return schedule;
@@ -47,20 +48,25 @@ public void setMinutes(int minutes) {
this.minutes = minutes;
}
- public boolean created() {
+ public void execute() {
try {
Program p = schedule.addProgram(programName, episodeName, channel,
buildStartDateTime(), minutes);
lastId = p.getId();
+ lastCreationSuccessful = true;
} catch (ConflictingProgramException e) {
- lastId = "n/a";
- return false;
+ lastCreationSuccessful = false;
}
- return true;
+ }
+
+ public boolean created() {
+ return lastCreationSuccessful;
}
public String lastId() {
- return lastId;
+ if (lastCreationSuccessful)
+ return lastId;
+ return "n/a";
}
private Date buildStartDateTime() {
View
7 DVR/src/com/om/example/dvr/fixtures/ClearProgramSchedule.java
@@ -0,0 +1,7 @@
+package com.om.example.dvr.fixtures;
+
+public class ClearProgramSchedule {
+ public ClearProgramSchedule() {
+ AddProgramsToSchedule.getSchedule().clear();
+ }
+}
View
24 DVR/src/com/om/example/dvr/fixtures/CreateSeasonPassFor.java
@@ -0,0 +1,24 @@
+package com.om.example.dvr.fixtures;
+
+import com.om.example.dvr.domain.Program;
+import com.om.example.dvr.domain.SeasonPassManager;
+
+public class CreateSeasonPassFor {
+ private static SeasonPassManager seasonPassManager = new SeasonPassManager(
+ AddProgramsToSchedule.getSchedule());
+ private Program lastProgramFound;
+
+ public static SeasonPassManager getSeasonPassManager() {
+ return seasonPassManager;
+ }
+
+ public CreateSeasonPassFor(String programName, int channel) {
+ lastProgramFound = seasonPassManager.createNewSeasonPass(programName, channel);
+ }
+
+ public String idOfProgramScheduled() {
+ if (lastProgramFound != null)
+ return lastProgramFound.getId();
+ return "n/a";
+ }
+}
View
24 DVR/src/com/om/example/dvr/fixtures/EpisodesInToDoList.java
@@ -0,0 +1,24 @@
+package com.om.example.dvr.fixtures;
+
+import java.util.List;
+
+import com.om.example.dvr.domain.Program;
+import com.om.query.QueryResultBuilder;
+import com.om.query.domain.QueryResult;
+
+public class EpisodesInToDoList {
+ private final String programId;
+
+ public EpisodesInToDoList(String programId) {
+ this.programId = programId;
+ }
+
+ public List<Object> query() {
+ List<Program> programs = CreateSeasonPassFor.getSeasonPassManager()
+ .toDoListContentsFor(programId);
+ QueryResultBuilder builder = new QueryResultBuilder(Program.class);
+ builder.register("timeSlot", new TimeSlotPropertyHandler());
+ QueryResult result = builder.build(programs);
+ return result.render();
+ }
+}
View
52 DVR/src/com/om/example/dvr/fixtures/QueryResultBuilderExampleTest.java
@@ -0,0 +1,52 @@
+package com.om.example.dvr.fixtures;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.om.example.dvr.domain.Program;
+import com.om.example.dvr.domain.Schedule;
+import com.om.example.dvr.domain.SeasonPassManager;
+import com.om.query.QueryResultBuilder;
+import com.om.query.domain.QueryResult;
+
+public class QueryResultBuilderExampleTest {
+ private SeasonPassManager seasonPassManager;
+ private Schedule schedule;
+
+ private Date createDate(int year, int month, int day, int hour, int min) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.clear();
+ calendar.set(Calendar.YEAR, year);
+ calendar.set(Calendar.MONTH, month);
+ calendar.set(Calendar.DAY_OF_MONTH, day);
+ calendar.set(Calendar.HOUR, hour);
+ calendar.set(Calendar.MINUTE, min);
+
+ return calendar.getTime();
+ }
+
+ @Before
+ public void init() {
+ schedule = new Schedule();
+ schedule.addProgram("p1", "e1", 7, createDate(2008, 4, 5, 7, 0), 60);
+ schedule.addProgram("p2", "e2", 7, createDate(2008, 4, 5, 8, 0), 60);
+ seasonPassManager = new SeasonPassManager(schedule);
+ }
+
+ @Test
+ public void queryResultBuilderCanTranslateToDoListCorrectly() {
+ seasonPassManager.createNewSeasonPass("p1", 7);
+
+ QueryResultBuilder builder = new QueryResultBuilder(Program.class);
+ builder.register("timeSlot", new TimeSlotPropertyHandler());
+ QueryResult result = builder.build(seasonPassManager.toDoListIterator());
+ List<Object> renderedObjects = result.render();
+ assertEquals(1, renderedObjects.size());
+ }
+}
View
26 DVR/src/com/om/example/dvr/fixtures/TimeSlotPropertyHandler.java
@@ -0,0 +1,26 @@
+package com.om.example.dvr.fixtures;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import com.om.example.dvr.domain.TimeSlot;
+import com.om.query.domain.ObjectDescription;
+import com.om.query.handler.PropertyHandler;
+import com.om.reflection.PropertyGetter;
+
+public class TimeSlotPropertyHandler extends PropertyHandler {
+ static SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy");
+ static SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm");
+
+ @Override
+ public void handle(PropertyGetter propertyGetter, Object targetObject,
+ ObjectDescription objectDescription) {
+ TimeSlot timeSlot = propertyGetter.getValue(targetObject, TimeSlot.class);
+
+ Date startDateTime = timeSlot.startDateTime;
+
+ objectDescription.addPropertyDescription("date", dateFormat.format(startDateTime));
+ objectDescription.addPropertyDescription("startTime", timeFormat
+ .format(startDateTime));
+ }
+}

0 comments on commit 8126876

Please sign in to comment.