Advertisement
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 class ChangeProcessingHandler {
- public CodeDelta triggerProcessingChangeOf(Code code, Change change) {
- verifyChangeOf(code, change);
- if (change instanceof Refactoring) {
- return processRefactoring(code, (Refactoring) change);
- } else if (change instanceof Improvement) {
- return processImprovement(code, (Improvement) change);
- } else if (change instanceof Growth) {
- return processGrowth(code, (Growth) change);
- } else {
- throw new UnsuportedChangeException();
- }
- }
- private void verifyChangeOf(Code code, Change change) {
- // TODO: Add verification logic here.
- change.verifyChangeOn(code);
- }
- private CodeDelta processRefactoring(Code code, Refactoring change) {
- return new CodeDelta(code.toString() + ", " + change.toString());
- }
- private CodeDelta processImprovement(Code code, Improvement change) {
- return new CodeDelta(code.toString() + ", " + change.toString());
- }
- private CodeDelta processGrowth(Code code, Growth change) {
- return new CodeDelta(code.toString() + ", " + change.toString());
- }
- // some more 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 void testMockUnsuportedChange() {
- MockUnsuportedChange improvement = new MockUnsuportedChange("my test Improvement instance for " + this.getName());
- ChangeProcessingHandler handler = new ChangeProcessingHandler();
- Code code = new MockCode("my Code for " + this.getName());
- try {
- handler.triggerProcessingChangeOf(code, improvement);
- fail("Expecting UnsuportedChangeException.");
- } catch (UnsuportedChangeException unsuportedChangeException) {
- // Expected path. Nothing more to verify.
- }
- }
- }
- 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.
- }
- // some code
- }
- public class Improvement extends Change {
- public Improvement(String id) {
- super(id);
- }
- @Override
- public void verifyChangeOn(Code code) {
- // TODO: Add verification logic here.
- }
- // 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 MockUnsuportedChange extends Change {
- protected MockUnsuportedChange(String id) {
- super(id);
- }
- @Override
- public void verifyChangeOn(Code code) {
- // Verification "successful" in all cases.
- }
- }
- 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.
- }
- // some code
- }
- public class UnsuportedChangeException extends RuntimeException {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement