Update of /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/test/mock2
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18160/javasrc/biz/xsoftware/test/mock2
Modified Files:
Tag: branchForOffice
ListenerOne.java
Added Files:
Tag: branchForOffice
MyProxyImpl.java TestProxyClass.java
Log Message:
quick test on proxy stuff.
--- 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;
}
}
Index: ListenerOne.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib2/input/javasrc/biz/xsoftware/test/mock2/ListenerOne.java,v
retrieving revision 1.1
retrieving revision 1.1.2.1
diff -C2 -d -r1.1 -r1.1.2.1
*** ListenerOne.java 1 Jan 2006 00:35:57 -0000 1.1
--- ListenerOne.java 7 Feb 2006 00:59:06 -0000 1.1.2.1
***************
*** 29,31 ****
--- 29,33 ----
public void noParams();
+
+ public int getField();
}
|