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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Andy Clement - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.systemtest.ajc160;
  12. import java.io.File;
  13. import junit.framework.Test;
  14. import org.aspectj.apache.bcel.classfile.Attribute;
  15. import org.aspectj.apache.bcel.classfile.Code;
  16. import org.aspectj.apache.bcel.classfile.JavaClass;
  17. import org.aspectj.apache.bcel.classfile.Method;
  18. import org.aspectj.testing.XMLBasedAjcTestCase;
  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. // Incredibly trivial test programs that check the compiler works at all (these are easy-ish to debug)
  27. public void testSimpleJava_A() {
  28. runTest("simple - a");
  29. }
  30. public void testSimpleJava_B() {
  31. runTest("simple - b");
  32. }
  33. public void testSimpleCode_C() {
  34. runTest("simple - c");
  35. }
  36. public void testSimpleCode_D() {
  37. runTest("simple - d");
  38. }
  39. public void testSimpleCode_E() {
  40. runTest("simple - e");
  41. }
  42. public void testSimpleCode_F() {
  43. runTest("simple - f");
  44. }
  45. public void testSimpleCode_G() {
  46. runTest("simple - g");
  47. }
  48. public void testSimpleCode_H() {
  49. runTest("simple - h", true);
  50. }
  51. public void testSimpleCode_I() {
  52. runTest("simple - i");
  53. }
  54. // Check the version number in the classfiles is correct when Java6 options specified
  55. public void testVersionCorrect1() throws ClassNotFoundException {
  56. runTest("simple - j");
  57. checkVersion("A", 50, 0);
  58. }
  59. public void testVersionCorrect2() throws ClassNotFoundException {
  60. runTest("simple - k");
  61. checkVersion("A", 50, 0);
  62. }
  63. public void testVersionCorrect3() throws ClassNotFoundException {
  64. runTest("simple - l");
  65. checkVersion("A", 50, 0);
  66. }
  67. public void testVersionCorrect4() throws ClassNotFoundException {// check it is 49.0 when -1.5 is specified
  68. runTest("simple - m");
  69. checkVersion("A", 49, 0);
  70. }
  71. // Check the stackmap stuff appears for methods in a Java6 file
  72. // public void testStackMapAttributesAppear() throws ClassNotFoundException {
  73. // runTest("simple - n");
  74. // checkStackMapExistence("A","<init>_<clinit>");
  75. // checkStackMapExistence("X","<init>_<clinit>_ajc$pointcut$$complicatedPointcut$1fe");
  76. // }
  77. /* For the specified class, check that each method has a stackmap attribute */
  78. @SuppressWarnings("unused")
  79. private void checkStackMapExistence(String classname, String toIgnore) throws ClassNotFoundException {
  80. toIgnore = "_" + (toIgnore == null ? "" : toIgnore) + "_";
  81. JavaClass jc = getClassFrom(ajc.getSandboxDirectory(), classname);
  82. Method[] methods = jc.getMethods();
  83. for (int i = 0; i < methods.length; i++) {
  84. Method method = methods[i];
  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 (int i = 0; i < attrs.length; i++) {
  99. Attribute attribute = attrs[i];
  100. if (attribute.getName().equals(attributeName)) {
  101. return true;
  102. }
  103. // System.out.println(attribute.getName());
  104. if (attribute.getName().equals("Code")) {
  105. Code c = (Code) attribute;
  106. Attribute[] codeAttributes = c.getAttributes();
  107. for (int j = 0; j < codeAttributes.length; j++) {
  108. Attribute codeAttribute = codeAttributes[j];
  109. if (codeAttribute.getName().equals(attributeName)) {
  110. return true;
  111. // System.out.println(codeAttribute.getName());
  112. }
  113. }
  114. }
  115. }
  116. return false;
  117. }
  118. private void checkVersion(String classname, int major, int minor) throws ClassNotFoundException {
  119. JavaClass jc = getClassFrom(ajc.getSandboxDirectory(), classname);
  120. if (jc.getMajor() != major) {
  121. fail("Expected major version to be " + major + " but was " + jc.getMajor());
  122. }
  123. if (jc.getMinor() != minor) {
  124. fail("Expected minor version to be " + minor + " but was " + jc.getMinor());
  125. }
  126. }
  127. // Check the stackmap stuff is removed when a method gets woven (for now...)
  128. // public void testStackMapAttributesDeletedInWovenCode() {
  129. // fail("Not implemented");
  130. // }
  131. // ///////////////////////////////////////
  132. public static Test suite() {
  133. return XMLBasedAjcTestCase.loadSuite(SanityTests.class);
  134. }
  135. protected java.net.URL getSpecFile() {
  136. return getClassResource("sanity-tests.xml");
  137. }
  138. }