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.

TypePatternTestCase.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.*;
  14. import java.util.*;
  15. import org.aspectj.weaver.bcel.*;
  16. import org.aspectj.bridge.AbortException;
  17. import org.aspectj.util.FuzzyBoolean;
  18. import junit.framework.TestCase;
  19. import org.aspectj.weaver.*;
  20. public class TypePatternTestCase extends TestCase {
  21. /**
  22. * Constructor for PatternTestCase.
  23. * @param name
  24. */
  25. public TypePatternTestCase(String name) {
  26. super(name);
  27. }
  28. World world;
  29. public void testStaticMatch() {
  30. world = new BcelWorld();
  31. checkMatch("java.lang.Object", "java.lang.Object", true);
  32. checkMatch("java.lang.Object+", "java.lang.Object", true);
  33. checkMatch("java.lang.Object+", "java.lang.String", true);
  34. checkMatch("java.lang.String+", "java.lang.Object", false);
  35. checkMatch("java.lang.Integer", "java.lang.String", false);
  36. checkMatch("java.lang.Integer", "int", false);
  37. checkMatch("java.lang.Number+", "java.lang.Integer", true);
  38. checkMatch("java..*", "java.lang.Integer", true);
  39. checkMatch("java..*", "java.lang.reflect.Modifier", true);
  40. checkMatch("java..*", "int", false);
  41. checkMatch("java..*", "javax.swing.Action", false);
  42. checkMatch("java..*+", "javax.swing.Action", true);
  43. checkMatch("*.*.Object", "java.lang.Object", true);
  44. checkMatch("*.Object", "java.lang.Object", false);
  45. checkMatch("*..*", "java.lang.Object", true);
  46. checkMatch("*..*", "int", false);
  47. checkMatch("java..Modifier", "java.lang.reflect.Modifier", true);
  48. checkMatch("java.lang.reflect.Mod..ifier", "java.lang.reflect.Modifier", false);
  49. checkMatch("java..reflect..Modifier", "java.lang.reflect.Modifier", true);
  50. checkMatch("java..lang..Modifier", "java.lang.reflect.Modifier", true);
  51. checkMatch("java..*..Modifier", "java.lang.reflect.Modifier", true);
  52. checkMatch("java..*..*..Modifier", "java.lang.reflect.Modifier", true);
  53. checkMatch("java..*..*..*..Modifier", "java.lang.reflect.Modifier", false);
  54. //checkMatch("java..reflect..Modifier", "java.lang.reflect.Modxifier", false);
  55. checkMatch("ja*va..Modifier", "java.lang.reflect.Modifier", true);
  56. checkMatch("java..*..Mod*ifier", "java.lang.reflect.Modifier", true);
  57. }
  58. // three levels:
  59. // 0. defined in current compilation unit, or imported by name
  60. // 1. defined in current package/type/whatever
  61. // 2. defined in package imported by *
  62. /**
  63. * We've decided not to test this here, but rather in any compilers
  64. */
  65. public void testImportResolve() {
  66. world = new BcelWorld();
  67. // checkIllegalImportResolution("List", new String[] { "java.util", "java.awt", },
  68. // ZERO_STRINGS);
  69. }
  70. // Assumption for bcweaver: Already resolved type patterns with no *s or ..'s into exact type
  71. // patterns. Exact type patterns don't have import lists. non-exact-type pattens don't
  72. // care about precedence, so the current package can be included with all the other packages,
  73. // and we don't care about compilation units, and we don't care about ordering.
  74. // only giving this wild-type patterns
  75. public void testImportMatch() {
  76. world = new BcelWorld();
  77. checkImportMatch("*List", new String[] { "java.awt.", }, ZERO_STRINGS, "java.awt.List", true);
  78. checkImportMatch("*List", new String[] { "java.awt.", }, ZERO_STRINGS, "java.awt.List", true);
  79. checkImportMatch("*List", new String[] { "java.awt.", }, ZERO_STRINGS, "java.util.List", false);
  80. checkImportMatch("*List", new String[] { "java.util.", }, ZERO_STRINGS, "java.awt.List", false);
  81. checkImportMatch("*List", new String[] { "java.util.", }, ZERO_STRINGS, "java.util.List", true);
  82. checkImportMatch("*List", ZERO_STRINGS, new String[] { "java.awt.List", }, "java.awt.List", true);
  83. checkImportMatch("awt.*List", ZERO_STRINGS, new String[] { "java.awt.List", }, "java.awt.List", false);
  84. checkImportMatch("*Foo", ZERO_STRINGS, new String[] { "java.awt.List", }, "java.awt.List", false);
  85. checkImportMatch("*List", new String[] { "java.util.", "java.awt.", },
  86. ZERO_STRINGS, "java.util.List", true);
  87. checkImportMatch("*List", new String[] { "java.util.", "java.awt.", },
  88. ZERO_STRINGS, "java.awt.List", true);
  89. checkImportMatch("*..List", new String[] { "java.util." }, ZERO_STRINGS, "java.util.List", true);
  90. checkImportMatch("*..List", new String[] { "java.util." }, ZERO_STRINGS, "java.awt.List", true);
  91. }
  92. public void testImportMatchWithInners() {
  93. world = new BcelWorld();
  94. checkImportMatch("*Entry", new String[] { "java.util.", "java.util.Map$"}, ZERO_STRINGS,
  95. "java.util.Map$Entry", true);
  96. checkImportMatch("java.util.Map.*Entry", ZERO_STRINGS, ZERO_STRINGS,
  97. "java.util.Map$Entry", true);
  98. checkImportMatch("*Entry", new String[] { "java.util.", }, ZERO_STRINGS,
  99. "java.util.Map$Entry", false);
  100. checkImportMatch("*.Entry", new String[] { "java.util.", }, ZERO_STRINGS,
  101. "java.util.Map$Entry", true);
  102. checkImportMatch("Map.*", new String[] { "java.util.", }, ZERO_STRINGS,
  103. "java.util.Map$Entry", true);
  104. checkImportMatch("Map.*", ZERO_STRINGS, new String[] { "java.util.Map" },
  105. "java.util.Map$Entry", true);
  106. }
  107. private void checkImportMatch(
  108. String wildPattern,
  109. String[] importedPackages,
  110. String[] importedNames,
  111. String matchName,
  112. boolean shouldMatch)
  113. {
  114. WildTypePattern p = makeResolvedWildTypePattern(wildPattern, importedPackages, importedNames);
  115. checkPatternMatch(p, matchName, shouldMatch);
  116. }
  117. private WildTypePattern makeResolvedWildTypePattern(
  118. String wildPattern,
  119. String[] importedPackages,
  120. String[] importedNames)
  121. {
  122. WildTypePattern unresolved = (WildTypePattern) new PatternParser(wildPattern).parseTypePattern();
  123. WildTypePattern resolved = resolve(unresolved, importedPackages, importedNames);
  124. return resolved;
  125. }
  126. private WildTypePattern resolve(
  127. WildTypePattern unresolved,
  128. String[] importedPrefixes,
  129. String[] importedNames)
  130. {
  131. TestScope scope = makeTestScope();
  132. scope.setImportedPrefixes(importedPrefixes);
  133. scope.setImportedNames(importedNames);
  134. return (WildTypePattern) unresolved.resolveBindings(scope, Bindings.NONE, false, false);
  135. }
  136. public static final String[] ZERO_STRINGS = new String[0];
  137. public void testInstanceofMatch() {
  138. world = new BcelWorld();
  139. checkInstanceofMatch("java.lang.Object", "java.lang.Object", FuzzyBoolean.YES);
  140. checkIllegalInstanceofMatch("java.lang.Object+", "java.lang.Object");
  141. checkIllegalInstanceofMatch("java.lang.Object+", "java.lang.String");
  142. checkIllegalInstanceofMatch("java.lang.String+", "java.lang.Object");
  143. checkIllegalInstanceofMatch("java.lang.*", "java.lang.Object");
  144. checkInstanceofMatch("java.lang.Integer", "java.lang.String", FuzzyBoolean.NO);
  145. checkInstanceofMatch("java.lang.Number", "java.lang.Integer", FuzzyBoolean.YES);
  146. checkInstanceofMatch("java.lang.Integer", "java.lang.Number", FuzzyBoolean.MAYBE);
  147. checkIllegalInstanceofMatch("java..Integer", "java.lang.Integer");
  148. checkInstanceofMatch("*", "java.lang.Integer", FuzzyBoolean.YES);
  149. }
  150. private void checkIllegalInstanceofMatch(String pattern, String name) {
  151. try {
  152. TypePattern p = makeTypePattern(pattern);
  153. ResolvedTypeX type = world.resolve(name);
  154. FuzzyBoolean result = p.matchesInstanceof(type);
  155. } catch (AbortException e) {
  156. return;
  157. }
  158. assertTrue("matching " + pattern + " with " + name + " should fail", false);
  159. }
  160. private void checkInstanceofMatch(String pattern, String name, FuzzyBoolean shouldMatch) {
  161. TypePattern p = makeTypePattern(pattern);
  162. ResolvedTypeX type = world.resolve(name);
  163. p = p.resolveBindings(makeTestScope(), null, false, false);
  164. //System.out.println("type: " + p);
  165. FuzzyBoolean result = p.matchesInstanceof(type);
  166. String msg = "matches " + pattern + " to " + type;
  167. assertEquals(msg, shouldMatch, result);
  168. }
  169. private TestScope makeTestScope() {
  170. TestScope scope = new TestScope(ZERO_STRINGS, ZERO_STRINGS, world);
  171. return scope;
  172. }
  173. private TypePattern makeTypePattern(String pattern) {
  174. return new PatternParser(pattern).parseSingleTypePattern();
  175. }
  176. private void checkMatch(String pattern, String name, boolean shouldMatch) {
  177. TypePattern p = makeTypePattern(pattern);
  178. p = p.resolveBindings(makeTestScope(), null, false, false);
  179. checkPatternMatch(p, name, shouldMatch);
  180. }
  181. private void checkPatternMatch(
  182. TypePattern p,
  183. String name,
  184. boolean shouldMatch)
  185. {
  186. ResolvedTypeX type = world.resolve(name);
  187. //System.out.println("type: " + type);
  188. boolean result = p.matchesStatically(type);
  189. String msg = "matches " + p + " to " + type + " expected ";
  190. if (shouldMatch) {
  191. assertTrue(msg + shouldMatch, result);
  192. } else {
  193. assertTrue(msg + shouldMatch, !result);
  194. }
  195. }
  196. public void testSerialization() throws IOException {
  197. String[] patterns = new String[] {
  198. "java.lang.Object", "java.lang.Object+", "java.lang.Integer",
  199. "int", "java..*", "java..util..*", "*.*.Object", "*",
  200. };
  201. for (int i=0, len=patterns.length; i < len; i++) {
  202. checkSerialization(patterns[i]);
  203. }
  204. }
  205. /**
  206. * Method checkSerialization.
  207. * @param string
  208. */
  209. private void checkSerialization(String string) throws IOException {
  210. TypePattern p = makeTypePattern(string);
  211. ByteArrayOutputStream bo = new ByteArrayOutputStream();
  212. DataOutputStream out = new DataOutputStream(bo);
  213. p.write(out);
  214. out.close();
  215. ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
  216. DataInputStream in = new DataInputStream(bi);
  217. TypePattern newP = TypePattern.read(in, null);
  218. assertEquals("write/read", p, newP);
  219. }
  220. }