Guest User

SO question on why JMockIt intervenes with getter X.getE()

a guest
Jan 1st, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. package demonstrate;
  2.  
  3. import mockit.Delegate;
  4. import mockit.Mocked;
  5. import mockit.NonStrictExpectations;
  6. import mockit.StrictExpectations;
  7.  
  8. import org.hamcrest.BaseMatcher;
  9. import org.hamcrest.Description;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12.  
  13. public class SomeTest {
  14.     @Mocked
  15.     Factory factory;
  16.     @Mocked
  17.     Consumer consumer;
  18.  
  19.     @Before
  20.     public void setUp() {
  21.         new NonStrictExpectations() {{
  22.             factory.create(anyInt, (E) any);
  23.             result = new Delegate<Factory>() {
  24.                 @SuppressWarnings("unused")
  25.                 public X create(int i, E e) {
  26.                     return new X(i, e);
  27.                 }
  28.             };
  29.         }};
  30.     }
  31.  
  32.     @Test
  33.     public void testWithMatcher() {
  34.         System.err.println("-----------------------------------------");
  35.         new StrictExpectations() {{
  36.             consumer.consume(withArgThat(new BaseMatcher<X>() {
  37.                 final int i = 2; final E e = E.B;
  38.                 @Override
  39.                 public boolean matches(Object item) {
  40.                     return runChecks((X) item, i, e);
  41.                 }
  42.                 @Override
  43.                 public void describeTo(Description description) {
  44.                     description.appendText("\"i=" + i + ", e=" + e.name() + "\"");
  45.                 }
  46.             }));
  47.         }};
  48.         new Service(factory, consumer).interact(2, E.B);
  49.     }
  50.  
  51.     @Test
  52.     public void testWithDelegate() {
  53.         System.err.println("-----------------------------------------");
  54.         new StrictExpectations() {{
  55.             consumer.consume(with(new Delegate<X>() {
  56.                 @SuppressWarnings("unused")
  57.                 public boolean check(X x) {
  58.                     return runChecks(x, 2, E.B);
  59.                 }
  60.             }));
  61.         }};
  62.         new Service(factory, consumer).interact(2, E.B);
  63.     }
  64.  
  65.     private static boolean runChecks(X actual, int i, E e) {
  66.         System.err.println("RUNNING CHECKS ON: " + actual);
  67.         if (actual.getI() != i) {
  68.             System.err.println("Primitive int mismatch!");
  69.             return false;
  70.         }
  71.         if (actual.e == e) {
  72.             System.err.println("Direct reference match.");
  73.         } else {
  74.             System.err.println("Reference mismatch!");
  75.             return false;
  76.         }
  77.         E otherE = actual.getE();
  78.         if (otherE != e) {
  79.             System.err.println("Reference through getter mismatch!");
  80.             System.err.println("Ordinal through getter reference: "
  81.                     + otherE.ordinal());
  82.             System.err.println("Identity hashcode from getter reference: "
  83.                     + System.identityHashCode(otherE));
  84.             System.err.println("Identity hashcode of expected reference: "
  85.                     + System.identityHashCode(e));
  86.             return false;
  87.         }
  88.         return true;
  89.     }
  90.  
  91.     public enum E {
  92.         A, B
  93.     }
  94.  
  95.     public static class X {
  96.         private final int i;
  97.         public final E e;
  98.  
  99.         public X(int i, E e) {
  100.             this.i = i;
  101.             this.e = e;
  102.         }
  103.  
  104.         @Override
  105.         public String toString() {
  106.             return "i=" + i + ", e=" + e.name();
  107.         }
  108.  
  109.         public int getI() {
  110.             return i;
  111.         }
  112.  
  113.         public E getE() {
  114.             return e;
  115.         }
  116.     }
  117.  
  118.     public static class Factory {
  119.         public X create(int i, E e) {
  120.             return new X(i, e);
  121.         }
  122.     }
  123.  
  124.     public static class Consumer {
  125.         public void consume(X arg) {
  126.         }
  127.     }
  128.  
  129.     public static class Service {
  130.         private final Factory f;
  131.         private final Consumer c;
  132.  
  133.         public Service(Factory f, Consumer c) {
  134.             super();
  135.             this.f = f;
  136.             this.c = c;
  137.         }
  138.  
  139.         public void interact(int i, E e) {
  140.             X v = f.create(i, e);
  141.             c.consume(v);
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment