Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class Change {
- private final String _id;
- protected Change(String id) {
- _id = id;
- }
- public abstract void verifyChangeOn(Code code);
- @Override
- public String toString() {
- return this.getClass().getSimpleName() + "(" + _id + ")";
- }
- public abstract CodeDelta accept(Visitator visitator);
- }
- public class ChangeProcessingHandler {
- public CodeDelta triggerProcessingChangeOf(Code code, Change change) {
- verifyChangeOf(code, change);
- return change.accept(new ChangeProcessor(code));
- }
- private void verifyChangeOf(Code code, Change change) {
- // TODO: Add verification logic here.
- change.verifyChangeOn(code);
- }
- }
- import junit.framework.TestCase;
- public class ChangeProcessingHandlerTest extends TestCase {
- public void testRefactoring() {
- ChangeProcessingHandler handler = new ChangeProcessingHandler();
- Refactoring refactoring = new Refactoring("my test Refactoring instance for " + this.getName());
- Code code = new MockCode("my Code for " + this.getName());
- CodeDelta delta = handler.triggerProcessingChangeOf(code, refactoring);
- assertEquals("CodeDelta(MockCode(my Code for testRefactoring), Refactoring(my test Refactoring instance for testRefactoring))", delta.toString());
- }
- public void testRefactoringError() {
- final MockVerifyChangeException mockVerifyChangeException = new MockVerifyChangeException();
- Refactoring refactoring = new Refactoring("my test Refactoring instance for " + this.getName()) {
- @Override
- public void verifyChangeOn(Code code) {
- throw mockVerifyChangeException;
- }
- };
- ChangeProcessingHandler handler = new ChangeProcessingHandler();
- Code code = new MockCode("my Code for " + this.getName());
- try {
- handler.triggerProcessingChangeOf(code, refactoring);
- fail("Expecting MockVerifyChangeException.");
- } catch (MockVerifyChangeException verifyChangeException) {
- assertSame(mockVerifyChangeException, verifyChangeException);
- }
- }
- public void testGrowth() {
- ChangeProcessingHandler handler = new ChangeProcessingHandler();
- Growth growth = new Growth("my test Growth instance for " + this.getName());
- Code code = new MockCode("my Code for " + this.getName());
- CodeDelta delta = handler.triggerProcessingChangeOf(code, growth);
- assertEquals("CodeDelta(MockCode(my Code for testGrowth), Growth(my test Growth instance for testGrowth))", delta.toString());
- }
- public void testGrowthError() {
- final MockVerifyChangeException mockVerifyChangeException = new MockVerifyChangeException();
- Growth growth = new Growth("my test Growth instance for " + this.getName()) {
- @Override
- public void verifyChangeOn(Code code) {
- throw mockVerifyChangeException;
- }
- };
- ChangeProcessingHandler handler = new ChangeProcessingHandler();
- Code code = new MockCode("my Code for " + this.getName());
- try {
- handler.triggerProcessingChangeOf(code, growth);
- fail("Expecting MockVerifyChangeException.");
- } catch (MockVerifyChangeException verifyChangeException) {
- assertSame(mockVerifyChangeException, verifyChangeException);
- }
- }
- public void testImprovement() {
- ChangeProcessingHandler handler = new ChangeProcessingHandler();
- Improvement improvement = new Improvement("my test Improvement instance for " + this.getName());
- Code code = new MockCode("my Code for " + this.getName());
- CodeDelta delta = handler.triggerProcessingChangeOf(code, improvement);
- assertEquals("CodeDelta(MockCode(my Code for testImprovement), Improvement(my test Improvement instance for testImprovement))", delta.toString());
- }
- public void testImprovementError() {
- final MockVerifyChangeException mockVerifyChangeException = new MockVerifyChangeException();
- Improvement improvement = new Improvement("my test Improvement instance for " + this.getName()) {
- @Override
- public void verifyChangeOn(Code code) {
- throw mockVerifyChangeException;
- }
- };
- ChangeProcessingHandler handler = new ChangeProcessingHandler();
- Code code = new MockCode("my Code for " + this.getName());
- try {
- handler.triggerProcessingChangeOf(code, improvement);
- fail("Expecting MockVerifyChangeException.");
- } catch (MockVerifyChangeException verifyChangeException) {
- assertSame(mockVerifyChangeException, verifyChangeException);
- }
- }
- }
- public class ChangeProcessor implements Visitator {
- private final Code code;
- public ChangeProcessor(Code code) {
- this.code = code;
- }
- @Override
- public CodeDelta visit(Refactoring refactoring) {
- return new CodeDelta(code.toString() + ", " + refactoring.toString());
- }
- @Override
- public CodeDelta visit(Improvement improvement) {
- return new CodeDelta(code.toString() + ", " + improvement.toString());
- }
- @Override
- public CodeDelta visit(Growth growth) {
- return new CodeDelta(code.toString() + ", " + growth.toString());
- }
- }
- public interface Code {
- }
- public class CodeDelta {
- private final String _id;
- public CodeDelta(String id) {
- _id = id;
- }
- @Override
- public String toString() {
- return "CodeDelta(" + _id + ")";
- }
- }
- public class Growth extends Change {
- public Growth(String id) {
- super(id);
- }
- @Override
- public void verifyChangeOn(Code code) {
- // TODO: Add verification logic here.
- }
- @Override
- public CodeDelta accept(Visitator visitator) {
- return visitator.visit(this);
- }
- // some code
- }
- public class Improvement extends Change {
- public Improvement(String id) {
- super(id);
- }
- @Override
- public void verifyChangeOn(Code code) {
- // TODO: Add verification logic here.
- }
- @Override
- public CodeDelta accept(Visitator visitator) {
- return visitator.visit(this);
- }
- // some code
- }
- public class MockCode implements Code {
- final String _id;
- public MockCode(String id) {
- _id = id;
- }
- @Override
- public String toString() {
- return "MockCode(" + _id + ")";
- }
- }
- public class MockVerifyChangeException extends RuntimeException {
- }
- public class Refactoring extends Change {
- public Refactoring(String id) {
- super(id);
- }
- @Override
- public void verifyChangeOn(Code code) {
- // TODO: Add verification logic here.
- }
- @Override
- public CodeDelta accept(Visitator visitator) {
- return visitator.visit(this);
- }
- // some code
- public interface Visitator {
- CodeDelta visit(Refactoring refactoring);
- CodeDelta visit(Improvement improvement);
- CodeDelta visit(Growth growth);
- }
Add Comment
Please, Sign In to add comment