Skip to content

Commit

Permalink
ClasspathManagerTestCase cosmetics
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Kriegisch <[email protected]>
  • Loading branch information
kriegaex committed Apr 26, 2024
1 parent 975671f commit e756040
Showing 1 changed file with 132 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import org.aspectj.apache.bcel.classfile.ClassFormatException;
Expand All @@ -30,23 +31,22 @@
import junit.framework.TestCase;

/**
* Should run these tests 3 times on each JDK level (8, 9, 11). On each one 3 of the
* tests should pass indicating that JDK can successfully access system types in
* each JDK level.
* Should run these tests 3 times on each JDK level (8, 9, 11). On each one 3 of the tests should pass indicating that
* JDK can successfully access system types in each JDK level.
*
* @author Andy Clement
*/
public class ClasspathManagerTestCase extends TestCase {

// Works on my machine where all jvms under ~/jvms
private static String java18_rtjar = findJvm("j.*18.*","rt.jar");
private static String java9_jrtfsjar = findJvm("j.*9.*","jrt-fs.jar");;
private static String java11_jrtfsjar = findJvm("j.*11.*","jrt-fs.jar");;
private static final String java18_rtjar = findJvm("j.*18.*", "rt.jar");
private static final String java9_jrtfsjar = findJvm("j.*9.*", "jrt-fs.jar");
private static final String java11_jrtfsjar = findJvm("j.*11.*", "jrt-fs.jar");

private static String findJvm(String pattern, String jar) {
String start = System.getProperty("user.home")+"/jvms";
String start = System.getProperty("user.home") + "/jvms";
if (new File(start).isDirectory()) {
for (File f : new File(start).listFiles()) {
for (File f : Objects.requireNonNull(new File(start).listFiles())) {
if (f.isDirectory() && Pattern.matches(pattern, f.getName())) {
File result = walk(f, jar);
if (result != null) {
Expand All @@ -61,15 +61,14 @@ private static String findJvm(String pattern, String jar) {

private static File walk(File dir, String jar) {
File[] fs = dir.listFiles();
if (fs!=null) {
for (File f: fs) {
if (f.getName().equals(jar)) {
if (fs != null) {
for (File f : fs) {
if (f.getName().equals(jar))
return f;
} else if (f.isDirectory()) {
else if (f.isDirectory()) {
File s = walk(f, jar);
if (s!=null) {
if (s != null)
return s;
}
}
}
}
Expand Down Expand Up @@ -106,119 +105,122 @@ public void testUnfoundClassPerformance() {
);
}

public void xtestSanity18accessing18RTJAR() throws IOException {
if (LangUtil.getVmVersion()>8) fail("Must be Java 8");
List<String> classpath = new ArrayList<>();
classpath.add(java18_rtjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/Object;"));
assertNotNull(t);
}

public void xtestJava18accessing11JRT() throws ClassFormatException, IOException {
if (LangUtil.getVmVersion()>8) fail("Must be Java 8");
List<String> classpath = new ArrayList<>();
classpath.add(java11_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(),t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11
long c = Arrays.asList(clazz.getMethods()).stream().filter(m -> m.getName().equals("isBlank")).count();
assertEquals(1,c);
}

public void xtestJava18accessing19JRT() throws ClassFormatException, IOException {
if (LangUtil.getVmVersion()>8) fail("Must be Java 8");
List<String> classpath = new ArrayList<>();
classpath.add(java9_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(),t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11, but not on Java9
long c = Arrays.asList(clazz.getMethods()).stream().filter(m -> m.getName().equals("isBlank")).count();
assertEquals(0,c);
}


public void xtestSanity19accessing18RTJAR() throws IOException {
assertEquals(9.0,LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java18_rtjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/Object;"));
assertNotNull(t);
}

public void xtestJava19accessing11JRT() throws ClassFormatException, IOException {
assertEquals(9.0,LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java11_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(),t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11
long c = Arrays.asList(clazz.getMethods()).stream().filter(m -> m.getName().equals("isBlank")).count();
assertEquals(1,c);
}

public void xtestJava19accessing19JRT() throws ClassFormatException, IOException {
assertEquals(9.0,LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java9_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(),t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11, but not on Java9
long c = Arrays.asList(clazz.getMethods()).stream().filter(m -> m.getName().equals("isBlank")).count();
assertEquals(0,c);
}

public void xtestSanity11accessing18RTJAR() throws IOException {
assertEquals(11.0,LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java18_rtjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/Object;"));
assertNotNull(t);
}

public void xtestJava11accessing11JRT() throws ClassFormatException, IOException {
assertEquals(11.0,LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java11_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(),t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11
long c = Arrays.asList(clazz.getMethods()).stream().filter(m -> m.getName().equals("isBlank")).count();
assertEquals(1,c);
}

public void xtestJava11accessing19JRT() throws ClassFormatException, IOException {
assertEquals(11.0,LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java9_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(),t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11, but not on Java9
long c = Arrays.asList(clazz.getMethods()).stream().filter(m -> m.getName().equals("isBlank")).count();
assertEquals(0,c);
}

static class MH implements IMessageHandler {
public void xtestSanity18accessing18RTJAR() {
if (LangUtil.getVmVersion() > 8)
fail("Must be Java 8");
List<String> classpath = new ArrayList<>();
classpath.add(java18_rtjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/Object;"));
assertNotNull(t);
}

public void xtestJava18accessing11JRT() throws ClassFormatException, IOException {
if (LangUtil.getVmVersion() > 8)
fail("Must be Java 8");
List<String> classpath = new ArrayList<>();
classpath.add(java11_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(), t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11
long c = Arrays.stream(clazz.getMethods()).filter(m -> m.getName().equals("isBlank")).count();
assertEquals(1, c);
}

public void xtestJava18accessing19JRT() throws ClassFormatException, IOException {
if (LangUtil.getVmVersion() > 8)
fail("Must be Java 8");
List<String> classpath = new ArrayList<>();
classpath.add(java9_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(), t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11, but not on Java9
long c = Arrays.stream(clazz.getMethods()).filter(m -> m.getName().equals("isBlank")).count();
assertEquals(0, c);
}


public void xtestSanity19accessing18RTJAR() {
assertEquals(9.0, LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java18_rtjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/Object;"));
assertNotNull(t);
}

public void xtestJava19accessing11JRT() throws ClassFormatException, IOException {
assertEquals(9.0, LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java11_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(), t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11
long c = Arrays.stream(clazz.getMethods()).filter(m -> m.getName().equals("isBlank")).count();
assertEquals(1, c);
}

public void xtestJava19accessing19JRT() throws ClassFormatException, IOException {
assertEquals(9.0, LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java9_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(), t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11, but not on Java9
long c = Arrays.stream(clazz.getMethods()).filter(m -> m.getName().equals("isBlank")).count();
assertEquals(0, c);
}

public void xtestSanity11accessing18RTJAR() {
assertEquals(11.0, LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java18_rtjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/Object;"));
assertNotNull(t);
}

public void xtestJava11accessing11JRT() throws ClassFormatException, IOException {
assertEquals(11.0, LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java11_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(), t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11
long c = Arrays.stream(clazz.getMethods()).filter(m -> m.getName().equals("isBlank")).count();
assertEquals(1, c);
}

public void xtestJava11accessing19JRT() throws ClassFormatException, IOException {
assertEquals(11.0, LangUtil.getVmVersion());
List<String> classpath = new ArrayList<>();
classpath.add(java9_jrtfsjar);
ClassPathManager cpm = new ClassPathManager(classpath, new MH());
ClassFile t = cpm.find(UnresolvedType.forSignature("Ljava/lang/String;"));
assertNotNull(t);
ClassParser classParser = new ClassParser(t.getInputStream(), t.getPath());
JavaClass clazz = classParser.parse();
// isBlank() exists on Java 11, but not on Java9
long c = Arrays.stream(clazz.getMethods()).filter(m -> m.getName().equals("isBlank")).count();
assertEquals(0, c);
}

static class MH implements IMessageHandler {

@Override
public boolean handleMessage(IMessage message) throws AbortException {
Expand All @@ -232,12 +234,10 @@ public boolean isIgnoring(Kind kind) {
}

@Override
public void dontIgnore(Kind kind) {
}
public void dontIgnore(Kind kind) {}

@Override
public void ignore(Kind kind) {
}
public void ignore(Kind kind) {}

}
}
}

0 comments on commit e756040

Please sign in to comment.