1 | /* |
2 | * Copyright (c) 2001-2009, Jean Tessier |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * |
12 | * * Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * * Neither the name of Jean Tessier nor the names of his contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | */ |
32 | |
33 | package com.jeantessier.classreader; |
34 | |
35 | import org.jmock.*; |
36 | import org.jmock.integration.junit3.*; |
37 | |
38 | public class TestSymbolGathererWithStrategy extends MockObjectTestCase { |
39 | private SymbolGathererStrategy mockStrategy; |
40 | |
41 | private SymbolGatherer sut; |
42 | |
43 | protected void setUp() throws Exception { |
44 | super.setUp(); |
45 | |
46 | mockStrategy = mock(SymbolGathererStrategy.class); |
47 | |
48 | sut = new SymbolGatherer(mockStrategy); |
49 | } |
50 | |
51 | public void testVisitClassfile_NotMatching() { |
52 | final Classfile mockClassfile = mock(Classfile.class); |
53 | |
54 | checking(new Expectations() {{ |
55 | one (mockStrategy).isMatching(mockClassfile); will(returnValue(false)); |
56 | |
57 | one (mockClassfile).getAttributes(); |
58 | one (mockClassfile).getAllFields(); |
59 | one (mockClassfile).getAllMethods(); |
60 | }}); |
61 | |
62 | sut.visitClassfile(mockClassfile); |
63 | |
64 | assertTrue("Added non-matching class " + sut.getCollection(), sut.getCollection().isEmpty()); |
65 | } |
66 | |
67 | public void testVisitClassfile_Matching() { |
68 | final String expectedName = "Foobar"; |
69 | |
70 | final Classfile mockClassfile = mock(Classfile.class); |
71 | |
72 | checking(new Expectations() {{ |
73 | one (mockStrategy).isMatching(mockClassfile); will(returnValue(true)); |
74 | one (mockClassfile).getClassName(); will(returnValue(expectedName)); |
75 | |
76 | one (mockClassfile).getAttributes(); |
77 | one (mockClassfile).getAllFields(); |
78 | one (mockClassfile).getAllMethods(); |
79 | }}); |
80 | |
81 | sut.visitClassfile(mockClassfile); |
82 | |
83 | assertEquals("Wrong size for " + sut.getCollection(), 1, sut.getCollection().size()); |
84 | assertTrue("Missing class name " + sut.getCollection(), sut.getCollection().contains(expectedName)); |
85 | } |
86 | |
87 | public void testVisitField_info_NotMatching() { |
88 | final Field_info mockField = mock(Field_info.class); |
89 | |
90 | checking(new Expectations() {{ |
91 | one (mockStrategy).isMatching(mockField); will(returnValue(false)); |
92 | |
93 | one (mockField).getAttributes(); |
94 | }}); |
95 | |
96 | sut.visitField_info(mockField); |
97 | |
98 | assertTrue("Added non-matching class " + sut.getCollection(), sut.getCollection().isEmpty()); |
99 | } |
100 | |
101 | public void testVisitField_info_Matching() { |
102 | final String expectedName = "Foobar.foobar"; |
103 | |
104 | final Field_info mockField = mock(Field_info.class); |
105 | |
106 | checking(new Expectations() {{ |
107 | one (mockStrategy).isMatching(mockField); will(returnValue(true)); |
108 | one (mockField).getFullSignature(); will(returnValue(expectedName)); |
109 | |
110 | one (mockField).getAttributes(); |
111 | }}); |
112 | |
113 | sut.visitField_info(mockField); |
114 | |
115 | assertEquals("Wrong size for " + sut.getCollection(), 1, sut.getCollection().size()); |
116 | assertTrue("Missing class name " + sut.getCollection(), sut.getCollection().contains(expectedName)); |
117 | } |
118 | |
119 | public void testVisitMethod_info_NotMatching() { |
120 | final Method_info mockMethod = mock(Method_info.class); |
121 | |
122 | checking(new Expectations() {{ |
123 | one (mockStrategy).isMatching(mockMethod); will(returnValue(false)); |
124 | |
125 | one (mockMethod).getAttributes(); |
126 | }}); |
127 | |
128 | sut.visitMethod_info(mockMethod); |
129 | |
130 | assertTrue("Added non-matching class " + sut.getCollection(), sut.getCollection().isEmpty()); |
131 | } |
132 | |
133 | public void testVisitMethod_info_Matching() { |
134 | final String expectedName = "Foobar.foobar"; |
135 | |
136 | final Method_info mockMethod = mock(Method_info.class); |
137 | |
138 | checking(new Expectations() {{ |
139 | one (mockStrategy).isMatching(mockMethod); will(returnValue(true)); |
140 | one (mockMethod).getFullSignature(); will(returnValue(expectedName)); |
141 | |
142 | one (mockMethod).getAttributes(); |
143 | }}); |
144 | |
145 | sut.visitMethod_info(mockMethod); |
146 | |
147 | assertEquals("Wrong size for " + sut.getCollection(), 1, sut.getCollection().size()); |
148 | assertTrue("Missing class name " + sut.getCollection(), sut.getCollection().contains(expectedName)); |
149 | } |
150 | |
151 | |
152 | public void testVisitLocalVariable_NotMatching() { |
153 | final LocalVariable mockLocalVariable = mock(LocalVariable.class); |
154 | |
155 | checking(new Expectations() {{ |
156 | one (mockStrategy).isMatching(mockLocalVariable); will(returnValue(false)); |
157 | }}); |
158 | |
159 | sut.visitLocalVariable(mockLocalVariable); |
160 | |
161 | assertTrue("Added non-matching class " + sut.getCollection(), sut.getCollection().isEmpty()); |
162 | } |
163 | |
164 | public void testVisitLocalVariable_Matching() { |
165 | final String expectedName = "Foobar.foobar"; |
166 | |
167 | final Method_info mockMethod = mock(Method_info.class); |
168 | final LocalVariable mockLocalVariable = mock(LocalVariable.class); |
169 | |
170 | checking(new Expectations() {{ |
171 | one (mockStrategy).isMatching(mockLocalVariable); will(returnValue(true)); |
172 | one (mockMethod).getFullSignature(); will(returnValue(expectedName)); |
173 | one (mockLocalVariable).getName(); will(returnValue(expectedName)); |
174 | }}); |
175 | |
176 | sut.setCurrentMethodForTesting(mockMethod); |
177 | sut.visitLocalVariable(mockLocalVariable); |
178 | |
179 | assertEquals("Wrong size for " + sut.getCollection(), 1, sut.getCollection().size()); |
180 | } |
181 | } |