[Mockobject-checkins] mockobject2/input/javasrc/biz/xsoftware/test TestMockCreator.java,1.1,1.2
Brought to you by:
fastdragon
From: Nobody <fas...@us...> - 2005-03-18 16:01:08
|
Update of /cvsroot/mockobject/mockobject2/input/javasrc/biz/xsoftware/test In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2922/input/javasrc/biz/xsoftware/test Modified Files: TestMockCreator.java Log Message: Index: TestMockCreator.java =================================================================== RCS file: /cvsroot/mockobject/mockobject2/input/javasrc/biz/xsoftware/test/TestMockCreator.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** TestMockCreator.java 12 Sep 2004 07:14:10 -0000 1.1 --- TestMockCreator.java 18 Mar 2005 16:00:59 -0000 1.2 *************** *** 1,161 **** ! /* ! * 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; ! ! import java.io.IOException; ! import java.util.logging.Logger; ! ! 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)); ! ! String name = ""+l;//test toString does not throw exception. ! ! } ! ! 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; ! ! int expected = 1; ! l.callMeFirst(1); ! l.callMeSecond("second"); ! ! String[] methods = new String[] { MockObject.ANY, "callMeSecond" }; ! mockList.expectOrderedCalls(methods); ! } ! private void smallTest(String s) { ! s.trim(); ! } ! public static void main(String[] args) { ! TestSuite suite = new TestSuite(); ! suite.addTest(new TestMockCreator("testExceptionThrown")); ! // suite.addTest(new TestMockSuperclass("testMockCreator")); ! // suite.addTest(new TestMockSuperclass("testMockCreatorFail")); ! // suite.addTest(new TestMockSuperclass("testMockCreatorCreatesCar")); ! // suite.addTest(new TestMockSuperclass("testThrowCheckedException")); ! ! TestRunner.run(suite); ! } ! } --- 1,174 ---- ! /* ! * 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; ! ! import java.io.IOException; ! import java.util.logging.Logger; ! ! 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)); ! ! String name = ""+l;//test toString does not throw exception. ! ! } ! ! 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; ! ! int expected = 1; ! 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) { ! } ! } ! ! private void smallTest(String s) { ! s.trim(); ! } ! ! ! 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); ! } ! } |