Skip to content

Commit

Permalink
Improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurens-W committed Nov 27, 2024
1 parent 5c675c6 commit 70cc111
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@
package org.openrewrite.staticanalysis;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.FileAttributes;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Tree;
import org.openrewrite.*;
import org.openrewrite.csharp.tree.Cs;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.java;

Expand Down Expand Up @@ -340,40 +334,52 @@ void foo() throws IOException {

@Test
void verifyCsharpImplicitThrow() {
Cs.CompilationUnit compilationUnit = new Cs.CompilationUnit(Tree.randomId(), Space.EMPTY,
Markers.EMPTY, Path.of("test.cs"),
new FileAttributes(null, null, null, true, true, true, 0l),
null, false, null, null, null, null,
List.of(JRightPadded.build(
new J.ClassDeclaration(Tree.randomId(), Space.EMPTY, Markers.EMPTY,
Collections.emptyList(), Collections.emptyList(),
new J.ClassDeclaration.Kind(Tree.randomId(), Space.EMPTY, Markers.EMPTY, Collections.emptyList(), J.ClassDeclaration.Kind.Type.Class),
new J.Identifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, Collections.emptyList(), "doSome", null, null),
null, null, null, null, null,
new J.Block(Tree.randomId(), Space.EMPTY, Markers.EMPTY, JRightPadded.build(false),
List.of(JRightPadded.build(new J.Try(Tree.randomId(), Space.EMPTY, Markers.EMPTY, null,
new J.Block(Tree.randomId(), Space.EMPTY, Markers.EMPTY, JRightPadded.build(false),
List.of(JRightPadded.build(new J.Throw(Tree.randomId(), Space.EMPTY, Markers.EMPTY, new J.NewClass(
Tree.randomId(), Space.EMPTY, Markers.EMPTY, null, Space.EMPTY, TypeTree.build("java.lang.IllegalAccessException"), JContainer.empty(), null, null)))),
Space.EMPTY),
List.of(new J.Try.Catch(Tree.randomId(), Space.EMPTY, Markers.EMPTY, new J.ControlParentheses<J.VariableDeclarations>(Tree.randomId(), Space.EMPTY, Markers.EMPTY,
JRightPadded.build(new J.VariableDeclarations(Tree.randomId(), Space.EMPTY, Markers.EMPTY, Collections.emptyList(), Collections.emptyList(), TypeTree.build("java.lang.IllegalAccessException"), null, List.of(), List.of(JRightPadded.build(
new J.VariableDeclarations.NamedVariable(Tree.randomId(), Space.EMPTY, Markers.EMPTY,
new J.Identifier(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, Collections.emptyList(), "e", null, null),
List.of(), null, null)))))),
new J.Block(Tree.randomId(), Space.EMPTY, Markers.EMPTY, JRightPadded.build(false),
List.of(JRightPadded.build(new J.Throw(Tree.randomId(), Space.EMPTY, Markers.EMPTY, new J.Empty(Tree.randomId(), Space.EMPTY, Markers.EMPTY)))),
Space.EMPTY))),
null))),
Space.EMPTY),
null)))
, Space.EMPTY);

assertThat(compilationUnit.getMembers().get(0)).isInstanceOf(J.ClassDeclaration.class);
assertThat(((J.ClassDeclaration)compilationUnit.getMembers().get(0)).getBody().getStatements().get(0)).isInstanceOf(J.Try.class);

Cs.CompilationUnit output = (Cs.CompilationUnit) new CatchClauseOnlyRethrows().getVisitor().visit(compilationUnit, new InMemoryExecutionContext());
assertThat(output.getMembers().get(0)).isInstanceOf(J.ClassDeclaration.class);
assertThat(((J.ClassDeclaration)output.getMembers().get(0)).getBody().getStatements().get(0)).isInstanceOf(J.Throw.class);
rewriteRun(
spec -> spec.recipe(Recipe.noop()),
//language=java
java(
"""
class A {
void foo() throws IllegalAccessException {
try {
throw new IllegalAccessException();
} catch (Exception e) {
throw e;
}
}
}"""
, spec -> spec.beforeRecipe(compUnit -> {
Cs.CompilationUnit cSharpCompUnit = (Cs.CompilationUnit) new JavaVisitor<ExecutionContext>() {
@Override
public J visitThrow(J.Throw thrown, ExecutionContext executionContext) {
if (thrown.getException() instanceof J.Identifier) {
return thrown.withException(new J.Empty(Tree.randomId(), Space.EMPTY, Markers.EMPTY));
}
return thrown;
}
}.visit(JavaToCsharp.compilationUnit(compUnit), new InMemoryExecutionContext());

assertThat(cSharpCompUnit.getMembers().get(0)).isInstanceOf(J.ClassDeclaration.class);
assertThat(((J.ClassDeclaration)cSharpCompUnit.getMembers().get(0)).getBody().getStatements().get(0)).isInstanceOf(J.MethodDeclaration.class);
J.MethodDeclaration md = (J.MethodDeclaration) ((J.ClassDeclaration)cSharpCompUnit.getMembers().get(0)).getBody().getStatements().get(0);
assertThat(md.getBody().getStatements().get(0)).isInstanceOf(J.Try.class);

Cs.CompilationUnit output = (Cs.CompilationUnit) new CatchClauseOnlyRethrows().getVisitor().visit(cSharpCompUnit, new InMemoryExecutionContext());

assertThat(output.getMembers().get(0)).isInstanceOf(J.ClassDeclaration.class);
assertThat(((J.ClassDeclaration)output.getMembers().get(0)).getBody().getStatements().get(0)).isInstanceOf(J.MethodDeclaration.class);
md = (J.MethodDeclaration) ((J.ClassDeclaration)output.getMembers().get(0)).getBody().getStatements().get(0);
assertThat(md.getBody().getStatements().get(0)).isInstanceOf(J.Throw.class);
}
)
)
);
}

public JavaVisitor<ExecutionContext> getJavaToCsharpVisitor() {
return new JavaVisitor<ExecutionContext>() {


};
}
}
32 changes: 32 additions & 0 deletions src/test/java/org/openrewrite/staticanalysis/JavaToCsharp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.staticanalysis;

import org.openrewrite.csharp.tree.Cs;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JRightPadded;
import org.openrewrite.java.tree.Statement;

import java.util.List;
import java.util.stream.Collectors;

public class JavaToCsharp {

public static Cs.CompilationUnit compilationUnit(J.CompilationUnit cu) {
return new Cs.CompilationUnit(cu.getId(), cu.getPrefix(), cu.getMarkers(), cu.getSourcePath(),
cu.getFileAttributes(), cu.getCharset().name(), cu.isCharsetBomMarked(), cu.getChecksum(), List.of(), List.of(), List.of(), cu.getClasses().stream().map(Statement.class::cast).map(cd -> JRightPadded.build(cd)).collect(Collectors.toList()), cu.getEof());
}
}

0 comments on commit 70cc111

Please sign in to comment.