InsertableMethodBuilder { public StateTargetClassSet insertIntoClass(Class<?> clazzToInsertInto) throws NotFoundException { this.classToInsertInto = ClassPool.getDefault().get( clazzToInsertInto.getName()); return new StateTargetClassSet(); } InsertableMethodBuilder(AfterBurner afterBurner);  InsertableMethodBuilder(AfterBurner afterBurner,
            CtMethodJavaWriter signatureExtractor); StateTargetClassSet insertIntoClass(Class<?> clazzToInsertInto); StateTargetClassSet insertIntoClass(CtClass classToInsertInto);  }
@Test public void testDoIt_calls_afterburner() throws CannotCompileException, AfterBurnerImpossibleException { afterBurnerMock = EasyMock.createMock(AfterBurner.class); afterBurnerMock.addOrInsertMethod((InsertableMethod) EasyMock.anyObject()); EasyMock.replay(afterBurnerMock); builder = new InsertableMethodBuilder(afterBurnerMock, null); CtClass classToInsertInto = CtClass.intType; String targetMethod = ""; String insertionAfterMethod = ""; String fullMethod = ""; String body = ""; builder .insertIntoClass(classToInsertInto) .inMethodIfExists(targetMethod) .afterACallTo(insertionAfterMethod) .withBody(body) .elseCreateMethodIfNotExists(fullMethod) .doIt(); EasyMock.verify(afterBurnerMock); }
@Test public void testDoIt_calls_afterburner_with_after_override() throws CannotCompileException, AfterBurnerImpossibleException, NotFoundException { afterBurnerMock = EasyMock.createMock(AfterBurner.class); afterBurnerMock.addOrInsertMethod((InsertableMethod) EasyMock.anyObject()); EasyMock.replay(afterBurnerMock); signatureExtractorMock = EasyMock.createMock(CtMethodJavaWriter.class); EasyMock.expect(signatureExtractorMock.invokeSuper((CtMethod) EasyMock.anyObject())).andReturn("super.foo()"); EasyMock.expect(signatureExtractorMock.createJavaSignature((CtMethod) EasyMock.anyObject())).andReturn("public void foo()"); EasyMock.replay(signatureExtractorMock); CtClass targetClassAncestor = ClassPool.getDefault().makeClass( "TargetAncestor" + TestCounter.testCounter++); targetClassAncestor.addMethod(CtNewMethod.make("public void foo() { }", targetClassAncestor)); CtClass targetClass = ClassPool.getDefault().makeClass( "Target" + TestCounter.testCounter++); targetClass.setSuperclass(targetClassAncestor); targetClass.addMethod(CtNewMethod.make("public void foo() { super.foo(); }", targetClass)); builder = new InsertableMethodBuilder(afterBurnerMock, signatureExtractorMock); CtClass classToInsertInto = targetClass; String targetMethod = "foo"; String body = ""; builder .insertIntoClass(classToInsertInto) .afterOverrideMethod(targetMethod) .withBody(body) .doIt(); EasyMock.verify(afterBurnerMock); }
@Test public void testDoIt_calls_afterburner_with_after_override_when_no_override() throws CannotCompileException, AfterBurnerImpossibleException, NotFoundException { afterBurnerMock = EasyMock.createMock(AfterBurner.class); afterBurnerMock.addOrInsertMethod((InsertableMethod) EasyMock.anyObject()); EasyMock.replay(afterBurnerMock); signatureExtractorMock = EasyMock.createMock(CtMethodJavaWriter.class); EasyMock.expect(signatureExtractorMock.invokeSuper((CtMethod) EasyMock.anyObject())).andReturn("super.foo()"); EasyMock.expect(signatureExtractorMock.createJavaSignature((CtMethod) EasyMock.anyObject())).andReturn("public void foo()"); EasyMock.replay(signatureExtractorMock); CtClass targetClassAncestor = ClassPool.getDefault().makeClass( "TargetAncestor" + TestCounter.testCounter++); targetClassAncestor.addMethod(CtNewMethod.make("public void foo() { }", targetClassAncestor)); CtClass targetClass = ClassPool.getDefault().makeClass( "Target" + TestCounter.testCounter++); targetClass.setSuperclass(targetClassAncestor); builder = new InsertableMethodBuilder(afterBurnerMock, signatureExtractorMock); CtClass classToInsertInto = targetClass; String targetMethod = "foo"; String body = ""; builder .insertIntoClass(classToInsertInto) .afterOverrideMethod(targetMethod) .withBody(body) .doIt(); EasyMock.verify(afterBurnerMock); }
@Test(expected=NotFoundException.class) public void testDoIt_calls_afterburner_with_after_override_when_no_method() throws CannotCompileException, AfterBurnerImpossibleException, NotFoundException { afterBurnerMock = EasyMock.createMock(AfterBurner.class); CtClass targetClass = ClassPool.getDefault().makeClass( "Target" + TestCounter.testCounter++); builder = new InsertableMethodBuilder(afterBurnerMock, signatureExtractorMock); CtClass classToInsertInto = targetClass; String targetMethod = "foo"; String body = ""; builder .insertIntoClass(classToInsertInto.toClass()) .afterOverrideMethod(targetMethod) .withBody(body) .doIt(); fail(); }
@Test public void testDoIt_calls_afterburner_with_before_override() throws CannotCompileException, AfterBurnerImpossibleException, NotFoundException { afterBurnerMock = EasyMock.createMock(AfterBurner.class); afterBurnerMock.addOrInsertMethod((InsertableMethod) EasyMock.anyObject()); EasyMock.replay(afterBurnerMock); signatureExtractorMock = EasyMock.createMock(CtMethodJavaWriter.class); EasyMock.expect(signatureExtractorMock.invokeSuper((CtMethod) EasyMock.anyObject())).andReturn("super.foo()"); EasyMock.expect(signatureExtractorMock.createJavaSignature((CtMethod) EasyMock.anyObject())).andReturn("public void foo()"); EasyMock.replay(signatureExtractorMock); CtClass targetClassAncestor = ClassPool.getDefault().makeClass( "TargetAncestor" + TestCounter.testCounter++); targetClassAncestor.addConstructor(CtNewConstructor.make("public " + targetClassAncestor.getName()+ "() { }", targetClassAncestor)); targetClassAncestor.addMethod(CtNewMethod.make("public void foo() { }", targetClassAncestor)); CtClass targetClass = ClassPool.getDefault().makeClass( "Target" + TestCounter.testCounter++); targetClass.setSuperclass(targetClassAncestor); targetClass.addMethod(CtNewMethod.make("public void foo() { super.foo(); }", targetClass)); targetClassAncestor.toClass(); builder = new InsertableMethodBuilder(afterBurnerMock, signatureExtractorMock); CtClass classToInsertInto = targetClass; String targetMethod = "foo"; String body = ""; builder .insertIntoClass(classToInsertInto.toClass()) .beforeOverrideMethod(targetMethod) .withBody(body) .doIt(); EasyMock.verify(afterBurnerMock); }
@Test public void testDoIt_calls_afterburner_with_before_override_when_no_override() throws CannotCompileException, AfterBurnerImpossibleException, NotFoundException { afterBurnerMock = EasyMock.createMock(AfterBurner.class); afterBurnerMock.addOrInsertMethod((InsertableMethod) EasyMock.anyObject()); EasyMock.replay(afterBurnerMock); signatureExtractorMock = EasyMock.createMock(CtMethodJavaWriter.class); EasyMock.expect(signatureExtractorMock.invokeSuper((CtMethod) EasyMock.anyObject())).andReturn("super.foo()"); EasyMock.expect(signatureExtractorMock.createJavaSignature((CtMethod) EasyMock.anyObject())).andReturn("public void foo()"); EasyMock.replay(signatureExtractorMock); CtClass targetClassAncestor = ClassPool.getDefault().makeClass( "TargetAncestor" + TestCounter.testCounter++); targetClassAncestor.addConstructor(CtNewConstructor.make("public " + targetClassAncestor.getName()+ "() { }", targetClassAncestor)); targetClassAncestor.addMethod(CtNewMethod.make("public void foo() { }", targetClassAncestor)); CtClass targetClass = ClassPool.getDefault().makeClass( "Target" + TestCounter.testCounter++); targetClass.setSuperclass(targetClassAncestor); targetClassAncestor.toClass(); builder = new InsertableMethodBuilder(afterBurnerMock, signatureExtractorMock); CtClass classToInsertInto = targetClass; String targetMethod = "foo"; String body = ""; builder .insertIntoClass(classToInsertInto.toClass()) .beforeOverrideMethod(targetMethod) .withBody(body) .doIt(); EasyMock.verify(afterBurnerMock); }
@Test(expected=NotFoundException.class) public void testDoIt_calls_afterburner_with_before_override_when_no_method() throws CannotCompileException, AfterBurnerImpossibleException, NotFoundException { afterBurnerMock = EasyMock.createMock(AfterBurner.class); CtClass targetClass = ClassPool.getDefault().makeClass( "Target" + TestCounter.testCounter++); builder = new InsertableMethodBuilder(afterBurnerMock, signatureExtractorMock); CtClass classToInsertInto = targetClass; String targetMethod = "foo"; String body = ""; builder .insertIntoClass(classToInsertInto.toClass()) .beforeOverrideMethod(targetMethod) .withBody(body) .doIt(); fail(); }
@Test public void testCheckAllFields_should_succeed_with_insert_after_method_defined() throws AfterBurnerImpossibleException { CtClass classToInsertInto = CtClass.intType; String targetMethod = ""; String insertionAfterMethod = ""; String fullMethod = ""; String body = ""; InsertableMethod method = builder .insertIntoClass(classToInsertInto) .inMethodIfExists(targetMethod) .afterACallTo(insertionAfterMethod) .withBody(body) .elseCreateMethodIfNotExists(fullMethod) .createInsertableMethod(); assertNotNull(method); assertEquals(classToInsertInto, method.getClassToInsertInto()); assertEquals(targetMethod, method.getTargetMethodName()); assertNull(method.getInsertionBeforeMethod()); assertEquals(insertionAfterMethod, method.getInsertionAfterMethod()); assertEquals(fullMethod, method.getFullMethod()); assertEquals(body, method.getBody()); }
@Test public void testCheckAllFields_should_succeed_with_insert_before_method_defined() throws AfterBurnerImpossibleException { CtClass classToInsertInto = CtClass.intType; String targetMethod = "target"; String insertionBeforeMethod = "insertionBeforeMethod"; String fullMethod = "fullMethod"; String body = "body"; InsertableMethod method = builder .insertIntoClass(classToInsertInto) .inMethodIfExists(targetMethod) .beforeACallTo(insertionBeforeMethod) .withBody(body) .elseCreateMethodIfNotExists(fullMethod) .createInsertableMethod(); assertNotNull(method); assertEquals(classToInsertInto, method.getClassToInsertInto()); assertEquals(targetMethod, method.getTargetMethodName()); assertEquals(insertionBeforeMethod, method.getInsertionBeforeMethod()); assertNull(method.getInsertionAfterMethod()); assertEquals(fullMethod, method.getFullMethod()); assertEquals(body, method.getBody()); }