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.

ModifiersPatternTestCase.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.ByteArrayInputStream;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.IOException;
  16. import java.lang.reflect.Modifier;
  17. import org.aspectj.weaver.CompressingDataOutputStream;
  18. import org.aspectj.weaver.VersionedDataInputStream;
  19. import org.aspectj.weaver.World;
  20. import org.aspectj.weaver.reflect.ReflectionWorld;
  21. public class ModifiersPatternTestCase extends PatternsTestCase {
  22. public void testMatch() {
  23. int[] publicMatches = new int[] { Modifier.PUBLIC, Modifier.PUBLIC | Modifier.STATIC,
  24. Modifier.PUBLIC | Modifier.STATIC | Modifier.STRICT | Modifier.FINAL, };
  25. int[] publicFailures = new int[] { Modifier.PRIVATE, 0, Modifier.STATIC | Modifier.STRICT | Modifier.FINAL, };
  26. int[] publicStaticMatches = new int[] { Modifier.PUBLIC | Modifier.STATIC,
  27. Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL | Modifier.STRICT, };
  28. int[] publicStaticFailures = new int[] { 0, Modifier.PUBLIC, Modifier.STATIC, };
  29. int[] trickMatches = new int[] { Modifier.PRIVATE, Modifier.PRIVATE | Modifier.ABSTRACT, Modifier.PRIVATE | Modifier.FINAL, };
  30. int[] trickFailures = new int[] { Modifier.PUBLIC, Modifier.PRIVATE | Modifier.STATIC, Modifier.PRIVATE | Modifier.STRICT, };
  31. int[] none = new int[0];
  32. checkMatch("", publicMatches, none);
  33. checkMatch("", publicFailures, none);
  34. checkMatch("!public", publicFailures, publicMatches);
  35. checkMatch("public", publicMatches, publicFailures);
  36. checkMatch("public static", none, publicFailures);
  37. checkMatch("public static", publicStaticMatches, publicStaticFailures);
  38. checkMatch("private !static !strictfp", trickMatches, trickFailures);
  39. checkMatch("private !static !strictfp", none, publicMatches);
  40. checkMatch("private !static !strictfp", none, publicStaticMatches);
  41. }
  42. private ModifiersPattern makeModifiersPattern(String pattern) {
  43. return new PatternParser(pattern).parseModifiersPattern();
  44. }
  45. private void checkMatch(String pattern, int[] shouldMatch, int[] shouldFail) {
  46. ModifiersPattern p = makeModifiersPattern(pattern);
  47. checkMatch(p, shouldMatch, true);
  48. checkMatch(p, shouldFail, false);
  49. }
  50. private void checkMatch(ModifiersPattern p, int[] matches, boolean shouldMatch) {
  51. for (int i = 0; i < matches.length; i++) {
  52. boolean result = p.matches(matches[i]);
  53. String msg = "matches " + p + " to " + Modifier.toString(matches[i]) + " expected ";
  54. if (shouldMatch) {
  55. assertTrue(msg + shouldMatch, result);
  56. } else {
  57. assertTrue(msg + shouldMatch, !result);
  58. }
  59. }
  60. }
  61. public void testSerialization() throws IOException {
  62. String[] patterns = new String[] { "", "!public", "public", "public static", "private !static !strictfp", };
  63. for (int i = 0, len = patterns.length; i < len; i++) {
  64. checkSerialization(patterns[i]);
  65. }
  66. }
  67. /**
  68. * Method checkSerialization.
  69. *
  70. * @param string
  71. */
  72. private void checkSerialization(String string) throws IOException {
  73. ModifiersPattern p = makeModifiersPattern(string);
  74. ByteArrayOutputStream bo = new ByteArrayOutputStream();
  75. ConstantPoolSimulator cps = new ConstantPoolSimulator();
  76. CompressingDataOutputStream out = new CompressingDataOutputStream(bo, cps);
  77. p.write(out);
  78. out.close();
  79. ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
  80. VersionedDataInputStream in = new VersionedDataInputStream(bi, cps);
  81. ModifiersPattern newP = ModifiersPattern.read(in);
  82. assertEquals("write/read", p, newP);
  83. }
  84. public World getWorld() {
  85. return new ReflectionWorld(true, this.getClass().getClassLoader());
  86. }
  87. }