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

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