Skip to content

Instantly share code, notes, and snippets.

@namutaka
Created July 1, 2012 14:01
Show Gist options
  • Save namutaka/3028515 to your computer and use it in GitHub Desktop.
Save namutaka/3028515 to your computer and use it in GitHub Desktop.
PropertiesEnhancer work in Play1.2.5
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Test;
import play.Logger;
import play.test.UnitTest;
public class HogeTest extends UnitTest {
public static class Hoge {
public String attr;
public final String finalAttr = "final value";
public String value;
public void setValue(String val) {
Logger.info("invoke setValue(%s)", val);
value = val;
}
public String getValue() {
Logger.info("invoke getValue()");
return this.value;
}
public void func() {
Logger.info("begin func()");
value = "value in func";
Logger.info(value);
Logger.info("end func()");
}
}
@Test
public void testAttr() throws Exception {
Logger.info("== testAttr");
Hoge hoge = new Hoge();
Method methodSet = Hoge.class
.getDeclaredMethod("setAttr", String.class);
methodSet.invoke(hoge, "val");
Logger.info("exists " + methodSet.getName());
Method methodGet = Hoge.class.getDeclaredMethod("getAttr");
String val = (String) methodGet.invoke(hoge);
Logger.info("exists " + methodGet.getName());
assertEquals("val", val);
Logger.info("hoge.getAttr() = " + val);
Logger.info("hoge.attr = " + hoge.attr);
}
@Test
public void testFinalAttr() throws Exception {
Logger.info("== testFinalAttr");
Hoge hoge = new Hoge();
Method method = Hoge.class.getDeclaredMethod("getFinalAttr");
String val = (String) method.invoke(hoge);
Logger.info("exists " + method.getName());
assertEquals("final value", val);
Logger.info("hoge.getFinalAttr() = " + val);
Logger.info("hoge.finalAttr = " + hoge.finalAttr);
}
@Test
public void testVal() {
Logger.info("== testVal");
Logger.info("-- outer");
Hoge hoge = new Hoge();
hoge.value = "abc";
String val = hoge.value;
Logger.info("-- inner");
hoge.func();
}
}
10:02:25,806 INFO ~ == testAttr
10:02:25,806 INFO ~ exists setAttr
10:02:25,806 INFO ~ exists getAttr
10:02:25,806 INFO ~ hoge.getAttr() = val
10:02:25,806 INFO ~ hoge.attr = val
10:02:25,806 INFO ~ == testFinalAttr
10:02:25,806 INFO ~ exists getFinalAttr
10:02:25,807 INFO ~ hoge.getFinalAttr() = final value
10:02:25,807 INFO ~ hoge.finalAttr = final value
10:02:25,807 INFO ~ == testVal
10:02:25,807 INFO ~ -- outer
10:02:25,807 INFO ~ invoke setValue(abc)
10:02:25,807 INFO ~ invoke getValue()
10:02:25,807 INFO ~ -- inner
10:02:25,807 INFO ~ begin func()
10:02:25,807 INFO ~ invoke setValue(value in func)
10:02:25,808 INFO ~ invoke getValue()
10:02:25,808 INFO ~ value in func
10:02:25,808 INFO ~ end func()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment