Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TypePatternTestCase.java 9.9KB

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