Skip to content

Commit

Permalink
Fix JavaParserTest to load resources from classpath. Also makes the t…
Browse files Browse the repository at this point in the history
…est pass on Windows
  • Loading branch information
sambsnyd committed May 26, 2023
1 parent d87127d commit 17b89bb
Showing 1 changed file with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package org.openrewrite.java;

import io.github.classgraph.ClassGraph;
import io.github.classgraph.Resource;
import io.github.classgraph.ResourceList;
import io.github.classgraph.ScanResult;
import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.openrewrite.InMemoryExecutionContext;
Expand All @@ -24,6 +29,7 @@
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.test.RewriteTest;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -129,45 +135,40 @@ List<Integer> getList() {

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/3222")
void parseFromByteArray() throws Exception {
// Load classes from test resources folder
Path start = Paths.get(getClass().getResource("/javaparser-byte-array-tests").getPath());
byte[][] classes;
try (Stream<Path> pathStream = Files.find(start, 2, (p, a) -> p.toString().endsWith(".class"))) {
classes = pathStream
.map(p -> {
void parseFromByteArray() {
try (ScanResult scan = new ClassGraph().scan()) {
byte[][] classes = scan.getResourcesMatchingWildcard("javaparser-byte-array-tests/**.class").stream()
.map(it -> {
try {
return Files.readAllBytes(p);
} catch (Exception ex) {
throw new IllegalStateException(ex);
return it.read().array();
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.toArray(byte[][]::new);
assertThat(classes.length).isEqualTo(2);
}

JavaParser parser = JavaParser.fromJavaVersion()
.classpath(classes)
.build();
JavaParser parser = JavaParser.fromJavaVersion()
.classpath(classes)
.build();

//language=java
String source = """
import example.InterfaceA;
public class User implements InterfaceA, InterfaceB {
@Override
public void methodA() {}
@Language("java")
String source = """
import example.InterfaceA;
public class User implements InterfaceA, InterfaceB {
@Override
public void methodA() {}
@Override
public void methodB() {}
}
""";
List<J.CompilationUnit> compilationUnits = parser.parse(new InMemoryExecutionContext(Throwable::printStackTrace), source);
assertThat(compilationUnits).singleElement()
.satisfies(cu -> assertThat(cu.getClasses()).singleElement()
.satisfies(cd -> assertThat(cd.getImplements()).satisfiesExactly(
i -> assertThat(i.getType()).hasToString("example.InterfaceA"),
i -> assertThat(i.getType()).hasToString("InterfaceB")
)));
@Override
public void methodB() {}
}
""";
List<J.CompilationUnit> compilationUnits = parser.parse(new InMemoryExecutionContext(Throwable::printStackTrace), source);
assertThat(compilationUnits).singleElement()
.satisfies(cu -> assertThat(cu.getClasses()).singleElement()
.satisfies(cd -> assertThat(cd.getImplements()).satisfiesExactly(
i -> assertThat(i.getType()).hasToString("example.InterfaceA"),
i -> assertThat(i.getType()).hasToString("InterfaceB")
)));
}
}

}

0 comments on commit 17b89bb

Please sign in to comment.