use of uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil in project microservice_framework by CJSCommonPlatform.
the class SubscriptionMediaTypeToSchemaIdMapperClassBuilderTest method writeSourceFileAndCompile.
@SuppressWarnings("ConstantConditions")
private Class<?> writeSourceFileAndCompile(final String packageName, final TypeSpec typeSpec) throws IOException {
final File outputFolderRoot = outputFolder.getRoot();
JavaFile.builder(packageName, typeSpec).build().writeTo(outputFolderRoot);
return new JavaCompilerUtil(outputFolderRoot, outputFolderRoot).compiledClassesOf(packageName).stream().filter(clazz -> !clazz.getName().equals("java.lang.Object")).findFirst().orElseGet(null);
}
use of uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil in project microservice_framework by CJSCommonPlatform.
the class EventListenerInterceptorChainProviderCodeGeneratorTest method shouldGenerateAWorkingEventListenerInterceptorChainProviderWithTheCorrectInterceptorChainEntiresAndComponentName.
@Test
public void shouldGenerateAWorkingEventListenerInterceptorChainProviderWithTheCorrectInterceptorChainEntiresAndComponentName() throws Exception {
final String componentName = "MY_CUSTOM_EVENT_LISTENER";
final String packageName = "uk.gov.justice.api.interceptor.filter";
final String simpleName = "MyCustomEventListenerInterceptorChainProvider";
final ClassName eventListenerInterceptorChainProviderClassName = ClassName.get(packageName, simpleName);
final ClassName eventFilterInterceptorClassName = ClassName.get(StubEventFilterInterceptor.class);
final ClassNameFactory classNameFactory = mock(ClassNameFactory.class);
when(classNameFactory.classNameFor(EVENT_LISTENER_INTERCEPTOR_CHAIN_PROVIDER)).thenReturn(eventListenerInterceptorChainProviderClassName);
when(classNameFactory.classNameFor(EVENT_FILTER_INTERCEPTOR)).thenReturn(eventFilterInterceptorClassName);
final TypeSpec typeSpec = eventListenerInterceptorChainProviderCodeGenerator.generate(componentName, classNameFactory);
final File codeGenerationOutputDirectory = getDirectory(CODE_GENERATION_OUTPUT_DIRECTORY);
final File compilationOutputDirectory = getDirectory(COMPILATION_OUTPUT_DIRECTORY);
builder(packageName, typeSpec).build().writeTo(codeGenerationOutputDirectory);
final JavaCompilerUtil compiler = new JavaCompilerUtil(codeGenerationOutputDirectory, compilationOutputDirectory);
final Class<?> compiledClass = compiler.compiledClassOf(packageName, simpleName);
final InterceptorChainEntryProvider interceptorChainEntryProvider = (InterceptorChainEntryProvider) compiledClass.newInstance();
assertThat(interceptorChainEntryProvider.component(), is(componentName));
final List<InterceptorChainEntry> interceptorChainEntries = interceptorChainEntryProvider.interceptorChainTypes();
assertThat(interceptorChainEntries, hasItem(new InterceptorChainEntry(1000, EventBufferInterceptor.class)));
assertThat(interceptorChainEntries, hasItem(new InterceptorChainEntry(2000, StubEventFilterInterceptor.class)));
}
use of uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil in project microservice_framework by CJSCommonPlatform.
the class ActionNameToMediaTypesMapperClassBuilderTest method writeSourceFileAndCompile.
private Class<?> writeSourceFileAndCompile(final String packageName, final TypeSpec typeSpec) throws IOException {
final File outputFolderRoot = outputFolder.getRoot();
JavaFile.builder(packageName, typeSpec).build().writeTo(outputFolderRoot);
return new JavaCompilerUtil(outputFolderRoot, outputFolderRoot).compiledClassesOf(packageName).stream().filter(clazz -> !clazz.getName().equals("java.lang.Object")).findFirst().orElseGet(null);
}
use of uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil in project microservice_framework by CJSCommonPlatform.
the class DynamicAggregateTestClassGenerator method generatedTestAggregateClassOf.
public <T extends Aggregate> Class<T> generatedTestAggregateClassOf(long serialVersionUid, String basePackage, String fileNameToBeGenerated) throws IOException, ClassNotFoundException {
final MethodSpec constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).build();
final MethodSpec apply = MethodSpec.methodBuilder("apply").addModifiers(Modifier.PUBLIC).addCode(" return match(event).with(\n" + " when(uk.gov.justice.domain.event.EventA.class).apply(x -> {\n" + " ++count;\n" + " }\n" + " ));").returns(Object.class).addAnnotation(Override.class).addParameter(Object.class, "event").build();
final MethodSpec applyObjectStream = MethodSpec.methodBuilder("apply").addModifiers(Modifier.PUBLIC).addCode(" return events\n" + " .map(this::apply)\n" + " .collect(toList())\n" + " .stream();").addAnnotation(Override.class).returns(ParameterizedTypeName.get(Stream.class, Object.class)).addParameter(ParameterizedTypeName.get(Stream.class, Object.class), "events").build();
final FieldSpec.Builder fieldSpecSerialVersionId = FieldSpec.builder(TypeName.LONG.unbox(), "serialVersionUID", Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL);
fieldSpecSerialVersionId.initializer(serialVersionUid + "L");
final FieldSpec.Builder fieldSpecCount = FieldSpec.builder(TypeName.INT.unbox(), "count", Modifier.PRIVATE);
final TypeSpec dynamicTestAggregate = TypeSpec.classBuilder(fileNameToBeGenerated).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(constructor).addField(fieldSpecSerialVersionId.build()).addField(fieldSpecCount.build()).addMethod(apply).addMethod(applyObjectStream).addSuperinterface(Aggregate.class).build();
final ClassName eventSwitcherMatch = ClassName.get("uk.gov.justice.domain.aggregate", "matcher", "EventSwitcher");
final ClassName eventSwitcherWhen = ClassName.get("uk.gov.justice.domain.aggregate", "matcher", "EventSwitcher");
final ClassName toList = ClassName.get("java.util", "stream", "Collectors");
final JavaFile javaFile = JavaFile.builder(basePackage, dynamicTestAggregate).addStaticImport(eventSwitcherMatch, "match").addStaticImport(eventSwitcherWhen, "when").addStaticImport(toList, "toList").build();
final String path = DynamicAggregateTestClassGenerator.class.getClassLoader().getResource("").getPath();
final File pathRoot = new File(path);
javaFile.writeTo(pathRoot);
javaFile.writeTo(System.out);
final JavaCompilerUtil compiler = new JavaCompilerUtil(pathRoot, pathRoot);
final Class<?> generatedClass = compiler.compiledClassOf(basePackage, fileNameToBeGenerated);
return (Class<T>) generatedClass;
}
Aggregations