forked from cucumber/cucumber-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cucumber#965: First draft of a working solution
- Loading branch information
Showing
6 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
spring/src/main/java/cucumber/runtime/java/spring/GlueScopeConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cucumber.runtime.java.spring; | ||
|
||
import org.springframework.beans.factory.config.CustomScopeConfigurer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class GlueScopeConfig { | ||
|
||
@Bean | ||
public CustomScopeConfigurer glueScopeConfigurer(){ | ||
CustomScopeConfigurer toReturn = new CustomScopeConfigurer(); | ||
|
||
toReturn.addScope("cucumber-glue", new GlueCodeScope()); | ||
|
||
return toReturn; | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
spring/src/main/java/cucumber/runtime/java/spring/GlueScopeConfigEnhancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package cucumber.runtime.java.spring; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.asm.AnnotationVisitor; | ||
import org.springframework.asm.ClassReader; | ||
import org.springframework.asm.ClassVisitor; | ||
import org.springframework.asm.ClassWriter; | ||
import org.springframework.asm.Opcodes; | ||
import org.springframework.asm.Type; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
public class GlueScopeConfigEnhancer implements Opcodes{ | ||
|
||
private class ASMClassLoader extends ClassLoader{ | ||
public Class defineClass(String name, byte[] content){ | ||
return defineClass(name, content, 0, content.length); | ||
} | ||
} | ||
|
||
public Class enhance(Class class1) { | ||
try { | ||
String className = Type.getInternalName(class1); | ||
|
||
ClassWriter cw = new ClassWriter(0); | ||
|
||
ContextConfigurationClassVisitor contextCv = new ContextConfigurationClassVisitor(cw); | ||
|
||
ClassReader cr = new ClassReader(className); | ||
cr.accept(contextCv, 0); | ||
|
||
return new ASMClassLoader().defineClass(className.replace('/', '.'), cw.toByteArray()); | ||
|
||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public class ContextConfigurationClassVisitor extends ClassVisitor{ | ||
public ContextConfigurationClassVisitor(ClassVisitor cv){ | ||
super(ASM5, cv); | ||
} | ||
|
||
@Override | ||
public AnnotationVisitor visitAnnotation(String desc, boolean visible) { | ||
if(Type.getDescriptor(ContextConfiguration.class).equals(desc)){ | ||
return new ContextConfigurationAnnotationVisitor(super.visitAnnotation(desc, visible)); | ||
} | ||
return super.visitAnnotation(desc, visible); | ||
} | ||
|
||
} | ||
|
||
public class ContextConfigurationAnnotationVisitor extends AnnotationVisitor { | ||
|
||
public ContextConfigurationAnnotationVisitor(AnnotationVisitor av){ | ||
super(ASM5, av); | ||
} | ||
|
||
@Override | ||
public AnnotationVisitor visitArray(String name) { | ||
if("classes".equals(name)){ | ||
return new ContextConfigurationAnnotationArrayVisitor(super.visitArray(name)); | ||
} | ||
return super.visitArray(name); | ||
} | ||
} | ||
|
||
public class ContextConfigurationAnnotationArrayVisitor extends AnnotationVisitor { | ||
|
||
public ContextConfigurationAnnotationArrayVisitor(AnnotationVisitor av){ | ||
super(ASM5, av); | ||
} | ||
|
||
@Override | ||
public void visit(String name, Object value) { | ||
super.visit(name, value); | ||
} | ||
|
||
@Override | ||
public void visitEnd() { | ||
visit(null, Type.getType(GlueScopeConfig.class)); | ||
super.visitEnd(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
spring/src/test/java/cucumber/runtime/java/spring/GlueScopeConfigEnhancerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cucumber.runtime.java.spring; | ||
|
||
import java.io.PrintWriter; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.mockito.asm.ClassReader; | ||
import org.mockito.asm.ClassWriter; | ||
import org.mockito.asm.Type; | ||
import org.mockito.asm.util.ASMifierClassVisitor; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
public class GlueScopeConfigEnhancerTest { | ||
|
||
@Test | ||
public void stepWithContextConfigurationAnnotationAndValue_enhance_GlueScopeConfigAdded() { | ||
GlueScopeConfigEnhancer underTest = new GlueScopeConfigEnhancer(); | ||
|
||
Class enhanced = underTest.enhance(StepWithContexttConfigurationAnnotationAndValue.class); | ||
|
||
Assert.assertNotNull(enhanced); | ||
|
||
ContextConfiguration annotation = (ContextConfiguration) enhanced.getAnnotation(ContextConfiguration.class); | ||
|
||
Assert.assertNotNull(annotation); | ||
Assert.assertEquals(GlueScopeConfig.class, annotation.classes()[annotation.classes().length-1]); | ||
} | ||
|
||
|
||
@ContextConfiguration(classes=Object.class) | ||
public static class StepWithContexttConfigurationAnnotationAndValue { | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters