Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/test/mock
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv7561/input/javasrc/biz/xsoftware/test/mock
Modified Files:
TestMockCreator.java
Added Files:
Identical.java
Log Message:
add behavior to mock library.
--- NEW FILE: Identical.java ---
/**
* Copyright (C) 2006 Carrier Access, Corp.
*/
package biz.xsoftware.test.mock;
/**
*/
public interface Identical
{
public byte[] doThat(byte[] data);
}
Index: TestMockCreator.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/test/mock/TestMockCreator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** TestMockCreator.java 1 Jan 2006 00:35:56 -0000 1.1
--- TestMockCreator.java 10 Jun 2006 12:41:03 -0000 1.2
***************
*** 11,14 ****
--- 11,15 ----
import junit.framework.TestSuite;
import junit.textui.TestRunner;
+ import biz.xsoftware.mock.Behavior;
import biz.xsoftware.mock.CalledMethod;
import biz.xsoftware.mock.ExpectFailedException;
***************
*** 152,155 ****
--- 153,231 ----
}
+ public void testBehavior() {
+ MockObject mock = MockObjectFactory.createMock(Identical.class);
+
+ mock.addBehavior("doThat", new MyBehavior());
+
+ Identical ident = (Identical)mock;
+
+ byte[] original = new byte[] { 1, 2, 3, 4, 0, 0, 0, 0};
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 0 ,0,0,0};
+
+ byte[] newBytes = ident.doThat(bytes);
+
+ CalledMethod m = mock.expectCall("doThat");
+ byte[] passedIn = (byte[])m.getAllParams()[0];
+
+ assertEquals(original.length, passedIn.length);
+ for(int i = 0; i < original.length; i++) {
+ assertEquals(original[i], passedIn[i]);
+ }
+
+ //make sure this is the same byte array we passed in
+ assertSame(bytes, newBytes);
+
+ for(int i = 0; i < 4; i++) {
+ assertEquals(newBytes[i], newBytes[i+4]);
+ }
+ }
+
+ private static class MyBehavior implements Behavior {
+
+ /**
+ * @see biz.xsoftware.mock.Behavior#clone(java.lang.Object[])
+ */
+ public Object[] clone(Object[] params)
+ {
+ Object[] retVal = new Object[params.length];
+ for(int i = 0; i < retVal.length; i++) {
+ Object val = params[i];
+ if(val instanceof byte[]) {
+ retVal[i] = cloneBytes((byte[])val);
+ }
+ }
+ return retVal;
+ }
+
+ /**
+ * @param bs
+ * @return
+ */
+ private Object cloneBytes(byte[] bytes)
+ {
+ byte[] newBytes = new byte[bytes.length];
+ for(int i = 0; i < newBytes.length; i++) {
+ newBytes[i] = bytes[i];
+ }
+ return newBytes;
+ }
+
+ /**
+ * @see biz.xsoftware.mock.Behavior#runMethod(java.lang.Object[])
+ */
+ public Object runMethod(Object[] params)
+ {
+ //we know the test does an 4 byte array, so do that
+ byte[] bytes = (byte[])params[0];
+ for(int i = 0; i < 4; i++) {
+ bytes[i+4] = bytes[i];
+ }
+
+ //make sure to return the same byte buffer so we are really testing this...
+ return bytes;
+ }
+
+ }
+
public static void main(String[] args) {
TestSuite suite = new TestSuite();
|