| 1 | /* |
|---|
| 2 | * $Id: ArraysUtil.java 1011 2008-06-16 17:57:36Z amandel $ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.tracing; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | import org.objectweb.asm.Opcodes; |
|---|
| 37 | |
|---|
| 38 | import junit.framework.TestCase; |
|---|
| 39 | |
|---|
| 40 | public class AspectPatternTest extends TestCase |
|---|
| 41 | { |
|---|
| 42 | |
|---|
| 43 | public void testAspectPattern() |
|---|
| 44 | { |
|---|
| 45 | AspectPattern pat |
|---|
| 46 | = new AspectPattern("int foo.*.Bar.method()"); |
|---|
| 47 | assertEquals("No modifier!", pat.getModifiers(), 0); |
|---|
| 48 | assertEquals("method\\(\\)I", pat.getMethodRegex()); |
|---|
| 49 | assertTrue(pat.matches(0, |
|---|
| 50 | "foo/test/Bar", "method()I")); |
|---|
| 51 | assertFalse(pat.matches(0, |
|---|
| 52 | "foo/test/Bar", "method(I)I")); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public void testAspectPattern2() |
|---|
| 56 | { |
|---|
| 57 | AspectPattern pat |
|---|
| 58 | = new AspectPattern("int *.method(*)"); |
|---|
| 59 | assertEquals("No modifier!", pat.getModifiers(), 0); |
|---|
| 60 | assertEquals("method\\(\\[*([VZCBSIFJD]|L[^;]+;)\\)I", pat.getMethodRegex()); |
|---|
| 61 | assertEquals("[^/]*", pat.getClassRegex()); |
|---|
| 62 | assertTrue(pat.matches(0, |
|---|
| 63 | "Foo", "method(I)I")); |
|---|
| 64 | assertTrue(pat.matches(0, |
|---|
| 65 | "Foo", "method([I)I")); |
|---|
| 66 | assertTrue(pat.matches(0, |
|---|
| 67 | "Foo", "method(Ljava/lang/String;)I")); |
|---|
| 68 | assertFalse(pat.matches(0, |
|---|
| 69 | "Foo", "method()I")); |
|---|
| 70 | assertFalse(pat.matches(0, |
|---|
| 71 | "pkg/Foo", "method(I)I")); |
|---|
| 72 | assertFalse(pat.matches(0, |
|---|
| 73 | "pkg/Foo", "method(II)I")); |
|---|
| 74 | assertFalse(pat.matches(0, |
|---|
| 75 | "Foo", "method()I")); |
|---|
| 76 | assertFalse(pat.matches(0, |
|---|
| 77 | "Foo", "method(I)V")); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public void testAspectPattern3() |
|---|
| 81 | { |
|---|
| 82 | AspectPattern pat |
|---|
| 83 | = new AspectPattern("int foo.*.*.method(*,int)"); |
|---|
| 84 | assertEquals("No modifier!", pat.getModifiers(), 0); |
|---|
| 85 | // assertEquals("method\\(\\[*([VZCBSIFJD]|L[^;]+;)\\)I", pat.mMethodPattern); |
|---|
| 86 | // assertEquals("[^/]*", pat.mClassName); |
|---|
| 87 | assertTrue(pat.matches(0, |
|---|
| 88 | "foo/foo/Foo", "method(JI)I")); |
|---|
| 89 | assertTrue(pat.matches(0, |
|---|
| 90 | "foo/test/Bar", "method(Ljava/lang/String;I)I")); |
|---|
| 91 | assertTrue(pat.matches(0, |
|---|
| 92 | "foo/test/Bar", "method(II)I")); |
|---|
| 93 | assertFalse(pat.matches(0, |
|---|
| 94 | "foo/Bar", "method(II)I")); |
|---|
| 95 | assertFalse(pat.matches(0, |
|---|
| 96 | "foo/test/Bar", "method(IJ)I")); |
|---|
| 97 | assertFalse(pat.matches(0, |
|---|
| 98 | "foo/test/Bar", "method(II)V")); |
|---|
| 99 | assertFalse(pat.matches(0, |
|---|
| 100 | "test/test/Bar", "method(II)I")); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | public void testAspectPattern4() |
|---|
| 104 | { |
|---|
| 105 | AspectPattern pat |
|---|
| 106 | = new AspectPattern("public * org.jcoderz..*.*(..)"); |
|---|
| 107 | assertTrue("Pattern should match class!" + pat, |
|---|
| 108 | pat.matchClass("org/jcoderz/tracing/Test")); |
|---|
| 109 | assertTrue("Pattern should match method!" + pat, |
|---|
| 110 | pat.matchMethod("method(JI)I")); |
|---|
| 111 | assertTrue("Pattern should match!" + pat, pat.matches(1, |
|---|
| 112 | "org/jcoderz/tracing/Test", "method(JI)I")); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | public void testAspectPattern5() |
|---|
| 116 | { |
|---|
| 117 | AspectPattern pat |
|---|
| 118 | = new AspectPattern("public * org.jcoderz.tracing.logging.test.*.*(..)"); |
|---|
| 119 | assertTrue("Pattern should match class!" + pat, |
|---|
| 120 | pat.matchClass("org/jcoderz/tracing/logging/test/Test")); |
|---|
| 121 | assertTrue("Pattern should match method!" + pat, |
|---|
| 122 | pat.matchMethod("method(JI)I")); |
|---|
| 123 | assertTrue("Pattern should match!" + pat, pat.matches(1, |
|---|
| 124 | "org/jcoderz/tracing/logging/test/Test", "method(JI)I")); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | public void testAspectPattern6() |
|---|
| 128 | { |
|---|
| 129 | AspectPattern pat |
|---|
| 130 | = new AspectPattern("* *.set*(..)"); |
|---|
| 131 | assertTrue("Pattern should match class!" + pat, |
|---|
| 132 | pat.matchClass("Test")); |
|---|
| 133 | assertTrue("Pattern should match method!" + pat, |
|---|
| 134 | pat.matchMethod("setMethod(JI)I")); |
|---|
| 135 | assertFalse("Pattern should not match class!" + pat, |
|---|
| 136 | pat.matchClass("com/Test")); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | public void testAspectPattern7() |
|---|
| 140 | { |
|---|
| 141 | AspectPattern pat |
|---|
| 142 | = new AspectPattern("public void Account.set*(*)"); |
|---|
| 143 | assertTrue("Pattern should match class! " + pat, |
|---|
| 144 | pat.matchClass("Account")); |
|---|
| 145 | assertTrue("Pattern should match method! " + pat, |
|---|
| 146 | pat.matchMethod("setMethod(I)V")); |
|---|
| 147 | assertTrue("Pattern should match method! " + pat, |
|---|
| 148 | pat.matchAccess(Opcodes.ACC_PUBLIC)); |
|---|
| 149 | assertFalse("Pattern should not access level! " + pat, |
|---|
| 150 | pat.matchAccess(Opcodes.ACC_PRIVATE)); |
|---|
| 151 | assertFalse("Pattern should not match method! " + pat, |
|---|
| 152 | pat.matchMethod("getMethod(I)V")); |
|---|
| 153 | assertFalse("Pattern should not match class! " + pat, |
|---|
| 154 | pat.matchClass("com.Account")); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | public void testAspectPattern8() |
|---|
| 158 | { |
|---|
| 159 | AspectPattern pat |
|---|
| 160 | = new AspectPattern("public static void ...main(String[])"); |
|---|
| 161 | assertTrue("Pattern should match class! " + pat, |
|---|
| 162 | pat.matchClass("Account")); |
|---|
| 163 | assertTrue("Pattern should match class! " + pat, |
|---|
| 164 | pat.matchClass("com/Account")); |
|---|
| 165 | assertTrue("Pattern should match method! " + pat, |
|---|
| 166 | pat.matchMethod("main([Ljava/lang/String;)V")); |
|---|
| 167 | assertTrue("Pattern should match access level! " + pat, |
|---|
| 168 | pat.matchAccess(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC)); |
|---|
| 169 | assertFalse("Pattern should not match access level! " + pat, |
|---|
| 170 | pat.matchAccess(Opcodes.ACC_STATIC)); |
|---|
| 171 | assertFalse("Pattern should not access level! " + pat, |
|---|
| 172 | pat.matchAccess(Opcodes.ACC_PRIVATE)); |
|---|
| 173 | assertFalse("Pattern should not match method! " + pat, |
|---|
| 174 | pat.matchMethod("getMethod(I)V")); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | public void testAspectPattern9() |
|---|
| 178 | { |
|---|
| 179 | AspectPattern pat |
|---|
| 180 | = new AspectPattern("public static * org.jcoderz.tracing.db.DaoUtil.*(..)"); |
|---|
| 181 | assertTrue("Pattern should match class! " + pat, |
|---|
| 182 | pat.matchClass("org/jcoderz/tracing/db/DaoUtil")); |
|---|
| 183 | assertTrue("Pattern should match method! " + pat + " '" |
|---|
| 184 | + pat.getMethodPattern() + "'", |
|---|
| 185 | pat.matchMethod("create(Ljava/lang/Object;)Ljava/lang/Object;")); |
|---|
| 186 | assertTrue("Pattern should match access level! " + pat, |
|---|
| 187 | pat.matchAccess(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC)); |
|---|
| 188 | assertFalse("Pattern should not match access level! " + pat, |
|---|
| 189 | pat.matchAccess(Opcodes.ACC_STATIC)); |
|---|
| 190 | assertFalse("Pattern should not access level! " + pat, |
|---|
| 191 | pat.matchAccess(Opcodes.ACC_PRIVATE)); |
|---|
| 192 | } |
|---|
| 193 | } |
|---|