You can subscribe to this list here.
2001 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
(13) |
Aug
(151) |
Sep
(21) |
Oct
(6) |
Nov
(70) |
Dec
(8) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2002 |
Jan
(47) |
Feb
(66) |
Mar
(23) |
Apr
(115) |
May
(24) |
Jun
(53) |
Jul
(10) |
Aug
(279) |
Sep
(84) |
Oct
(149) |
Nov
(138) |
Dec
(52) |
2003 |
Jan
(22) |
Feb
(20) |
Mar
(29) |
Apr
(106) |
May
(170) |
Jun
(122) |
Jul
(70) |
Aug
(64) |
Sep
(27) |
Oct
(71) |
Nov
(49) |
Dec
(9) |
2004 |
Jan
(7) |
Feb
(38) |
Mar
(3) |
Apr
(9) |
May
(22) |
Jun
(4) |
Jul
(1) |
Aug
(2) |
Sep
(2) |
Oct
|
Nov
(15) |
Dec
(2) |
2005 |
Jan
(1) |
Feb
(1) |
Mar
|
Apr
(1) |
May
(28) |
Jun
(3) |
Jul
(11) |
Aug
(5) |
Sep
(1) |
Oct
(5) |
Nov
(2) |
Dec
(3) |
2006 |
Jan
(8) |
Feb
(3) |
Mar
(8) |
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: Jeff M. <cus...@us...> - 2003-04-10 10:34:00
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects In directory sc8-pr-cvs1:/tmp/cvs-serv24519/src/core/test/mockobjects Modified Files: TestReturnObjectBag.java Log Message: Added support for int Index: TestReturnObjectBag.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/TestReturnObjectBag.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- TestReturnObjectBag.java 23 Jan 2003 13:09:13 -0000 1.2 +++ TestReturnObjectBag.java 10 Apr 2003 10:33:57 -0000 1.3 @@ -9,6 +9,8 @@ private final ReturnObjectBag bag = new ReturnObjectBag(getName()); private static final String KEY1 = "key1"; private static final String KEY2 = "key2"; + private static final short SHORT_KEY1 = 1; + private static final short SHORT_KEY2 = 2; private static final String VALUE_ONE = "one"; private static final String VALUE_TWO = "two"; @@ -38,7 +40,16 @@ assertEquals("Should be second result", VALUE_TWO, bag.getNextReturnObject(KEY2)); bag.verify(); } - + + public void testShortKey(){ + bag.putObjectToReturn(SHORT_KEY1, VALUE_ONE); + bag.putObjectToReturn(SHORT_KEY2, VALUE_TWO); + + assertEquals("Should be first result", VALUE_ONE, bag.getNextReturnObject(SHORT_KEY1)); + assertEquals("Should be second result", VALUE_TWO, bag.getNextReturnObject(SHORT_KEY2)); + bag.verify(); + } + public void testNoListForKey(){ try { bag.getNextReturnObject(KEY1); |
From: Jeff M. <cus...@us...> - 2003-04-10 10:33:26
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects In directory sc8-pr-cvs1:/tmp/cvs-serv24273/src/core/com/mockobjects Modified Files: ReturnObjectBag.java Log Message: Added support for int Index: ReturnObjectBag.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/ReturnObjectBag.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ReturnObjectBag.java 23 Jan 2003 13:09:13 -0000 1.2 +++ ReturnObjectBag.java 10 Apr 2003 10:33:22 -0000 1.3 @@ -5,8 +5,7 @@ import com.mockobjects.util.AssertMo; import com.mockobjects.util.Null; -import java.util.HashMap; -import java.util.Iterator; +import java.util.*; /** * The ReturnObjectBag is a map containing instances of ReturnObjectList. @@ -20,7 +19,7 @@ * @version $Revision$ */ public class ReturnObjectBag implements Verifiable { - private final HashMap returnObjectLists = new HashMap(); + private final Hashtable returnObjectLists = new Hashtable(); private final String name; /** @@ -37,10 +36,10 @@ * @see ReturnObjectList#addObjectToReturn */ public void putObjectToReturn(Object key, Object value) { - ReturnObjectList returnObjectList = (ReturnObjectList) returnObjectLists.get(key); if (key == null) { key = Null.NULL; } + ReturnObjectList returnObjectList = (ReturnObjectList) returnObjectLists.get(key); if (returnObjectList == null) { returnObjectList = new ReturnObjectList(name + "." + key.toString()); returnObjectLists.put(key, returnObjectList); @@ -50,6 +49,16 @@ } /** + * Places an object into the list of return objects for a particular int key + * @param key the key against which the object will be stored + * @param value the value to be added to the list for that key + * @see ReturnObjectList#addObjectToReturn + */ + public void putObjectToReturn(int key, Object value) { + putObjectToReturn(new Integer(key), value); + } + + /** * Checks each the list for each key to verify that all no objects remain * in the list for that key. * @see ReturnObjectList#verify @@ -76,4 +85,17 @@ AssertMo.assertNotNull(name + " does not contain " + key.toString(), returnObjectList); return returnObjectList.nextReturnObject(); } + + /** + * Returns the next object in the ReturnObjectList for a given int key. + * The call will throw an AssertFailError if the requested key is + * not present within this ReturnObjectBag. + * @param key The key for which the next object should be returned. + * @return The next object from the ReturnObjectList stored against the given key. + * @see ReturnObjectList#nextReturnObject + */ + public Object getNextReturnObject(int key) { + return getNextReturnObject(new Integer(key)); + } + } |
From: Tim M. <ma...@us...> - 2003-04-10 01:04:15
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv2619/src/core/com/mockobjects/dynamic Modified Files: Tag: DynamicMockExperiment C.java Log Message: Conveniance for eq Constraint[] Index: C.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/C.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- C.java 4 Apr 2003 16:47:38 -0000 1.1.2.1 +++ C.java 10 Apr 2003 01:04:11 -0000 1.1.2.2 @@ -30,6 +30,14 @@ return new IsEqual(o); } + public static Constraint[] eq( Object[] o ) { + Constraint[] result = new Constraint[o.length]; + for (int i = 0; i < result.length; i++) { + result[i] = new IsEqual(o[i]); + } + return result; + } + public static Constraint eq( int n ) { return new IsEqual( new Integer(n) ); } |
From: Nat P. <nat...@b1...> - 2003-04-08 15:44:17
|
While supping a latte in a pavement cafe on Upper Street (ah, the joys of unemployment!) a particle of inspiration happened to penetrate my dense skull and hit a receptive spot within, sparking some thoughts about the new dynamic mock branch. I'm commiting them to email in case I forget them before I get a chance to implement them, and Cc'ing them to the mockobjects mailing list for review by everyone else. Firstly, I think that the "CallMocker" interface would be better renamed to "Callable", and "CallSet" would be better named "CallableSelection". Secondly, I have come to agree with your (Tim's) idea about separating the sugar methods from the composite callable classes. You're right -- it will makes the methods much easier to test. I also now think that the Mock class should *not* inherit from CallableSelection. By extending CallableSelection, Mock objects inherit the matches method, which is of no use to their clients. By separating the sugar methods from the composite implementations, and making the Mock class extend the sugar class, we get a much simpler design, make the sugar methods easier to test, remove the unnecessary matches method from the Mock class, and allow the user to choose whether a mock expects a selection or sequence of callables at construction time. A problem we had on Monday was how to make it easy for users to create composites *and* use sugar methods for those composites. The solution is for the sugar methods that create composites (expectSequence for example) to create and return a wrapper that provides sugar methods for the new composite, rather than return the composite itself. I've attached a quick UML class diagram that illustrates this. Apologies for the BDUF, but I have no easy access to CVS at the moment and so cannot implement it directly (and I would prefer to pair program it anyway -- perhaps at an XTC evening). I've called the sugar class "CallCollection", and the base interface of CallableSequence and CallableSelection, "CompositeCallable". CompositeCallable just adds the method "void add( Call call )" to the Callable interface. Use of the sugar methods would look the same as in your (Tim's) email but would always return a CallCollection rather than a CallSequence or CallSelection: Mock aMock = new Mock(HTTPServletResponse.class); CallCollection seq = aMock.expectSequence(); seq.expectVoid( "setContentType", C.args("text/html") ); CallCollection sel = seq.expectSelection(); // these headers must be set, but can be in any order sel.expectVoid("setHeader", C.args( C.eq("header1") ); sel.expectVoid("setHeader", C.args( C.eq("header2") ); sel.expectVoid("setHeader", C.args( C.eq("header3") ); // additional headers are optional sel.permitVoid("setHeader", C.args( C.IS_ANYTHING ) ); seq.expectAndReturn( "getWriter", C.args(), WRITER ); Cheers, Nat. _______________________ Dr. Nathaniel Pryce B13media Ltd. http://www.b13media.com +44 (0)7712 526 661 |
From: Nat P. <nat...@b1...> - 2003-04-08 10:28:56
|
Here's some more info about the new branch, and how it improves the old design, and some thoughts on naming. Mock now inherits from CallSet (bad name,see below) and, when CallSequence is also implemented, will inherit from CallCollection. Mock adds the dynamic proxying to a collection of calls. The CallCollection base class will hold the implementation of the syntactic sugar, so new syntactic sugar will automagically be supported by CallSet, CallSequence and Mock objects. This removes a major deficiency in the previous design that duplicated the sugar methods in the Mock and CallSequence classes. We felt that the code was driving us to pull out a "Call" class that represented a call in progress. Call objects would store the method name, argument types, argument values and possible the receiver mock and proxy objects. Methods which now receive a method name and object array as arguments would be changed to receive a reference to a Call object. This would also make it easy to support overridden methods, which the current implementation doesn't yet do. Naming... Tim and I wrote this new version quite fast, and there were several times when we couldn't think of a good name for something and chose to press on with implementation and refactor later. So feel free to suggest new names for classes and/or methods. Currently the names "CallSet" and "CallMocker" make me feel particularly uncomfortable. CallSet does not implement either a mathematical set or a mathematical bag. Since it chooses CallMocker to call, I would now rename it to "CallSelector", which I think fits well with the name "CallSequence". I can't currently think of a good name for CallMocker, but when we implement the Call class a new name will be pretty urgent. Probably because of the meta nature of using mocks to test mocks that make calls to objects that mock calls, the code is currently full of classes with the works "call" and "mock" in them in various combinations and orders, which makes it confusing. The worst examples are where we had to use the MockCallMocker class to test CallMocker subclasses! Finally, because I'm currently in the middle of moving house (on to a boat actually) I will be disconnected for a while and won't be able to work on the new branch. After this weekend I'll have time to pair with somebody again, but I will not be able to do so at my place for a week or two. Regards, Nat. _______________________ Dr. Nathaniel Pryce B13media Ltd. http://www.b13media.com +44 (0)7712 526 661 ----- Original Message ----- From: "Tim Mackinnon" <tim...@po...> To: "Mockobjects-Java-Dev" <moc...@li...> Cc: <ch...@e2...>; "Paolo Polce" <pao...@bt...>; <ma...@co...>; "Ivan Moore" <iv...@ta...> Sent: Tuesday, April 08, 2003 2:16 AM Subject: [MO-java-dev] Dynamic Mock Updates > Guys - > > The recent rash of cvs commits are an attempt by Nat and I (and Steve too) > to get a version of the java dynamic mock implementation in a state that it > supports the creation of simple, easily readable tests, but at the same time > supports more complicated constraint based calls for the rarer cases that > need something extra. The followig is a bit of a ramble (or brain dump) - > but i thought its probably best to dump away so people know whats going on > (and can try it out. I want to give it a spin for a few days and see how it > feels to). > > As we all know, supporting hard coded mocks is a bit of pain (sometimes - as > with the file io stuff - there isn't any simple way... although the work on > NMock and rupert's team gives some other possibilities with class loader > method replacements). In doing this work we generated some hard mocks and > noted that we need to sort out MockMakers' reliance on things that aren't in > the core library (or are, but are called something different). > > For clean, TDD code - dynamic mocks give a nice TDD feel if there isn't too > much overhead in writing the tests. The previous implementation established > the usefulness of a dynamic approach but it was a bit awkward to use in > practice and fixing some of its defficiencies were tricky. > > The aim of this new branch is too: > - make writing normal tests as easy as possible (see the acceptance test > below). The discussion when with a partner should be natural with not too > much initial complexity. > - make failed mock tests break cleanly and clearly with good error messages > (the classic library did a reasonably good job of this, but had no tests to > show it) > - provide a tested framework that will support modifications (e.g. eat our > own dogfood) > - allow support for more aesoteric<sp?> call orderings. e.g. *Sequence*Bag* > > Our test functional test was to write a servlet test that could look like > this: > > public void testDoGetNew() throws ServletException, IOException { > Mock mockHttpServletResponse = new Mock(HttpServletResponse.class, > "response"); > Mock mockHttpServletRequest = new Mock(HttpServletRequest.class); // name > optional > > mockHttpServletRequest.expectAndReturn( > "getParameter", C.args(C.eq("subject")), "Mail Subject" ); > mockHttpServletRequest.expectAndReturn( > "getParameter", C.args(C.eq("body")), "Mail Body" ); > mockHttpServletRequest.permitAndReturn( > "getParameter", C.args(C.eq("browser-identifier")), "MSIE-5.0" ); > > final StringWriter contentWriter = new StringWriter(); > // Not yet implemented - proposed way of introducing a sequence with not to > much syntax > // CallSequence seq = mockHttpServletResponse.expectSequence(); > // seq.expectVoid( "setContentType", C.args(C.eq("text/html")) ); > // seq.expectAndReturn( "getWriter", C.args(), contentWriter ); > > SimpleServlet aServlet = new SimpleServlet(); > aServlet.doGet( > (HttpServletRequest) mockHttpServletRequest.proxy(), > (HttpServletResponse) mockHttpServletResponse.proxy()); > > mockHttpServletRequest.verify(); > mockHttpServletResponse.verify(); > } > > In the branch we haven't got ordered sequences working yet (it should be > simple - a proposed syntax is shown. We want something that isn't too heavy > wait and works will with TDD). In eclipse - to find the branch search for > tags on the Mock class (e.g. tag is DynamicMockExperiment). > > Some notes: > > a mock defaults to a type of CallSet (we can't agree on this - tim thinks it > has a type of CallSet). Either way, there is a primitive add method that has > CallMatch and ExpectedCall objects (that decorate each other) to give the > symantics we want. There is no CallSequence yet, adding it should give a > CallCollection super class. > > We haven't yet got a SingleCall decoration to limit a CallMatch to a single > invocation (again should be easy). CallSet has a DecoratorFactory so we have > a hook to test the syntactic sugar for expectAndReturn (by the way - > permitAndReturn is a way of stubbing without an expectation, permit because > the return is based on a call that conforms to the constraints). > > Everything conforms to CallMocker (an awful name - can't think of a good > one). Having this interface meant we could use mockmaker to generate hard > mocks to allow us to test the implementation. We didn't do this soon enough > (thinking it was too meta circular, but if you don't mix dynamic and hard > mocks it works out well and it simplified a lot of our testing. > > Not all the names are correct, and some of the tests could do with a partner > for Nat or I to work with to cause a cleanup (anyone up for remote pairing?) > > I think thats it for now. > > Tim > --- > Outgoing mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.467 / Virus Database: 266 - Release Date: 01/04/2003 > > > > ------------------------------------------------------- > This SF.net email is sponsored by: ValueWeb: > Dedicated Hosting for just $79/mo with 500 GB of bandwidth! > No other company gives more support or power for your dedicated server > http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/ > _______________________________________________ > Mockobjects-java-dev mailing list > Moc...@li... > https://lists.sourceforge.net/lists/listinfo/mockobjects-java-dev |
From: Tim M. <tim...@po...> - 2003-04-08 01:13:05
|
Guys - The recent rash of cvs commits are an attempt by Nat and I (and Steve too) to get a version of the java dynamic mock implementation in a state that it supports the creation of simple, easily readable tests, but at the same time supports more complicated constraint based calls for the rarer cases that need something extra. The followig is a bit of a ramble (or brain dump) - but i thought its probably best to dump away so people know whats going on (and can try it out. I want to give it a spin for a few days and see how it feels to). As we all know, supporting hard coded mocks is a bit of pain (sometimes - as with the file io stuff - there isn't any simple way... although the work on NMock and rupert's team gives some other possibilities with class loader method replacements). In doing this work we generated some hard mocks and noted that we need to sort out MockMakers' reliance on things that aren't in the core library (or are, but are called something different). For clean, TDD code - dynamic mocks give a nice TDD feel if there isn't too much overhead in writing the tests. The previous implementation established the usefulness of a dynamic approach but it was a bit awkward to use in practice and fixing some of its defficiencies were tricky. The aim of this new branch is too: - make writing normal tests as easy as possible (see the acceptance test below). The discussion when with a partner should be natural with not too much initial complexity. - make failed mock tests break cleanly and clearly with good error messages (the classic library did a reasonably good job of this, but had no tests to show it) - provide a tested framework that will support modifications (e.g. eat our own dogfood) - allow support for more aesoteric<sp?> call orderings. e.g. *Sequence*Bag* Our test functional test was to write a servlet test that could look like this: public void testDoGetNew() throws ServletException, IOException { Mock mockHttpServletResponse = new Mock(HttpServletResponse.class, "response"); Mock mockHttpServletRequest = new Mock(HttpServletRequest.class); // name optional mockHttpServletRequest.expectAndReturn( "getParameter", C.args(C.eq("subject")), "Mail Subject" ); mockHttpServletRequest.expectAndReturn( "getParameter", C.args(C.eq("body")), "Mail Body" ); mockHttpServletRequest.permitAndReturn( "getParameter", C.args(C.eq("browser-identifier")), "MSIE-5.0" ); final StringWriter contentWriter = new StringWriter(); // Not yet implemented - proposed way of introducing a sequence with not to much syntax // CallSequence seq = mockHttpServletResponse.expectSequence(); // seq.expectVoid( "setContentType", C.args(C.eq("text/html")) ); // seq.expectAndReturn( "getWriter", C.args(), contentWriter ); SimpleServlet aServlet = new SimpleServlet(); aServlet.doGet( (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy()); mockHttpServletRequest.verify(); mockHttpServletResponse.verify(); } In the branch we haven't got ordered sequences working yet (it should be simple - a proposed syntax is shown. We want something that isn't too heavy wait and works will with TDD). In eclipse - to find the branch search for tags on the Mock class (e.g. tag is DynamicMockExperiment). Some notes: a mock defaults to a type of CallSet (we can't agree on this - tim thinks it has a type of CallSet). Either way, there is a primitive add method that has CallMatch and ExpectedCall objects (that decorate each other) to give the symantics we want. There is no CallSequence yet, adding it should give a CallCollection super class. We haven't yet got a SingleCall decoration to limit a CallMatch to a single invocation (again should be easy). CallSet has a DecoratorFactory so we have a hook to test the syntactic sugar for expectAndReturn (by the way - permitAndReturn is a way of stubbing without an expectation, permit because the return is based on a call that conforms to the constraints). Everything conforms to CallMocker (an awful name - can't think of a good one). Having this interface meant we could use mockmaker to generate hard mocks to allow us to test the implementation. We didn't do this soon enough (thinking it was too meta circular, but if you don't mix dynamic and hard mocks it works out well and it simplified a lot of our testing. Not all the names are correct, and some of the tests could do with a partner for Nat or I to work with to cause a cleanup (anyone up for remote pairing?) I think thats it for now. Tim --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.467 / Virus Database: 266 - Release Date: 01/04/2003 |
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv3129/src/core/test/mockobjects/dynamic Modified Files: Tag: DynamicMockExperiment ExpectedCallTest.java MockTest.java CallSetTest.java Added Files: Tag: DynamicMockExperiment CallMatchTest.java MockCallFactory.java Log Message: Basic implementation - adds syntactic sugar for permitAndReturn, expectAndReturn etc. - separated CallMatch from ExpectedCall Still needs Sequence, SingleCall, CallCount support --- NEW FILE: CallMatchTest.java --- /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; import com.mockobjects.constraint.*; import com.mockobjects.dynamic.*; import com.mockobjects.util.*; import junit.framework.*; public class CallMatchTest extends TestCase { final String methodName = "methodName"; Mock mock = new Mock(DummyInterface.class,"mock"); MockCallMocker mockDecorated = new MockCallMocker(); CallMatch call = new CallMatch( methodName, new Constraint[0], mockDecorated ); public CallMatchTest(String name) { super(name); } public void testCallArgumentsArePropagatedToDecorated() throws Throwable { final Object[] arguments = new Object[0]; final String result = "result"; mockDecorated.setExpectedCall( mock, methodName, arguments ); mockDecorated.setupCallReturn( result ); call.call(mock, methodName, arguments); mockDecorated.verifyExpectations(); } public void testUncalledCallVerifies() { mockDecorated.setExpectedVerifyCalls(1); call.verify(); mockDecorated.verifyExpectations(); } public void testSuccessfulCallVerifies() throws Throwable { mockDecorated.setExpectedVerifyCalls(1); mockDecorated.setupCallReturn("result"); call.call( mock, "methodName", new Object[0] ); call.verify(); mockDecorated.verifyExpectations(); } public void testMultipleCallsSucceed() throws Throwable { mockDecorated.setupCallReturn("result"); call.call( mock, methodName, new Object[0] ); call.call( mock, methodName, new Object[0] ); mockDecorated.verifyExpectations(); } public void testArgumentsCheckedAgainstConstraints() throws Throwable { String[] args = { "arg1", "arg2", "arg3" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[2], true ) }; mockDecorated.setupCallReturn("result"); CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); call.call( mock, methodName, args ); for( int i = 0; i < constraints.length; i++ ) { constraints[i].verify(); } mockDecorated.verifyExpectations(); } public void testWrongNumberOfArguments() throws Throwable { String[] args = { "arg1", "arg2" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[1], true ) }; CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); boolean passed = true; try { call.call( mock, methodName, args ); passed = false; } catch (AssertionFailedError ex) { AssertMo.assertIncludes("Should show expected number of arguments", Integer.toString(constraints.length), ex.getMessage()); AssertMo.assertIncludes("Should show actual number of arguments", Integer.toString(args.length), ex.getMessage()); AssertMo.assertIncludes("Should include the method name", methodName, ex.getMessage()); } assertTrue("Should fail is call doesn't give an exception", passed); mockDecorated.verifyExpectations(); } public void testConstraintFailure() throws Throwable { String[] args = { "argA", "argB", "argC" }; MockConstraint[] constraints = { new MockConstraint( "constraintA", args[0], true ), new MockConstraint( "constraintB", args[1], false ), new MockConstraint( "constraintC", args[2], true ) }; CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); boolean passed = true; try { call.call( mock, methodName, args ); passed = false; } catch (AssertionFailedError ex) { AssertMo.assertIncludes("Should include the method name", methodName, ex.getMessage()); AssertMo.assertIncludes("Should show index of failed argument", "1", ex.getMessage() ); AssertMo.assertIncludes("Should show expected arguments", AssertMo.join(constraints), ex.getMessage()); AssertMo.assertIncludes("Should show actual arguments", AssertMo.join(args), ex.getMessage()); } assertTrue("Should fail is call doesn't give an exception", passed); mockDecorated.verifyExpectations(); } public void testCallMatchesArguments() throws Throwable { String[] args = { "arg1", "arg2", "arg3" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[2], true ) }; mockDecorated.setExpectedMatches( methodName, args ); mockDecorated.setupMatchesReturn(true); CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); assertTrue( "call matches", call.matches( methodName, args) ); mockDecorated.verifyExpectations(); } public void testCallDoesNotMatchIfDecoratedDoesNotMatch() throws Throwable { String[] args = { "arg1", "arg2", "arg3" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[2], true ) }; mockDecorated.setExpectedMatches( methodName, args ); mockDecorated.setupMatchesReturn(false); CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); assertFalse( "call does not match when decorated does not match", call.matches( methodName, args) ); mockDecorated.verifyExpectations(); } public void testCallDoesNotMatchWhenWrongName() throws Throwable { CallMatch call = new CallMatch( methodName, new Constraint[0], mockDecorated ); assertFalse( "call does not match", call.matches( "anotherName", new Object[0]) ); mockDecorated.verifyExpectations(); } public void testCallDoesNotMatchWhenWrongNumberOfArguments() throws Throwable { String[] args = { "arg1", "arg2" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[1], true ) }; CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); assertFalse( "call does not match", call.matches( methodName, args) ); mockDecorated.verifyExpectations(); } public void testCallDoesNotMatchWhenConstraintIsViolated() throws Throwable { String[] args = { "argA", "argB", "argC" }; MockConstraint[] constraints = { new MockConstraint( "constraintA", args[0], true ), new MockConstraint( "constraintB", args[1], false ), new MockConstraint( "constraintC", args[2], true ) }; CallMatch call = new CallMatch( methodName, (Constraint[])constraints, mockDecorated ); assertFalse( "call does not match", call.matches( methodName, args) ); mockDecorated.verifyExpectations(); } public void testMatchesAfterCalls() throws Throwable { mockDecorated.setupCallReturn("result"); mockDecorated.setupMatchesReturn(true); CallMatch call = new CallMatch(methodName, C.args(), mockDecorated ); call.call( mock, methodName, new Object[0] ); assertTrue( "matches after first call", call.matches( methodName, new Object[0]) ); call.call( mock, methodName, new Object[0] ); assertTrue( "matches after further calls", call.matches( methodName, new Object[0]) ); mockDecorated.verifyExpectations(); } } --- NEW FILE: MockCallFactory.java --- package test.mockobjects.dynamic; import mockmaker.ReturnValues; import mockmaker.VoidReturnValues; import mockmaker.ExceptionalReturnValue; import com.mockobjects.*; import com.mockobjects.dynamic.CallFactory; import com.mockobjects.dynamic.CallMocker; import com.mockobjects.constraint.Constraint; public class MockCallFactory implements CallFactory{ private ExpectationCounter myCreateReturnStubCalls = new ExpectationCounter("MockCallFactory.createReturnStub(Object)"); private ReturnValues myActualCreateReturnStubReturnValues = new ReturnValues("MockCallFactory.createReturnStub(Object)", true); private ExpectationList myCreateReturnStubParameter0Values = new ExpectationList("MockCallFactory.createReturnStub(Object) java.lang.Object"); private ExpectationCounter myCreateThrowStubCalls = new ExpectationCounter("MockCallFactory.createThrowStub(Throwable)"); private ReturnValues myActualCreateThrowStubReturnValues = new ReturnValues("MockCallFactory.createThrowStub(Throwable)", true); private ExpectationList myCreateThrowStubParameter0Values = new ExpectationList("MockCallFactory.createThrowStub(Throwable) java.lang.Throwable"); private ExpectationCounter myCreateExpectedCallCalls = new ExpectationCounter("MockCallFactory.createExpectedCall(CallMocker)"); private ReturnValues myActualCreateExpectedCallReturnValues = new ReturnValues("MockCallFactory.createExpectedCall(CallMocker)", true); private ExpectationList myCreateExpectedCallParameter0Values = new ExpectationList("MockCallFactory.createExpectedCall(CallMocker) com.mockobjects.dynamic.CallMocker"); private ExpectationCounter myCreateCallMatchCalls = new ExpectationCounter("MockCallFactory.createCallMatch(String, Constraint[], CallMocker)"); private ReturnValues myActualCreateCallMatchReturnValues = new ReturnValues("MockCallFactory.createCallMatch(String, Constraint[], CallMocker)", true); private ExpectationList myCreateCallMatchParameter0Values = new ExpectationList("MockCallFactory.createCallMatch(String, Constraint[], CallMocker) java.lang.String"); private ExpectationList myCreateCallMatchParameter1Values = new ExpectationList("MockCallFactory.createCallMatch(String, Constraint[], CallMocker) com.mockobjects.constraint.Constraint"); private ExpectationList myCreateCallMatchParameter2Values = new ExpectationList("MockCallFactory.createCallMatch(String, Constraint[], CallMocker) com.mockobjects.dynamic.CallMocker"); public void setExpectedCreateReturnStubCalls(int calls){ myCreateReturnStubCalls.setExpected(calls); } public void addExpectedCreateReturnStub(Object arg0){ myCreateReturnStubParameter0Values.addExpected(arg0); } public CallMocker createReturnStub(Object arg0){ myCreateReturnStubCalls.inc(); myCreateReturnStubParameter0Values.addActual(arg0); Object nextReturnValue = myActualCreateReturnStubReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return (CallMocker) nextReturnValue; } public void setupExceptionCreateReturnStub(Throwable arg){ myActualCreateReturnStubReturnValues.add(new ExceptionalReturnValue(arg)); } public void setupCreateReturnStub(CallMocker arg){ myActualCreateReturnStubReturnValues.add(arg); } public void setExpectedCreateThrowStubCalls(int calls){ myCreateThrowStubCalls.setExpected(calls); } public void addExpectedCreateThrowStub(Throwable arg0){ myCreateThrowStubParameter0Values.addExpected(arg0); } public CallMocker createThrowStub(Throwable arg0){ myCreateThrowStubCalls.inc(); myCreateThrowStubParameter0Values.addActual(arg0); Object nextReturnValue = myActualCreateThrowStubReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return (CallMocker) nextReturnValue; } public void setupExceptionCreateThrowStub(Throwable arg){ myActualCreateThrowStubReturnValues.add(new ExceptionalReturnValue(arg)); } public void setupCreateThrowStub(CallMocker arg){ myActualCreateThrowStubReturnValues.add(arg); } public void setExpectedCreateExpectedCallCalls(int calls){ myCreateExpectedCallCalls.setExpected(calls); } public void addExpectedCreateExpectedCall(CallMocker arg0){ myCreateExpectedCallParameter0Values.addExpected(arg0); } public CallMocker createExpectedCall(CallMocker arg0){ myCreateExpectedCallCalls.inc(); myCreateExpectedCallParameter0Values.addActual(arg0); Object nextReturnValue = myActualCreateExpectedCallReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return (CallMocker) nextReturnValue; } public void setupExceptionCreateExpectedCall(Throwable arg){ myActualCreateExpectedCallReturnValues.add(new ExceptionalReturnValue(arg)); } public void setupCreateExpectedCall(CallMocker arg){ myActualCreateExpectedCallReturnValues.add(arg); } public void setExpectedCreateCallMatchCalls(int calls){ myCreateCallMatchCalls.setExpected(calls); } public void addExpectedCreateCallMatch(String arg0, Constraint[] arg1, CallMocker arg2){ myCreateCallMatchParameter0Values.addExpected(arg0); myCreateCallMatchParameter1Values.addExpected(arg1); myCreateCallMatchParameter2Values.addExpected(arg2); } public CallMocker createCallMatch(String arg0, Constraint[] arg1, CallMocker arg2){ myCreateCallMatchCalls.inc(); myCreateCallMatchParameter0Values.addActual(arg0); myCreateCallMatchParameter1Values.addActual(arg1); myCreateCallMatchParameter2Values.addActual(arg2); Object nextReturnValue = myActualCreateCallMatchReturnValues.getNext(); if (nextReturnValue instanceof ExceptionalReturnValue && ((ExceptionalReturnValue)nextReturnValue).getException() instanceof RuntimeException) throw (RuntimeException)((ExceptionalReturnValue)nextReturnValue).getException(); return (CallMocker) nextReturnValue; } public void setupExceptionCreateCallMatch(Throwable arg){ myActualCreateCallMatchReturnValues.add(new ExceptionalReturnValue(arg)); } public void setupCreateCallMatch(CallMocker arg){ myActualCreateCallMatchReturnValues.add(arg); } public void verify(){ myCreateReturnStubCalls.verify(); myCreateReturnStubParameter0Values.verify(); myCreateThrowStubCalls.verify(); myCreateThrowStubParameter0Values.verify(); myCreateExpectedCallCalls.verify(); myCreateExpectedCallParameter0Values.verify(); myCreateCallMatchCalls.verify(); myCreateCallMatchParameter0Values.verify(); myCreateCallMatchParameter1Values.verify(); myCreateCallMatchParameter2Values.verify(); } } Index: ExpectedCallTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/Attic/ExpectedCallTest.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- ExpectedCallTest.java 7 Apr 2003 14:27:01 -0000 1.1.2.1 +++ ExpectedCallTest.java 7 Apr 2003 17:38:38 -0000 1.1.2.2 @@ -1,189 +1,102 @@ /* - * Created on 04-Apr-2003 - * - * To change this generated comment go to - * Window>Preferences>Java>Code Generation>Code and Comments + * Created on 07-Apr-2003 */ package test.mockobjects.dynamic; -import com.mockobjects.constraint.*; import com.mockobjects.dynamic.*; import com.mockobjects.util.*; import junit.framework.*; +/** + * @author dev + */ public class ExpectedCallTest extends TestCase { + final String result = "result!"; final String methodName = "methodName"; - Mock mock = new Mock(DummyInterface.class,"mock"); - ExpectedCall call; + final Object[] args = { "arg1", "arg2" }; + final String decoratedDescription = AssertMo.methodToString( methodName, args ); + + Mock ignoredMock = null; + MockCallMocker mockDecorated = new MockCallMocker(); + ExpectedCall call = new ExpectedCall( mockDecorated ); public ExpectedCallTest(String name) { super(name); } + + public void setUp() { + mockDecorated.setupGetDescription(decoratedDescription); + } - public void testCallArgumentsArePropagatedToDecorated() throws Throwable { - final Object[] arguments = new Object[0]; - final String result = "result"; - - MockCallMocker mockDecorated = new MockCallMocker(); - mockDecorated.setExpectedCall( mock, methodName, arguments ); - mockDecorated.setupCallReturn( result ); - - ExpectedCall call = new ExpectedCall( methodName, new Constraint[0], mockDecorated ); - - call.call(mock, methodName, arguments); - - mockDecorated.verifyExpectations(); + public void testDescription() { + AssertMo.assertIncludes( "should contain decorated's description", + decoratedDescription, call.getDescription() ); + AssertMo.assertIncludes( "should say that decorated call is mandatory", + "mandatory", call.getDescription() ); } - public void testUncalledCallDoesNotVerify() { - ExpectedCall call = new ExpectedCall(methodName, C.args(), new ReturnStub("ignored") ); - + public void testDoesNotVerifyIfNotCalled() { try { call.verify(); } catch( AssertionFailedError ex ) { - AssertMo.assertIncludes("should include call name", methodName, ex.getMessage() ); + AssertMo.assertIncludes( "should include description of expected call", + decoratedDescription, ex.getMessage() ); return; } - fail("expected verify to fail"); - } - - public void testSuccessfulCallVerifies() throws Throwable { - ExpectedCall call = new ExpectedCall("methodName", C.args(), new ReturnStub("ignored") ); - - call.call( mock, "methodName", new Object[0] ); - call.verify(); - } - - public void testMultipleCallsSucceed() throws Throwable { - ExpectedCall call = new ExpectedCall(methodName, C.args(), new ReturnStub("ignored") ); - call.call( mock, methodName, new Object[0] ); - call.call( mock, methodName, new Object[0] ); + fail( "verify did not fail when expected call not called"); } - public void testArgumentsCheckedAgainstConstraints() throws Throwable { - String[] args = { "arg1", "arg2", "arg3" }; - MockConstraint[] constraints = { - new MockConstraint( "constraint1", args[0], true ), - new MockConstraint( "constraint2", args[1], true ), - new MockConstraint( "constraint3", args[2], true ) - }; - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); + public void testVerifiesIfCalled() throws Throwable { + mockDecorated.setupCallReturn( result ); + mockDecorated.setExpectedVerifyCalls(1); - call.call( mock, methodName, args ); + call.call( ignoredMock, methodName, args ); + call.verify(); - for( int i = 0; i < constraints.length; i++ ) { - constraints[i].verify(); - } + mockDecorated.verifyExpectations(); } - public void testWrongNumberOfArguments() throws Throwable { - String[] args = { "arg1", "arg2" }; - MockConstraint[] constraints = { - new MockConstraint( "constraint1", args[0], true ), - new MockConstraint( "constraint2", args[1], true ), - new MockConstraint( "constraint3", args[1], true ) - }; - - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); - - boolean passed = true; - try { - call.call( mock, methodName, args ); - passed = false; - } catch (AssertionFailedError ex) { - AssertMo.assertIncludes("Should show expected number of arguments", - Integer.toString(constraints.length), ex.getMessage()); - AssertMo.assertIncludes("Should show actual number of arguments", - Integer.toString(args.length), ex.getMessage()); - AssertMo.assertIncludes("Should include the method name", - methodName, ex.getMessage()); - } - - assertTrue("Should fail is call doesn't give an exception", passed); + public void testMatchesDelegated() throws Throwable { + mockDecorated.setExpectedMatches( methodName, args ); + mockDecorated.setupMatchesReturn(true); + assertTrue( "returns matches to be true", call.matches( methodName, args ) ); + mockDecorated.verifyExpectations(); } - public void testConstraintFailure() throws Throwable { - String[] args = { "argA", "argB", "argC" }; - MockConstraint[] constraints = { - new MockConstraint( "constraintA", args[0], true ), - new MockConstraint( "constraintB", args[1], false ), - new MockConstraint( "constraintC", args[2], true ) - }; - - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); - - boolean passed = true; - try { - call.call( mock, methodName, args ); - passed = false; - } catch (AssertionFailedError ex) { - AssertMo.assertIncludes("Should include the method name", - methodName, ex.getMessage()); - AssertMo.assertIncludes("Should show index of failed argument", - "1", ex.getMessage() ); - AssertMo.assertIncludes("Should show expected arguments", - AssertMo.join(constraints), ex.getMessage()); - AssertMo.assertIncludes("Should show actual arguments", - AssertMo.join(args), ex.getMessage()); - } + public void testCallArgumentsPassedThrough() throws Throwable { + mockDecorated.setExpectedCall(ignoredMock, methodName, args); + mockDecorated.setupCallReturn(result); - assertTrue("Should fail is call doesn't give an exception", passed); + call.call( ignoredMock, methodName, args ); + mockDecorated.verifyExpectations(); } - - public void testCallMatchesArguments() throws Throwable { - String[] args = { "arg1", "arg2", "arg3" }; - MockConstraint[] constraints = { - new MockConstraint( "constraint1", args[0], true ), - new MockConstraint( "constraint2", args[1], true ), - new MockConstraint( "constraint3", args[2], true ) - }; - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); + public void testDecoratedResultPassedThrough() throws Throwable { + mockDecorated.setupCallReturn(result); - assertTrue( "call matches", call.matches( methodName, args) ); - } - - public void testCallDoesNotMatchWhenWrongName() throws Throwable { - ExpectedCall call = new ExpectedCall( methodName, new Constraint[0], new ReturnStub("ignored") ); - assertFalse( "call does not match", call.matches( "anotherName", new Object[0]) ); + assertSame( "should return decorated's result", + result, call.call( ignoredMock, methodName, args ) ); + mockDecorated.verifyExpectations(); } - - public void testCallDoesNotMatchWhenWrongNumberOfArguments() throws Throwable { - String[] args = { "arg1", "arg2" }; - MockConstraint[] constraints = { - new MockConstraint( "constraint1", args[0], true ), - new MockConstraint( "constraint2", args[1], true ), - new MockConstraint( "constraint3", args[1], true ) - }; - - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); - assertFalse( "call does not match", call.matches( methodName, args) ); - } - public void testCallDoesNotMatchWhenConstraintIsViolated() throws Throwable { - String[] args = { "argA", "argB", "argC" }; - MockConstraint[] constraints = { - new MockConstraint( "constraintA", args[0], true ), - new MockConstraint( "constraintB", args[1], false ), - new MockConstraint( "constraintC", args[2], true ) - }; + public void testDecoratedExceptionPassedThrough() throws Throwable { + final Throwable exception = new DummyThrowable(); - ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); - - assertFalse( "call does not match", call.matches( methodName, args) ); - } - - public void testMatchesAfterCalls() throws Throwable { - ExpectedCall call = new ExpectedCall(methodName, C.args(), new ReturnStub("ignored") ); + mockDecorated.setupCallThrow(exception); - call.call( mock, methodName, new Object[0] ); - assertTrue( "matches after first call", call.matches( methodName, new Object[0]) ); - call.call( mock, methodName, new Object[0] ); - assertTrue( "matches after further calls", call.matches( methodName, new Object[0]) ); + try { + call.call( ignoredMock, methodName, args ); + fail("expected decorated's throwable to be thrown"); + } + catch( DummyThrowable ex ) { + // expected + } + + mockDecorated.verifyExpectations(); } } Index: MockTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockTest.java,v retrieving revision 1.8.2.2 retrieving revision 1.8.2.3 diff -u -r1.8.2.2 -r1.8.2.3 --- MockTest.java 7 Apr 2003 14:26:57 -0000 1.8.2.2 +++ MockTest.java 7 Apr 2003 17:38:46 -0000 1.8.2.3 @@ -55,7 +55,7 @@ mockMethod.setExpectedCall( aMock, methodName, expectedArgs ); mockMethod.setupCallReturn( "result ignored" ); - aMock.expect( mockMethod ); + aMock.add( mockMethod ); proxy.aMethod( expectedArgs[0], expectedArgs[1] ); @@ -72,7 +72,7 @@ mockMethod.setExpectedCall( aMock, methodName, expectedArgs ); mockMethod.setupCallReturn( "result ignored" ); - aMock.expect( mockMethod ); + aMock.add( mockMethod ); proxy.aMethodWithNoArguments(); @@ -82,7 +82,7 @@ public void testCallMockerResultReturnedByProxy() throws Throwable { final String result = "result"; - aMock.expect( new ReturnStub(result) ); + aMock.add( new ReturnStub(result) ); assertSame( "result is returned by mock", result, proxy.aMethod( "hello", "world" ) ); @@ -91,7 +91,7 @@ public void testCallMockerThrowableThrownThroughProxy() throws Throwable { final Throwable throwable = new DummyThrowable(); - aMock.expect( new ThrowStub(throwable) ); + aMock.add( new ThrowStub(throwable) ); try { proxy.aMethod( "hello", "world" ); @@ -106,7 +106,7 @@ final String originalMessage = "original message"; final AssertionFailedError error = new AssertionFailedError(originalMessage); - aMock.expect( new ThrowStub(error) ); + aMock.add( new ThrowStub(error) ); try { proxy.aMethod( "hello", "world" ); Index: CallSetTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/Attic/CallSetTest.java,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- CallSetTest.java 7 Apr 2003 14:27:01 -0000 1.1.2.2 +++ CallSetTest.java 7 Apr 2003 17:38:49 -0000 1.1.2.3 @@ -3,6 +3,7 @@ */ package test.mockobjects.dynamic; +import com.mockobjects.constraint.*; import com.mockobjects.dynamic.*; import com.mockobjects.util.*; @@ -12,13 +13,21 @@ * @author dev */ public class CallSetTest extends TestCase { - private CallSet callSet = new CallSet(); + private MockCallFactory mockCallFactory = new MockCallFactory(); + private CallSet callSet = new CallSet(mockCallFactory); + private MockCallMocker mockReturnStub = new MockCallMocker(); + private MockCallMocker mockCallMatch = new MockCallMocker(); + private MockCallMocker mockExpectedCall = new MockCallMocker(); + private MockCallMocker mockThrowStub = new MockCallMocker(); + private Mock unusedMock = null; private MockCallMocker methodA = new MockCallMocker(); private MockCallMocker methodB = new MockCallMocker(); final String methodAName = "methodA"; final String[] methodAArgs = new String[] { "a1", "a2" }; + final Constraint[] methodAConstraints = C.args( C.eq("a1"), C.eq("a2") ); final String methodAResult = "resultA"; + final Throwable methodAException = new DummyThrowable("Configured test throwable"); final String methodBName = "methodB"; final String[] methodBArgs = new String[] { "b1", "b2" }; @@ -36,7 +45,7 @@ public void testVerifyFailsIfContainedElementDoesNotVerify() throws Exception { methodA.setExpectedVerifyCalls(1); methodA.setupVerifyThrow( new AssertionFailedError("verify failed") ); - callSet.expect( methodA ); + callSet.add( methodA ); try { callSet.verify(); @@ -51,14 +60,94 @@ methodA.setExpectedVerifyCalls(1); methodB.setExpectedVerifyCalls(1); - callSet.expect( methodA ); - callSet.expect( methodB ); + callSet.add( methodA ); + callSet.add( methodB ); callSet.verify(); methodA.verifyExpectations(); methodB.verifyExpectations(); } + public void testExpectAndReturn() throws Throwable{ + mockExpectedCall.setupMatchesReturn(true); + mockExpectedCall.setupCallReturn(methodAResult); + + mockCallFactory.addExpectedCreateReturnStub( methodAResult ); + mockCallFactory.setupCreateReturnStub( mockReturnStub ); + mockCallFactory.addExpectedCreateCallMatch( methodAName, methodAConstraints, mockReturnStub ); + mockCallFactory.setupCreateCallMatch( mockCallMatch ); + mockCallFactory.addExpectedCreateExpectedCall( mockCallMatch ); + mockCallFactory.setupCreateExpectedCall( mockExpectedCall ); + + callSet.expectAndReturn( methodAName, methodAConstraints, methodAResult ); + + assertSame( "should be result of calling expected call", + methodAResult, callSet.call( unusedMock, methodAName, methodAArgs ) ); + + Verifier.verifyObject(this); + } + + public void testPermitAndReturn() throws Throwable{ + mockCallMatch.setupMatchesReturn(true); + mockCallMatch.setupCallReturn(methodAResult); + + mockCallFactory.addExpectedCreateReturnStub( methodAResult ); + mockCallFactory.setupCreateReturnStub( mockReturnStub ); + mockCallFactory.addExpectedCreateCallMatch( methodAName, methodAConstraints, mockReturnStub ); + mockCallFactory.setupCreateCallMatch( mockCallMatch ); + + callSet.permitAndReturn( methodAName, methodAConstraints, methodAResult ); + + assertSame( "should be result of calling expected call", + methodAResult, callSet.call( unusedMock, methodAName, methodAArgs ) ); + + Verifier.verifyObject(this); + } + + public void testExpectAndThrow() throws Throwable{ + mockExpectedCall.setupMatchesReturn(true); + mockExpectedCall.setupCallThrow(methodAException); + + mockCallFactory.addExpectedCreateThrowStub(methodAException); + mockCallFactory.setupCreateThrowStub( mockThrowStub ); + mockCallFactory.addExpectedCreateCallMatch( methodAName, methodAConstraints, mockThrowStub ); + mockCallFactory.setupCreateCallMatch( mockCallMatch ); + mockCallFactory.addExpectedCreateExpectedCall( mockCallMatch ); + mockCallFactory.setupCreateExpectedCall( mockExpectedCall ); + + callSet.expectAndThrow( methodAName, methodAConstraints, methodAException ); + + try { + callSet.call( unusedMock, methodAName, methodAArgs ); + } catch (DummyThrowable ex) { + Verifier.verifyObject(this); + return; + } + + fail("Should have thrown configured exception"); + } + + public void testPermitAndThrow() throws Throwable{ + mockCallMatch.setupMatchesReturn(true); + mockCallMatch.setupCallThrow(methodAException); + + mockCallFactory.addExpectedCreateThrowStub(methodAException); + mockCallFactory.setupCreateThrowStub( mockThrowStub ); + mockCallFactory.addExpectedCreateCallMatch( methodAName, methodAConstraints, mockThrowStub ); + mockCallFactory.setupCreateCallMatch( mockCallMatch ); + + callSet.permitAndThrow( methodAName, methodAConstraints, methodAException ); + + try { + callSet.call( unusedMock, methodAName, methodAArgs ); + } catch (DummyThrowable ex) { + Verifier.verifyObject(this); + return; + } + + fail("Should have thrown configured exception"); + } + public void testCallPassedToContainedElements() throws Throwable { methodA.setExpectedMatches( methodAName, methodAArgs ); methodA.setupMatchesReturn(true); @@ -67,8 +156,8 @@ methodB.setExpectedCallCount( 0 ); - callSet.expect( methodA ); - callSet.expect( methodB ); + callSet.add( methodA ); + callSet.add( methodB ); assertSame( "expected result from method A", methodAResult, callSet.call( unusedMock, methodAName, methodAArgs ) ); @@ -87,8 +176,8 @@ methodB.setExpectedMatches( methodBName, methodBArgs ); methodB.setupMatchesReturn(true); - callSet.expect( methodA ); - callSet.expect( methodB ); + callSet.add( methodA ); + callSet.add( methodB ); assertSame( "expected result from method B", methodBResult, callSet.call( unusedMock, methodBName, methodBArgs ) ); @@ -104,14 +193,14 @@ methodA.setExpectedMatches( methodCName, methodCArgs ); methodA.setupMatchesReturn(false); methodA.setExpectedCallCount( 0 ); - methodA.setupGetDescription("***methodA-toString****"); + methodA.setupGetDescription("***methodA-description****"); methodB.setExpectedCall( unusedMock, methodCName, methodCArgs ); methodB.setupMatchesReturn(false); methodB.setExpectedCallCount( 0 ); - methodB.setupGetDescription("***methodB-toString****"); + methodB.setupGetDescription("***methodB-description****"); - callSet.expect( methodA ); - callSet.expect( methodB ); + callSet.add( methodA ); + callSet.add( methodB ); try { callSet.call( unusedMock, methodCName, methodCArgs ); @@ -124,9 +213,9 @@ methodCArgs[1], ex.getMessage() ); AssertMo.assertIncludes( "shows set contents (A)", - methodA.toString(), ex.getMessage() ); + methodA.getDescription(), ex.getMessage() ); AssertMo.assertIncludes( "shows set contents (B)", - methodB.toString(), ex.getMessage() ); + methodB.getDescription(), ex.getMessage() ); return; } |
From: Tim M. <ma...@us...> - 2003-04-07 17:39:05
|
Update of /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv3129/src/examples/com/mockobjects/examples/dynamic Modified Files: Tag: DynamicMockExperiment SimpleServletTest.java SimpleServlet.java Log Message: Basic implementation - adds syntactic sugar for permitAndReturn, expectAndReturn etc. - separated CallMatch from ExpectedCall Still needs Sequence, SingleCall, CallCount support Index: SimpleServletTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/dynamic/Attic/SimpleServletTest.java,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- SimpleServletTest.java 7 Apr 2003 14:27:01 -0000 1.1.2.2 +++ SimpleServletTest.java 7 Apr 2003 17:38:25 -0000 1.1.2.3 @@ -55,26 +55,15 @@ Mock mockHttpServletResponse = new Mock(HttpServletResponse.class, "response"); Mock mockHttpServletRequest = new Mock(HttpServletRequest.class); - mockHttpServletRequest.expect( new ExpectedCall( "getParameter", C.args(C.eq("subject")), new ReturnStub("Mail Subject") )); - mockHttpServletRequest.expect( new ExpectedCall( "getParameter", C.args(C.eq("body")), new ReturnStub("Mail Body") )); - //mockHttpServletRequest.permit( new ArgumentChecker( "getParameter", C.args(C.eq("browser-identifier"))), new ReturnStub("MSIE 5.0") ); - -// mockHttpServletRequest.expect( "getParameter", new Object[] {"subject"}, C.returnValue("Mail Subject") ); -// mockHttpServletRequest.expectAndReturn( "getParameter", new Object[] {"subject"}, "Mail Subject"); -// mockHttpServletRequest.expectAndThrow( "getParameter", new Object[] {"subject"}, C.throwException(new Exception()) ); - - // mockHttpServletRequest.expect( "getParameter", "subject"); //.returnValue("mail subject"); - - //mockHttpServletRequest.expect( "getParameter", "body").returnValue("mail body"); - //mockHttpServletRequest.expect( "getParameter", "recipients").returnValue( new String[] { "person1", "person2" } ); - + mockHttpServletRequest.permitAndReturn( "getParameter", C.args(C.eq("browser-identifier")), "MSIE-5.0" ); + mockHttpServletRequest.expectAndReturn( "getParameter", C.args(C.eq("subject")), "Mail Subject" ); + mockHttpServletRequest.expectAndReturn( "getParameter", C.args(C.eq("body")), "Mail Body" ); final StringWriter contentWriter = new StringWriter(); -// mockHttpServletResponse.expect( new CallMocker[] { -// new SingleCall( "setContentType", C.args(C.eq("text/html")) ), -// new SingleCall( "getWriter", C.args() ).returnValue( contentWriter ), -// } ); +// CallSequence seq = mockHttpServletResponse.expectSequence(); +// seq.expectVoid( "setContentType", C.args(C.eq("text/html")) ); +// seq.expectAndReturn( "getWriter", C.args(), contentWriter ); SimpleServlet aServlet = new SimpleServlet(); aServlet.doGet((HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy()); Index: SimpleServlet.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/dynamic/Attic/SimpleServlet.java,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- SimpleServlet.java 7 Apr 2003 14:27:02 -0000 1.1.2.2 +++ SimpleServlet.java 7 Apr 2003 17:38:28 -0000 1.1.2.3 @@ -18,7 +18,10 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //super.doGet(arg0, arg1); + String body = request.getParameter("body"); String subject = request.getParameter("subject"); + String browser = request.getParameter("browser-identifier"); + String pet = request.getParameter("favourite-pet"); } } |
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv3129/src/core/com/mockobjects/dynamic Modified Files: Tag: DynamicMockExperiment ExpectedCall.java CallSet.java Added Files: Tag: DynamicMockExperiment DefaultCallFactory.java CallFactory.java CallMatch.java Log Message: Basic implementation - adds syntactic sugar for permitAndReturn, expectAndReturn etc. - separated CallMatch from ExpectedCall Still needs Sequence, SingleCall, CallCount support --- NEW FILE: DefaultCallFactory.java --- /* * Created on 07-Apr-2003 */ package com.mockobjects.dynamic; import com.mockobjects.constraint.*; /** * @author dev */ public class DefaultCallFactory implements CallFactory { public CallMocker createReturnStub(Object result) { return new ReturnStub(result); } public CallMocker createThrowStub( Throwable exception ) { return new ThrowStub(exception); } public CallMocker createExpectedCall(CallMocker call) { return new ExpectedCall(call); } public CallMocker createCallMatch(String methodName, Constraint[] constraints, CallMocker call) { return new CallMatch( methodName, constraints, call ); } } --- NEW FILE: CallFactory.java --- /* * Created on 07-Apr-2003 */ package com.mockobjects.dynamic; import com.mockobjects.constraint.*; /** * @author dev */ public interface CallFactory { CallMocker createReturnStub( Object result ); CallMocker createThrowStub( Throwable throwable ); CallMocker createExpectedCall( CallMocker call ); CallMocker createCallMatch( String methodName, Constraint[] constraints, CallMocker call ); } --- NEW FILE: CallMatch.java --- /* * Created on 04-Apr-2003 */ package com.mockobjects.dynamic; import junit.framework.*; import com.mockobjects.constraint.*; import com.mockobjects.util.*; public class CallMatch extends Assert implements CallMocker { private String methodName; private Constraint[] constraints; private CallMocker decorated; public CallMatch( String methodName, Constraint[] constraints, CallMocker decorated ) { this.methodName = methodName; this.constraints = (Constraint[])constraints.clone(); this.decorated = decorated; } public Object call( Mock mock, String methodName, Object[] args ) throws Throwable { assertEquals( methodName + " received the wrong number of arguments", constraints.length, args.length ); for( int i = 0; i < args.length; i++ ) { if( !constraints[i].eval(args[i]) ) { throw new AssertionFailedError( AssertMo.expectedErrorMessage( methodName + " received incorrect argument " + i, AssertMo.join(constraints), AssertMo.join(args) ) ); } } return decorated.call( mock, methodName, args ); } public void verify() { decorated.verify(); } public boolean matches(String methodName, Object[] args) { if( !this.methodName.equals(methodName) ) return false; if( args.length != constraints.length ) return false; for (int i = 0; i < args.length; i++) { if( !constraints[i].eval(args[i]) ) return false; } return decorated.matches( methodName, args ); } public String getDescription() { return AssertMo.methodToString(methodName, constraints); } } Index: ExpectedCall.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Attic/ExpectedCall.java,v retrieving revision 1.2.2.1 retrieving revision 1.2.2.2 diff -u -r1.2.2.1 -r1.2.2.2 --- ExpectedCall.java 7 Apr 2003 14:26:52 -0000 1.2.2.1 +++ ExpectedCall.java 7 Apr 2003 17:38:22 -0000 1.2.2.2 @@ -1,64 +1,39 @@ /* - * Created on 04-Apr-2003 - * + * Created on 07-Apr-2003 */ package com.mockobjects.dynamic; import junit.framework.*; -import com.mockobjects.constraint.*; -import com.mockobjects.util.*; - -public class ExpectedCall extends Assert implements CallMocker -{ - private String methodName; - private Constraint[] constraints; - private boolean wasCalled = false; +/** + * @author dev + */ +public class ExpectedCall implements CallMocker { private CallMocker decorated; + private boolean wasCalled = false; - public ExpectedCall( String methodName, Constraint[] constraints, CallMocker decorated ) { - this.methodName = methodName; - this.constraints = (Constraint[])constraints.clone(); + public ExpectedCall( CallMocker decorated ) { this.decorated = decorated; } - - public Object call( Mock mock, String methodName, Object[] args ) - throws Throwable - { - assertEquals( methodName + " received the wrong number of arguments", - constraints.length, args.length ); - - for( int i = 0; i < args.length; i++ ) { - if( !constraints[i].eval(args[i]) ) { - throw new AssertionFailedError( - AssertMo.expectedErrorMessage( methodName + " received incorrect argument " + i, - AssertMo.join(constraints), - AssertMo.join(args) ) ); - } - } - + + public String getDescription() { + return decorated.getDescription() + " [mandatory]"; + } + + public Object call(Mock mock, String methodName, Object[] args) throws Throwable { wasCalled = true; - return decorated.call( mock, methodName, args ); } - public void verify() { - if( !wasCalled ) { - throw new AssertionFailedError( methodName + " was not called" ); - } + public boolean matches(String methodName, Object[] args) { + return decorated.matches( methodName, args ); } - public boolean matches(String methodName, Object[] args) { - if( !this.methodName.equals(methodName) ) return false; - if( args.length != constraints.length ) return false; - - for (int i = 0; i < args.length; i++) { - if( !constraints[i].eval(args[i]) ) return false; + public void verify() { + if( !wasCalled ) { + throw new AssertionFailedError( decorated.getDescription() + " was expected but not called" ); } - return true; - } - - public String getDescription() { - return AssertMo.methodToString(methodName, constraints); + + decorated.verify(); } } Index: CallSet.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Attic/CallSet.java,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- CallSet.java 7 Apr 2003 14:26:53 -0000 1.1.2.2 +++ CallSet.java 7 Apr 2003 17:38:23 -0000 1.1.2.3 @@ -5,6 +5,7 @@ import java.util.*; +import com.mockobjects.constraint.*; import com.mockobjects.util.*; import junit.framework.*; @@ -13,11 +14,16 @@ * @author dev */ public class CallSet implements CallMocker { - private List expectedCalls = new ArrayList(); + private CallFactory callFactory; + - public CallSet() { + this( new DefaultCallFactory() ); + } + + public CallSet( CallFactory callFactory ) { + this.callFactory = callFactory; } public Object call(Mock mock, String methodName, Object[] args) throws Throwable { @@ -45,7 +51,7 @@ buf.append( "one of:\n" ); for( Iterator i = expectedCalls.iterator(); i.hasNext(); ) { - buf.append( ((CallMocker)i.next()).toString() ); + buf.append( ((CallMocker)i.next()).getDescription() ); buf.append( "\n" ); } @@ -61,12 +67,29 @@ } - public void expect(CallMocker call) { + public void add(CallMocker call) { expectedCalls.add(call); } - public boolean matches(String methodName, Object[] args) { - return false; + public void permitAndReturn( String methodName, Constraint[] args, Object result ) { + add( callFactory.createCallMatch( methodName, args, + callFactory.createReturnStub(result) ) ); + } + + public void permitAndThrow( String methodName, Constraint[] args, Throwable throwable ) { + add( callFactory.createCallMatch( methodName, args, + callFactory.createThrowStub(throwable) ) ); + } + + public void expectAndReturn( String methodName, Constraint[] args, Object result ) { + add( callFactory.createExpectedCall( callFactory.createCallMatch( methodName, args, callFactory.createReturnStub(result)))); } + public void expectAndThrow( String methodName, Constraint[] args, Throwable exception ) { + add( callFactory.createExpectedCall( callFactory.createCallMatch( methodName, args, callFactory.createThrowStub(exception)))); + } + + public boolean matches(String methodName, Object[] args) { + throw new Error("not implemented"); + } } |
From: Tim M. <ma...@us...> - 2003-04-07 17:38:56
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/constraint In directory sc8-pr-cvs1:/tmp/cvs-serv3129/src/core/com/mockobjects/constraint Modified Files: Tag: DynamicMockExperiment IsEqual.java Log Message: Basic implementation - adds syntactic sugar for permitAndReturn, expectAndReturn etc. - separated CallMatch from ExpectedCall Still needs Sequence, SingleCall, CallCount support Index: IsEqual.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/constraint/IsEqual.java,v retrieving revision 1.2 retrieving revision 1.2.2.1 diff -u -r1.2 -r1.2.2.1 --- IsEqual.java 24 Nov 2002 11:20:19 -0000 1.2 +++ IsEqual.java 7 Apr 2003 17:38:50 -0000 1.2.2.1 @@ -23,6 +23,6 @@ } public String toString() { - return "a value equal to <" + _object + ">"; + return _object.toString(); } } |
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv10938/src/core/test/mockobjects/dynamic Modified Files: Tag: DynamicMockExperiment MockTest.java CallSetTest.java Added Files: Tag: DynamicMockExperiment MockCallMocker.java DummyThrowable.java MockConstraint.java DummyInterface.java ExpectedCallTest.java StubTest.java Removed Files: Tag: DynamicMockExperiment TestInterface.java TestThrowable.java SingleCallTest.java TestConstraint.java Log Message: Refactored class hierarchy: - call actions can be stubbed - stubs can be decorated with expectations - calls can be grouped in a CallSet (set/bag) - a mock object inherits from CallSet --- NEW FILE: MockCallMocker.java --- package test.mockobjects.dynamic; import junit.framework.*; import com.mockobjects.*; import com.mockobjects.dynamic.CallMocker; import com.mockobjects.dynamic.Mock; import com.mockobjects.util.*; public class MockCallMocker implements CallMocker { private ExpectationCounter callCount = new ExpectationCounter("call.count"); private ExpectationValue callMock = new ExpectationValue("call.mock"); private ExpectationValue callMethodName = new ExpectationValue("call.methodName"); private ExpectationList callArgs = new ExpectationList("call.args"); private ReturnValue callResult = new ReturnValue("call.return"); private Throwable callThrow = null; private ExpectationValue matchesMethodName = new ExpectationValue("matches.methodName"); private ExpectationList matchesArgs = new ExpectationList("matches.args"); private ReturnValue matchesResult = new ReturnValue("matches.return"); private ExpectationCounter verifyCount = new ExpectationCounter("verify.count"); private AssertionFailedError verifyError = null; private ReturnValue toStringResult = new ReturnValue("toString.return"); public void setExpectedCallCount( int count ) { callCount.setExpected(count); } public void setExpectedCall( Mock mock, String methodName, Object[] args ) { callMock.setExpected(mock); callMethodName.setExpected(methodName); callArgs.addExpectedMany(args); } public void setupCallReturn( Object result ) { callResult.setValue(result); } public void setupCallThrow( Throwable thrown ) { callThrow = thrown; } public Object call(Mock mock, String methodName, Object[] args) throws Throwable { callMock.setActual(mock); callMethodName.setActual(methodName); callArgs.addActualMany(args); callCount.inc(); if( callThrow != null ) { throw callThrow; } else { return callResult.getValue(); } } public void setExpectedMatches( String methodName, Object[] args ) { matchesMethodName.setExpected(methodName); matchesArgs.addExpectedMany(args); } public void setupMatchesReturn( boolean result ) { matchesResult.setValue(result); } public boolean matches(String methodName, Object[] args) { matchesMethodName.setActual(methodName); matchesArgs.addActualMany(args); return matchesResult.getBooleanValue(); } public void setExpectedVerifyCalls( int count ) { verifyCount.setExpected(count); } public void setupVerifyThrow( AssertionFailedError err ) { verifyError = err; } /** @deprecated to avoid calling verify instead of verifyExpectations */ public void verify() { verifyCount.inc(); if( verifyError != null ) throw verifyError; } /** We have to rename 'verify' because we want to mock the behaviour of the * verify method itself. */ public void verifyExpectations() { Verifier.verifyObject(this); } public void setupGetDescription( String result ) { toStringResult.setValue(result); } public String getDescription() { return (String)toStringResult.getValue(); } } --- NEW FILE: DummyThrowable.java --- /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; /** * @author dev */ public class DummyThrowable extends Throwable { public DummyThrowable() { super(); } public DummyThrowable(String message) { super(message); } public DummyThrowable(String message, Throwable cause) { super(message, cause); } public DummyThrowable(Throwable cause) { super(cause); } } --- NEW FILE: MockConstraint.java --- /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; import junit.framework.*; import com.mockobjects.*; import com.mockobjects.constraint.*; /** * @author dev */ public class MockConstraint extends Assert implements Constraint, Verifiable { private String description; private Object expectedArg; private boolean result; private boolean wasChecked = false; public MockConstraint( String description, Object expectedArg, boolean result ) { this.description = description; this.expectedArg = expectedArg; this.result = result; } public String toString() { return description; } public boolean eval( Object o ) { assertSame( "should be expected argument", expectedArg, o ); wasChecked = true; return result; } public void verify() { assertTrue( description + " should have been checked", wasChecked ); } } --- NEW FILE: DummyInterface.java --- /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; /** * @author dev */ public interface DummyInterface { public String aMethod( String arg1, String arg2 ) throws Throwable; public void aMethodWithNoArguments(); } --- NEW FILE: ExpectedCallTest.java --- /* * Created on 04-Apr-2003 * * To change this generated comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ package test.mockobjects.dynamic; import com.mockobjects.constraint.*; import com.mockobjects.dynamic.*; import com.mockobjects.util.*; import junit.framework.*; public class ExpectedCallTest extends TestCase { final String methodName = "methodName"; Mock mock = new Mock(DummyInterface.class,"mock"); ExpectedCall call; public ExpectedCallTest(String name) { super(name); } public void testCallArgumentsArePropagatedToDecorated() throws Throwable { final Object[] arguments = new Object[0]; final String result = "result"; MockCallMocker mockDecorated = new MockCallMocker(); mockDecorated.setExpectedCall( mock, methodName, arguments ); mockDecorated.setupCallReturn( result ); ExpectedCall call = new ExpectedCall( methodName, new Constraint[0], mockDecorated ); call.call(mock, methodName, arguments); mockDecorated.verifyExpectations(); } public void testUncalledCallDoesNotVerify() { ExpectedCall call = new ExpectedCall(methodName, C.args(), new ReturnStub("ignored") ); try { call.verify(); } catch( AssertionFailedError ex ) { AssertMo.assertIncludes("should include call name", methodName, ex.getMessage() ); return; } fail("expected verify to fail"); } public void testSuccessfulCallVerifies() throws Throwable { ExpectedCall call = new ExpectedCall("methodName", C.args(), new ReturnStub("ignored") ); call.call( mock, "methodName", new Object[0] ); call.verify(); } public void testMultipleCallsSucceed() throws Throwable { ExpectedCall call = new ExpectedCall(methodName, C.args(), new ReturnStub("ignored") ); call.call( mock, methodName, new Object[0] ); call.call( mock, methodName, new Object[0] ); } public void testArgumentsCheckedAgainstConstraints() throws Throwable { String[] args = { "arg1", "arg2", "arg3" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[2], true ) }; ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); call.call( mock, methodName, args ); for( int i = 0; i < constraints.length; i++ ) { constraints[i].verify(); } } public void testWrongNumberOfArguments() throws Throwable { String[] args = { "arg1", "arg2" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[1], true ) }; ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); boolean passed = true; try { call.call( mock, methodName, args ); passed = false; } catch (AssertionFailedError ex) { AssertMo.assertIncludes("Should show expected number of arguments", Integer.toString(constraints.length), ex.getMessage()); AssertMo.assertIncludes("Should show actual number of arguments", Integer.toString(args.length), ex.getMessage()); AssertMo.assertIncludes("Should include the method name", methodName, ex.getMessage()); } assertTrue("Should fail is call doesn't give an exception", passed); } public void testConstraintFailure() throws Throwable { String[] args = { "argA", "argB", "argC" }; MockConstraint[] constraints = { new MockConstraint( "constraintA", args[0], true ), new MockConstraint( "constraintB", args[1], false ), new MockConstraint( "constraintC", args[2], true ) }; ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); boolean passed = true; try { call.call( mock, methodName, args ); passed = false; } catch (AssertionFailedError ex) { AssertMo.assertIncludes("Should include the method name", methodName, ex.getMessage()); AssertMo.assertIncludes("Should show index of failed argument", "1", ex.getMessage() ); AssertMo.assertIncludes("Should show expected arguments", AssertMo.join(constraints), ex.getMessage()); AssertMo.assertIncludes("Should show actual arguments", AssertMo.join(args), ex.getMessage()); } assertTrue("Should fail is call doesn't give an exception", passed); } public void testCallMatchesArguments() throws Throwable { String[] args = { "arg1", "arg2", "arg3" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[2], true ) }; ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); assertTrue( "call matches", call.matches( methodName, args) ); } public void testCallDoesNotMatchWhenWrongName() throws Throwable { ExpectedCall call = new ExpectedCall( methodName, new Constraint[0], new ReturnStub("ignored") ); assertFalse( "call does not match", call.matches( "anotherName", new Object[0]) ); } public void testCallDoesNotMatchWhenWrongNumberOfArguments() throws Throwable { String[] args = { "arg1", "arg2" }; MockConstraint[] constraints = { new MockConstraint( "constraint1", args[0], true ), new MockConstraint( "constraint2", args[1], true ), new MockConstraint( "constraint3", args[1], true ) }; ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); assertFalse( "call does not match", call.matches( methodName, args) ); } public void testCallDoesNotMatchWhenConstraintIsViolated() throws Throwable { String[] args = { "argA", "argB", "argC" }; MockConstraint[] constraints = { new MockConstraint( "constraintA", args[0], true ), new MockConstraint( "constraintB", args[1], false ), new MockConstraint( "constraintC", args[2], true ) }; ExpectedCall call = new ExpectedCall( methodName, (Constraint[])constraints, new ReturnStub("ignored") ); assertFalse( "call does not match", call.matches( methodName, args) ); } public void testMatchesAfterCalls() throws Throwable { ExpectedCall call = new ExpectedCall(methodName, C.args(), new ReturnStub("ignored") ); call.call( mock, methodName, new Object[0] ); assertTrue( "matches after first call", call.matches( methodName, new Object[0]) ); call.call( mock, methodName, new Object[0] ); assertTrue( "matches after further calls", call.matches( methodName, new Object[0]) ); } } --- NEW FILE: StubTest.java --- /* * Created on 07-Apr-2003 */ package test.mockobjects.dynamic; import com.mockobjects.dynamic.*; import junit.framework.*; /** * @author dev */ public class StubTest extends TestCase { public StubTest(String name) { super(name); } public void testReturnStub() throws Throwable { final String result = "result"; ReturnStub stub = new ReturnStub(result); String ignoredMethodName = "methodName"; Mock ignoredMock = null; Object[] ignoredArgs = new Object[0]; assertSame( "Should be the same result object", result, stub.call( ignoredMock, ignoredMethodName, ignoredArgs ) ); } public void testThrowStub() { final Throwable throwable = new DummyThrowable(); ThrowStub stub = new ThrowStub(throwable); String ignoredMethodName = "methodName"; Mock ignoredMock = null; Object[] ignoredArgs = new Object[0]; try { stub.call( ignoredMock, ignoredMethodName, ignoredArgs ); } catch( Throwable t ) { assertSame( "Should be the same throwable", throwable, t ); } } } Index: MockTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockTest.java,v retrieving revision 1.8.2.1 retrieving revision 1.8.2.2 diff -u -r1.8.2.1 -r1.8.2.2 --- MockTest.java 4 Apr 2003 16:47:39 -0000 1.8.2.1 +++ MockTest.java 7 Apr 2003 14:26:57 -0000 1.8.2.2 @@ -1,8 +1,5 @@ /* * Created on 04-Apr-2003 - * - * To change this generated comment go to - * Window>Preferences>Java>Code Generation>Code and Comments */ package test.mockobjects.dynamic; @@ -13,14 +10,11 @@ /** * @author dev - * - * To change this generated comment go to - * Window>Preferences>Java>Code Generation>Code and Comments */ public class MockTest extends TestCase { private Mock aMock; - private TestInterface proxy; + private DummyInterface proxy; private static final String MOCK_NAME = "Test mock"; public MockTest(String name) { @@ -28,9 +22,9 @@ } public void setUp() { - aMock = new Mock( TestInterface.class, MOCK_NAME ); + aMock = new Mock( DummyInterface.class, MOCK_NAME ); try { - proxy = (TestInterface)aMock.proxy(); + proxy = (DummyInterface)aMock.proxy(); } catch( ClassCastException ex ) { fail("proxy is not of expected interface type"); @@ -42,120 +36,62 @@ } public void testMockNameFromClass() throws Exception { - assertEquals( "mock-String", Mock.mockNameFromClass(String.class) ); + assertEquals( "mockString", Mock.mockNameFromClass(String.class) ); } - public void testVerifyFailsIfExpectedCallNotCalled() throws Throwable { - aMock.expect( new CallMocker() { - public void verify() { throw new AssertionFailedError("example error"); } - public boolean matches(String methodName, Object[] args) { - return false; - } - public Object call(Mock mock, String methodName, Object[] args) { - return null; - } - } ); - - try { - aMock.verify(); - } - catch( AssertionFailedError ex ) { - AssertMo.assertIncludes( "mock name should be in exception message", - MOCK_NAME, ex.getMessage() ); - return; - } - fail("verify should have failed"); - } - - public void testVerifiesIfExpectedCallIsCalled() throws Throwable { - aMock.expect( new CallMocker() { - public void verify() {} - public boolean matches(String methodName, Object[] args) { - return false; - } - - public Object call(Mock mock, String methodName, Object[] args) { - return null; - } - } ); - - proxy.aMethod( "hello", "world" ); - aMock.verify(); + public void testMockToStringContainsName() { + AssertMo.assertIncludes( "result of toString() should include name", + MOCK_NAME, aMock.toString() ); } - + public void testCallMockerReceivesArgumentsFromProxy() throws Throwable { + final String methodName = "aMethod"; final String[] expectedArgs = { "hello", "world" }; final String[] receivedArgs = new String[2]; - aMock.expect( new CallMocker() { - public void verify() {} - public boolean matches(String methodName, Object[] args) { - return false; - } - - public Object call(Mock mock, String methodName, Object[] args) { - assertEquals( "should be expected number of args", expectedArgs.length, args.length ); - System.arraycopy( args, 0, receivedArgs, 0, args.length ); - return null; - } - } ); + MockCallMocker mockMethod = new MockCallMocker(); + mockMethod.setExpectedMatches( methodName, expectedArgs ); + mockMethod.setupMatchesReturn(true); + mockMethod.setExpectedCall( aMock, methodName, expectedArgs ); + mockMethod.setupCallReturn( "result ignored" ); + + aMock.expect( mockMethod ); proxy.aMethod( expectedArgs[0], expectedArgs[1] ); - for( int i = 0; i < expectedArgs.length; i++ ) { - assertSame( "arg " + (i+1), expectedArgs[i], receivedArgs[i] ); - } + + mockMethod.verifyExpectations(); } public void testCallMockerReceivesEmptyArrayWhenNoArguments() throws Exception { - aMock.expect( new CallMocker() { - public void verify() {} - public boolean matches(String methodName, Object[] args) { - return false; - } - public Object call( Mock mock, String methodName, Object[] args ) { - assertNotNull( "Arg array should not be null", args ); - assertEquals( "Arg array should have zero length", 0, args.length ); - return null; - } - } ); + final String methodName = "aMethodWithNoArguments"; + final Object[] expectedArgs = {}; + + MockCallMocker mockMethod = new MockCallMocker(); + mockMethod.setExpectedMatches( methodName, expectedArgs ); + mockMethod.setupMatchesReturn(true); + mockMethod.setExpectedCall( aMock, methodName, expectedArgs ); + mockMethod.setupCallReturn( "result ignored" ); + aMock.expect( mockMethod ); + proxy.aMethodWithNoArguments(); + + mockMethod.verifyExpectations(); } public void testCallMockerResultReturnedByProxy() throws Throwable { final String result = "result"; - aMock.expect( new CallMocker() { - public void verify() {} - - public Object call(Mock mock, String methodName, Object[] args) { - return result; - } - - public boolean matches(String methodName, Object[] args) { - return false; - } - } ); + aMock.expect( new ReturnStub(result) ); assertSame( "result is returned by mock", result, proxy.aMethod( "hello", "world" ) ); } public void testCallMockerThrowableThrownThroughProxy() throws Throwable { - final Throwable throwable = new TestThrowable(); + final Throwable throwable = new DummyThrowable(); - aMock.expect( new CallMocker() { - public void verify() {} - public boolean matches(String methodName, Object[] args) { - return false; - } - - public Object call(Mock mock, String methodName, Object[] args) - throws Throwable - { - throw throwable; - } - } ); + aMock.expect( new ThrowStub(throwable) ); try { proxy.aMethod( "hello", "world" ); @@ -170,18 +106,7 @@ final String originalMessage = "original message"; final AssertionFailedError error = new AssertionFailedError(originalMessage); - aMock.expect( new CallMocker() { - public void verify() {} - public boolean matches(String methodName, Object[] args) { - return false; - } - - public Object call(Mock mock, String methodName, Object[] args) - throws Throwable - { - throw error; - } - } ); + aMock.expect( new ThrowStub(error) ); try { proxy.aMethod( "hello", "world" ); @@ -193,30 +118,4 @@ MOCK_NAME, err.getMessage() ); } } - - public void testMockDispatchesCallToMatchingCallMocker() throws Throwable { - final String[] expectedArgs = { "hello", "world" }; - final String[] receivedArgs = new String[2]; - - aMock.expect( new CallMocker() { - public void verify() {} - public boolean matches(String methodName, Object[] args) { - return false; - } - - public Object call(Mock mock, String methodName, Object[] args) { - assertEquals( "should be expected number of args", expectedArgs.length, args.length ); - System.arraycopy( args, 0, receivedArgs, 0, args.length ); - return null; - } - } ); - - proxy.aMethod( expectedArgs[0], expectedArgs[1] ); - - for( int i = 0; i < expectedArgs.length; i++ ) { - assertSame( "arg " + (i+1), expectedArgs[i], receivedArgs[i] ); - } - } - - } Index: CallSetTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/Attic/CallSetTest.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- CallSetTest.java 4 Apr 2003 16:47:40 -0000 1.1.2.1 +++ CallSetTest.java 7 Apr 2003 14:27:01 -0000 1.1.2.2 @@ -4,6 +4,7 @@ package test.mockobjects.dynamic; import com.mockobjects.dynamic.*; +import com.mockobjects.util.*; import junit.framework.*; @@ -13,39 +14,135 @@ public class CallSetTest extends TestCase { private CallSet callSet = new CallSet(); private Mock unusedMock = null; + private MockCallMocker methodA = new MockCallMocker(); + private MockCallMocker methodB = new MockCallMocker(); + final String methodAName = "methodA"; + final String[] methodAArgs = new String[] { "a1", "a2" }; + final String methodAResult = "resultA"; + + final String methodBName = "methodB"; + final String[] methodBArgs = new String[] { "b1", "b2" }; + final String methodBResult = "resultB"; + public CallSetTest(String name) { super(name); } - + public void testEmptySetVerifies() throws Exception { callSet.verify(); } - public void testSingleItemNotCalledFails() throws Exception { - callSet.add(new SingleCall("methodThatWontBeCalled",C.args())); + public void testVerifyFailsIfContainedElementDoesNotVerify() throws Exception { + methodA.setExpectedVerifyCalls(1); + methodA.setupVerifyThrow( new AssertionFailedError("verify failed") ); + callSet.expect( methodA ); try { callSet.verify(); } catch (AssertionFailedError ex) { + methodA.verifyExpectations(); return; } - - fail("Should have got a failure for a method that wasn't called"); + fail("Should have got a failure for contained element failing"); } - public void testSingleItemCalledSuccessfully() throws Throwable { - callSet.add(new SingleCall("methodThatWillBeCalled",C.args())); - callSet.call( unusedMock, "methodThatWillBeCalled", new Object[0] ); + public void testVerifiesIfAllContainedElementsVerify() throws Throwable { + methodA.setExpectedVerifyCalls(1); + methodB.setExpectedVerifyCalls(1); + + callSet.expect( methodA ); + callSet.expect( methodB ); callSet.verify(); + + methodA.verifyExpectations(); + methodB.verifyExpectations(); } - public void testTwoItemsCalledSameOrder() throws Throwable { - callSet.add(new SingleCall("methodA",C.args())); - callSet.add(new SingleCall("methodB",C.args())); - callSet.call( unusedMock, "methodA", new Object[0] ); - callSet.call( unusedMock, "methodB", new Object[0] ); - callSet.verify(); + public void testCallPassedToContainedElements() throws Throwable { + methodA.setExpectedMatches( methodAName, methodAArgs ); + methodA.setupMatchesReturn(true); + methodA.setExpectedCall( unusedMock, methodAName, methodAArgs ); + methodA.setupCallReturn(methodAResult); + + methodB.setExpectedCallCount( 0 ); + + callSet.expect( methodA ); + callSet.expect( methodB ); + + assertSame( "expected result from method A", + methodAResult, callSet.call( unusedMock, methodAName, methodAArgs ) ); + + methodA.verifyExpectations(); + methodB.verifyExpectations(); + } + + public void testCallPassedToContainedElementsOtherOrder() throws Throwable { + methodA.setExpectedMatches( methodBName, methodBArgs ); + methodA.setupMatchesReturn(false); + methodA.setExpectedCallCount( 0 ); + methodB.setExpectedCall( unusedMock, methodBName, methodBArgs ); + + methodB.setupCallReturn(methodBResult); + methodB.setExpectedMatches( methodBName, methodBArgs ); + methodB.setupMatchesReturn(true); + + callSet.expect( methodA ); + callSet.expect( methodB ); + + assertSame( "expected result from method B", + methodBResult, callSet.call( unusedMock, methodBName, methodBArgs ) ); + + methodA.verifyExpectations(); + methodB.verifyExpectations(); + } + + public void testFailureIfNoElementMatches() throws Throwable { + final String methodCName = "methodC"; + final String[] methodCArgs = { "c1", "c2" }; + + methodA.setExpectedMatches( methodCName, methodCArgs ); + methodA.setupMatchesReturn(false); + methodA.setExpectedCallCount( 0 ); + methodA.setupGetDescription("***methodA-toString****"); + methodB.setExpectedCall( unusedMock, methodCName, methodCArgs ); + methodB.setupMatchesReturn(false); + methodB.setExpectedCallCount( 0 ); + methodB.setupGetDescription("***methodB-toString****"); + + callSet.expect( methodA ); + callSet.expect( methodB ); + + try { + callSet.call( unusedMock, methodCName, methodCArgs ); + } catch (AssertionFailedError ex) { + AssertMo.assertIncludes( "method name is in error message", + "methodC", ex.getMessage() ); + AssertMo.assertIncludes( "argument is in error message (1)", + methodCArgs[0], ex.getMessage() ); + AssertMo.assertIncludes( "argument is in error message (2)", + methodCArgs[1], ex.getMessage() ); + + AssertMo.assertIncludes( "shows set contents (A)", + methodA.toString(), ex.getMessage() ); + AssertMo.assertIncludes( "shows set contents (B)", + methodB.toString(), ex.getMessage() ); + return; + } + + fail("Should fail for a missing item"); + } + + public void testCallFailsOnEmptySet() throws Throwable { + try { + callSet.call( unusedMock, "methodC", new Object[] { "arg1", "arg2" } ); + } catch (AssertionFailedError ex) { + AssertMo.assertIncludes( "reports empty set in error message", + "no methods", ex.getMessage() ); + return; + } + + fail("Should fail for a missing item"); } } --- TestInterface.java DELETED --- --- TestThrowable.java DELETED --- --- SingleCallTest.java DELETED --- --- TestConstraint.java DELETED --- |
From: Tim M. <ma...@us...> - 2003-04-07 14:27:31
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv10938/src/core/com/mockobjects/dynamic Modified Files: Tag: DynamicMockExperiment Mock.java CallMocker.java CallSet.java Added Files: Tag: DynamicMockExperiment ExpectedCall.java ReturnStub.java CallStub.java ThrowStub.java Removed Files: Tag: DynamicMockExperiment SingleCall.java Log Message: Refactored class hierarchy: - call actions can be stubbed - stubs can be decorated with expectations - calls can be grouped in a CallSet (set/bag) - a mock object inherits from CallSet --- NEW FILE: ExpectedCall.java --- /* * Created on 04-Apr-2003 * */ package com.mockobjects.dynamic; import junit.framework.*; import com.mockobjects.constraint.*; import com.mockobjects.util.*; public class ExpectedCall extends Assert implements CallMocker { private String methodName; private Constraint[] constraints; private boolean wasCalled = false; private CallMocker decorated; public ExpectedCall( String methodName, Constraint[] constraints, CallMocker decorated ) { this.methodName = methodName; this.constraints = (Constraint[])constraints.clone(); this.decorated = decorated; } public Object call( Mock mock, String methodName, Object[] args ) throws Throwable { assertEquals( methodName + " received the wrong number of arguments", constraints.length, args.length ); for( int i = 0; i < args.length; i++ ) { if( !constraints[i].eval(args[i]) ) { throw new AssertionFailedError( AssertMo.expectedErrorMessage( methodName + " received incorrect argument " + i, AssertMo.join(constraints), AssertMo.join(args) ) ); } } wasCalled = true; return decorated.call( mock, methodName, args ); } public void verify() { if( !wasCalled ) { throw new AssertionFailedError( methodName + " was not called" ); } } public boolean matches(String methodName, Object[] args) { if( !this.methodName.equals(methodName) ) return false; if( args.length != constraints.length ) return false; for (int i = 0; i < args.length; i++) { if( !constraints[i].eval(args[i]) ) return false; } return true; } public String getDescription() { return AssertMo.methodToString(methodName, constraints); } } --- NEW FILE: ReturnStub.java --- /* * Created on 07-Apr-2003 */ package com.mockobjects.dynamic; /** * @author dev */ public class ReturnStub extends CallStub { private Object result; public ReturnStub( Object result ) { this.result = result; } public Object call(Mock mock, String methodName, Object[] args) throws Throwable { return result; } public String getDescription() { return "returns <" + result + ">"; } } --- NEW FILE: CallStub.java --- /* * Created on 07-Apr-2003 */ package com.mockobjects.dynamic; /** * @author dev */ public abstract class CallStub implements CallMocker { public boolean matches(String methodName, Object[] args) { return true; } public void verify() { } } --- NEW FILE: ThrowStub.java --- /* * Created on 07-Apr-2003 */ package com.mockobjects.dynamic; /** * @author dev */ public class ThrowStub extends CallStub { private Throwable throwable; public ThrowStub( Throwable throwable ) { this.throwable = throwable; } public Object call(Mock mock, String methodName, Object[] args) throws Throwable { throw throwable; } public String getDescription() { return "throws <" + throwable + ">"; } } Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.16.2.1 retrieving revision 1.16.2.2 diff -u -r1.16.2.1 -r1.16.2.2 --- Mock.java 4 Apr 2003 16:47:34 -0000 1.16.2.1 +++ Mock.java 7 Apr 2003 14:26:52 -0000 1.16.2.2 @@ -8,15 +8,13 @@ import junit.framework.*; -import com.mockobjects.*; /** * @author dev */ -public class Mock implements Verifiable, InvocationHandler { +public class Mock extends CallSet implements InvocationHandler { private String name; private Object proxy; - private CallMocker call; public Mock( Class mockedClass, String name) { @@ -34,12 +32,16 @@ String name = c.getName(); int dotIndex = name.lastIndexOf('.'); if( dotIndex >= 0 ) { - return "mock-" + name.substring(dotIndex + 1 ); + return "mock" + name.substring(dotIndex + 1 ); } else { - return "mock-" + name; + return "mock" + name; } } - + + public String toString() { + return name; + } + public Object proxy() { return proxy; } @@ -48,25 +50,19 @@ throws Throwable { try { - return call.call( this, method.getName(), (args == null ? new Object[0] : args) ); + return call( this, method.getName(), (args == null ? new Object[0] : args) ); } catch( AssertionFailedError ex ) { throw new AssertionFailedError( name + ": " + ex.getMessage() ); } } - - public void expect(CallMocker mocker) { - call = mocker; - } public void verify() { - if( call != null ) { - try { - call.verify(); - } - catch( AssertionFailedError ex ) { - throw new AssertionFailedError( name + ": " + ex.getMessage() ); - } - } + try { + super.verify(); + } + catch( AssertionFailedError ex ) { + throw new AssertionFailedError( name + ": " + ex.getMessage() ); + } } } Index: CallMocker.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Attic/CallMocker.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- CallMocker.java 4 Apr 2003 16:47:37 -0000 1.1.2.1 +++ CallMocker.java 7 Apr 2003 14:26:53 -0000 1.1.2.2 @@ -7,6 +7,9 @@ public interface CallMocker extends Verifiable { + String getDescription(); + Object call( Mock mock, String methodName, Object[] args ) throws Throwable; + boolean matches(String methodName, Object[] args); } Index: CallSet.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Attic/CallSet.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- CallSet.java 4 Apr 2003 16:47:38 -0000 1.1.2.1 +++ CallSet.java 7 Apr 2003 14:26:53 -0000 1.1.2.2 @@ -5,6 +5,10 @@ import java.util.*; +import com.mockobjects.util.*; + +import junit.framework.*; + /** * @author dev */ @@ -14,8 +18,6 @@ public CallSet() { - super(); - // dodgy Auto-generated constructor stub } public Object call(Mock mock, String methodName, Object[] args) throws Throwable { @@ -25,9 +27,32 @@ return element.call(mock, methodName, args); } } - return null; + + StringBuffer buf = new StringBuffer(); + buf.append("Unexpected call to "); + buf.append( AssertMo.methodToString( methodName, args ) ); + buf.append( "\n" ); + buf.append( "Expected " ); + buf.append( getDescription() ); + throw new AssertionFailedError( buf.toString() ); } - + + public String getDescription() { + if( expectedCalls.isEmpty() ) { + return "no methods"; + } else { + StringBuffer buf = new StringBuffer(); + + buf.append( "one of:\n" ); + for( Iterator i = expectedCalls.iterator(); i.hasNext(); ) { + buf.append( ((CallMocker)i.next()).toString() ); + buf.append( "\n" ); + } + + return buf.toString(); + } + } + public void verify() { for (Iterator call = expectedCalls.iterator(); call.hasNext();) { CallMocker element = (CallMocker) call.next(); @@ -36,7 +61,7 @@ } - public void add(CallMocker call) { + public void expect(CallMocker call) { expectedCalls.add(call); } --- SingleCall.java DELETED --- |
From: Tim M. <ma...@us...> - 2003-04-07 14:27:31
|
Update of /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/password In directory sc8-pr-cvs1:/tmp/cvs-serv10938/src/examples/com/mockobjects/examples/password Modified Files: Tag: DynamicMockExperiment MailingException.java Log Message: Refactored class hierarchy: - call actions can be stubbed - stubs can be decorated with expectations - calls can be grouped in a CallSet (set/bag) - a mock object inherits from CallSet Index: MailingException.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/password/MailingException.java,v retrieving revision 1.1 retrieving revision 1.1.6.1 diff -u -r1.1 -r1.1.6.1 --- MailingException.java 17 Nov 2001 10:17:17 -0000 1.1 +++ MailingException.java 7 Apr 2003 14:26:57 -0000 1.1.6.1 @@ -1,4 +1,4 @@ -package src.examples.com.mockobjects.examples.password; +package com.mockobjects.examples.password; public class MailingException extends Exception { public MailingException() { |
From: Tim M. <ma...@us...> - 2003-04-07 14:27:30
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/constraint In directory sc8-pr-cvs1:/tmp/cvs-serv10938/src/core/test/mockobjects/constraint Modified Files: Tag: DynamicMockExperiment ConstraintsTest.java Log Message: Refactored class hierarchy: - call actions can be stubbed - stubs can be decorated with expectations - calls can be grouped in a CallSet (set/bag) - a mock object inherits from CallSet Index: ConstraintsTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/constraint/ConstraintsTest.java,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -u -r1.1 -r1.1.2.1 --- ConstraintsTest.java 12 Nov 2002 17:42:40 -0000 1.1 +++ ConstraintsTest.java 7 Apr 2003 14:26:56 -0000 1.1.2.1 @@ -5,7 +5,6 @@ package test.mockobjects.constraint; import com.mockobjects.constraint.*; -import com.mockobjects.constraint.*; import java.util.EventObject; |
From: Tim M. <ma...@us...> - 2003-04-07 14:27:06
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/util In directory sc8-pr-cvs1:/tmp/cvs-serv10938/src/core/com/mockobjects/util Modified Files: Tag: DynamicMockExperiment AssertMo.java Log Message: Refactored class hierarchy: - call actions can be stubbed - stubs can be decorated with expectations - calls can be grouped in a CallSet (set/bag) - a mock object inherits from CallSet Index: AssertMo.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/util/AssertMo.java,v retrieving revision 1.2.4.1 retrieving revision 1.2.4.2 diff -u -r1.2.4.1 -r1.2.4.2 --- AssertMo.java 4 Apr 2003 16:47:41 -0000 1.2.4.1 +++ AssertMo.java 7 Apr 2003 14:27:02 -0000 1.2.4.2 @@ -118,4 +118,15 @@ "\tExpected: " + expectedMsg + "\n" + "\tReceived: " + receivedMsg + "\n"; } + + public static String methodToString( String name, Object[] args ) { + StringBuffer buf = new StringBuffer(); + + buf.append(name); + buf.append("("); + buf.append(join(args)); + buf.append(")"); + + return buf.toString(); + } } |
From: Tim M. <ma...@us...> - 2003-04-07 14:27:06
|
Update of /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv10938/src/examples/com/mockobjects/examples/dynamic Modified Files: Tag: DynamicMockExperiment SimpleServletTest.java SimpleServlet.java Log Message: Refactored class hierarchy: - call actions can be stubbed - stubs can be decorated with expectations - calls can be grouped in a CallSet (set/bag) - a mock object inherits from CallSet Index: SimpleServletTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/dynamic/Attic/SimpleServletTest.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- SimpleServletTest.java 4 Apr 2003 16:47:41 -0000 1.1.2.1 +++ SimpleServletTest.java 7 Apr 2003 14:27:01 -0000 1.1.2.2 @@ -55,8 +55,13 @@ Mock mockHttpServletResponse = new Mock(HttpServletResponse.class, "response"); Mock mockHttpServletRequest = new Mock(HttpServletRequest.class); - mockHttpServletRequest.expect( new SingleCall( "getParameter", C.args(C.eq("subject")) ) ); - mockHttpServletRequest.expect( new SingleCall( "getParameter", C.args(C.eq("body")) ) ); + mockHttpServletRequest.expect( new ExpectedCall( "getParameter", C.args(C.eq("subject")), new ReturnStub("Mail Subject") )); + mockHttpServletRequest.expect( new ExpectedCall( "getParameter", C.args(C.eq("body")), new ReturnStub("Mail Body") )); + //mockHttpServletRequest.permit( new ArgumentChecker( "getParameter", C.args(C.eq("browser-identifier"))), new ReturnStub("MSIE 5.0") ); + +// mockHttpServletRequest.expect( "getParameter", new Object[] {"subject"}, C.returnValue("Mail Subject") ); +// mockHttpServletRequest.expectAndReturn( "getParameter", new Object[] {"subject"}, "Mail Subject"); +// mockHttpServletRequest.expectAndThrow( "getParameter", new Object[] {"subject"}, C.throwException(new Exception()) ); // mockHttpServletRequest.expect( "getParameter", "subject"); //.returnValue("mail subject"); @@ -66,8 +71,10 @@ final StringWriter contentWriter = new StringWriter(); - mockHttpServletResponse.expect( new SingleCall( "setContentType", C.args(C.eq("text/html")) ) ); - //mockHttpServletResponse.expect( "getWriter" ).returnValue( contentWriter ); +// mockHttpServletResponse.expect( new CallMocker[] { +// new SingleCall( "setContentType", C.args(C.eq("text/html")) ), +// new SingleCall( "getWriter", C.args() ).returnValue( contentWriter ), +// } ); SimpleServlet aServlet = new SimpleServlet(); aServlet.doGet((HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy()); Index: SimpleServlet.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/examples/com/mockobjects/examples/dynamic/Attic/SimpleServlet.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- SimpleServlet.java 4 Apr 2003 16:47:41 -0000 1.1.2.1 +++ SimpleServlet.java 7 Apr 2003 14:27:02 -0000 1.1.2.2 @@ -18,7 +18,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //super.doGet(arg0, arg1); - String subject = request.getParameter("subjectx"); + String subject = request.getParameter("subject"); } } |
From: Tim M. <ma...@us...> - 2003-04-06 23:08:37
|
Update of /cvsroot/mockobjects/mockobjects-java/src/jdk/common/com/mockobjects/io In directory sc8-pr-cvs1:/tmp/cvs-serv21048/src/jdk/common/com/mockobjects/io Added Files: MockFileInputStream.java Log Message: Some of the basic non-proprietary connextra utilities for them to add to. --- NEW FILE: MockFileInputStream.java --- package com.mockobjects.io; import java.io.*; import junit.framework.*; import com.mockobjects.*; public class MockFileInputStream extends FileInputStream { public static final String KNOWN_FILE = "c:\\autoexec.bat"; private String myContents = ""; private int myIndex = 0; private MockFileInputStream() throws FileNotFoundException { // Unfortunately as there is no interface we have to call the supertype, // which requires a file. super(KNOWN_FILE); } public static MockFileInputStream newMockFileInputStream(){ try { return new MockFileInputStream(); } catch (FileNotFoundException e) { throw new AssertionFailedError("couldn't create MockFileInputStream (requires known file: " + KNOWN_FILE +") "+e); } } public int available() throws IOException { return myContents.length(); } public void mark(int readlimit) { MockObject.notYetImplemented("MockFileInputStream.mark"); } public int read() throws IOException { if (myIndex == myContents.length()) return -1; return myContents.charAt(myIndex++); } public int read(byte b[]) throws IOException { if (myIndex == myContents.length()) return -1; for (int i = 0; i < myContents.length(); i++) { b[i] = (byte) read(); } return myContents.length(); } public int read(byte b[], int off, int len) throws IOException { if (myIndex == myContents.length()) return -1; int bytesRead = 0; for (int i = off; i < len; i++) { b[i] = (byte) myContents.charAt(myIndex); bytesRead++; myIndex++; if (myIndex == myContents.length()) { break; } } return bytesRead; } public void setupContents(String contents) { myContents = contents; myIndex = 0; } public long skip(long n) throws IOException { MockObject.notYetImplemented("MockFileInputStream.skip"); return -1; } } |
From: Tim M. <ma...@us...> - 2003-04-06 23:08:37
|
Update of /cvsroot/mockobjects/mockobjects-java/src/extensions/test/connextra/util In directory sc8-pr-cvs1:/tmp/cvs-serv21048/src/extensions/test/connextra/util Added Files: ClockTest.java TestVector.java Log Message: Some of the basic non-proprietary connextra utilities for them to add to. --- NEW FILE: ClockTest.java --- package test.connextra.util; import junit.framework.*; import com.connextra.util.*; public class ClockTest extends TestCase { private static final Class THIS = ClockTest.class; public ClockTest(String name) { super(name); } public static Test suite() { return new TestSuite(THIS); } public void testRunning() throws InterruptedException { Clock aClock = new RealClock(); long start = aClock.getTime(); Thread.currentThread().sleep(10); long stop = aClock.getTime(); assertTrue("Should keep running", stop > start); } public void testStop() throws InterruptedException { Clock aClock = new RealClock(); aClock.stop(); long start = aClock.getTime(); Thread.currentThread().sleep(10); long stop = aClock.getTime(); assertEquals("Should not advance time", start, stop); } } --- NEW FILE: TestVector.java --- package test.connextra.util; import java.util.Enumeration; import java.util.Vector; public class TestVector extends Vector { public TestVector() { super(); } public TestVector(Object[] defaultValues) { super(); for (int i = 0; i < defaultValues.length; i++) { addElement(defaultValues[i]); } } public TestVector(Object defaultValue) { this(new Object[] {defaultValue}); } public TestVector(Object defaultValue1, Object defaultValue2) { this(new Object[] {defaultValue1, defaultValue2}); } public TestVector(Object defaultValue1, Object defaultValue2, Object defaultValue3) { this(new Object[] {defaultValue1, defaultValue2, defaultValue3}); } public TestVector(Object defaultValue1, Object defaultValue2, Object defaultValue3, Object defaultValue4) { this(new Object[] {defaultValue1, defaultValue2, defaultValue3, defaultValue4}); } public TestVector(Object defaultValue1, Object defaultValue2, Object defaultValue3, Object defaultValue4, Object defaultValue5) { this(new Object[] {defaultValue1, defaultValue2, defaultValue3, defaultValue4, defaultValue5}); } public TestVector(Object defaultValue1, Object defaultValue2, Object defaultValue3, Object defaultValue4, Object defaultValue5, Object defaultValue6) { this(new Object[] {defaultValue1, defaultValue2, defaultValue3, defaultValue4, defaultValue5, defaultValue6}); } public TestVector(Object defaultValue1, Object defaultValue2, Object defaultValue3, Object defaultValue4, Object defaultValue5, Object defaultValue6, Object defaultValue7) { this(new Object[] {defaultValue1, defaultValue2, defaultValue3, defaultValue4, defaultValue5, defaultValue6, defaultValue7}); } public TestVector(Enumeration defaultValues) { while (defaultValues.hasMoreElements()) { addElement(defaultValues.nextElement()); } } } |
Update of /cvsroot/mockobjects/mockobjects-java/src/extensions/com/connextra/util In directory sc8-pr-cvs1:/tmp/cvs-serv21048/src/extensions/com/connextra/util Added Files: WriterFactory.java RealWriterFactory.java RealReaderFactory.java ReaderFactory.java Clock.java RealClock.java Log Message: Some of the basic non-proprietary connextra utilities for them to add to. --- NEW FILE: WriterFactory.java --- package com.connextra.util; import java.io.*; public interface WriterFactory { public Writer create(OutputStream anOutputStream); public Writer createFileWriter(String filename) throws IOException; } --- NEW FILE: RealWriterFactory.java --- package com.connextra.util; import java.io.*; public class RealWriterFactory implements WriterFactory { public RealWriterFactory() { super(); } public Writer create(OutputStream anOutputStream) { return new OutputStreamWriter(anOutputStream); } public Writer createFileWriter(String filename) throws java.io.IOException { return new FileWriter(new File(filename)); } } --- NEW FILE: RealReaderFactory.java --- package com.connextra.util; import java.io.*; import java.net.*; public class RealReaderFactory implements ReaderFactory { public Reader create(File aFile) throws IOException { return new FileReader(aFile); } public Reader create(InputStream anInputStream) { return new InputStreamReader(anInputStream); } public BufferedReader create(URL aURL) throws IOException { return new BufferedReader(new InputStreamReader(aURL.openStream())); } } --- NEW FILE: ReaderFactory.java --- package com.connextra.util; import java.io.*; import java.net.*; public interface ReaderFactory { public Reader create(File aFile) throws IOException; public Reader create(InputStream anInputStream); // BufferedReader due to ElectricXML parsing requiring a reader with mark interface public BufferedReader create(URL aURL) throws IOException; } --- NEW FILE: Clock.java --- package com.connextra.util; import java.util.*; public interface Clock { public Date getDate(); public long getTime(); public void stop(); } --- NEW FILE: RealClock.java --- package com.connextra.util; import java.util.*; public class RealClock implements Clock { private long myStoppedTime = -1; public RealClock() { super(); } public Date getDate() { return new Date(); } public long getTime() { if (myStoppedTime != -1) { return myStoppedTime; } return System.currentTimeMillis(); } public void stop() { myStoppedTime = getTime(); } public String toString() { return String.valueOf(getTime()); } } |
From: Tim M. <ma...@us...> - 2003-04-06 23:08:30
|
Update of /cvsroot/mockobjects/mockobjects-java/src/extensions/test/connextra In directory sc8-pr-cvs1:/tmp/cvs-serv20958/src/extensions/test/connextra Log Message: Directory /cvsroot/mockobjects/mockobjects-java/src/extensions/test/connextra added to the repository |
From: Tim M. <ma...@us...> - 2003-04-06 23:08:30
|
Update of /cvsroot/mockobjects/mockobjects-java/src/extensions/test In directory sc8-pr-cvs1:/tmp/cvs-serv20958/src/extensions/test Log Message: Directory /cvsroot/mockobjects/mockobjects-java/src/extensions/test added to the repository |
From: Tim M. <ma...@us...> - 2003-04-06 23:08:29
|
Update of /cvsroot/mockobjects/mockobjects-java/src/extensions/test/connextra/util In directory sc8-pr-cvs1:/tmp/cvs-serv20958/src/extensions/test/connextra/util Log Message: Directory /cvsroot/mockobjects/mockobjects-java/src/extensions/test/connextra/util added to the repository |
From: Tim M. <ma...@us...> - 2003-04-06 23:08:29
|
Update of /cvsroot/mockobjects/mockobjects-java/src/extensions/com/connextra In directory sc8-pr-cvs1:/tmp/cvs-serv20958/src/extensions/com/connextra Log Message: Directory /cvsroot/mockobjects/mockobjects-java/src/extensions/com/connextra added to the repository |
From: Tim M. <ma...@us...> - 2003-04-06 23:08:29
|
Update of /cvsroot/mockobjects/mockobjects-java/src/extensions/com/connextra/util In directory sc8-pr-cvs1:/tmp/cvs-serv20958/src/extensions/com/connextra/util Log Message: Directory /cvsroot/mockobjects/mockobjects-java/src/extensions/com/connextra/util added to the repository |
From: Steve F. <st...@m3...> - 2003-04-06 23:05:08
|
> If I am using jdk1.4 I would expect to include: /src/jdk/1.4 > /src/jdk/common That's your minimum > /src/j2ee/1.4 /src/2jee/common if you want these, then you have to include an appropriate j2ee.jar. The version below j2ee (1.2 or 1.3) is of the j2ee library, not the jdk. The 1.4 version should never have been committed. > but if I do this I am missing classes. If I incclude both 1.3 and 1.4 > I get conflicts - the best I appear to be able to do is: the reason for the separate directories is because of those conflicts. It sounds like you might want to do this with someone in tow. You've been in VisualAge too long ;-) S. |