Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/advanced
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv24740/input/javasrc/biz/xsoftware/examples/advanced
Added Files:
User.java Location.java TaskSystem.java TestExample.java
TaskRecordService.java SysUnderTest.java
Log Message:
original commit of mocklib2 that will become mocklib3
--- NEW FILE: TaskSystem.java ---
/*
* Created on Jul 15, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.advanced;
import biz.xsoftware.examples.timer.ScheduleListener;
/**
*
* @author Dean Hiller
*/
public interface TaskSystem {
public void addScheduleListener(ScheduleListener l);
public void removeScheduleListener(ScheduleListener l);
}
--- NEW FILE: TestExample.java ---
/*
* Created on Jun 28, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.advanced;
import junit.framework.TestCase;
import biz.xsoftware.examples.timer.ScheduleListener;
import biz.xsoftware.mock.MockObjectFactory;
import biz.xsoftware.mock.MockObject;
/**
* JUnit Suite of TestCases for demonstrating mocking out a more
* complex api.
*
* @author Dean Hiller
*/
public class TestExample extends TestCase {
private SysUnderTest sysUnderTest;
private MockObject mockTaskSvc;
private MockObject mockRecord;
/**
* @showcode
*/
public TestExample(String name) {
super(name);
}
/**
* @showcode
*/
@Override
public void setUp() {
mockTaskSvc = MockObjectFactory.createMock(TaskSystem.class);
mockRecord = MockObjectFactory.createMock(TaskRecordService.class);
sysUnderTest = new SysUnderTest((TaskRecordService)mockRecord, (TaskSystem)mockTaskSvc);
}
/**
* Some api's can be more OO oriented then others and have no
* one facade into the whole subsystem as it may be too large,
* so this demonstrates how even those api's can be mocked out.
*
* This also demonstrates a susbystem that relies on two subsystems
* such that you can trigger an event in one subsystem and test
* the interaction with the other subsystem.
*
* @showcode
*/
public void testSystemInteractionWithSubsytemsAPI() {
String expectedName = "joe";
String expectedTask = "printpaper";
sysUnderTest.sendUserMailWhenTaskStarted(expectedName, expectedTask);
Object o = mockTaskSvc.expect("addScheduleListener").getAllParams()[0];
ScheduleListener l = (ScheduleListener)o;
//set an object to return from mockRecord as we know we are testing
//that a call to getUser results from an event
MockObject mockUser = MockObjectFactory.createMock(User.class);
mockUser.addReturnValue("addTaskDone", "foo");
mockRecord.addReturnValue("getUser", mockUser);
//have mockTaskSvc fire an event that should cause sysUnderTest
//to interact with mockMessaging
l.eventStarted(null);
String actualUserId = (String)mockRecord.expect("getUser").getAllParams()[0];
assertEquals("User id was incorrect", expectedName, actualUserId);
String actualTask = (String)mockUser.expect("addTaskDone").getAllParams()[0];
assertEquals("task name was incorrect", expectedTask, actualTask);
}
}
--- NEW FILE: SysUnderTest.java ---
/*
* Created on Jun 28, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.advanced;
import biz.xsoftware.examples.timer.ScheduleListener;
/**
* The SysUnderTest here is a WebStore or something which needs
* to authorize purchases.
*/
public class SysUnderTest {
private TaskRecordService msgSystem;
private TaskSystem taskSystem;
/**
* @showcode
*/
public SysUnderTest(TaskRecordService msgSystem, TaskSystem t) {
this.msgSystem = msgSystem;
this.taskSystem = t;
}
/**
* @showcode
*/
public void sendUserMailWhenTaskStarted(String id, String taskName) {
taskSystem.addScheduleListener(new TaskStartedListener(id, taskName));
}
private class TaskStartedListener implements ScheduleListener {
private String name;
private String userId;
/**
* @showcode
*/
public TaskStartedListener(String userId, String name) {
this.name = name;
this.userId = userId;
}
/**
* @showcode
* @see biz.xsoftware.examples.timer.ScheduleListener#eventStarted(java.lang.String)
*/
public void eventStarted(String title) {
User u = msgSystem.getUser(userId);
u.addTaskDone(name);
}
}
}
--- NEW FILE: TaskRecordService.java ---
/*
* Created on Jul 15, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.advanced;
/**
* @author Dean Hiller
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public interface TaskRecordService {
public User getUser(String id);
}
--- NEW FILE: User.java ---
/*
* Created on Jul 15, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.advanced;
/**
* An interface defining the more complex api we are mocking out.
*
* @author Dean Hiller
*/
public interface User {
public String addTaskDone(String taskName);
}
--- NEW FILE: Location.java ---
/*
* Created on Jul 15, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.advanced;
/**
* @author Dean Hiller
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public interface Location {
public String getOfficeNumber();
public String getStreet();
public String getStreetNumber();
public String getZipCode();
}
|