You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JImageTestCase.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* *******************************************************************
  2. * Copyright (c) 2017 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. * ******************************************************************/
  9. package org.aspectj.weaver.bcel;
  10. import java.io.IOException;
  11. import java.net.URI;
  12. import java.nio.file.FileSystem;
  13. import java.nio.file.FileSystems;
  14. import java.nio.file.FileVisitResult;
  15. import java.nio.file.Files;
  16. import java.nio.file.Path;
  17. import java.nio.file.SimpleFileVisitor;
  18. import java.nio.file.attribute.BasicFileAttributes;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.Map;
  22. import org.aspectj.bridge.AbortException;
  23. import org.aspectj.bridge.IMessage;
  24. import org.aspectj.bridge.IMessage.Kind;
  25. import org.aspectj.bridge.IMessageHandler;
  26. import org.aspectj.util.LangUtil;
  27. import org.aspectj.weaver.UnresolvedType;
  28. import org.aspectj.weaver.bcel.ClassPathManager.ClassFile;
  29. import org.aspectj.weaver.bcel.ClassPathManager.Entry;
  30. import org.aspectj.weaver.bcel.ClassPathManager.JImageEntry;
  31. import junit.framework.TestCase;
  32. /**
  33. * Exercise the JImage handling in @link {@link org.aspectj.weaver.bcel.ClassPathManager}.
  34. *
  35. * @author Andy Clement
  36. */
  37. public class JImageTestCase extends TestCase {
  38. ClassPathManager cpm;
  39. public void setUp() throws Exception {
  40. List<String> paths = new ArrayList<>();
  41. paths.add(LangUtil.getJrtFsFilePath());
  42. cpm = new ClassPathManager(paths,new TestMessageHandler());
  43. }
  44. public void testOnJava9() {
  45. if (!LangUtil.is9VMOrGreater()) {
  46. System.out.println("SKIPPING JIMAGE TESTS AS NOT ON 1.9 OR LATER");
  47. }
  48. }
  49. public void testBasicStructureAndCapabilities() {
  50. if (!LangUtil.is9VMOrGreater()) return;
  51. // Should be one entry for finding JRT contents
  52. List<Entry> entries = cpm.getEntries();
  53. assertEquals(1,entries.size());
  54. assertEquals(JImageEntry.class,entries.get(0).getClass());
  55. ClassFile stringClassFile = cpm.find(UnresolvedType.JL_STRING);
  56. assertNotNull(stringClassFile);
  57. assertEquals("java/lang/String.class",stringClassFile.getPath());
  58. }
  59. public void testBehaviour() throws Exception {
  60. if (!LangUtil.is9VMOrGreater()) return;
  61. JImageEntry jie = getJImageEntry();
  62. Map<String, Path> packageCache = jie.getPackageCache();
  63. assertTrue(packageCache.size()>0);
  64. // Note: seems to be about 1625 entries in it for Java9
  65. Path path = packageCache.get("java/lang");
  66. assertEquals("modules/java.base/java/lang", path.toString());
  67. path = packageCache.get("java/io");
  68. assertEquals("modules/java.base/java/io", path.toString());
  69. assertNotNull(jie.find("java/lang/String"));
  70. assertNotNull(jie.find("java/io/File"));
  71. // TODO test the filecache, hard because difficult to simulate collection of SoftReferences
  72. }
  73. static class TestMessageHandler implements IMessageHandler {
  74. @Override
  75. public boolean handleMessage(IMessage message) throws AbortException {
  76. return false;
  77. }
  78. @Override
  79. public boolean isIgnoring(Kind kind) {
  80. return false;
  81. }
  82. @Override
  83. public void dontIgnore(Kind kind) {
  84. }
  85. @Override
  86. public void ignore(Kind kind) {
  87. }
  88. }
  89. // ---
  90. private JImageEntry getJImageEntry() {
  91. return (JImageEntry) cpm.getEntries().get(0);
  92. }
  93. public List<String> getAllTheClasses() {
  94. final List<String> result = new ArrayList<>();
  95. URI JRT_URI = URI.create("jrt:/"); //$NON-NLS-1$
  96. FileSystem fs = FileSystems.getFileSystem(JRT_URI);
  97. Iterable<java.nio.file.Path> roots = fs.getRootDirectories();
  98. try {
  99. for (java.nio.file.Path path : roots) {
  100. Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
  101. @Override
  102. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  103. if (file.getNameCount()>3 && file.toString().endsWith(".class")) {
  104. String withClassSuffix = file.subpath(2, file.getNameCount()).toString();
  105. result.add(withClassSuffix.substring(0,withClassSuffix.length()-".class".length()));
  106. }
  107. return FileVisitResult.CONTINUE;
  108. }
  109. });
  110. }
  111. } catch (IOException e) {
  112. throw new RuntimeException(e);
  113. }
  114. return result;
  115. }
  116. }