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.

ModuleTests.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*******************************************************************************
  2. * Copyright (c) 2017 Contributors
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *******************************************************************************/
  8. package org.aspectj.systemtest.ajc190;
  9. import java.io.File;
  10. import org.aspectj.apache.bcel.classfile.Attribute;
  11. import org.aspectj.apache.bcel.classfile.Code;
  12. import org.aspectj.apache.bcel.classfile.JavaClass;
  13. import org.aspectj.apache.bcel.classfile.Method;
  14. import org.aspectj.testing.XMLBasedAjcTestCase;
  15. import org.aspectj.testing.XMLBasedAjcTestCaseForJava9OrLater;
  16. import org.aspectj.util.LangUtil;
  17. import junit.framework.Test;
  18. /**
  19. * Building and weaving with modules in the picture.
  20. *
  21. * Module options from http://openjdk.java.net/jeps/261
  22. *
  23. * @author Andy Clement
  24. *
  25. */
  26. public class ModuleTests extends XMLBasedAjcTestCaseForJava9OrLater {
  27. public void testBuildAModule() {
  28. runTest("build a module");
  29. }
  30. public void testRunModuleClassPath() {
  31. runTest("run a module - classpath");
  32. }
  33. public void testRunModuleModulePath() {
  34. runTest("run a module - modulepath");
  35. }
  36. public void testPackageAndRunModuleFromModulePath() {
  37. runTest("package and run a module - modulepath");
  38. }
  39. public void testBuildModuleIncludingAspects() {
  40. runTest("compile module including aspects");
  41. }
  42. public void testBuildModuleAndApplyAspectsFromAspectPath() {
  43. runTest("compile module and apply aspects via aspectpath");
  44. }
  45. public void testBinaryWeavingAModuleJar() {
  46. // Pass a module on inpath, does it weave ok with a source aspect, does it run afterwards?
  47. runTest("binary weaving module");
  48. }
  49. public void testModulepathClasspathResolution1() {
  50. runTest("module path vs classpath 1");
  51. }
  52. // public void testModulepathClasspathResolution2() {
  53. // runTest("module path vs classpath 2");
  54. // }
  55. // --add-modules
  56. // This tests that when using --add-modules with one of the JDK modules (in the jmods subfolder of the JDK)
  57. // that it can be found without needing to set --module-path (this seems to be implicitly included by javac too)
  58. public void testAddModules1() {
  59. if (LangUtil.is11VMOrGreater()) {
  60. // java.xml.bind is gone in Java11
  61. return;
  62. }
  63. runTest("compile use of java.xml.bind");
  64. }
  65. // This tests that we can use add-modules to pull in something from the JDK jmods package and that
  66. // when subsequently weaving we can see types from those modules
  67. public void testWovenAfterAddModules() {
  68. if (LangUtil.is11VMOrGreater()) {
  69. // java.xml.bind is gone in Java11
  70. return;
  71. }
  72. runTest("weave use of java.xml.bind");
  73. }
  74. // --limit-modules
  75. public void testLimitModules1() {
  76. if (LangUtil.is11VMOrGreater()) {
  77. // java.xml.bind is gone in Java11
  78. return;
  79. }
  80. runTest("limit modules 1");
  81. }
  82. // --add-reads
  83. public void testAddReads1() {
  84. if (LangUtil.is11VMOrGreater()) {
  85. // java.xml.bind is gone in Java11
  86. return;
  87. }
  88. runTest("add reads 1");
  89. }
  90. // ---
  91. /* For the specified class, check that each method has a stackmap attribute */
  92. private void checkStackMapExistence(String classname, String toIgnore) throws ClassNotFoundException {
  93. toIgnore = "_" + (toIgnore == null ? "" : toIgnore) + "_";
  94. JavaClass jc = getClassFrom(ajc.getSandboxDirectory(), classname);
  95. Method[] methods = jc.getMethods();
  96. for (int i = 0; i < methods.length; i++) {
  97. Method method = methods[i];
  98. if (toIgnore.contains("_" + method.getName() + "_")) {
  99. continue;
  100. }
  101. boolean hasStackMapAttribute = findAttribute(method.getAttributes(), "StackMapTable");
  102. if (!hasStackMapAttribute) {
  103. fail("Could not find StackMap attribute for method " + method.getName());
  104. }
  105. }
  106. }
  107. private boolean findAttribute(Attribute[] attrs, String attributeName) {
  108. if (attrs == null) {
  109. return false;
  110. }
  111. for (int i = 0; i < attrs.length; i++) {
  112. Attribute attribute = attrs[i];
  113. if (attribute.getName().equals(attributeName)) {
  114. return true;
  115. }
  116. // System.out.println(attribute.getName());
  117. if (attribute.getName().equals("Code")) {
  118. Code c = (Code) attribute;
  119. Attribute[] codeAttributes = c.getAttributes();
  120. for (int j = 0; j < codeAttributes.length; j++) {
  121. Attribute codeAttribute = codeAttributes[j];
  122. if (codeAttribute.getName().equals(attributeName)) {
  123. return true;
  124. // System.out.println(codeAttribute.getName());
  125. }
  126. }
  127. }
  128. }
  129. return false;
  130. }
  131. private void checkVersion(String classname, int major, int minor) throws ClassNotFoundException {
  132. JavaClass jc = getClassFrom(ajc.getSandboxDirectory(), classname);
  133. if (jc.getMajor() != major) {
  134. fail("Expected major version to be " + major + " but was " + jc.getMajor());
  135. }
  136. if (jc.getMinor() != minor) {
  137. fail("Expected minor version to be " + minor + " but was " + jc.getMinor());
  138. }
  139. }
  140. // Check the stackmap stuff is removed when a method gets woven (for now...)
  141. // public void testStackMapAttributesDeletedInWovenCode() {
  142. // fail("Not implemented");
  143. // }
  144. // ///////////////////////////////////////
  145. public static Test suite() {
  146. return XMLBasedAjcTestCase.loadSuite(ModuleTests.class);
  147. }
  148. @Override
  149. protected File getSpecFile() {
  150. return getClassResource("ajc190.xml");
  151. }
  152. }