Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/test/mock2
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14392/input/javasrc/biz/xsoftware/test/mock2
Modified Files:
ListenerOne.java
Added Files:
TestProxyClass.java TestMockCreator.java MyProxyImpl.java
Log Message:
merge brachForOffice to HEAD
--- NEW FILE: TestProxyClass.java ---
package biz.xsoftware.test.mock2;
import java.lang.reflect.Proxy;
import java.util.logging.Logger;
import junit.framework.TestCase;
public class TestProxyClass extends TestCase {
private static final Logger log = Logger.getLogger(TestProxyClass.class.getName());
public TestProxyClass(String arg0) {
super(arg0);
}
public void testProxyFields() {
ClassLoader cl = TestProxyClass.class.getClassLoader();
MyProxyImpl impl = new MyProxyImpl();
//change value through impl first
impl.setField(5);
ListenerOne one = (ListenerOne)Proxy.newProxyInstance(
cl, new Class[] {ListenerOne.class}, impl);
assertEquals(5, impl.getField());
assertEquals(impl.getField(), one.getField());
//now change value through proxy
one.callMeFirst(6);
assertEquals(6, impl.getField());
assertEquals(impl.getField(), one.getField());
}
}
--- NEW FILE: MyProxyImpl.java ---
package biz.xsoftware.test.mock2;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.logging.Logger;
public class MyProxyImpl implements InvocationHandler {
private static final Logger log = Logger.getLogger(MyProxyImpl.class.getName());
private int someField = 0;
public void setField(int i) {
someField = i;
}
public int getField() {
return someField;
}
public Object invoke(Object instance, Method m, Object[] params)
throws Throwable {
log.info("instance="+instance.getClass().getName());
log.info("is instanceof ListenerOne="+(instance instanceof ListenerOne));
log.info("is instanceof MyProxyImpl="+(instance instanceof MyProxyImpl));
if(params != null && params.length > 0 && params[0] instanceof Integer)
someField = (Integer)params[0];
else if("getField".equals(m.getName()))
return someField;
return null;
}
}
--- NEW FILE: TestMockCreator.java ---
package biz.xsoftware.test.mock2;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.TestCase;
import biz.xsoftware.mock2.CalledMethod;
import biz.xsoftware.mock2.Messages;
import biz.xsoftware.mock2.MockObject;
import biz.xsoftware.mock2.MockObjectFactory;
public class TestMockCreator extends TestCase {
private static final Logger log = Logger.getLogger(TestMockCreator.class.getName());
public TestMockCreator(String name){
super(name);
}
public void testMockCreator()throws Exception{
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l=(ListenerOne)m;
String methodName="callMeSecond";
CalledMethod method=m.expect(methodName);
// String param="some params";
// l.callMeSecond(param);
try {
method.getParameters();
fail("Since verify was not called, CalledMethod methods should not work");
} catch(IllegalStateException e) {
}
//assertEquals("the methodName should be the same",methodName,method.getMethodName());
//assertEquals("params should equal", param, method.getParameters()[0]);
//assertEquals("param count should be 1", 1, method.getParameters().length);
}
public void testNoVerifyCalled() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l=(ListenerOne)m;
String methodName1 = "callMeFirst";
String methodName2="callMeSecond";
CalledMethod callMeFirst =m.expect(methodName1);
CalledMethod callMeSecond=m.expect(methodName2);
try {
callMeFirst.getParameters();
fail("The method was not called yet and verify() was not called, so above should throw exceptoin");
} catch(IllegalStateException e) {
}
}
public void testTwoMethods() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l=(ListenerOne)m;
String methodName1 = "callMeFirst";
String methodName2="callMeSecond";
CalledMethod callMeFirst =m.expect(methodName1);
CalledMethod callMeSecond=m.expect(methodName2);
int var1 = 4;
l.callMeFirst(var1);
String param="some params";
l.callMeSecond(param);
m.verify();
assertEquals("the methodName should be the same",methodName1,callMeFirst.getMethodName());
Object[] array = callMeFirst.getParameters();
assertEquals("params should equal", var1, array[0]);
assertEquals("param count should be 1", 1, callMeFirst.getParameters().length);
assertEquals("the methodName should be the same",methodName2,callMeSecond.getMethodName());
assertEquals("params should equal", param, callMeSecond.getParameters()[0]);
assertEquals("param count should be 1", 1, callMeSecond.getParameters().length);
}
public void testTwoMethodsOnThread() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
final ListenerOne l = (ListenerOne) m;
String methodName1 = "callMeFirst";
String methodName2 = "callMeSecond";
CalledMethod callMeFirst = m.expect(methodName1);
CalledMethod callMeSecond = m.expect(methodName2);
final int var1 = 4;
final String param = "some params";
Thread t = new Thread() {
public void run() {
try {
Thread.sleep(1000);
l.callMeFirst(var1);
l.callMeSecond(param);
} catch (Exception e) {
log.log(Level.WARNING, "exception", e);
throw new RuntimeException(e);
}
}
};
t.start();
m.verify();
assertEquals("the methodName should be the same", methodName1,
callMeFirst.getMethodName());
assertEquals("params should equal", var1,
callMeFirst.getParameters()[0]);
assertEquals("param count should be 1", 1,
callMeFirst.getParameters().length);
assertEquals("the methodName should be the same", methodName2,
callMeSecond.getMethodName());
assertEquals("params should equal", param,
callMeSecond.getParameters()[0]);
assertEquals("param count should be 1", 1,
callMeSecond.getParameters().length);
}
public void testFailure() throws Exception {
MockObject m = MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l=(ListenerOne)m;
String param="some params";
l.callMeSecond(param);
String methodName="callMeSecond";
CalledMethod method=m.expect(methodName);
try {
assertEquals("params should equal", param, method.getParameters()[0]);
fail("This should fail since m.verify() is not called");
} catch(IllegalStateException e) {
assertEquals(Messages.VERIFY_NOT_CALLED, e.getMessage());
}
}
public void testExpectWithRetVal() throws Exception{
MockObject m=MockObjectFactory.createMock(ListenerOne.class);
ListenerOne l=(ListenerOne)m;
Object returnVal="OK";
String methodName="callWithRetVal";
CalledMethod method=m.expect(returnVal,methodName);
String param="some params";
String retVal=l.callWithRetVal(param);
m.verify();
assertEquals("The method name should be equal",method.getMethodName(),methodName);
assertEquals("the params should be the same ",method.getParameters()[0],param);
assertEquals("the length of param should be equal",1,method.getParameters().length);
assertEquals("the return value should be the same",returnVal,retVal);
}
public void testRemoveCalledMethod(){
}
}
Index: ListenerOne.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/test/mock2/ListenerOne.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** ListenerOne.java 1 Jan 2006 00:35:57 -0000 1.1
--- ListenerOne.java 16 Feb 2006 15:37:35 -0000 1.2
***************
*** 22,31 ****
public void callMeSecond(String s) throws IOException;
! public void multipleParams(String x, Integer i);
!
! public int respondTo(Map map);
!
! public boolean responsd();
public void noParams();
}
--- 22,30 ----
public void callMeSecond(String s) throws IOException;
! public String callWithRetVal(String s);
public void noParams();
+
+ public int getField();
+
}
|