Search in sources :

Example 1 with TestCompiler

use of org.springframework.boot.testsupport.compiler.TestCompiler in project spring-boot by spring-projects.

the class PropertyDescriptorResolverTests method process.

private void process(Class<?> target, Collection<Class<?>> additionalClasses, BiConsumer<TypeElement, MetadataGenerationEnvironment> consumer) throws IOException {
    BiConsumer<RoundEnvironmentTester, MetadataGenerationEnvironment> internalConsumer = (roundEnv, metadataEnv) -> {
        TypeElement element = roundEnv.getRootElement(target);
        consumer.accept(element, metadataEnv);
    };
    TestableAnnotationProcessor<MetadataGenerationEnvironment> processor = new TestableAnnotationProcessor<>(internalConsumer, new MetadataGenerationEnvironmentFactory());
    TestCompiler compiler = new TestCompiler(this.tempDir);
    ArrayList<Class<?>> allClasses = new ArrayList<>();
    allClasses.add(target);
    allClasses.addAll(additionalClasses);
    compiler.getTask(allClasses.toArray(new Class<?>[0])).call(processor);
}
Also used : HierarchicalProperties(org.springframework.boot.configurationsample.simple.HierarchicalProperties) Arrays(java.util.Arrays) RoundEnvironmentTester(org.springframework.boot.configurationprocessor.test.RoundEnvironmentTester) HierarchicalPropertiesParent(org.springframework.boot.configurationsample.simple.HierarchicalPropertiesParent) ImmutableClassConstructorBindingProperties(org.springframework.boot.configurationsample.immutable.ImmutableClassConstructorBindingProperties) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestableAnnotationProcessor(org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor) SimpleProperties(org.springframework.boot.configurationsample.simple.SimpleProperties) TypeElement(javax.lang.model.element.TypeElement) ImmutableMultiConstructorProperties(org.springframework.boot.configurationsample.immutable.ImmutableMultiConstructorProperties) ArrayList(java.util.ArrayList) ItemMetadata(org.springframework.boot.configurationprocessor.metadata.ItemMetadata) AutowiredProperties(org.springframework.boot.configurationsample.simple.AutowiredProperties) BiConsumer(java.util.function.BiConsumer) TwoConstructorsExample(org.springframework.boot.configurationsample.specific.TwoConstructorsExample) TestCompiler(org.springframework.boot.testsupport.compiler.TestCompiler) ImmutableNameAnnotationProperties(org.springframework.boot.configurationsample.immutable.ImmutableNameAnnotationProperties) ImmutableDeducedConstructorBindingProperties(org.springframework.boot.configurationsample.immutable.ImmutableDeducedConstructorBindingProperties) LombokExplicitProperties(org.springframework.boot.configurationsample.lombok.LombokExplicitProperties) Collection(java.util.Collection) ImmutableSimpleProperties(org.springframework.boot.configurationsample.immutable.ImmutableSimpleProperties) IOException(java.io.IOException) LombokSimpleValueProperties(org.springframework.boot.configurationsample.lombok.LombokSimpleValueProperties) HierarchicalPropertiesGrandparent(org.springframework.boot.configurationsample.simple.HierarchicalPropertiesGrandparent) File(java.io.File) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Stream(java.util.stream.Stream) TempDir(org.junit.jupiter.api.io.TempDir) LombokSimpleProperties(org.springframework.boot.configurationsample.lombok.LombokSimpleProperties) Collections(java.util.Collections) LombokSimpleDataProperties(org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties) TestableAnnotationProcessor(org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor) TestCompiler(org.springframework.boot.testsupport.compiler.TestCompiler) TypeElement(javax.lang.model.element.TypeElement) RoundEnvironmentTester(org.springframework.boot.configurationprocessor.test.RoundEnvironmentTester) ArrayList(java.util.ArrayList)

Example 2 with TestCompiler

use of org.springframework.boot.testsupport.compiler.TestCompiler in project spring-boot by spring-projects.

the class JarLauncherTests method explodedJarDefinedPackagesIncludeManifestAttributes.

