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.

TypePatternListTestCase.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.Arrays;
  15. import org.aspectj.weaver.bcel.*;
  16. import org.aspectj.util.*;
  17. import junit.framework.TestCase;
  18. import org.aspectj.weaver.*;
  19. /**
  20. * @author hugunin
  21. *
  22. * To change this generated comment edit the template variable "typecomment":
  23. * Window>Preferences>Java>Templates.
  24. * To enable and disable the creation of type comments go to
  25. * Window>Preferences>Java>Code Generation.
  26. */
  27. public class TypePatternListTestCase extends TestCase {
  28. /**
  29. * Constructor for PatternTestCase.
  30. * @param name
  31. */
  32. public TypePatternListTestCase(String name) {
  33. super(name);
  34. }
  35. World world;
  36. //XXX when instanceof matching works add tests for that here
  37. public void testMatch() {
  38. world = new BcelWorld();
  39. checkStaticMatch("()", new String[] {}, FuzzyBoolean.YES);
  40. checkStaticMatch("()", new String[] {"java.lang.Object"}, FuzzyBoolean.NO);
  41. checkStaticMatch("(java.lang.Object)", new String[] {"java.lang.Object"}, FuzzyBoolean.YES);
  42. checkStaticMatch("(java.lang.String)", new String[] {"java.lang.Object"}, FuzzyBoolean.NO);
  43. checkStaticMatch("(java.lang.Object)", new String[] {"java.lang.String"}, FuzzyBoolean.NO);
  44. checkStaticMatch("()", new String[] {"java.lang.Object"}, FuzzyBoolean.NO);
  45. checkStaticMatch("(..)", new String[] {}, FuzzyBoolean.YES);
  46. checkStaticMatch("(..)", new String[] {"int", "char"}, FuzzyBoolean.YES);
  47. checkStaticMatch("(int,..,int)", new String[] {"int", "int"}, FuzzyBoolean.YES);
  48. checkStaticMatch("(int,..)", new String[] {}, FuzzyBoolean.NO);
  49. checkStaticMatch("(int,..)", new String[] {"int"}, FuzzyBoolean.YES);
  50. checkStaticMatch("(..,int,..)", new String[] {"int"}, FuzzyBoolean.YES);
  51. // these checks are taken from new/ExpandedDotPattern.java
  52. stupidCheck("( .., .., ..)", new boolean[] { true, true, true, true, true });
  53. stupidCheck("( .., .., int)", new boolean[] { false, true, true, true, true });
  54. stupidCheck("( .., int, ..)", new boolean[] { false, true, true, true, true });
  55. stupidCheck("( .., int, int)", new boolean[] { false, false, true, true, true });
  56. stupidCheck("(int, .., ..)", new boolean[] { false, true, true, true, true });
  57. stupidCheck("(int, .., int)", new boolean[] { false, false, true, true, true });
  58. stupidCheck("(int, int, ..)", new boolean[] { false, false, true, true, true });
  59. stupidCheck("(int, int, int)", new boolean[] { false, false, false, true, false });
  60. stupidCheck("( .., .., .., ..)", new boolean[] { true, true, true, true, true });
  61. stupidCheck("( .., .., .., int)", new boolean[] { false, true, true, true, true });
  62. stupidCheck("( .., .., int, ..)", new boolean[] { false, true, true, true, true });
  63. stupidCheck("( .., .., int, int)", new boolean[] { false, false, true, true, true });
  64. stupidCheck("( .., int, .., ..)", new boolean[] { false, true, true, true, true });
  65. stupidCheck("( .., int, .., int)", new boolean[] { false, false, true, true, true });
  66. stupidCheck("( .., int, int, ..)", new boolean[] { false, false, true, true, true });
  67. stupidCheck("( .., int, int, int)", new boolean[] { false, false, false, true, true });
  68. stupidCheck("(int, .., .., ..)", new boolean[] { false, true, true, true, true });
  69. stupidCheck("(int, .., .., int)", new boolean[] { false, false, true, true, true });
  70. stupidCheck("(int, .., int, ..)", new boolean[] { false, false, true, true, true });
  71. stupidCheck("(int, .., int, int)", new boolean[] { false, false, false, true, true });
  72. stupidCheck("(int, int, .., ..)", new boolean[] { false, false, true, true, true });
  73. stupidCheck("(int, int, .., int)", new boolean[] { false, false, false, true, true });
  74. stupidCheck("(int, int, int, ..)", new boolean[] { false, false, false, true, true });
  75. stupidCheck("(int, int, int, int)", new boolean[] { false, false, false, false, true });
  76. }
  77. private TypePatternList makeArgumentsPattern(String pattern) {
  78. return new PatternParser(pattern).parseArgumentsPattern();
  79. }
  80. private void checkStaticMatch(String pattern, String[] names,
  81. FuzzyBoolean shouldMatchStatically) {
  82. // We're only doing TypePattern.STATIC matching here because my intent was
  83. // to test the wildcarding, and we don't do DYNAMIC matching on wildcarded things.
  84. TypePatternList p = makeArgumentsPattern(pattern);
  85. ResolvedTypeX[] types = new ResolvedTypeX[names.length];
  86. for (int i = 0; i < names.length; i++) {
  87. types[i] = world.resolve(names[i]);
  88. }
  89. p.resolveBindings(makeTestScope(), Bindings.NONE, false, false);
  90. //System.out.println("type: " + type);
  91. FuzzyBoolean result = p.matches(types, TypePattern.STATIC);
  92. String msg = "matches statically " + pattern + " to " + Arrays.asList(types);
  93. assertEquals(msg, shouldMatchStatically, result);
  94. }
  95. private TestScope makeTestScope() {
  96. TestScope scope = new TestScope(CollectionUtil.NO_STRINGS, CollectionUtil.NO_STRINGS, world);
  97. return scope;
  98. }
  99. public void stupidCheck(String pattern, boolean[] matches) {
  100. TypePatternList p = makeArgumentsPattern(pattern);
  101. p.resolveBindings(makeTestScope(), Bindings.NONE, false, false);
  102. int len = matches.length;
  103. for (int j = 0; j < len; j++) {
  104. ResolvedTypeX[] types = new ResolvedTypeX[j];
  105. for (int i = 0; i < j; i++) {
  106. types[i] = world.resolve("int");
  107. }
  108. FuzzyBoolean result = p.matches(types, TypePattern.STATIC);
  109. String msg = "matches statically " + pattern + " to " + Arrays.asList(types);
  110. assertEquals(msg, FuzzyBoolean.fromBoolean(matches[j]), result);
  111. }
  112. }
  113. public void testSerialization() throws IOException {
  114. String[] patterns = new String[] {
  115. "( .., .., .., int)",
  116. "( .., .., int, ..)",
  117. "( .., .., int, int)",
  118. "( .., int, .., ..)",
  119. "( .., int, .., int)",
  120. "( .., int, int, ..)",
  121. "( .., int, int, int)",
  122. "(int, .., .., ..)",
  123. "(int, .., .., int)",
  124. "(int, .., int, ..)",
  125. "(int, .., int, int)",
  126. "(int, int, .., ..)",
  127. "(int, int, .., int)",
  128. "(int, int, int, ..)",
  129. "(int, int, int, int)"
  130. };
  131. for (int i=0, len=patterns.length; i < len; i++) {
  132. checkSerialization(patterns[i]);
  133. }
  134. }
  135. /**
  136. * Method checkSerialization.
  137. * @param string
  138. */
  139. private void checkSerialization(String string) throws IOException {
  140. TypePatternList p = makeArgumentsPattern(string);
  141. ByteArrayOutputStream bo = new ByteArrayOutputStream();
  142. DataOutputStream out = new DataOutputStream(bo);
  143. p.write(out);
  144. out.close();
  145. ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
  146. DataInputStream in = new DataInputStream(bi);
  147. TypePatternList newP = TypePatternList.read(in, null);
  148. assertEquals("write/read", p, newP);
  149. }
  150. }