Search in sources :

Example 6 with JavaCompilerUtil

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);
}
Also used : JavaCompilerUtil(uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile)

Example 7 with JavaCompilerUtil

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)));
}
Also used : JavaCompilerUtil(uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil) ClassNameFactory(uk.gov.justice.subscription.jms.core.ClassNameFactory) ClassName(com.squareup.javapoet.ClassName) InterceptorChainEntryProvider(uk.gov.justice.services.core.interceptor.InterceptorChainEntryProvider) File(java.io.File) TypeSpec(com.squareup.javapoet.TypeSpec) InterceptorChainEntry(uk.gov.justice.services.core.interceptor.InterceptorChainEntry) Test(org.junit.Test)

Example 8 with JavaCompilerUtil

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);
}
Also used : JavaCompilerUtil(uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile)

Example 9 with JavaCompilerUtil

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;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) FieldSpec(com.squareup.javapoet.FieldSpec) JavaCompilerUtil(uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) Stream(java.util.stream.Stream) Aggregate(uk.gov.justice.domain.aggregate.Aggregate) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

JavaCompilerUtil (uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil)9 File (java.io.File)7 ClassName (com.squareup.javapoet.ClassName)4 JavaFile (com.squareup.javapoet.JavaFile)4 TypeSpec (com.squareup.javapoet.TypeSpec)4 Test (org.junit.Test)3 ClassNameFactory (uk.gov.justice.subscription.jms.core.ClassNameFactory)3 Before (org.junit.Before)2 FieldSpec (com.squareup.javapoet.FieldSpec)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 Stream (java.util.stream.Stream)1 Aggregate (uk.gov.justice.domain.aggregate.Aggregate)1 GeneratorPropertiesFactory (uk.gov.justice.raml.jms.config.GeneratorPropertiesFactory)1 InterceptorChainEntry (uk.gov.justice.services.core.interceptor.InterceptorChainEntry)1 InterceptorChainEntryProvider (uk.gov.justice.services.core.interceptor.InterceptorChainEntryProvider)1 HeaderConstants (uk.gov.justice.services.messaging.jms.HeaderConstants)1