From d4dc28c782dfa801c8a59c4b1895d98f235f52bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Kr=C3=BCger?= Date: Thu, 10 Aug 2023 09:19:30 +0200 Subject: [PATCH] Fix tests and adjust to changes in OR 8.1.2 --- .../engine/context/ProjectContextHolder.java | 2 + .../impl/DependenciesChangedEventHandler.java | 8 ++- .../sbm/java/impl/OpenRewriteType.java | 15 ++++- .../sbm/java/impl/RewriteJavaParser.java | 1 + .../parser/ProjectContextInitializer.java | 4 +- .../sbm/java/impl/OpenRewriteTypeTest.java | 64 ++++++++++--------- .../OpenRewriteMavenBuildFileTest.java | 3 +- .../project/resource/TestProjectContext.java | 3 + 8 files changed, 62 insertions(+), 38 deletions(-) diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContextHolder.java b/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContextHolder.java index 67f37d8fe..e9d37c4d9 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContextHolder.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContextHolder.java @@ -17,11 +17,13 @@ import lombok.Getter; import lombok.Setter; +import org.springframework.sbm.scopes.annotations.ScanScope; import org.springframework.stereotype.Component; @Component @Getter @Setter +@ScanScope public class ProjectContextHolder { private ProjectContext projectContext; } diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/DependenciesChangedEventHandler.java b/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/DependenciesChangedEventHandler.java index 13b7d03f4..f522502f4 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/DependenciesChangedEventHandler.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/DependenciesChangedEventHandler.java @@ -21,9 +21,9 @@ import org.springframework.sbm.engine.context.ProjectContextHolder; import lombok.RequiredArgsConstructor; import org.openrewrite.Parser; -import org.openrewrite.java.JavaParser; import org.openrewrite.java.tree.J; import org.springframework.context.event.EventListener; +import org.springframework.sbm.parsers.JavaParserBuilder; import org.springframework.stereotype.Component; import java.io.ByteArrayInputStream; @@ -39,7 +39,7 @@ @RequiredArgsConstructor public class DependenciesChangedEventHandler { private final ProjectContextHolder projectContextHolder; - private final JavaParser javaParser; + private final JavaParserBuilder javaParserBuilder; private final ExecutionContext executionContext; @EventListener @@ -53,7 +53,7 @@ public void onDependenciesChanged(DependenciesChangedEvent event) { Path projectRootDirectory = projectContextHolder.getProjectContext().getProjectRootDirectory(); - Stream parsedCompilationUnits = javaParser.parseInputs(compilationUnits, null, executionContext); + Stream parsedCompilationUnits = javaParserBuilder.build().parseInputs(compilationUnits, null, executionContext); // ((J.VariableDeclarations)parsedCompilationUnits.get(0).getClasses().get(0).getBody().getStatements().get(0)).getLeadingAnnotations().get(0).getType() parsedCompilationUnits .filter(J.CompilationUnit.class::isInstance) @@ -63,6 +63,8 @@ public void onDependenciesChanged(DependenciesChangedEvent event) { .filter(js -> js.getResource().getAbsolutePath().equals(projectRootDirectory.resolve(cu.getSourcePath()).normalize())) .forEach(js -> js.getResource().replaceWith(cu)); }); + } else { + throw new IllegalStateException("Could not get ProjectContext from ProjectContextHolder."); } } } diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/OpenRewriteType.java b/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/OpenRewriteType.java index 594db5b6e..82d2729df 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/OpenRewriteType.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/OpenRewriteType.java @@ -16,7 +16,6 @@ package org.springframework.sbm.java.impl; import org.openrewrite.*; -import org.openrewrite.internal.InMemoryLargeSourceSet; import org.openrewrite.java.*; import org.openrewrite.java.format.WrappingAndBraces; import org.openrewrite.java.tree.*; @@ -24,7 +23,6 @@ import org.springframework.sbm.java.migration.visitor.RemoveImplementsVisitor; import org.springframework.sbm.java.refactoring.JavaRefactoring; import org.springframework.sbm.parsers.JavaParserBuilder; -import org.springframework.sbm.project.resource.SbmApplicationProperties; import org.springframework.sbm.project.resource.RewriteSourceFileHolder; import lombok.extern.slf4j.Slf4j; import org.openrewrite.java.search.DeclaresMethod; @@ -156,6 +154,9 @@ public void addMethod(String methodTemplate, Set requiredImports) { @Override public ClassDeclaration visitClassDeclaration(ClassDeclaration classDecl, ExecutionContext executionContext) { ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext); + J.CompilationUnit cu = getCursor().dropParentUntil(J.CompilationUnit.class::isInstance).getValue(); + checkTypeAvailability(cu, requiredImports); + JavaTemplate template = JavaTemplate .builder(methodTemplate) .javaParser(javaParserBuilder) @@ -169,6 +170,16 @@ public ClassDeclaration visitClassDeclaration(ClassDeclaration classDecl, Execut this.apply(new WrappingAndBraces()); } + private void checkTypeAvailability(J.CompilationUnit cd, Set requiredImports) { + List missingTypes = cd.getTypesInUse().getTypesInUse().stream() + .map(JavaType::toString) + .filter(t -> !requiredImports.contains(t)) + .toList(); + if(!missingTypes.isEmpty()) { + throw new IllegalArgumentException("These types %s are not available to in compilation unit %s".formatted(missingTypes, cd.getSourcePath())); + } + } + private List findORAnnotations(String annotation) { return getClassDeclaration().getLeadingAnnotations() diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/RewriteJavaParser.java b/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/RewriteJavaParser.java index 8edc4a402..f7140c0cb 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/RewriteJavaParser.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/RewriteJavaParser.java @@ -36,6 +36,7 @@ @Component @ScanScope +@Deprecated public class RewriteJavaParser implements JavaParser { private final SbmApplicationProperties sbmApplicationProperties; diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/project/parser/ProjectContextInitializer.java b/components/sbm-core/src/main/java/org/springframework/sbm/project/parser/ProjectContextInitializer.java index 96c74f78b..5a62e1efc 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/project/parser/ProjectContextInitializer.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/project/parser/ProjectContextInitializer.java @@ -21,6 +21,7 @@ import org.springframework.core.io.Resource; import org.springframework.sbm.engine.context.ProjectContext; import org.springframework.sbm.engine.context.ProjectContextFactory; +import org.springframework.sbm.engine.context.ProjectContextHolder; import org.springframework.sbm.engine.git.Commit; import org.springframework.sbm.engine.git.GitSupport; import org.springframework.sbm.project.RewriteSourceFileWrapper; @@ -41,6 +42,7 @@ public class ProjectContextInitializer { private final GitSupport gitSupport; private final RewriteSourceFileWrapper rewriteSourceFileWrapper; private final ExecutionContext executionContext; + private final ProjectContextHolder projectContextHolder; public ProjectContext initProjectContext(Path projectDir, List resources) { final Path absoluteProjectDir = projectDir.toAbsolutePath().normalize(); @@ -54,7 +56,7 @@ public ProjectContext initProjectContext(Path projectDir, List resourc ProjectContext projectContext = projectContextFactory.createProjectContext(projectDir, projectResourceSet); storeGitCommitHash(projectDir, projectContext); - + projectContextHolder.setProjectContext(projectContext); return projectContext; } diff --git a/components/sbm-core/src/test/java/org/springframework/sbm/java/impl/OpenRewriteTypeTest.java b/components/sbm-core/src/test/java/org/springframework/sbm/java/impl/OpenRewriteTypeTest.java index dd6c407fa..3a597205a 100644 --- a/components/sbm-core/src/test/java/org/springframework/sbm/java/impl/OpenRewriteTypeTest.java +++ b/components/sbm-core/src/test/java/org/springframework/sbm/java/impl/OpenRewriteTypeTest.java @@ -309,12 +309,14 @@ void testAddMethod() { @Test void testAddMethod2() { String template = - "@Bean\n" + - "IntegrationFlow http_routeFlow() {\n" + - "return IntegrationFlows.from(Http.inboundChannelAdapter(\"/test\")).handle((p, h) -> p)\n" + - ".log(LoggingHandler.Level.INFO)\n" + - ".get();\n" + - "}\n"; + """ + @Bean + IntegrationFlow http_routeFlow() { + return IntegrationFlows.from(Http.inboundChannelAdapter("/test")).handle((p, h) -> p) + .log(LoggingHandler.Level.INFO) + .get(); + } + """; Set requiredImports = Set.of("org.springframework.integration.transformer.ObjectToStringTransformer", "org.springframework.context.annotation.Configuration", @@ -326,40 +328,40 @@ void testAddMethod2() { "org.springframework.integration.http.dsl.Http"); ProjectContext context = TestProjectContext.buildProjectContext() - .withBuildFileHavingDependencies("org.springframework.boot:spring-boot-starter-integration:2.5.5", - "org.springframework.boot:spring-boot-starter-web:2.5.5") - .withJavaSource("src/main/java/Config.java", "public class Config {}") + .withBuildFileHavingDependencies( + "org.springframework.boot:spring-boot-starter-integration:2.5.5", + "org.springframework.boot:spring-boot-starter-web:2.5.5" + ) + .withJavaSource("src/main/java", "public class Config {}") .build(); - long before = System.currentTimeMillis(); - - context.getBuildFile().addDependency(Dependency.builder() + context.getApplicationModules().getRootModule().getBuildFile().addDependency(Dependency.builder() .groupId("org.springframework.integration") .artifactId("spring-integration-http") .version("5.4.4") .build()); - long timeSpent = System.currentTimeMillis() - before; - System.out.println("Adding a dependency took: " + (timeSpent/1000) + " sec."); - - Type type = context.getProjectJavaSources().list().get(0).getTypes().get(0); + JavaSource javaSource = context.getProjectJavaSources().findJavaSourceDeclaringType("Config").get(); + Type type = javaSource.getTypes().get(0); type.addMethod(template, requiredImports); - assertThat(context.getProjectJavaSources().list().get(0).print()).isEqualTo( - "import org.springframework.context.annotation.Bean;\n" + - "import org.springframework.integration.dsl.IntegrationFlow;\n" + - "import org.springframework.integration.dsl.IntegrationFlows;\n" + - "import org.springframework.integration.handler.LoggingHandler;\n" + - "import org.springframework.integration.http.dsl.Http;\n" + - "\n" + - "public class Config {\n" + - " @Bean\n" + - " IntegrationFlow http_routeFlow() {\n" + - " return IntegrationFlows.from(Http.inboundChannelAdapter(\"/test\")).handle((p, h) -> p)\n" + - " .log(LoggingHandler.Level.INFO)\n" + - " .get();\n" + - " }\n" + - "}" + assertThat(javaSource.print()).isEqualTo( + """ + import org.springframework.context.annotation.Bean; + import org.springframework.integration.dsl.IntegrationFlow; + import org.springframework.integration.dsl.IntegrationFlows; + import org.springframework.integration.handler.LoggingHandler; + import org.springframework.integration.http.dsl.Http; + + public class Config { + @Bean + IntegrationFlow http_routeFlow() { + return IntegrationFlows.from(Http.inboundChannelAdapter("/test")).handle((p, h) -> p) + .log(LoggingHandler.Level.INFO) + .get(); + } + } + """ ); } diff --git a/components/sbm-core/src/test/java/org/springframework/sbm/project/buildfile/OpenRewriteMavenBuildFileTest.java b/components/sbm-core/src/test/java/org/springframework/sbm/project/buildfile/OpenRewriteMavenBuildFileTest.java index 3e7b85856..e09e02557 100644 --- a/components/sbm-core/src/test/java/org/springframework/sbm/project/buildfile/OpenRewriteMavenBuildFileTest.java +++ b/components/sbm-core/src/test/java/org/springframework/sbm/project/buildfile/OpenRewriteMavenBuildFileTest.java @@ -35,6 +35,7 @@ import org.springframework.sbm.java.impl.DependenciesChangedEventHandler; import org.springframework.sbm.java.impl.RewriteJavaParser; import org.springframework.sbm.openrewrite.RewriteExecutionContext; +import org.springframework.sbm.parsers.JavaParserBuilder; import org.springframework.sbm.project.resource.SbmApplicationProperties; import org.springframework.sbm.project.resource.TestProjectContext; @@ -563,7 +564,7 @@ public class Cat { assertThat(fireEvent.getResolvedDependencies().get(0).toString()).endsWith("javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar"); // call DependenciesChangedEventHandler to trigger recompile - RewriteJavaParser rewriteJavaParser = new RewriteJavaParser(new SbmApplicationProperties(), executionContext); + JavaParserBuilder rewriteJavaParser = new JavaParserBuilder(); ProjectContextHolder projectContextHolder = new ProjectContextHolder(); projectContextHolder.setProjectContext(context); DependenciesChangedEventHandler handler = new DependenciesChangedEventHandler(projectContextHolder, rewriteJavaParser, executionContext); diff --git a/components/sbm-core/src/test/java/org/springframework/sbm/project/resource/TestProjectContext.java b/components/sbm-core/src/test/java/org/springframework/sbm/project/resource/TestProjectContext.java index d4037da8e..cfe92fc96 100644 --- a/components/sbm-core/src/test/java/org/springframework/sbm/project/resource/TestProjectContext.java +++ b/components/sbm-core/src/test/java/org/springframework/sbm/project/resource/TestProjectContext.java @@ -384,6 +384,9 @@ public Builder withJavaSource(Path sourcePathDir, String sourceCode) { if (sourcePathDir.isAbsolute()) { throw new IllegalArgumentException("Source path must be relative to project root dir."); } + if(sourcePathDir.toString().endsWith(".java")) { + throw new IllegalArgumentException("The provided path '%s' should only be the source path, e.g. 'src/main/'java'. Package and filename will be calculated from provided source code.".formatted(sourcePathDir)); + } String fqName = JavaSourceUtil.retrieveFullyQualifiedClassFileName(sourceCode); Path sourcePath = sourcePathDir.resolve(fqName); this.resourcesWithRelativePaths.put(sourcePath, sourceCode);