Advertisement
JeffGrigg

instanceof_initial

Oct 31st, 2016
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.79 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. }
  17.  
  18. public class ChangeProcessingHandler {
  19.  
  20.     public CodeDelta triggerProcessingChangeOf(Code code, Change change) {
  21.         verifyChangeOf(code, change);
  22.  
  23.         if (change instanceof Refactoring) {
  24.             return processRefactoring(code, (Refactoring) change);
  25.         } else if (change instanceof Improvement)  {
  26.             return processImprovement(code, (Improvement) change);
  27.         } else if (change instanceof Growth) {
  28.             return processGrowth(code, (Growth) change);
  29.         } else {
  30.             throw new UnsuportedChangeException();
  31.         }
  32.     }
  33.  
  34.     private void verifyChangeOf(Code code, Change change) {
  35.         // TODO: Add verification logic here.
  36.         change.verifyChangeOn(code);
  37.     }
  38.  
  39.     private CodeDelta processRefactoring(Code code, Refactoring change) {
  40.         return new CodeDelta(code.toString() + ", " + change.toString());
  41.     }
  42.  
  43.     private CodeDelta processImprovement(Code code, Improvement change) {
  44.         return new CodeDelta(code.toString() + ", " + change.toString());
  45.     }
  46.  
  47.     private CodeDelta processGrowth(Code code, Growth change) {
  48.         return new CodeDelta(code.toString() + ", " + change.toString());
  49.     }
  50.  
  51.     // some more code
  52. }
  53.  
  54. import junit.framework.TestCase;
  55.  
  56. public class ChangeProcessingHandlerTest extends TestCase {
  57.  
  58.     public void testRefactoring() {
  59.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  60.         Refactoring refactoring = new Refactoring("my test Refactoring instance for " + this.getName());
  61.         Code code = new MockCode("my Code for " + this.getName());
  62.  
  63.         CodeDelta delta = handler.triggerProcessingChangeOf(code, refactoring);
  64.  
  65.         assertEquals("CodeDelta(MockCode(my Code for testRefactoring), Refactoring(my test Refactoring instance for testRefactoring))", delta.toString());
  66.     }
  67.  
  68.     public void testRefactoringError() {
  69.         final MockVerifyChangeException mockVerifyChangeException = new MockVerifyChangeException();
  70.  
  71.         Refactoring refactoring = new Refactoring("my test Refactoring instance for " + this.getName()) {
  72.             @Override
  73.             public void verifyChangeOn(Code code) {
  74.                 throw mockVerifyChangeException;
  75.             }
  76.         };
  77.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  78.         Code code = new MockCode("my Code for " + this.getName());
  79.  
  80.         try {
  81.             handler.triggerProcessingChangeOf(code, refactoring);
  82.             fail("Expecting MockVerifyChangeException.");
  83.         } catch (MockVerifyChangeException verifyChangeException) {
  84.             assertSame(mockVerifyChangeException, verifyChangeException);
  85.         }
  86.     }
  87.  
  88.     public void testGrowth() {
  89.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  90.         Growth growth = new Growth("my test Growth instance for " + this.getName());
  91.         Code code = new MockCode("my Code for " + this.getName());
  92.  
  93.         CodeDelta delta = handler.triggerProcessingChangeOf(code, growth);
  94.  
  95.         assertEquals("CodeDelta(MockCode(my Code for testGrowth), Growth(my test Growth instance for testGrowth))", delta.toString());
  96.     }
  97.  
  98.     public void testGrowthError() {
  99.         final MockVerifyChangeException mockVerifyChangeException = new MockVerifyChangeException();
  100.  
  101.         Growth growth = new Growth("my test Growth instance for " + this.getName()) {
  102.             @Override
  103.             public void verifyChangeOn(Code code) {
  104.                 throw mockVerifyChangeException;
  105.             }
  106.         };
  107.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  108.         Code code = new MockCode("my Code for " + this.getName());
  109.  
  110.         try {
  111.             handler.triggerProcessingChangeOf(code, growth);
  112.             fail("Expecting MockVerifyChangeException.");
  113.         } catch (MockVerifyChangeException verifyChangeException) {
  114.             assertSame(mockVerifyChangeException, verifyChangeException);
  115.         }
  116.     }
  117.  
  118.     public void testImprovement() {
  119.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  120.         Improvement improvement = new Improvement("my test Improvement instance for " + this.getName());
  121.         Code code = new MockCode("my Code for " + this.getName());
  122.  
  123.         CodeDelta delta = handler.triggerProcessingChangeOf(code, improvement);
  124.  
  125.         assertEquals("CodeDelta(MockCode(my Code for testImprovement), Improvement(my test Improvement instance for testImprovement))", delta.toString());
  126.     }
  127.  
  128.     public void testImprovementError() {
  129.         final MockVerifyChangeException mockVerifyChangeException = new MockVerifyChangeException();
  130.  
  131.         Improvement improvement = new Improvement("my test Improvement instance for " + this.getName()) {
  132.             @Override
  133.             public void verifyChangeOn(Code code) {
  134.                 throw mockVerifyChangeException;
  135.             }
  136.         };
  137.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  138.         Code code = new MockCode("my Code for " + this.getName());
  139.  
  140.         try {
  141.             handler.triggerProcessingChangeOf(code, improvement);
  142.             fail("Expecting MockVerifyChangeException.");
  143.         } catch (MockVerifyChangeException verifyChangeException) {
  144.             assertSame(mockVerifyChangeException, verifyChangeException);
  145.         }
  146.     }
  147.  
  148.     public void testMockUnsuportedChange() {
  149.         MockUnsuportedChange improvement = new MockUnsuportedChange("my test Improvement instance for " + this.getName());
  150.         ChangeProcessingHandler handler = new ChangeProcessingHandler();
  151.         Code code = new MockCode("my Code for " + this.getName());
  152.  
  153.         try {
  154.             handler.triggerProcessingChangeOf(code, improvement);
  155.             fail("Expecting UnsuportedChangeException.");
  156.         } catch (UnsuportedChangeException unsuportedChangeException) {
  157.             // Expected path. Nothing more to verify.
  158.         }
  159.     }
  160.  
  161. }
  162.  
  163. public interface Code {
  164. }
  165.  
  166. public class CodeDelta {
  167.  
  168.     private final String _id;
  169.  
  170.     public CodeDelta(String id) {
  171.         _id = id;
  172.     }
  173.  
  174.     @Override
  175.     public String toString() {
  176.         return "CodeDelta(" + _id + ")";
  177.     }
  178. }
  179.  
  180. public class Growth extends Change {
  181.  
  182.     public Growth(String id) {
  183.         super(id);
  184.     }
  185.  
  186.     @Override
  187.     public void verifyChangeOn(Code code) {
  188.         // TODO: Add verification logic here.
  189.     }
  190.  
  191.     // some code
  192. }
  193.  
  194. public class Improvement extends Change {
  195.  
  196.     public Improvement(String id) {
  197.         super(id);
  198.     }
  199.  
  200.     @Override
  201.     public void verifyChangeOn(Code code) {
  202.         // TODO: Add verification logic here.
  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 MockUnsuportedChange extends Change {
  224.  
  225.     protected MockUnsuportedChange(String id) {
  226.         super(id);
  227.     }
  228.  
  229.     @Override
  230.     public void verifyChangeOn(Code code) {
  231.         // Verification "successful" in all cases.
  232.     }
  233.  
  234. }
  235.  
  236. public class MockVerifyChangeException extends RuntimeException {
  237. }
  238.  
  239. public class Refactoring extends Change {
  240.  
  241.     public Refactoring(String id) {
  242.         super(id);
  243.     }
  244.  
  245.     @Override
  246.     public void verifyChangeOn(Code code) {
  247.         // TODO: Add verification logic here.
  248.     }
  249.  
  250.     // some code
  251. }
  252.  
  253. public class UnsuportedChangeException extends RuntimeException {
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement