Polish "Add module to support testing of generated code"

See gh-28120

Co-authored-by: Andy Wilkinson <wilkinsona@vmware.com>
This commit is contained in:
Stephane Nicoll
2022-03-02 07:55:06 +01:00
parent 653dc5951d
commit 7255a8b48e
16 changed files with 146 additions and 47 deletions

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example;
class PackagePrivate {
String perform() {
return "Hello from PackagePrivate";
}
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example;
public interface PublicInterface {
String perform();
}

View File

@@ -18,6 +18,7 @@ package org.springframework.aot.test.generator.compile;
import java.util.function.Supplier;
import com.example.PublicInterface;
import org.junit.jupiter.api.Test;
import org.springframework.aot.test.generator.file.ResourceFile;
@@ -33,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* Tests for {@link TestCompiler}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
class TestCompilerTests {
@@ -106,7 +108,7 @@ class TestCompilerTests {
assertThatExceptionOfType(CompilationException.class).isThrownBy(
() -> TestCompiler.forSystem().withSources(
SourceFile.of(HELLO_BAD)).compile(compiled -> {
}));
}));
}
@Test
@@ -167,6 +169,24 @@ class TestCompilerTests {
});
}
@Test
void compiledCodeCanAccessExistingPackagePrivateClass() {
SourceFiles sourceFiles = SourceFiles.of(SourceFile.of("""
package com.example;
public class Test implements PublicInterface {
public String perform() {
return new PackagePrivate().perform();
}
}
"""));
TestCompiler.forSystem().compile(sourceFiles, compiled -> assertThat(
compiled.getInstance(PublicInterface.class, "com.example.Test").perform())
.isEqualTo("Hello from PackagePrivate"));
}
private void assertSuppliesHelloWorld(Compiled compiled) {
assertThat(compiled.getInstance(Supplier.class).get()).isEqualTo("Hello World!");
}

View File

@@ -22,7 +22,6 @@ import com.thoughtworks.qdox.model.JavaSource;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
@@ -42,13 +41,6 @@ class SourceFileTests {
}
""";
@Test
void ofWhenContentIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(
() -> SourceFile.of((WritableContent) null)).withMessage(
"'writableContent' must not to be empty");
}
@Test
void ofWhenContentIsEmptyThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> SourceFile.of("")).withMessage(