Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/test/mock
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv24740/input/javasrc/biz/xsoftware/test/mock
Added Files:
TestUnorderedCalls.java FactoryInterface.java MockOne.java
Identical.java Car.java CarImpl.java TestOrderedCalls.java
OldBehavior.java TestMockCreator.java ListenerOne.java
Log Message:
original commit of mocklib2 that will become mocklib3
--- NEW FILE: TestUnorderedCalls.java ---
/*
* Created on Apr 24, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.test.mock;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import biz.xsoftware.mock.ExpectFailedException;
/**
* @author Dean Hiller
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class TestUnorderedCalls extends TestCase {
//private final static Logger log = Logger.getLogger(TestUnorderedCalls.class.getName());
/**
* @param name
*/
public TestUnorderedCalls(String name) {
super(name);
}
public void testUnorderedWorks() {
MockOne one = new MockOne(10);
one.callMeSecond("dummy");
one.callMeFirst(324);
String[] evts = new String[] { MockOne.FIRST, MockOne.SECOND };
one.expectUnorderedCalls(evts);
}
public void testExtraInUnorderedExpect() {
MockOne one = new MockOne(3000);
one.callMeSecond("dummy");
one.callMeExtra();
one.callMeFirst(5544);
String[] evts = new String[] { MockOne.FIRST, MockOne.SECOND };
try {
one.expectUnorderedCalls(evts);
fail("Should have failed this test case");
} catch(ExpectFailedException e) { /*gulp*/ }
}
public void testExtraAfterUnorderedExpect() {
MockOne one = new MockOne(10);
one.callMeSecond("dummy");
one.callMeFirst(322);
one.callMeExtra();
String[] evts = new String[] { MockOne.FIRST, MockOne.SECOND };
try {
one.expectUnorderedCalls(evts);
fail("Should have failed this test case");
} catch(ExpectFailedException e) { /*gulp*/ }
}
public void testTimeout() {
MockOne one = new MockOne(10);
one.callMeSecond("dummy");
try {
String[] evts = new String[] { MockOne.FIRST, MockOne.SECOND };
one.expectUnorderedCalls(evts);
} catch(ExpectFailedException e) {
assertEquals("reason must be correct", ExpectFailedException.TIMED_OUT, e.getReason());
}
}
public static void main(String[] args) {
TestSuite suite = new TestSuite();
// suite.addTest(new TestUnorderedCalls("testExceptionThrown"));
// suite.addTest(new TestUnorderedCalls("testMockCreator"));
// suite.addTest(new TestUnorderedCalls("testMockCreatorFail"));
// suite.addTest(new TestUnorderedCalls("testMockCreatorCreatesCar"));
suite.addTest(new TestUnorderedCalls("testTimeout"));
TestRunner.run(suite);
}
}
--- NEW FILE: TestMockCreator.java ---
/*
* Created on Apr 24, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.test.mock;
import java.io.IOException;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import biz.xsoftware.mock.CalledMethod;
import biz.xsoftware.mock.ExpectFailedException;
import biz.xsoftware.mock.MockObject;
import biz.xsoftware.mock.MockObjectFactory;
/**
* @author Dean Hiller
*/
public class TestMockCreator extends TestCase {
//private final static Logger log = Logger.getLogger(TestMockCreator.class.getName());
/**
* @param name
*/
public TestMockCreator(String name) {
super(name);
}
public void testMockCreator() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l = (ListenerOne)m;
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));
assertEquals("param count should be 1", 1, method.getParameterCount());
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);
ListenerOne l = (ListenerOne)m;
String param1 = "some param";
l.callMeSecond(param1);
try {
m.expect(MockObject.NONE);
fail("Should have failed with ExpectFailedException");
} catch(ExpectFailedException e) {
//gulp
}
}
public void testMultipleParams() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l = (ListenerOne)m;
String param1 = "someparam2";
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);
ListenerOne l = (ListenerOne)m;
l.callMeSecond("dummy");
try {
m.expect("callMeFirst");
fail("should have thrown exception");
} catch(ExpectFailedException e) {}
}
public void testMockCreatorCreatesCar() throws Exception {
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 {
l.callMeSecond("dummy");
fail("should have thrown IOException");
} 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()
{
MockObject mock = MockObjectFactory.createMock(Identical.class);
mock.addReturnValue("doThat", new byte[] {3});
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]);
retVal = ident.doThat(null);
mock.expect("doThat");
assertEquals(1, retVal.length);
assertEquals(5, retVal[0]);
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 {
mock.addReturnValue("getWheelCount", new Long(56));
car.getWheelCount();
fail("should have thrown exception");
} catch(IllegalArgumentException e) {
}
}
public void testNullPrimitive() {
MockObject mock = MockObjectFactory.createMock(Car.class);
Car car = (Car)mock;
try {
car.getWheelCount();
fail("should have thrown exception");
} catch(IllegalArgumentException e) {
}
}
public void testPrimitive() {
MockObject mock = MockObjectFactory.createMock(Car.class);
Car car = (Car)mock;
mock.addReturnValue("getWheelCount", new Integer(5));
car.getWheelCount();
mock.expect("getWheelCount");
}
public void testBehavior() {
MockObject mock = MockObjectFactory.createMock(Identical.class);
mock.addBehavior("doThat", new OldBehavior());
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 testAddRemoveIgnore()
{
MockObject mock = MockObjectFactory.createMock(Car.class);
mock.addIgnore("openDoor", "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"));
// suite.addTest(new TestMockSuperclass("testMockCreatorCreatesCar"));
// suite.addTest(new TestMockSuperclass("testThrowCheckedException"));
TestRunner.run(suite);
}
}
--- NEW FILE: CarImpl.java ---
/**
* Copyright (C) 2006 Carrier Access, Corp.
*/
package biz.xsoftware.test.mock;
/**
*/
public class CarImpl implements Car
{
/**
* @see biz.xsoftware.test.mock.Car#openDoor()
*/
public void openDoor()
{
}
/**
* @see biz.xsoftware.test.mock.Car#closeDoor()
*/
public void closeDoor()
{
}
/**
* @see biz.xsoftware.test.mock.Car#getWheelCount()
*/
public int getWheelCount()
{
return 0;
}
}
--- NEW FILE: ListenerOne.java ---
/*
* Created on Apr 24, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.test.mock;
import java.io.IOException;
/**
* @author Dean Hiller
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public interface ListenerOne {
public void callMeFirst(int s);
public void callMeSecond(String s) throws IOException;
public void multipleParams(String x, Integer i);
}
--- NEW FILE: Identical.java ---
/**
* Copyright (C) 2006 Carrier Access, Corp.
*/
package biz.xsoftware.test.mock;
/**
*/
public interface Identical
{
public byte[] doThat(byte[] data);
}
--- NEW FILE: OldBehavior.java ---
/**
*
*/
package biz.xsoftware.test.mock;
import biz.xsoftware.mock.Behavior;
class OldBehavior implements Behavior {
/**
* @see biz.xsoftware.mock.Behavior#clone(java.lang.Object[])
*/
public Object[] clone(Object[] params)
{
Object[] retVal = new Object[params.length];
for(int i = 0; i < retVal.length; i++) {
Object val = params[i];
if(val instanceof byte[]) {
retVal[i] = cloneBytes((byte[])val);
}
}
return retVal;
}
private Object cloneBytes(byte[] bytes)
{
byte[] newBytes = new byte[bytes.length];
for(int i = 0; i < newBytes.length; i++) {
newBytes[i] = bytes[i];
}
return newBytes;
}
/**
* @see biz.xsoftware.mock.Behavior#runMethod(java.lang.Object[])
*/
public Object runMethod(Object[] params)
{
//we know the test does an 4 byte array, so do that
byte[] bytes = (byte[])params[0];
for(int i = 0; i < 4; i++) {
bytes[i+4] = bytes[i];
}
//make sure to return the same byte buffer so we are really testing this...
return bytes;
}
}
--- NEW FILE: MockOne.java ---
/*
* Created on Apr 24, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.test.mock;
import biz.xsoftware.mock.MockSuperclass;
/**
* @author Dean Hiller
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class MockOne extends MockSuperclass implements ListenerOne {
public static final String FIRST = "callMeFirst";
public static final String SECOND = "callMeSecond";
public static final String EXTRA = "callMeExtra";
public static final String MULTIPLE_PARAMS = "multipleParams";
public MockOne(int delay) {
super(delay);
}
/* (non-Javadoc)
* @see test.biz.xsoftware.testlib.test.ListenerOne#callMeFirst()
*/
public void callMeFirst(int s) {
methodCalled(FIRST, new Integer(s));
}
/* (non-Javadoc)
* @see test.biz.xsoftware.testlib.test.ListenerOne#callMeSecond()
*/
public void callMeSecond(String s) {
methodCalled(SECOND, s);
}
public void callMeExtra() {
methodCalled(EXTRA, null);
}
/* (non-Javadoc)
* @see biz.xsoftware.test.ListenerOne#multipleParams(java.lang.String, java.lang.Integer)
*/
public void multipleParams(String x, Integer i) {
methodCalled(MULTIPLE_PARAMS, new Object[] { x, i });
}
/* (non-Javadoc)
* @see biz.xsoftware.mock.MockSuperclass#getClasses()
*/
@Override
public Class[] getClasses() {
return new Class[] { MockOne.class };
}
public Object inst() {
// TODO Auto-generated method stub
return null;
}
}
--- NEW FILE: FactoryInterface.java ---
/*
* Created on Jun 8, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.test.mock;
/**
* @author Dean Hiller
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public interface FactoryInterface {
public Car createCar(String id);
}
--- NEW FILE: Car.java ---
/*
* Created on Jun 8, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.test.mock;
/**
* @author Dean Hiller
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public interface Car {
public void openDoor();
public void closeDoor();
public int getWheelCount();
}
--- NEW FILE: TestOrderedCalls.java ---
/*
* Created on Apr 24, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.test.mock;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import biz.xsoftware.mock.ExpectFailedException;
/**
* @author Dean Hiller
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class TestOrderedCalls extends TestCase {
//private final static Logger log = Logger.getLogger(TestOrderedCalls.class.getName());
/**
* @param name
*/
public TestOrderedCalls(String name) {
super(name);
}
public void testBasicEventBeforeExpect() {
MockOne one = new MockOne(3000);
one.callMeFirst(4);
one.expectCall(MockOne.FIRST);
}
public void testBasicExpectBeforeEvent() {
final MockOne one = new MockOne(3000);
Thread r = new Thread() {
@Override
public void run() {
delay(1000);
one.callMeFirst(4);
}
};
r.start();
one.expectCall(MockOne.FIRST);
}
public void testOrderAfter() {
MockOne one = new MockOne(3000);
one.callMeFirst(4);
one.callMeSecond("dummy");
String[] evts = new String[] { MockOne.FIRST, MockOne.SECOND };
one.expectOrderedCalls(evts);
}
public void testOrderBefore() {
final MockOne one = new MockOne(3000);
Thread r = new Thread() {
@Override
public void run() {
delay(1000);
one.callMeFirst(4);
one.callMeSecond("dummy");
}
};
r.start();
String[] evts = new String[] { MockOne.FIRST, MockOne.SECOND };
one.expectOrderedCalls(evts);
}
public void testOrderAfterFails() {
MockOne one = new MockOne(3000);
one.callMeFirst(7);
one.callMeSecond("dummy");
String[] evts = new String[] { MockOne.SECOND, MockOne.FIRST };
try {
one.expectOrderedCalls(evts);
fail("Should have failed this test case");
} catch(ExpectFailedException e) { /*gulp*/ }
}
public void testOrderBeforeFails() {
final MockOne one = new MockOne(3000);
Thread r = new Thread() {
@Override
public void run() {
delay(1000);
one.callMeFirst(14);
one.callMeSecond("dummy");
}
};
String[] evts = new String[] { MockOne.SECOND, MockOne.FIRST };
try {
r.run();
one.expectOrderedCalls(evts);
fail("Should have failed this test case");
} catch(ExpectFailedException e) { /*gulp*/ }
}
public void testLeftOverEventFails() {
MockOne one = new MockOne(3000);
one.callMeFirst(234);
one.callMeSecond("dummy");
one.callMeFirst(43);
String[] evts = new String[] { MockOne.FIRST, MockOne.SECOND };
try {
one.expectOrderedCalls(evts);
fail("Should have failed this test case");
} catch(ExpectFailedException e) { /*gulp*/ }
}
public void testExceptionThrown() {
MockOne one = new MockOne(3000);
RuntimeException e = new IllegalStateException("Test for robustness");
one.addThrowException(MockOne.FIRST, e);
try {
one.callMeFirst(324);
fail("Should have thrown exception");
} catch(IllegalStateException e2) {}
one.callMeSecond("dummy");
String[] methods = new String[] {MockOne.FIRST, MockOne.SECOND };
one.expectOrderedCalls(methods);
}
private void delay(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
throw new RuntimeException("Sleep ended abrubtly");
}
}
public static void main(String[] args) {
TestSuite suite = new TestSuite();
suite.addTest(new TestOrderedCalls("testExceptionThrown"));
// suite.addTest(new TestMockSuperclass("testMockCreator"));
// suite.addTest(new TestMockSuperclass("testMockCreatorFail"));
// suite.addTest(new TestMockSuperclass("testMockCreatorCreatesCar"));
// suite.addTest(new TestMockSuperclass("testThrowCheckedException"));
TestRunner.run(suite);
}
}
|