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.dependencyfinder.ant; |
34 | |
35 | import java.util.*; |
36 | import java.io.*; |
37 | |
38 | import org.jmock.integration.junit3.*; |
39 | import org.apache.tools.ant.*; |
40 | |
41 | import com.jeantessier.classreader.*; |
42 | |
43 | public class TestListSymbols extends MockObjectTestCase { |
44 | private Classfile mockClassfile; |
45 | private Field_info mockField; |
46 | private Method_info mockMethod; |
47 | private LocalVariable mockLocalVariable; |
48 | |
49 | private ListSymbols sut; |
50 | |
51 | protected void setUp() throws Exception { |
52 | super.setUp(); |
53 | |
54 | mockClassfile = mock(Classfile.class); |
55 | mockField = mock(Field_info.class); |
56 | mockMethod = mock(Method_info.class); |
57 | mockLocalVariable = mock(LocalVariable.class); |
58 | |
59 | sut = new ListSymbols(); |
60 | } |
61 | |
62 | public void testAllMandatoryParameters() { |
63 | sut.createPath(); |
64 | sut.setDestfile(new File("foobar")); |
65 | |
66 | sut.validateParameters(); |
67 | } |
68 | |
69 | public void testPathNotSet() { |
70 | try { |
71 | sut.validateParameters(); |
72 | fail("executed without path being set"); |
73 | } catch (BuildException ex) { |
74 | assertEquals("Wrong message", "path must be set!", ex.getMessage()); |
75 | } |
76 | } |
77 | |
78 | public void testMissingDestfile() { |
79 | sut.createPath(); |
80 | |
81 | try { |
82 | sut.validateParameters(); |
83 | fail("executed without destfile being set"); |
84 | } catch (BuildException ex) { |
85 | assertEquals("Wrong message", "destfile must be set!", ex.getMessage()); |
86 | } |
87 | } |
88 | |
89 | public void testCreateStrategy_default() { |
90 | FilteringSymbolGathererStrategy strategy = (FilteringSymbolGathererStrategy) sut.createStrategy(); |
91 | assertEquals("strategy class", FilteringSymbolGathererStrategy.class, strategy.getClass()); |
92 | List<String> includes = strategy.getIncludes(); |
93 | assertEquals("filter includes", 1, includes.size()); |
94 | assertEquals("filter includes", "//", includes.get(0)); |
95 | List<String> excludes = strategy.getExcludes(); |
96 | assertEquals("filter excludes", 0, excludes.size()); |
97 | |
98 | SymbolGathererStrategy delegateStrategy = strategy.getDelegate(); |
99 | assertEquals("delegate strategy class", DefaultSymbolGathererStrategy.class, delegateStrategy.getClass()); |
100 | assertTrue("class names", delegateStrategy.isMatching(mockClassfile)); |
101 | assertTrue("field names", delegateStrategy.isMatching(mockField)); |
102 | assertTrue("method names", delegateStrategy.isMatching(mockMethod)); |
103 | assertTrue("local variable names", delegateStrategy.isMatching(mockLocalVariable)); |
104 | } |
105 | |
106 | public void testCreateStrategy_classnames() { |
107 | sut.setClassnames(true); |
108 | |
109 | SymbolGathererStrategy strategy = ((SymbolGathererStrategyDecorator) sut.createStrategy()).getDelegate(); |
110 | assertEquals("strategy class", DefaultSymbolGathererStrategy.class, strategy.getClass()); |
111 | assertTrue("class names", strategy.isMatching(mockClassfile)); |
112 | assertFalse("field names", strategy.isMatching(mockField)); |
113 | assertFalse("method names", strategy.isMatching(mockMethod)); |
114 | assertFalse("local variable names", strategy.isMatching(mockLocalVariable)); |
115 | } |
116 | |
117 | public void testCreateStrategy_fieldnames() { |
118 | sut.setFieldnames(true); |
119 | |
120 | SymbolGathererStrategy strategy = ((SymbolGathererStrategyDecorator) sut.createStrategy()).getDelegate(); |
121 | assertEquals("strategy class", DefaultSymbolGathererStrategy.class, strategy.getClass()); |
122 | assertFalse("class names", strategy.isMatching(mockClassfile)); |
123 | assertTrue("field names", strategy.isMatching(mockField)); |
124 | assertFalse("method names", strategy.isMatching(mockMethod)); |
125 | assertFalse("local variable names", strategy.isMatching(mockLocalVariable)); |
126 | } |
127 | |
128 | public void testCreateStrategy_methodnames() { |
129 | sut.setMethodnames(true); |
130 | |
131 | SymbolGathererStrategy strategy = ((SymbolGathererStrategyDecorator) sut.createStrategy()).getDelegate(); |
132 | assertEquals("strategy class", DefaultSymbolGathererStrategy.class, strategy.getClass()); |
133 | assertFalse("class names", strategy.isMatching(mockClassfile)); |
134 | assertFalse("field names", strategy.isMatching(mockField)); |
135 | assertTrue("method names", strategy.isMatching(mockMethod)); |
136 | assertFalse("local variable names", strategy.isMatching(mockLocalVariable)); |
137 | } |
138 | |
139 | public void testCreateStrategy_localnames() { |
140 | sut.setLocalnames(true); |
141 | |
142 | SymbolGathererStrategy strategy = ((SymbolGathererStrategyDecorator) sut.createStrategy()).getDelegate(); |
143 | assertEquals("strategy class", DefaultSymbolGathererStrategy.class, strategy.getClass()); |
144 | assertFalse("class names", strategy.isMatching(mockClassfile)); |
145 | assertFalse("field names", strategy.isMatching(mockField)); |
146 | assertFalse("method names", strategy.isMatching(mockMethod)); |
147 | assertTrue("local variable names", strategy.isMatching(mockLocalVariable)); |
148 | } |
149 | |
150 | public void testCreateStrategy_nonprivatefieldnames() { |
151 | sut.setNonprivatefieldnames(true); |
152 | |
153 | SymbolGathererStrategy strategy = ((SymbolGathererStrategyDecorator) sut.createStrategy()).getDelegate(); |
154 | assertEquals("strategy class", NonPrivateFieldSymbolGathererStrategy.class, strategy.getClass()); |
155 | } |
156 | |
157 | public void testCreateStrategy_finalmethodorclassnames() { |
158 | sut.setFinalmethodorclassnames(true); |
159 | |
160 | SymbolGathererStrategy strategy = ((SymbolGathererStrategyDecorator) sut.createStrategy()).getDelegate(); |
161 | assertEquals("strategy class", FinalMethodOrClassSymbolGathererStrategy.class, strategy.getClass()); |
162 | } |
163 | |
164 | public void testCreateStrategy_singleincludes() { |
165 | sut.setIncludes("/some/"); |
166 | |
167 | FilteringSymbolGathererStrategy strategy = (FilteringSymbolGathererStrategy) sut.createStrategy(); |
168 | assertEquals("strategy class", FilteringSymbolGathererStrategy.class, strategy.getClass()); |
169 | List<String> includes = strategy.getIncludes(); |
170 | assertEquals("filter includes", 1, includes.size()); |
171 | assertEquals("filter includes", "/some/", includes.get(0)); |
172 | } |
173 | |
174 | public void testCreateStrategy_multipleincludes() { |
175 | sut.setIncludes("/some/, /other/"); |
176 | |
177 | FilteringSymbolGathererStrategy strategy = (FilteringSymbolGathererStrategy) sut.createStrategy(); |
178 | assertEquals("strategy class", FilteringSymbolGathererStrategy.class, strategy.getClass()); |
179 | List<String> includes = strategy.getIncludes(); |
180 | assertEquals("filter includes", 2, includes.size()); |
181 | assertEquals("filter includes", "/some/", includes.get(0)); |
182 | assertEquals("filter includes", "/other/", includes.get(1)); |
183 | } |
184 | |
185 | public void testCreateStrategy_singleexcludes() { |
186 | sut.setExcludes("/some/"); |
187 | |
188 | FilteringSymbolGathererStrategy strategy = (FilteringSymbolGathererStrategy) sut.createStrategy(); |
189 | assertEquals("strategy class", FilteringSymbolGathererStrategy.class, strategy.getClass()); |
190 | List<String> excludes = strategy.getExcludes(); |
191 | assertEquals("filter excludes", 1, excludes.size()); |
192 | assertEquals("filter excludes", "/some/", excludes.get(0)); |
193 | } |
194 | |
195 | public void testCreateStrategy_multipleexcludes() { |
196 | sut.setExcludes("/some/, /other/"); |
197 | |
198 | FilteringSymbolGathererStrategy strategy = (FilteringSymbolGathererStrategy) sut.createStrategy(); |
199 | assertEquals("strategy class", FilteringSymbolGathererStrategy.class, strategy.getClass()); |
200 | List<String> excludes = strategy.getExcludes(); |
201 | assertEquals("filter excludes", 2, excludes.size()); |
202 | assertEquals("filter excludes", "/some/", excludes.get(0)); |
203 | assertEquals("filter excludes", "/other/", excludes.get(1)); |
204 | } |
205 | } |