@Test
void explodedJarDefinedPackagesIncludeManifestAttributes() throws Exception {
    Manifest manifest = new Manifest();
    Attributes attributes = manifest.getMainAttributes();
    attributes.put(Name.MANIFEST_VERSION, "1.0");
    attributes.put(Name.IMPLEMENTATION_TITLE, "test");
    File explodedRoot = explode(createJarArchive("archive.jar", manifest, "BOOT-INF", true, Collections.emptyList()));
    TestCompiler compiler = new TestCompiler(new File(explodedRoot, "BOOT-INF/classes"));
    File source = new File(this.tempDir, "explodedsample/ExampleClass.java");
    source.getParentFile().mkdirs();
    FileCopyUtils.copy(new File("src/test/resources/explodedsample/ExampleClass.txt"), source);
    compiler.getTask(Collections.singleton(source)).call();
    JarLauncher launcher = new JarLauncher(new ExplodedArchive(explodedRoot, true));
    Iterator<Archive> archives = launcher.getClassPathArchivesIterator();
    URLClassLoader classLoader = (URLClassLoader) launcher.createClassLoader(archives);
    Class<?> loaded = classLoader.loadClass("explodedsample.ExampleClass");
    assertThat(loaded.getPackage().getImplementationTitle()).isEqualTo("test");
}
Also used : ExplodedArchive(org.springframework.boot.loader.archive.ExplodedArchive) TestCompiler(org.springframework.boot.testsupport.compiler.TestCompiler) JarFileArchive(org.springframework.boot.loader.archive.JarFileArchive) Archive(org.springframework.boot.loader.archive.Archive) ExplodedArchive(org.springframework.boot.loader.archive.ExplodedArchive) URLClassLoader(java.net.URLClassLoader) Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with TestCompiler

use of org.springframework.boot.testsupport.compiler.TestCompiler in project spring-boot by spring-projects.

the class ValueObjectBinderTests method bindToRecordWithDefaultValue.

@Test
void bindToRecordWithDefaultValue(@TempDir File tempDir) throws IOException, ClassNotFoundException {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("test.record.property1", "value-from-config-1");
    this.sources.add(source);
    File recordProperties = new File(tempDir, "RecordProperties.java");
    try (PrintWriter writer = new PrintWriter(new FileWriter(recordProperties))) {
        writer.println("public record RecordProperties(");
        writer.println("@org.springframework.boot.context.properties.bind.DefaultValue(\"default-value-1\") String property1,");
        writer.println("@org.springframework.boot.context.properties.bind.DefaultValue(\"default-value-2\") String property2");
        writer.println(") {");
        writer.println("}");
    }
    TestCompiler compiler = new TestCompiler(tempDir);
    compiler.getTask(Arrays.asList(recordProperties)).call();
    ClassLoader ucl = new URLClassLoader(new URL[] { tempDir.toURI().toURL() });
    Object bean = this.binder.bind("test.record", Class.forName("RecordProperties", true, ucl)).get();
    assertThat(bean).hasFieldOrPropertyWithValue("property1", "value-from-config-1").hasFieldOrPropertyWithValue("property2", "default-value-2");
}
Also used : TestCompiler(org.springframework.boot.testsupport.compiler.TestCompiler) FileWriter(java.io.FileWriter) URLClassLoader(java.net.URLClassLoader) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) URLClassLoader(java.net.URLClassLoader) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.jupiter.api.Test)

Example 4 with TestCompiler

use of org.springframework.boot.testsupport.compiler.TestCompiler in project spring-boot by spring-projects.

the class PropertyDescriptorTests method process.

protected void process(Class<?> target, BiConsumer<RoundEnvironmentTester, MetadataGenerationEnvironment> consumer) throws IOException {
    TestableAnnotationProcessor<MetadataGenerationEnvironment> processor = new TestableAnnotationProcessor<>(consumer, new MetadataGenerationEnvironmentFactory());
    TestCompiler compiler = new TestCompiler(this.tempDir);
    compiler.getTask(target).call(processor);
}
Also used : TestableAnnotationProcessor(org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor) TestCompiler(org.springframework.boot.testsupport.compiler.TestCompiler)

Example 5 with TestCompiler

use of org.springframework.boot.testsupport.compiler.TestCompiler in project spring-boot by spring-projects.

the class TypeUtilsTests method process.

private void process(Class<?> target, BiConsumer<RoundEnvironmentTester, TypeUtils> consumer) throws IOException {
    TestableAnnotationProcessor<TypeUtils> processor = new TestableAnnotationProcessor<>(consumer, TypeUtils::new);
    TestCompiler compiler = new TestCompiler(this.tempDir);
    compiler.getTask(target).call(processor);
}
Also used : TestableAnnotationProcessor(org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor) TestCompiler(org.springframework.boot.testsupport.compiler.TestCompiler)

Aggregations

TestCompiler (org.springframework.boot.testsupport.compiler.TestCompiler)6 Test (org.junit.jupiter.api.Test)4 File (java.io.File)3 TestableAnnotationProcessor (org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor)3 URLClassLoader (java.net.URLClassLoader)2 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 Attributes (java.util.jar.Attributes)1 Manifest (java.util.jar.Manifest)1 Stream (java.util.stream.Stream)1 TypeElement (javax.lang.model.element.TypeElement)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 TempDir (org.junit.jupiter.api.io.TempDir)1