Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/test/mock
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv16959/input/javasrc/biz/xsoftware/test/mock
Modified Files:
TestMockCreator.java
Log Message:
Added addReturnValue(String method, Object ... o) [note the varargs]. This works excellently with games testing.
Index: TestMockCreator.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/test/mock/TestMockCreator.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** TestMockCreator.java 1 Oct 2006 03:18:47 -0000 1.9
--- TestMockCreator.java 15 Nov 2006 16:54:43 -0000 1.10
***************
*** 19,23 ****
//private final static Logger log = Logger.getLogger(TestMockCreator.class.getName());
!
/**
* @param name
--- 19,23 ----
//private final static Logger log = Logger.getLogger(TestMockCreator.class.getName());
!
/**
* @param name
***************
*** 32,39 ****
String param1 = "some param";
l.callMeSecond(param1);
!
String methodName = "callMeSecond";
CalledMethod method = m.expect(methodName);
!
assertEquals("method name should be the same", methodName, method.getMethodName());
assertEquals("params should equal", param1, method.getParameter(0));
--- 32,39 ----
String param1 = "some param";
l.callMeSecond(param1);
!
String methodName = "callMeSecond";
CalledMethod method = m.expect(methodName);
!
assertEquals("method name should be the same", methodName, method.getMethodName());
assertEquals("params should equal", param1, method.getParameter(0));
***************
*** 41,54 ****
assertEquals("1st param should be equal", param1, method.getAllParams()[0]);
}
!
public void testEqualsAndToString() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l = (ListenerOne)m;
!
assertTrue("These should not be equal", !l.equals(new Integer(4)));
assertTrue("These should be equal", l.equals(l));
!
}
!
public void testNoneFailure() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
--- 41,54 ----
assertEquals("1st param should be equal", param1, method.getAllParams()[0]);
}
!
public void testEqualsAndToString() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l = (ListenerOne)m;
!
assertTrue("These should not be equal", !l.equals(new Integer(4)));
assertTrue("These should be equal", l.equals(l));
!
}
!
public void testNoneFailure() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
***************
*** 56,60 ****
String param1 = "some param";
l.callMeSecond(param1);
!
try {
m.expect(MockObject.NONE);
--- 56,60 ----
String param1 = "some param";
l.callMeSecond(param1);
!
try {
m.expect(MockObject.NONE);
***************
*** 64,68 ****
}
}
!
public void testMultipleParams() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
--- 64,68 ----
}
}
!
public void testMultipleParams() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
***************
*** 71,80 ****
Integer param2 = new Integer(2345);
l.multipleParams(param1, param2);
!
CalledMethod method = m.expect("multipleParams");
assertEquals("param1 is not correct", param1, method.getParameter(0));
assertEquals("param2 is not correct", param2, method.getParameter(1));
}
!
public void testMockCreatorFail() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
--- 71,80 ----
Integer param2 = new Integer(2345);
l.multipleParams(param1, param2);
!
CalledMethod method = m.expect("multipleParams");
assertEquals("param1 is not correct", param1, method.getParameter(0));
assertEquals("param2 is not correct", param2, method.getParameter(1));
}
!
public void testMockCreatorFail() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
***************
*** 90,109 ****
MockObject factory = MockObjectFactory.createMock(FactoryInterface.class);
MockObject car = MockObjectFactory.createMock(Car.class);
!
factory.addReturnValue("createCar", car);
!
//now we would normally tweak code on the subsystem which
//would call createcar but we are only unit testing MockCreator
Car c = ((FactoryInterface)factory).createCar("someid");
factory.expect("createCar");
!
c.openDoor();
car.expect("openDoor");
}
!
public void testThrowCheckedException() throws Exception {
MockObject mockList = MockObjectFactory.createMock(ListenerOne.class);
mockList.addThrowException("callMeSecond", new IOException("test throwing IOException"));
!
ListenerOne l = (ListenerOne)mockList;
try {
--- 90,109 ----
MockObject factory = MockObjectFactory.createMock(FactoryInterface.class);
MockObject car = MockObjectFactory.createMock(Car.class);
!
factory.addReturnValue("createCar", car);
!
//now we would normally tweak code on the subsystem which
//would call createcar but we are only unit testing MockCreator
Car c = ((FactoryInterface)factory).createCar("someid");
factory.expect("createCar");
!
c.openDoor();
car.expect("openDoor");
}
!
public void testThrowCheckedException() throws Exception {
MockObject mockList = MockObjectFactory.createMock(ListenerOne.class);
mockList.addThrowException("callMeSecond", new IOException("test throwing IOException"));
!
ListenerOne l = (ListenerOne)mockList;
try {
***************
*** 112,163 ****
} catch(IOException e) {}
}
!
public void testUsingANY() throws Exception {
MockObject mockList = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l = (ListenerOne)mockList;
!
int expected = 1;
l.callMeFirst(expected);
!
CalledMethod m = mockList.expect(MockObject.ANY);
Integer i = (Integer)m.getAllParams()[0];
assertEquals("param should be 1", new Integer(expected), i);
}
!
public void testMultipleANYs() throws Exception {
MockObject mockList = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l = (ListenerOne)mockList;
!
l.callMeFirst(1);
l.callMeSecond("second");
!
String[] methods = new String[] { MockObject.ANY, "callMeSecond" };
mockList.expect(methods);
}
!
public void testWrongMethod() {
MockObject mock = MockObjectFactory.createMock(ListenerOne.class);
!
try {
mock.expect("noSuchMethod");
fail("This should throw an exception");
! } catch(IllegalArgumentException e) {
}
}
!
public void testDefaultRetVal() {
MockObject mock = MockObjectFactory.createMock(Identical.class);
!
mock.setDefaultReturnValue("doThat", new byte[] {4});
!
Identical ident = (Identical)mock;
byte[] retVal = ident.doThat(null);
!
mock.expect("doThat");
!
assertEquals(1, retVal.length);
assertEquals(4, retVal[0]);
}
!
public void testDefaultReturnWithOtherReturns()
{
--- 112,163 ----
} catch(IOException e) {}
}
!
public void testUsingANY() throws Exception {
MockObject mockList = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l = (ListenerOne)mockList;
!
int expected = 1;
l.callMeFirst(expected);
!
CalledMethod m = mockList.expect(MockObject.ANY);
Integer i = (Integer)m.getAllParams()[0];
assertEquals("param should be 1", new Integer(expected), i);
}
!
public void testMultipleANYs() throws Exception {
MockObject mockList = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l = (ListenerOne)mockList;
!
l.callMeFirst(1);
l.callMeSecond("second");
!
String[] methods = new String[] { MockObject.ANY, "callMeSecond" };
mockList.expect(methods);
}
!
public void testWrongMethod() {
MockObject mock = MockObjectFactory.createMock(ListenerOne.class);
!
try {
mock.expect("noSuchMethod");
fail("This should throw an exception");
! } catch(IllegalArgumentException e) {
}
}
!
public void testDefaultRetVal() {
MockObject mock = MockObjectFactory.createMock(Identical.class);
!
mock.setDefaultReturnValue("doThat", new byte[] {4});
!
Identical ident = (Identical)mock;
byte[] retVal = ident.doThat(null);
!
mock.expect("doThat");
!
assertEquals(1, retVal.length);
assertEquals(4, retVal[0]);
}
!
public void testDefaultReturnWithOtherReturns()
{
***************
*** 166,174 ****
mock.setDefaultReturnValue("doThat", new byte[] {4});
mock.addReturnValue("doThat", new byte[] {5});
!
Identical ident = (Identical)mock;
byte[] retVal = ident.doThat(null);
mock.expect("doThat");
!
assertEquals(1, retVal.length);
assertEquals(3, retVal[0]);
--- 166,174 ----
mock.setDefaultReturnValue("doThat", new byte[] {4});
mock.addReturnValue("doThat", new byte[] {5});
!
Identical ident = (Identical)mock;
byte[] retVal = ident.doThat(null);
mock.expect("doThat");
!
assertEquals(1, retVal.length);
assertEquals(3, retVal[0]);
***************
*** 176,180 ****
retVal = ident.doThat(null);
mock.expect("doThat");
!
assertEquals(1, retVal.length);
assertEquals(5, retVal[0]);
--- 176,180 ----
retVal = ident.doThat(null);
mock.expect("doThat");
!
assertEquals(1, retVal.length);
assertEquals(5, retVal[0]);
***************
*** 182,218 ****
retVal = ident.doThat(null);
mock.expect("doThat");
!
assertEquals(1, retVal.length);
assertEquals(4, retVal[0]);
}
!
!
public void testReturnInterface() {
MockObject mock = MockObjectFactory.createMock(FactoryInterface.class);
!
FactoryInterface factory = (FactoryInterface)mock;
!
mock.addReturnValue("createCar", new CarImpl());
Car car = factory.createCar("id");
!
assertEquals(car.getClass(), CarImpl.class);
!
mock.expect("createCar");
}
!
public void testReturnNull() {
MockObject mock = MockObjectFactory.createMock(FactoryInterface.class);
!
FactoryInterface factory = (FactoryInterface)mock;
!
factory.createCar("aaa");
!
mock.expect("createCar");
}
!
public void testBadPrimitive() {
MockObject mock = MockObjectFactory.createMock(Car.class);
!
Car car = (Car)mock;
try {
--- 182,218 ----
retVal = ident.doThat(null);
mock.expect("doThat");
!
assertEquals(1, retVal.length);
assertEquals(4, retVal[0]);
}
!
!
public void testReturnInterface() {
MockObject mock = MockObjectFactory.createMock(FactoryInterface.class);
!
FactoryInterface factory = (FactoryInterface)mock;
!
mock.addReturnValue("createCar", new CarImpl());
Car car = factory.createCar("id");
!
assertEquals(car.getClass(), CarImpl.class);
!
mock.expect("createCar");
}
!
public void testReturnNull() {
MockObject mock = MockObjectFactory.createMock(FactoryInterface.class);
!
FactoryInterface factory = (FactoryInterface)mock;
!
factory.createCar("aaa");
!
mock.expect("createCar");
}
!
public void testBadPrimitive() {
MockObject mock = MockObjectFactory.createMock(Car.class);
!
Car car = (Car)mock;
try {
***************
*** 222,230 ****
} catch(IllegalArgumentException e) {
}
! }
!
public void testNullPrimitive() {
MockObject mock = MockObjectFactory.createMock(Car.class);
!
Car car = (Car)mock;
try {
--- 222,230 ----
} catch(IllegalArgumentException e) {
}
! }
!
public void testNullPrimitive() {
MockObject mock = MockObjectFactory.createMock(Car.class);
!
Car car = (Car)mock;
try {
***************
*** 233,241 ****
} catch(IllegalArgumentException e) {
}
! }
public void testPrimitive() {
MockObject mock = MockObjectFactory.createMock(Car.class);
!
Car car = (Car)mock;
mock.addReturnValue("getWheelCount", new Integer(5));
--- 233,241 ----
} catch(IllegalArgumentException e) {
}
! }
public void testPrimitive() {
MockObject mock = MockObjectFactory.createMock(Car.class);
!
Car car = (Car)mock;
mock.addReturnValue("getWheelCount", new Integer(5));
***************
*** 243,286 ****
mock.expect("getWheelCount");
! }
public void testBehavior() {
MockObject mock = MockObjectFactory.createMock(Identical.class);
!
mock.addBehavior("doThat", new MyTestBehavior());
Identical ident = (Identical)mock;
!
byte[] original = new byte[] { 1, 2, 3, 4, 0, 0, 0, 0};
byte[] bytes = new byte[] { 1, 2, 3, 4, 0 ,0,0,0};
!
byte[] newBytes = ident.doThat(bytes);
!
CalledMethod m = mock.expect("doThat");
byte[] passedIn = (byte[])m.getAllParams()[0];
!
assertEquals(original.length, passedIn.length);
for(int i = 0; i < original.length; i++) {
assertEquals(original[i], passedIn[i]);
}
!
//make sure this is the same byte array we passed in
assertSame(bytes, newBytes);
!
for(int i = 0; i < 4; i++) {
assertEquals(newBytes[i], newBytes[i+4]);
}
! }
!
// public void testTemp() {
// MockObject mock = MockObjectFactory.createMock(ChannelManagerService.class);
// int value = 5;
// mock.addReturnValue("getWheelCount", value);
! //
// ExtendedCar car = (ExtendedCar)mock;
// int wheelCount = car.getWheelCount();
// assertEquals(value, wheelCount);
// }
!
public void testOverloadedMethod() {
MockObject mock = MockObjectFactory.createMock(ExtendedCar.class);
--- 243,302 ----
mock.expect("getWheelCount");
! }
+ public void testMultipleReturns()
+ {
+ MockObject mock = MockObjectFactory.createMock(Car.class);
+
+ Car car = (Car)mock;
+ mock.addReturnValue("getWheelCount", 1, 2, 3, 4, 5);
+ car.getWheelCount(2);
+ car.getWheelCount(2);
+ car.getWheelCount(2);
+ car.getWheelCount(2);
+ car.getWheelCount(2);
+ mock.expect("getWheelCount", "getWheelCount", "getWheelCount", "getWheelCount", "getWheelCount");
+
+ mock.expect(MockObject.NONE);
+ }
+
public void testBehavior() {
MockObject mock = MockObjectFactory.createMock(Identical.class);
!
mock.addBehavior("doThat", new MyTestBehavior());
Identical ident = (Identical)mock;
!
byte[] original = new byte[] { 1, 2, 3, 4, 0, 0, 0, 0};
byte[] bytes = new byte[] { 1, 2, 3, 4, 0 ,0,0,0};
!
byte[] newBytes = ident.doThat(bytes);
!
CalledMethod m = mock.expect("doThat");
byte[] passedIn = (byte[])m.getAllParams()[0];
!
assertEquals(original.length, passedIn.length);
for(int i = 0; i < original.length; i++) {
assertEquals(original[i], passedIn[i]);
}
!
//make sure this is the same byte array we passed in
assertSame(bytes, newBytes);
!
for(int i = 0; i < 4; i++) {
assertEquals(newBytes[i], newBytes[i+4]);
}
! }
!
// public void testTemp() {
// MockObject mock = MockObjectFactory.createMock(ChannelManagerService.class);
// int value = 5;
// mock.addReturnValue("getWheelCount", value);
! //
// ExtendedCar car = (ExtendedCar)mock;
// int wheelCount = car.getWheelCount();
// assertEquals(value, wheelCount);
// }
!
public void testOverloadedMethod() {
MockObject mock = MockObjectFactory.createMock(ExtendedCar.class);
***************
*** 288,318 ****
mock.addReturnValue("getWheelCount", value);
!
ExtendedCar car = (ExtendedCar)mock;
int val = car.getWheelCount("asdf");
assertEquals(value, val);
}
!
public void testAddRemoveIgnore()
{
MockObject mock = MockObjectFactory.createMock(Car.class);
mock.addIgnore("openDoor");
! mock.addIgnore("closeDoor");
!
Car car = (Car)mock;
car.openDoor();
car.closeDoor();
!
mock.expect(MockObject.NONE);
!
mock.removeIgnore("closeDoor");
car.closeDoor();
!
mock.expect("closeDoor");
}
!
public static void main(String[] args) {
TestSuite suite = new TestSuite();
! suite.addTest(new TestMockCreator("testWrongMethod"));
// suite.addTest(new TestMockSuperclass("testMockCreator"));
// suite.addTest(new TestMockSuperclass("testMockCreatorFail"));
--- 304,334 ----
mock.addReturnValue("getWheelCount", value);
!
ExtendedCar car = (ExtendedCar)mock;
int val = car.getWheelCount("asdf");
assertEquals(value, val);
}
!
public void testAddRemoveIgnore()
{
MockObject mock = MockObjectFactory.createMock(Car.class);
mock.addIgnore("openDoor");
! mock.addIgnore("closeDoor");
!
Car car = (Car)mock;
car.openDoor();
car.closeDoor();
!
mock.expect(MockObject.NONE);
!
mock.removeIgnore("closeDoor");
car.closeDoor();
!
mock.expect("closeDoor");
}
!
public static void main(String[] args) {
TestSuite suite = new TestSuite();
! suite.addTest(new TestMockCreator("testWrongMethod"));
// suite.addTest(new TestMockSuperclass("testMockCreator"));
// suite.addTest(new TestMockSuperclass("testMockCreatorFail"));
|