ProxyDelegatorTest.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * @author Ranides Atterwim <ranides@gmail.com>
  3. * @copyright Ranides Atterwim
  4. * @license WTFPL
  5. * @url http://ranides.net/projects/assira
  6. */
  7. package net.ranides.assira.asm;
  8. import net.ranides.assira.generic.BiFunction;
  9. import org.junit.Test;
  10. import static org.junit.Assert.*;
  11. /**
  12. *
  13. * @author ranides
  14. */
  15. public class ProxyDelegatorTest {
  16. private static final BiFunction<Object, Object> WRAPPER = new BiFunction<Object, Object>() {
  17. @Override
  18. public Object apply(Object value) {
  19. if(value instanceof Number) {
  20. return ((Number)value).doubleValue() * 100;
  21. } else {
  22. return value;
  23. }
  24. }
  25. @Override
  26. public Object inverse(Object value) {
  27. if(value instanceof Number) {
  28. return ((Number)value).doubleValue() / 100;
  29. } else {
  30. return value;
  31. }
  32. }
  33. };
  34. private interface JMath {
  35. void width(Number value);
  36. void height(Number value);
  37. double width();
  38. double height();
  39. double mul(double a, double b);
  40. }
  41. private static class CJMath implements JMath {
  42. private double w;
  43. private double h;
  44. public CJMath() {
  45. w = 1.0;
  46. h = 2.0;
  47. }
  48. @Override
  49. public void width(Number value) {
  50. this.w = value.doubleValue();
  51. }
  52. @Override
  53. public void height(Number value) {
  54. this.h = value.doubleValue();
  55. }
  56. @Override
  57. public double width() {
  58. return w;
  59. }
  60. @Override
  61. public double height() {
  62. return h;
  63. }
  64. @Override
  65. public double mul(double a, double b) {
  66. return a * b;
  67. }
  68. }
  69. public ProxyDelegatorTest() {
  70. }
  71. @Test
  72. public void testCreate() {
  73. final JMath jmath = new CJMath();
  74. final JMath wmath = ProxyDelegator.paramWrapper(jmath, JMath.class, WRAPPER);
  75. assertEquals(1.0, (Double)jmath.width(), 0.1);
  76. assertEquals(2.0, (Double)jmath.height(), 0.1);
  77. assertEquals(100*(Double)jmath.width(), (Double)wmath.width(), 0.1);
  78. assertEquals(100*(Double)jmath.height(), (Double)wmath.height(), 0.1);
  79. wmath.width(1900);
  80. jmath.height(7);
  81. assertEquals(19.0, (Double)jmath.width(), 0.1);
  82. assertEquals(7.0, (Double)jmath.height(), 0.1);
  83. assertEquals(100*(Double)jmath.width(), (Double)wmath.width(), 0.1);
  84. assertEquals(100*(Double)jmath.height(), (Double)wmath.height(), 0.1);
  85. }
  86. }