JeffGrigg

instanceof_onemethod

Oct 31st, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.51 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 process(Code code);
  17. }
  18.  
  19. public class ChangeProcessingHandler {
  20.  
  21.     public CodeDelta triggerProcessingChangeOf(Code code, Change change) {
  22.         verifyChangeOf(code, change);
  23.         return change.process(code);
  24.     }
  25.  
  26.     private void verifyChangeOf(Code code, Change change) {
  27.         // TODO: Add verification logic here.
  28.         change.verifyChangeOn(code);
  29.     }
  30.  
  31.     // some more code
  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 interface Code {
  131. }
  132.  
  133. public class CodeDelta {
  134.  
  135.     private final String _id;
  136.  
  137.     public CodeDelta(String id) {
  138.         _id = id;
  139.     }
  140.  
  141.     @Override
  142.     public String toString() {
  143.         return "CodeDelta(" + _id + ")";
  144.     }
  145. }
  146.  
  147. public class Growth extends Change {
  148.  
  149.     public Growth(String id) {
  150.         super(id);
  151.     }
  152.  
  153.     @Override
  154.     public void verifyChangeOn(Code code) {
  155.         // TODO: Add verification logic here.
  156.     }
  157.  
  158.     @Override
  159.     public CodeDelta process(Code code) {
  160.         return new CodeDelta(code.toString() + ", " + this.toString());
  161.     }
  162.  
  163.     // some code
  164. }
  165.  
  166. public class Improvement extends Change {
  167.  
  168.     public Improvement(String id) {
  169.         super(id);
  170.     }
  171.  
  172.     @Override
  173.     public void verifyChangeOn(Code code) {
  174.         // TODO: Add verification logic here.
  175.     }
  176.  
  177.     @Override
  178.     public CodeDelta process(Code code) {
  179.         return new CodeDelta(code.toString() + ", " + this.toString());
  180.     }
  181.  
  182.     // some code
  183. }
  184.  
  185. public class MockCode implements Code {
  186.  
  187.     final String _id;
  188.  
  189.     public MockCode(String id) {
  190.         _id = id;
  191.     }
  192.  
  193.     @Override
  194.     public String toString() {
  195.         return "MockCode(" + _id + ")";
  196.     }
  197.  
  198. }
  199.  
  200. public class MockVerifyChangeException extends RuntimeException {
  201. }
  202.  
  203. public class Refactoring extends Change {
  204.  
  205.     public Refactoring(String id) {
  206.         super(id);
  207.     }
  208.  
  209.     @Override
  210.     public void verifyChangeOn(Code code) {
  211.         // TODO: Add verification logic here.
  212.     }
  213.  
  214.     @Override
  215.     public CodeDelta process(Code code) {
  216.         return new CodeDelta(code.toString() + ", " + this.toString());
  217.     }
  218.  
  219.     // some code
  220. }
Add Comment
Please, Sign In to add comment