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);
}
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");
}
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");
}
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);
}
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);
}
Aggregations