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.

SanityTests.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*******************************************************************************
  2. * Copyright (c) 2006 IBM
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v 2.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  7. *
  8. * Contributors:
  9. * Andy Clement - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.systemtest.ajc160;
  12. import org.aspectj.apache.bcel.Constants;
  13. import org.aspectj.apache.bcel.classfile.Attribute;
  14. import org.aspectj.apache.bcel.classfile.Code;
  15. import org.aspectj.apache.bcel.classfile.JavaClass;
  16. import org.aspectj.apache.bcel.classfile.Method;
  17. import org.aspectj.testing.XMLBasedAjcTestCase;
  18. import junit.framework.Test;
  19. /*
  20. * Some very trivial tests that help verify things are OK.
  21. * Followed by some Java6 specific checks to ensure the class files are well formed.
  22. * A Java6 JDK is not required to run these tests as they introspect the .class files
  23. * rather than executing them.
  24. */
  25. public class SanityTests extends org.aspectj.testing.XMLBasedAjcTestCase {
  26. public static final int bytecode_version_for_JDK_level = Constants.ClassFileVersion.of(6).MAJOR;
  27. // Incredibly trivial test programs that check the compiler works at all (these are easy-ish to debug)
  28. public void testSimpleJava_A() {
  29. runTest("simple - a");
  30. }
  31. public void testSimpleJava_B() {
  32. runTest("simple - b");
  33. }
  34. public void testSimpleCode_C() {
  35. runTest("simple - c");
  36. }
  37. public void testSimpleCode_D() {
  38. runTest("simple - d");
  39. }
  40. public void testSimpleCode_E() {
  41. runTest("simple - e");
  42. }
  43. public void testSimpleCode_F() {
  44. runTest("simple - f");
  45. }
  46. public void testSimpleCode_G() {
  47. runTest("simple - g");
  48. }
  49. public void testSimpleCode_H() {
  50. runTest("simple - h", true);
  51. }
  52. public void testSimpleCode_I() {
  53. runTest("simple - i");
  54. }
  55. // Check the version number in the classfiles is correct when Java6 options specified
  56. public void testVersionCorrect1() throws ClassNotFoundException {
  57. runTest("simple - j");
  58. checkVersion("A", bytecode_version_for_JDK_level, 0);
  59. }
  60. public void testVersionCorrect2() throws ClassNotFoundException {
  61. runTest("simple - k");
  62. checkVersion("A", bytecode_version_for_JDK_level, 0);
  63. }
  64. public void testVersionCorrect3() throws ClassNotFoundException {
  65. runTest("simple - l");
  66. checkVersion("A", bytecode_version_for_JDK_level, 0);
  67. }
  68. public void testVersionCorrect4() throws ClassNotFoundException {// check it is 49.0 when -1.5 is specified
  69. runTest("simple - m");
  70. checkVersion("A", Constants.ClassFileVersion.of(5).MAJOR, 0);
  71. }
  72. // Check the stackmap stuff appears for methods in a Java6 file
  73. // public void testStackMapAttributesAppear() throws ClassNotFoundException {
  74. // runTest("simple - n");
  75. // checkStackMapExistence("A","<init>_<clinit>");
  76. // checkStackMapExistence("X","<init>_<clinit>_ajc$pointcut$$complicatedPointcut$1fe");
  77. // }
  78. /* For the specified class, check that each method has a stackmap attribute */
  79. @SuppressWarnings("unused")
  80. private void checkStackMapExistence(String classname, String toIgnore) throws ClassNotFoundException {
  81. toIgnore = "_" + (toIgnore == null ? "" : toIgnore) + "_";
  82. JavaClass jc = getClassFrom(ajc.getSandboxDirectory(), classname);
  83. Method[] methods = jc.getMethods();
  84. for (Method method : methods) {
  85. if (toIgnore.contains("_" + method.getName() + "_")) {
  86. continue;
  87. }
  88. boolean hasStackMapAttribute = findAttribute(method.getAttributes(), "StackMapTable");
  89. if (!hasStackMapAttribute) {
  90. fail("Could not find StackMap attribute for method " + method.getName());
  91. }
  92. }
  93. }
  94. private boolean findAttribute(Attribute[] attrs, String attributeName) {
  95. if (attrs == null) {
  96. return false;
  97. }
  98. for (Attribute attribute : attrs) {
  99. if (attribute.getName().equals(attributeName)) {
  100. return true;
  101. }
  102. // System.out.println(attribute.getName());
  103. if (attribute.getName().equals("Code")) {
  104. Code c = (Code) attribute;
  105. Attribute[] codeAttributes = c.getAttributes();
  106. for (Attribute codeAttribute : codeAttributes) {
  107. if (codeAttribute.getName().equals(attributeName)) {
  108. return true;
  109. // System.out.println(codeAttribute.getName());
  110. }
  111. }
  112. }
  113. }
  114. return false;
  115. }
  116. // Check the stackmap stuff is removed when a method gets woven (for now...)
  117. // public void testStackMapAttributesDeletedInWovenCode() {
  118. // fail("Not implemented");
  119. // }
  120. // ///////////////////////////////////////
  121. public static Test suite() {
  122. return XMLBasedAjcTestCase.loadSuite(SanityTests.class);
  123. }
  124. protected java.net.URL getSpecFile() {
  125. return getClassResource("sanity-tests.xml");
  126. }
  127. }