Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/examples/mock2/advanced
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9237/input/javasrc/biz/xsoftware/examples/mock2/advanced
Added Files:
TestExample.java SysUnderTest.java Location.java User.java
TaskRecordService.java TaskSystem.java
Log Message:
check in due to finished advanced test
--- 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.mock2.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.mock2.advanced;
import junit.framework.TestCase;
import biz.xsoftware.examples.timer.ScheduleListener;
import biz.xsoftware.mock2.CalledMethod;
import biz.xsoftware.mock2.MockObjectFactory;
import biz.xsoftware.mock2.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
*/
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";
CalledMethod addScheduleListener=mockTaskSvc.expect("addScheduleListener");
sysUnderTest.sendUserMailWhenTaskStarted(expectedName, expectedTask);
mockTaskSvc.verify();
Object o =addScheduleListener.getParameters()[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);
CalledMethod getUser=mockRecord.expect("getUser");
CalledMethod addTaskDone=mockUser.expect("addTaskDone");
mockRecord.setDefaultReturnValue( mockUser,"getUser");
//have mockTaskSvc fire an event that should cause sysUnderTest
//to interact with mockMessaging
l.eventStarted(null);
mockRecord.verify();
mockUser.verify();
String actualUserId = (String)getUser.getParameters()[0];
assertEquals("User id was incorrect", expectedName, actualUserId);
String actualTask = (String)addTaskDone.getParameters()[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.mock2.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.mock2.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.mock2.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.mock2.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();
}
|