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.

NamePatternTestCase.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.ByteArrayInputStream;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.DataOutputStream;
  16. import java.io.IOException;
  17. import org.aspectj.weaver.VersionedDataInputStream;
  18. import junit.framework.TestCase;
  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 NamePatternTestCase extends TestCase {
  28. static String[] matchAll = new String[] {
  29. "*****",
  30. "*"
  31. };
  32. static String[] match1 = new String[] {
  33. "abcde", "abc*", "abcd*", "abcde*",
  34. "*e", "*cde", "*abcde",
  35. "a*e", "ab*e", "abc*de",
  36. "*a*b*c*d*e*", "a*c*e", "a***bcde",
  37. "*d*",
  38. };
  39. static String[] match2 = new String[] {
  40. "abababab",
  41. "aba*", "abab*", "abababab*",
  42. "*b", "*ab", "*ababab", "*abababab",
  43. "a*b", "ab*b", "abab*abab",
  44. "*a*b*a*b*a*b*a*b*", "a*****b", "a**b", "ab*b*b",
  45. };
  46. /**
  47. * Constructor for PatternTestCase.
  48. * @param name
  49. */
  50. public NamePatternTestCase(String name) {
  51. super(name);
  52. }
  53. public void testMatch() {
  54. checkMatch("abcde", matchAll, true);
  55. checkMatch("abcde", match1, true);
  56. checkMatch("abcde", match2, false);
  57. checkMatch("abababab", matchAll, true);
  58. checkMatch("abababab", match1, false);
  59. checkMatch("abababab", match2, true);
  60. }
  61. /**
  62. * Method checkMatch.
  63. * @param string
  64. * @param matchAll
  65. * @param b
  66. */
  67. private void checkMatch(String string, String[] patterns, boolean shouldMatch) {
  68. for (int i=0, len=patterns.length; i < len; i++) {
  69. NamePattern p = new NamePattern(patterns[i]);
  70. checkMatch(string, p, shouldMatch);
  71. }
  72. }
  73. private void checkMatch(String string, NamePattern p, boolean shouldMatch) {
  74. String msg = "matching " + string + " to " + p;
  75. assertEquals(msg, shouldMatch, p.matches(string));
  76. }
  77. public void testSerialization() throws IOException {
  78. checkSerialization(matchAll);
  79. checkSerialization(match1);
  80. checkSerialization(match2);
  81. }
  82. private void checkSerialization(String[] patterns) throws IOException {
  83. for (int i=0, len=patterns.length; i < len; i++) {
  84. NamePattern p = new NamePattern(patterns[i]);
  85. checkSerialization(p);
  86. }
  87. }
  88. private void checkSerialization(NamePattern p) throws IOException {
  89. ByteArrayOutputStream bo = new ByteArrayOutputStream();
  90. DataOutputStream out = new DataOutputStream(bo);
  91. p.write(out);
  92. out.close();
  93. ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
  94. VersionedDataInputStream in = new VersionedDataInputStream(bi);
  95. NamePattern newP = NamePattern.read(in);
  96. assertEquals("write/read", p, newP);
  97. }
  98. }