JeffGrigg

instanceof_refactored

Oct 31st, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.21 KB | None | 0 0
  1. public abstract class Change {
  2.  
  3.     private final String _id;
  4.  
  5.     protected Change(String id) {
  6.         _id = id;
  7.     }
  8.  
  9.     public abstract void verifyChangeOn(Code code);
  10.  
  11.     @Override
  12.     public String toString() {
  13.         return this.getClass().getSimpleName() + "(" + _id + ")";
  14.     }
  15.  
  16.     public abstract CodeDelta accept(Visitator visitator);
  17.  
  18. }
  19.  
  20. public class ChangeProcessingHandler {
  21.  
  22.     public CodeDelta triggerProcessingChangeOf(Code code, Change change) {
  23.         verifyChangeOf(code, change);
  24.         return change.accept(new ChangeProcessor(code));
  25.     }
  26.  
  27.     private void verifyChangeOf(Code code, Change change) {
  28.         // TODO: Add verification logic here.
  29.         change.verifyChangeOn(code);
  30.     }
  31.  
  32. }
  33.  
  34. import junit.framework.TestCase;
  35.  
  36. public class ChangeProcessingHandlerTest extends TestCase {
  37.  
  38.     public void testRefactoring() {
  39.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  40.         Refactoring refactoring = new Refactoring("my test Refactoring instance for " + this.getName());
  41.         Code code = new MockCode("my Code for " + this.getName());
  42.  
  43.         CodeDelta delta = handler.triggerProcessingChangeOf(code, refactoring);
  44.  
  45.         assertEquals("CodeDelta(MockCode(my Code for testRefactoring), Refactoring(my test Refactoring instance for testRefactoring))", delta.toString());
  46.     }
  47.  
  48.     public void testRefactoringError() {
  49.         final MockVerifyChangeException mockVerifyChangeException = new MockVerifyChangeException();
  50.  
  51.         Refactoring refactoring = new Refactoring("my test Refactoring instance for " + this.getName()) {
  52.             @Override
  53.             public void verifyChangeOn(Code code) {
  54.                 throw mockVerifyChangeException;
  55.             }
  56.         };
  57.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  58.         Code code = new MockCode("my Code for " + this.getName());
  59.  
  60.         try {
  61.             handler.triggerProcessingChangeOf(code, refactoring);
  62.             fail("Expecting MockVerifyChangeException.");
  63.         } catch (MockVerifyChangeException verifyChangeException) {
  64.             assertSame(mockVerifyChangeException, verifyChangeException);
  65.         }
  66.     }
  67.  
  68.     public void testGrowth() {
  69.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  70.         Growth growth = new Growth("my test Growth instance for " + this.getName());
  71.         Code code = new MockCode("my Code for " + this.getName());
  72.  
  73.         CodeDelta delta = handler.triggerProcessingChangeOf(code, growth);
  74.  
  75.         assertEquals("CodeDelta(MockCode(my Code for testGrowth), Growth(my test Growth instance for testGrowth))", delta.toString());
  76.     }
  77.  
  78.     public void testGrowthError() {
  79.         final MockVerifyChangeException mockVerifyChangeException = new MockVerifyChangeException();
  80.  
  81.         Growth growth = new Growth("my test Growth instance for " + this.getName()) {
  82.             @Override
  83.             public void verifyChangeOn(Code code) {
  84.                 throw mockVerifyChangeException;
  85.             }
  86.         };
  87.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  88.         Code code = new MockCode("my Code for " + this.getName());
  89.  
  90.         try {
  91.             handler.triggerProcessingChangeOf(code, growth);
  92.             fail("Expecting MockVerifyChangeException.");
  93.         } catch (MockVerifyChangeException verifyChangeException) {
  94.             assertSame(mockVerifyChangeException, verifyChangeException);
  95.         }
  96.     }
  97.  
  98.     public void testImprovement() {
  99.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  100.         Improvement improvement = new Improvement("my test Improvement instance for " + this.getName());
  101.         Code code = new MockCode("my Code for " + this.getName());
  102.  
  103.         CodeDelta delta = handler.triggerProcessingChangeOf(code, improvement);
  104.  
  105.         assertEquals("CodeDelta(MockCode(my Code for testImprovement), Improvement(my test Improvement instance for testImprovement))", delta.toString());
  106.     }
  107.  
  108.     public void testImprovementError() {
  109.         final MockVerifyChangeException mockVerifyChangeException = new MockVerifyChangeException();
  110.  
  111.         Improvement improvement = new Improvement("my test Improvement instance for " + this.getName()) {
  112.             @Override
  113.             public void verifyChangeOn(Code code) {
  114.                 throw mockVerifyChangeException;
  115.             }
  116.         };
  117.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  118.         Code code = new MockCode("my Code for " + this.getName());
  119.  
  120.         try {
  121.             handler.triggerProcessingChangeOf(code, improvement);
  122.             fail("Expecting MockVerifyChangeException.");
  123.         } catch (MockVerifyChangeException verifyChangeException) {
  124.             assertSame(mockVerifyChangeException, verifyChangeException);
  125.         }
  126.     }
  127.  
  128. }
  129.  
  130. public class ChangeProcessor implements Visitator {
  131.     private final Code code;
  132.  
  133.     public ChangeProcessor(Code code) {
  134.         this.code = code;
  135.     }
  136.  
  137.     @Override
  138.     public CodeDelta visit(Refactoring refactoring) {
  139.         return new CodeDelta(code.toString() + ", " + refactoring.toString());
  140.     }
  141.  
  142.     @Override
  143.     public CodeDelta visit(Improvement improvement) {
  144.         return new CodeDelta(code.toString() + ", " + improvement.toString());
  145.     }
  146.  
  147.     @Override
  148.     public CodeDelta visit(Growth growth) {
  149.         return new CodeDelta(code.toString() + ", " + growth.toString());
  150.     }
  151. }
  152.  
  153. public interface Code {
  154. }
  155.  
  156. public class CodeDelta {
  157.  
  158.     private final String _id;
  159.  
  160.     public CodeDelta(String id) {
  161.         _id = id;
  162.     }
  163.  
  164.     @Override
  165.     public String toString() {
  166.         return "CodeDelta(" + _id + ")";
  167.     }
  168. }
  169.  
  170. public class Growth extends Change {
  171.  
  172.     public Growth(String id) {
  173.         super(id);
  174.     }
  175.  
  176.     @Override
  177.     public void verifyChangeOn(Code code) {
  178.         // TODO: Add verification logic here.
  179.     }
  180.  
  181.     @Override
  182.     public CodeDelta accept(Visitator visitator) {
  183.         return visitator.visit(this);
  184.     }
  185.  
  186.     // some code
  187. }
  188.  
  189. public class Improvement extends Change {
  190.  
  191.     public Improvement(String id) {
  192.         super(id);
  193.     }
  194.  
  195.     @Override
  196.     public void verifyChangeOn(Code code) {
  197.         // TODO: Add verification logic here.
  198.     }
  199.  
  200.     @Override
  201.     public CodeDelta accept(Visitator visitator) {
  202.         return visitator.visit(this);
  203.     }
  204.  
  205.     // some code
  206. }
  207.  
  208. public class MockCode implements Code {
  209.  
  210.     final String _id;
  211.  
  212.     public MockCode(String id) {
  213.         _id = id;
  214.     }
  215.  
  216.     @Override
  217.     public String toString() {
  218.         return "MockCode(" + _id + ")";
  219.     }
  220.  
  221. }
  222.  
  223. public class MockVerifyChangeException extends RuntimeException {
  224. }
  225.  
  226. public class Refactoring extends Change {
  227.  
  228.     public Refactoring(String id) {
  229.         super(id);
  230.     }
  231.  
  232.     @Override
  233.     public void verifyChangeOn(Code code) {
  234.         // TODO: Add verification logic here.
  235.     }
  236.  
  237.     @Override
  238.     public CodeDelta accept(Visitator visitator) {
  239.         return visitator.visit(this);
  240.     }
  241.  
  242.     // some code
  243.  
  244. public interface Visitator {
  245.     CodeDelta visit(Refactoring refactoring);
  246.     CodeDelta visit(Improvement improvement);
  247.     CodeDelta visit(Growth growth);
  248. }
Add Comment
Please, Sign In to add comment