Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/test/mock
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23654/input/javasrc/biz/xsoftware/test/mock
Added Files:
Car.java FactoryInterface.java ListenerOne.java MockOne.java
TestMockCreator.java TestOrderedCalls.java
TestUnorderedCalls.java
Log Message:
get code coverage back up to 80% by eliminating unfinished examples and running the example tests.
--- 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.MockObjectFactory;
import biz.xsoftware.mock.MockObject;
/**
* @author Dean Hiller
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
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.expectCall(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.expectCall(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.expectCall("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.expectCall("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)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.expectCall("createCar");
c.openDoor();
car.expectCall("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.expectCall(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.expectOrderedCalls(methods);
}
public void testWrongMethod() {
MockObject mock = MockObjectFactory.createMock(ListenerOne.class);
try {
mock.expectCall("noSuchMethod");
fail("This should throw an exception");
} catch(IllegalArgumentException e) {
}
}
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: 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: 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()
*/
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();
}
--- 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() {
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() {
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() {
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);
}
}
|