Added checkstyle for tests
This commit is contained in:
@@ -43,7 +43,7 @@ public class CompilationMessage {
|
||||
this.sourceCode = sourceCode;
|
||||
this.startPosition = startPosition;
|
||||
this.endPosition = endPosition;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the type of message
|
||||
@@ -84,7 +84,7 @@ public class CompilationMessage {
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append("==========\n");
|
||||
if (this.sourceCode != null) { // Cannot include source context if no source
|
||||
// available
|
||||
// available
|
||||
int[] lineStartEnd = getLineStartEnd(this.startPosition);
|
||||
s.append(this.sourceCode.substring(lineStartEnd[0], lineStartEnd[1]))
|
||||
.append("\n");
|
||||
|
||||
@@ -201,7 +201,7 @@ public final class InMemoryJavaFileObject implements JavaFileObject {
|
||||
InMemoryJavaFileObject.this.lastModifiedTime = System.currentTimeMillis();
|
||||
InMemoryJavaFileObject.this.content = new String(toCharArray())
|
||||
.getBytes(); // Ignoring encoding...
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ public class JrtEntryJavaFileObject implements JavaFileObject {
|
||||
*/
|
||||
public JrtEntryJavaFileObject(Path path) {
|
||||
this.pathToClassString = path.subpath(2, path.getNameCount()).toString(); // e.g.
|
||||
// java/lang/Object.class
|
||||
// java/lang/Object.class
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
|
||||
@@ -127,8 +127,8 @@ public class JrtFsEnumeration implements Enumeration<JrtEntryJavaFileObject> {
|
||||
throws IOException {
|
||||
int fnc = file.getNameCount();
|
||||
if (fnc >= 3 && file.toString().endsWith(".class")) { // There is a preceeding
|
||||
// module name - e.g.
|
||||
// /modules/java.base/java/lang/Object.class
|
||||
// module name - e.g.
|
||||
// /modules/java.base/java/lang/Object.class
|
||||
// file.subpath(2, fnc); // e.g. java/lang/Object.class
|
||||
JrtFsEnumeration.this.jfos.add(new JrtEntryJavaFileObject(file));
|
||||
}
|
||||
|
||||
@@ -207,8 +207,8 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
|
||||
URL[] urls = loader.getURLs();
|
||||
if (urls.length > 1) { // heuristic that catches Maven surefire tests
|
||||
if (!urls[0].toString().startsWith("jar:file:")) { // heuristic for
|
||||
// Spring Boot fat
|
||||
// jar
|
||||
// Spring Boot fat
|
||||
// jar
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (URL url : urls) {
|
||||
if (builder.length() > 0) {
|
||||
@@ -627,7 +627,7 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
|
||||
if (file.getNameCount() > 3 && file.toString().endsWith(".class")) {
|
||||
int fnc = file.getNameCount();
|
||||
if (fnc > 3) { // There is a package name - e.g.
|
||||
// /modules/java.base/java/lang/Object.class
|
||||
// /modules/java.base/java/lang/Object.class
|
||||
Path packagePath = file.subpath(2, fnc - 1); // e.g. java/lang
|
||||
String packagePathString = packagePath.toString() + "/";
|
||||
CompilationInfoCache.this.packageCache.put(packagePathString,
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.function.compiler;
|
||||
|
||||
import java.io.File;
|
||||
@@ -42,15 +43,13 @@ import org.springframework.cloud.function.compiler.java.RuntimeJavaCompiler;
|
||||
import org.springframework.cloud.function.core.FunctionFactoryUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests that verify dependency resolution. Dependencies can be resolved against simple
|
||||
* classpath entries or against classes under BOOT-INF/classes or in a nested jar under
|
||||
* under BOOT-INF/lib. Finding classes in those locations enables compilation against a
|
||||
* packaged boot jar.
|
||||
*
|
||||
*
|
||||
* @author Andy Clement
|
||||
*/
|
||||
public class CompilerDependencyResolutionTests {
|
||||
@@ -60,7 +59,7 @@ public class CompilerDependencyResolutionTests {
|
||||
ClassDescriptor t1 = compile("Test1",
|
||||
"package com.test;\npublic class Test1 { public static String doit() { return \"T1\";}}\n");
|
||||
String result = (String) t1.clazz.getDeclaredMethod("doit").invoke(null);
|
||||
assertEquals("T1", result);
|
||||
assertThat(result).isEqualTo("T1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -140,12 +139,12 @@ public class CompilerDependencyResolutionTests {
|
||||
"public class A {\n" + " public static Object run() {\n"
|
||||
+ " return new TestX();\n" + " }\n" + "}",
|
||||
jar.toURI().toString());
|
||||
assertTrue("Should be no problems: " + result.getCompilationMessages(),
|
||||
result.getCompilationMessages().isEmpty());
|
||||
assertThat(result.getCompilationMessages().isEmpty())
|
||||
.as("Should be no problems: " + result.getCompilationMessages()).isTrue();
|
||||
try (URLClassLoader cl = new TestClassLoader(tx, descriptorFromResult(result))) {
|
||||
Class<?> class1 = cl.loadClass("A");
|
||||
Object invoke = class1.getDeclaredMethod("run").invoke(null);
|
||||
assertEquals(tx.name, invoke.getClass().getName());
|
||||
assertThat(invoke.getClass().getName()).isEqualTo(tx.name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,12 +161,12 @@ public class CompilerDependencyResolutionTests {
|
||||
"public class A {\n" + " public static Object run() {\n"
|
||||
+ " return new TestX();\n" + " }\n" + "}",
|
||||
jar.toURI().toString());
|
||||
assertTrue("Should be no problems: " + result.getCompilationMessages(),
|
||||
result.getCompilationMessages().isEmpty());
|
||||
assertThat(result.getCompilationMessages().isEmpty())
|
||||
.as("Should be no problems: " + result.getCompilationMessages()).isTrue();
|
||||
try (URLClassLoader cl = new TestClassLoader(t1, descriptorFromResult(result))) {
|
||||
Class<?> class1 = cl.loadClass("A");
|
||||
Object invoke = class1.getDeclaredMethod("run").invoke(null);
|
||||
assertEquals(t1.name, invoke.getClass().getName());
|
||||
assertThat(invoke.getClass().getName()).isEqualTo(t1.name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,12 +184,12 @@ public class CompilerDependencyResolutionTests {
|
||||
"public class A {\n" + " public static Object run() {\n"
|
||||
+ " return new TestX();\n" + " }\n" + "}",
|
||||
jar2.toURI().toString());
|
||||
assertTrue("Should be no problems: " + result.getCompilationMessages(),
|
||||
result.getCompilationMessages().isEmpty());
|
||||
assertThat(result.getCompilationMessages().isEmpty())
|
||||
.as("Should be no problems: " + result.getCompilationMessages()).isTrue();
|
||||
try (URLClassLoader cl = new TestClassLoader(t1, descriptorFromResult(result))) {
|
||||
Class<?> class1 = cl.loadClass("A");
|
||||
Object invoke = class1.getDeclaredMethod("run").invoke(null);
|
||||
assertEquals(t1.name, invoke.getClass().getName());
|
||||
assertThat(invoke.getClass().getName()).isEqualTo(t1.name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,12 +204,12 @@ public class CompilerDependencyResolutionTests {
|
||||
+ " public static Object run() {\n" + " return new Test1();\n"
|
||||
+ " }\n" + "}",
|
||||
jar.toURI().toString());
|
||||
assertTrue("Should be no problems: " + result.getCompilationMessages(),
|
||||
result.getCompilationMessages().isEmpty());
|
||||
assertThat(result.getCompilationMessages().isEmpty())
|
||||
.as("Should be no problems: " + result.getCompilationMessages()).isTrue();
|
||||
try (URLClassLoader cl = new TestClassLoader(t1, descriptorFromResult(result))) {
|
||||
Class<?> class1 = cl.loadClass("A");
|
||||
Object invoke = class1.getDeclaredMethod("run").invoke(null);
|
||||
assertEquals(t1.name, invoke.getClass().getName());
|
||||
assertThat(invoke.getClass().getName()).isEqualTo(t1.name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,12 +225,12 @@ public class CompilerDependencyResolutionTests {
|
||||
+ " public static Object run() {\n" + " return new Test1();\n"
|
||||
+ " }\n" + "}",
|
||||
jar.toURI().toString());
|
||||
assertTrue("Should be no problems: " + result.getCompilationMessages(),
|
||||
result.getCompilationMessages().isEmpty());
|
||||
assertThat(result.getCompilationMessages().isEmpty())
|
||||
.as("Should be no problems: " + result.getCompilationMessages()).isTrue();
|
||||
try (URLClassLoader cl = new TestClassLoader(t1, descriptorFromResult(result))) {
|
||||
Class<?> class1 = cl.loadClass("A");
|
||||
Object invoke = class1.getDeclaredMethod("run").invoke(null);
|
||||
assertEquals(t1.name, invoke.getClass().getName());
|
||||
assertThat(invoke.getClass().getName()).isEqualTo(t1.name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,12 +247,12 @@ public class CompilerDependencyResolutionTests {
|
||||
+ " public static Object run() {\n" + " return new Test1();\n"
|
||||
+ " }\n" + "}",
|
||||
jar2.toURI().toString());
|
||||
assertTrue("Should be no problems: " + result.getCompilationMessages(),
|
||||
result.getCompilationMessages().isEmpty());
|
||||
assertThat(result.getCompilationMessages().isEmpty())
|
||||
.as("Should be no problems: " + result.getCompilationMessages()).isTrue();
|
||||
try (URLClassLoader cl = new TestClassLoader(t1, descriptorFromResult(result))) {
|
||||
Class<?> class1 = cl.loadClass("A");
|
||||
Object invoke = class1.getDeclaredMethod("run").invoke(null);
|
||||
assertEquals(t1.name, invoke.getClass().getName());
|
||||
assertThat(invoke.getClass().getName()).isEqualTo(t1.name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,8 +267,8 @@ public class CompilerDependencyResolutionTests {
|
||||
private ClassDescriptor compile(String className, String classSourceCode) {
|
||||
CompilationResult compile = new RuntimeJavaCompiler().compile(className,
|
||||
classSourceCode);
|
||||
assertTrue("Should be empty: \n" + compile.getCompilationMessages(),
|
||||
compile.getCompilationMessages().isEmpty());
|
||||
assertThat(compile.getCompilationMessages().isEmpty())
|
||||
.as("Should be empty: \n" + compile.getCompilationMessages()).isTrue();
|
||||
Class<?> clazz = compile.getCompiledClasses().get(0);
|
||||
return new ClassDescriptor(clazz.getName(),
|
||||
compile.getClassBytes(clazz.getName()),
|
||||
@@ -299,7 +298,7 @@ public class CompilerDependencyResolutionTests {
|
||||
clazzes.add(prefix + classDescriptor.name.replace('.', '/') + ".class");
|
||||
}
|
||||
walkJar(jar, (entry) -> clazzes.remove(entry.getName()));
|
||||
assertTrue("Should be empty: " + clazzes, clazzes.isEmpty());
|
||||
assertThat(clazzes.isEmpty()).as("Should be empty: " + clazzes).isTrue();
|
||||
}
|
||||
|
||||
private void walkJar(File jar, Consumer<JarEntry> fn) {
|
||||
@@ -336,7 +335,7 @@ public class CompilerDependencyResolutionTests {
|
||||
|
||||
final Class<?> clazz;
|
||||
|
||||
public ClassDescriptor(String name, byte[] bytes, Class<?> clazz) {
|
||||
ClassDescriptor(String name, byte[] bytes, Class<?> clazz) {
|
||||
this.name = name;
|
||||
this.bytes = bytes;
|
||||
this.clazz = clazz;
|
||||
@@ -344,7 +343,7 @@ public class CompilerDependencyResolutionTests {
|
||||
|
||||
}
|
||||
|
||||
static class JarBuilder {
|
||||
static final class JarBuilder {
|
||||
|
||||
File jarFile;
|
||||
|
||||
@@ -446,7 +445,7 @@ public class CompilerDependencyResolutionTests {
|
||||
|
||||
ClassDescriptor[] descriptors;
|
||||
|
||||
public TestClassLoader(ClassDescriptor... descriptors) {
|
||||
TestClassLoader(ClassDescriptor... descriptors) {
|
||||
super(new URL[0], TestClassLoader.class.getClassLoader());
|
||||
this.descriptors = descriptors;
|
||||
}
|
||||
|
||||
@@ -23,8 +23,7 @@ import java.util.function.Supplier;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Andy Clement
|
||||
@@ -37,7 +36,7 @@ public class RuntimeJavaCompilerTests {
|
||||
RuntimeJavaCompiler rjc = new RuntimeJavaCompiler();
|
||||
CompilationResult cr = rjc.compile("A", "public class A {}");
|
||||
List<CompilationMessage> compilationMessages = cr.getCompilationMessages();
|
||||
assertTrue(compilationMessages.isEmpty());
|
||||
assertThat(compilationMessages.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -52,18 +51,19 @@ public class RuntimeJavaCompilerTests {
|
||||
+ " String message = (String) exp.getValue();"
|
||||
+ " return message;\n" + " }\n" + "}");
|
||||
List<CompilationMessage> compilationMessages = cr.getCompilationMessages();
|
||||
assertEquals(3, compilationMessages.size());
|
||||
assertTrue(
|
||||
compilationMessages.get(0).getMessage().contains("cannot find symbol"));
|
||||
assertTrue(compilationMessages.get(0).getMessage()
|
||||
.contains("class ExpressionParser"));
|
||||
assertTrue(
|
||||
compilationMessages.get(1).getMessage().contains("cannot find symbol"));
|
||||
assertTrue(compilationMessages.get(1).getMessage()
|
||||
.contains("class SpelExpressionParser"));
|
||||
assertTrue(
|
||||
compilationMessages.get(2).getMessage().contains("cannot find symbol"));
|
||||
assertTrue(compilationMessages.get(2).getMessage().contains("class Expression"));
|
||||
assertThat(compilationMessages.size()).isEqualTo(3);
|
||||
assertThat(compilationMessages.get(0).getMessage().contains("cannot find symbol"))
|
||||
.isTrue();
|
||||
assertThat(compilationMessages.get(0).getMessage()
|
||||
.contains("class ExpressionParser")).isTrue();
|
||||
assertThat(compilationMessages.get(1).getMessage().contains("cannot find symbol"))
|
||||
.isTrue();
|
||||
assertThat(compilationMessages.get(1).getMessage()
|
||||
.contains("class SpelExpressionParser")).isTrue();
|
||||
assertThat(compilationMessages.get(2).getMessage().contains("cannot find symbol"))
|
||||
.isTrue();
|
||||
assertThat(compilationMessages.get(2).getMessage().contains("class Expression"))
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,12 +80,12 @@ public class RuntimeJavaCompilerTests {
|
||||
+ " return message;\n" + " }\n" + "}",
|
||||
"maven://org.springframework:spring-expression:4.3.9.RELEASE");
|
||||
List<CompilationMessage> compilationMessages = cr.getCompilationMessages();
|
||||
assertTrue(compilationMessages.isEmpty());
|
||||
assertThat(compilationMessages.isEmpty()).isTrue();
|
||||
try (SimpleClassLoader cl = new SimpleClassLoader(
|
||||
this.getClass().getClassLoader())) {
|
||||
Class<?> clazz = cl.defineClass("A", cr.getClassBytes("A"));
|
||||
Supplier<String> supplier = (Supplier<String>) clazz.newInstance();
|
||||
assertEquals("Hello World", supplier.get());
|
||||
assertThat(supplier.get()).isEqualTo("Hello World");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,27 +100,27 @@ public class RuntimeJavaCompilerTests {
|
||||
CompilationResult cr = rjc.compile("A", source,
|
||||
"maven://joda-time:joda-time:2.9.9");
|
||||
List<CompilationMessage> compilationMessages = cr.getCompilationMessages();
|
||||
assertTrue(compilationMessages.isEmpty());
|
||||
assertThat(compilationMessages.isEmpty()).isTrue();
|
||||
List<File> resolvedAdditionalDependencies = cr
|
||||
.getResolvedAdditionalDependencies();
|
||||
try (SimpleClassLoader cl = new SimpleClassLoader(resolvedAdditionalDependencies,
|
||||
this.getClass().getClassLoader())) {
|
||||
Class<?> clazz = cl.defineClass("A", cr.getClassBytes("A"));
|
||||
Supplier<String> supplier = (Supplier<String>) clazz.newInstance();
|
||||
assertEquals("true", supplier.get());
|
||||
assertThat(supplier.get()).isEqualTo("true");
|
||||
}
|
||||
|
||||
cr = rjc.compile("A", source,
|
||||
"maven://org.springframework:spring-expression:4.3.9.RELEASE",
|
||||
"maven://joda-time:joda-time:2.9.9");
|
||||
compilationMessages = cr.getCompilationMessages();
|
||||
assertTrue(compilationMessages.isEmpty());
|
||||
assertThat(compilationMessages.isEmpty()).isTrue();
|
||||
resolvedAdditionalDependencies = cr.getResolvedAdditionalDependencies();
|
||||
try (SimpleClassLoader cl = new SimpleClassLoader(resolvedAdditionalDependencies,
|
||||
this.getClass().getClassLoader())) {
|
||||
Class<?> clazz = cl.defineClass("A", cr.getClassBytes("A"));
|
||||
Supplier<String> supplier = (Supplier<String>) clazz.newInstance();
|
||||
assertEquals("true", supplier.get());
|
||||
assertThat(supplier.get()).isEqualTo("true");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,43 +130,45 @@ public class RuntimeJavaCompilerTests {
|
||||
RuntimeJavaCompiler rjc = new RuntimeJavaCompiler();
|
||||
CompilationResult cr = rjc.compile("A", "public class A {}",
|
||||
"maven://org.springframework:spring-expression2:4.3.9.RELEASE"); // extra
|
||||
// '2'
|
||||
// in
|
||||
// there
|
||||
// '2'
|
||||
// in
|
||||
// there
|
||||
List<CompilationMessage> compilationMessages = cr.getCompilationMessages();
|
||||
assertEquals(1, compilationMessages.size());
|
||||
assertThat(compilationMessages.size()).isEqualTo(1);
|
||||
// ERROR:org.eclipse.aether.resolution.ArtifactResolutionException: Could not find
|
||||
// artifact org.springframework:spring-expression2:jar:4.3.9.RELEASE in
|
||||
// spring-snapshots (https://repo.spring.io/libs-snapshot)
|
||||
assertTrue(compilationMessages.get(0).getMessage().contains(
|
||||
"Could not find artifact org.springframework:spring-expression2:jar:4.3.9.RELEASE"));
|
||||
assertThat(compilationMessages.get(0).getMessage().contains(
|
||||
"Could not find artifact org.springframework:spring-expression2:jar:4.3.9.RELEASE"))
|
||||
.isTrue();
|
||||
|
||||
// Failure:
|
||||
rjc = new RuntimeJavaCompiler();
|
||||
cr = rjc.compile("A", "public class A {}",
|
||||
"trouble://org.springframework:spring-expression:4.3.9.RELEASE"); // rogue
|
||||
// prefix
|
||||
// (should
|
||||
// be
|
||||
// "maven:")
|
||||
// prefix
|
||||
// (should
|
||||
// be
|
||||
// "maven:")
|
||||
compilationMessages = cr.getCompilationMessages();
|
||||
assertEquals(1, compilationMessages.size());
|
||||
assertTrue(compilationMessages.get(0).toString(), compilationMessages.get(0)
|
||||
.getMessage().contains("Unrecognized dependency: "));
|
||||
assertThat(compilationMessages.size()).isEqualTo(1);
|
||||
assertThat(compilationMessages.get(0).getMessage()
|
||||
.contains("Unrecognized dependency: "))
|
||||
.as(compilationMessages.get(0).toString()).isTrue();
|
||||
|
||||
// Success
|
||||
rjc = new RuntimeJavaCompiler();
|
||||
cr = rjc.compile("A", "public class A {}", "maven://joda-time:joda-time:2.9.9");
|
||||
compilationMessages = cr.getCompilationMessages();
|
||||
assertEquals(0, compilationMessages.size());
|
||||
assertThat(compilationMessages.size()).isEqualTo(0);
|
||||
List<File> resolvedAdditionalDependencies = cr
|
||||
.getResolvedAdditionalDependencies();
|
||||
assertEquals(1, resolvedAdditionalDependencies.size());
|
||||
assertTrue(
|
||||
"Expected this to end with 'joda-time-2.9.9.jar': "
|
||||
+ resolvedAdditionalDependencies.get(0).toString(),
|
||||
resolvedAdditionalDependencies.get(0).toString()
|
||||
.endsWith("joda-time-2.9.9.jar"));
|
||||
assertThat(resolvedAdditionalDependencies.size()).isEqualTo(1);
|
||||
assertThat(resolvedAdditionalDependencies.get(0).toString()
|
||||
.endsWith("joda-time-2.9.9.jar"))
|
||||
.as("Expected this to end with 'joda-time-2.9.9.jar': "
|
||||
+ resolvedAdditionalDependencies.get(0).toString())
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -87,7 +87,8 @@ public class ByteCodeLoadingFunctionTests {
|
||||
|
||||
@Test
|
||||
public void compileFluxFunction() throws Exception {
|
||||
CompiledFunctionFactory<Function<Flux<String>, Flux<String>>> compiled = new FunctionCompiler<Flux<String>, Flux<String>>(
|
||||
CompiledFunctionFactory<Function<Flux<String>, Flux<String>>> compiled = null;
|
||||
compiled = new FunctionCompiler<Flux<String>, Flux<String>>(
|
||||
String.class.getName()).compile("foos",
|
||||
"flux -> flux.map(v -> v.toUpperCase())", "Flux<String>",
|
||||
"Flux<String>");
|
||||
|
||||
@@ -5,4 +5,4 @@
|
||||
<root level="INFO">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</root>
|
||||
</configuration>
|
||||
</configuration>
|
||||
|
||||
Reference in New Issue
Block a user