1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*
* Copyright (c) 2001-2005 Ant-Contrib project. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sf.antcontrib.design;
import java.io.File;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.tools.ant.BuildFileTest;
import org.apache.tools.ant.util.JavaEnvUtils;
/**
* BIG NOTE***************************************************
* Always expect specific exceptions. Most of these test cases when
* first submitted were not and therefore were not testing what they said
* they were testing. Exceptions were being caused by other things and the
* tests were still passing. Now all tests expect a specific exception
* so if any other is thrown we will fail the test case.
* ************************************************************
*
* Testcase for <propertycopy>.
*/
public class VerifyDesignTest extends BuildFileTest {
private final static String REASON = "Build should have failed with proper message and did not";
private String baseDir = "test"+File.separator
+"resources"+File.separator
+"design"+File.separator;
private String c = File.separator;
public VerifyDesignTest(String name) {
super(name);
}
public void setUp() {
configureProject("test/resources/design/verifydesign.xml");
// project.log("ASFDSADF", Project.MSG_INFO);
}
private static String s = "";
public static void log(String msg) {
s += msg+"\n";
}
public void tearDown() {
executeTarget("cleanup");
//System.out.println("test log=\n"+s);
}
public void testArrayDepend() {
String class1 = "mod.arraydepend.ClassDependsOnArray";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testArrayDepend", Design.getErrorMessage(class1, class2));
}
public void testArrayDepend2() {
executeTarget("testArrayDepend2");
}
public void testArrayDepend3() {
String class1 = "mod.arraydepend3.ClassDependsOnArray";
//
// The trailing semi-colon makes the test pass,
// but likely reflects a problem in the VerifyDesign task
//
String class2 = "mod.dummy.DummyClass;";
expectDesignCheckFailure("testArrayDepend3", Design.getErrorMessage(class1, class2));
}
public void testBadXML() {
File designFile = new File("test/resources/design/designfiles/badxml.xml");
String msg = "\nProblem parsing design file='"
+ designFile.getAbsolutePath() + "'. \nline=3 column=1 Reason:\nElement type \"design\" must be followed by either attribute specifications, \">\" or \"/>\".\n";
expectSpecificBuildException("testBadXML", REASON, msg);
}
public void testCastDepend() {
String class1 = "mod.castdepend.ClassDependsOnCast";
String class2 = "mod.dummy.DummyInterface";
expectDesignCheckFailure("testCastDepend", Design.getErrorMessage(class1, class2));
}
public void testCatchDepend() {
String class1 = "mod.catchdepend.ClassDependsOnCatch";
String class2 = "mod.dummy.DummyRuntimeException";
expectDesignCheckFailure("testCatchDepend", Design.getErrorMessage(class1, class2));
}
public void testClassFiles() {
String class1 = "mod.castdepend.ClassDependsOnCast";
String class2 = "mod.dummy.DummyInterface";
expectDesignCheckFailure("testClassFiles", Design.getErrorMessage(class1, class2));
}
public void testDeclareJavaUtil() {
executeTarget("testDeclareJavaUtil");
}
public void testDeclareJavaUtilFail() {
String class1 = "mod.declarejavautil.ClassDependsOnJavaUtil";
String class2 = "java.util.List";
expectDesignCheckFailure("testDeclareJavaUtilFail", Design.getErrorMessage(class1, class2));
}
public void testDeclareJavax() {
String class1 = "mod.declarejavax.ClassDependsOnJavax";
String class2 = "javax.swing.JButton";
expectDesignCheckFailure("testDeclareJavax", Design.getErrorMessage(class1, class2));
}
public void testDeclareJavaxPass() {
executeTarget("testDeclareJavaxPass");
}
//tests to write
//depend on java.util should pass by default
//depend on java.util should fail after defining needDeclareTrue
//depend on javax.swing should pass after needDeclareFalse
//depend on dummy should pass after needDeclareFalse
public void testFieldDepend() {
String class1 = "mod.fielddepend.ClassDependsOnField";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testFieldDepend", Design.getErrorMessage(class1, class2));
}
public void testFieldRefDepend() {
if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)
|| JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)
|| JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) {
return;
}
String class1 = "mod.fieldrefdepend.ClassDependsOnReferenceInFieldDeclaration";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testFieldRefDepend", Design.getErrorMessage(class1, class2));
}
public void testInnerClassDepend() {
String class1 = "mod.innerclassdepend.InnerClassDependsOnSuper$Inner";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testInnerClassDepend", Design.getErrorMessage(class1, class2));
}
public void testInstanceOfDepend() {
String class1 = "mod.instanceofdepend.ClassDependsOnInstanceOf";
String class2 = "mod.dummy.DummyInterface";
expectDesignCheckFailure("testInstanceOfDepend", Design.getErrorMessage(class1, class2));
}
public void testInterfaceDepend() {
String class1 = "mod.interfacedepend.ClassDependsOnInterfaceMod2";
String class2 = "mod.dummy.DummyInterface";
expectDesignCheckFailure("testInterfaceDepend", Design.getErrorMessage(class1, class2));
}
public void testLocalVarDepend() {
String class1 = "mod.localvardepend.ClassDependsOnLocalVar";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testLocalVarDepend", Design.getErrorMessage(class1, class2));
}
public void testLocalVarRefDepend() {
if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)
|| JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)
|| JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) {
return;
}
String class1 = "mod.localvarrefdepend.ClassDependsOnLocalVariableReference";
String class2 = "mod.dummy.DummyInterface";
expectDesignCheckFailure("testLocalVarRefDepend", Design.getErrorMessage(class1, class2));
}
public void testMissingAttribute() {
File designFile = new File("test/resources/design/designfiles/missingattribute.xml");
String msg = "\nProblem parsing design file='"
+ designFile.getAbsolutePath() + "'. \nline=3 column=31 Reason:\nError in file="
+ designFile.getAbsolutePath() + ", package element must contain the 'name' attribute\n";
expectSpecificBuildException("testMissingAttribute", REASON, msg);
}
public void testMultipleErrors() {
File jarFile = new File(baseDir+File.separator+"build"+File.separator+"jar"
+File.separator+"test.jar");
String class1 = "mod.arraydepend.ClassDependsOnArray";
String class2 = "mod.dummy.DummyClass";
//executeTarget("testMultipleErrors");
String error1 = Design.getWrapperMsg(jarFile, Design.getErrorMessage(class1, class2));
class1 = "mod.castdepend.ClassDependsOnCast";
String error2 = Design.getWrapperMsg(jarFile, Design.getNoDefinitionError(class1));
class1 = "mod.catchdepend.ClassDependsOnCatch";
class2 = "mod.dummy.DummyRuntimeException";
String error3 = Design.getWrapperMsg(jarFile, Design.getErrorMessage(class1, class2));
String s = "\nEvaluating package=mod.arraydepend"+error1;
s += "\nEvaluating package=mod.castdepend"+error2;
s += "\nEvaluating package=mod.catchdepend"+error3;
s += "\nEvaluating package=mod.dummy";
// executeTarget("testMultipleErrors");
expectDesignCheckFailure("testMultipleErrors", s);
}
public void testNewDepend() {
String class1 = "mod.newdepend.ClassDependsOnNew";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testNewDepend", Design.getErrorMessage(class1, class2));
}
public void testNewDepend2() {
String class1 = "mod.newdepend2.ClassDependsOnNewInField";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testNewDepend2", Design.getErrorMessage(class1, class2));
}
public void testNoDebugOption() {
String class1 = "mod.nodebugoption.ClassDependsOnLocalVar";
expectDesignCheckFailure("testNoDebugOption", VisitorImpl.getNoDebugMsg(class1));
}
public void testNoJar() {
File jar = new File("test/resources/design/build/jar/test.jar");
expectSpecificBuildException("testNoJar", REASON, VisitorImpl.getNoFileMsg(jar));
}
public void testParamDepend() {
String class1 = "mod.paramdepend.ClassDependsOnParameter";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testParamDepend", Design.getErrorMessage(class1, class2));
}
public void testPassLocalDepend() {
executeTarget("testPassLocalDepend");
}
public void testPathElementLocation() {
executeTarget("testPathElementLocation");
}
public void testPathElementPath() {
executeTarget("testPathElementPath");
}
public void testPutStatic() {
executeTarget("testPutStatic");
}
public void testRecursion() {
executeTarget("testRecursion");
}
public void testRecursion2() {
executeTarget("testRecursion2");
}
public void testRecursion3() {
executeTarget("testRecursion3");
}
public void testReturnValDepend() {
String class1 = "mod.returnvaldepend.ClassDependsOnReturnValue";
String class2 = "mod.dummy.DummyInterface";
expectDesignCheckFailure("testReturnValDepend", Design.getErrorMessage(class1, class2));
}
public void testSignatureExceptionDepend() {
String class1 = "mod.signatureexceptiondepend.ClassDependsOnExceptionInMethodSignature";
String class2 = "mod.dummy.DummyException";
expectDesignCheckFailure("testSignatureExceptionDepend", Design.getErrorMessage(class1, class2));
}
public void testStaticDepend() {
String class1 = "mod.staticdepend.ClassDependsOnStatic";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testStaticDepend", Design.getErrorMessage(class1, class2));
}
public void testStaticField2Depend() {
String class1 = "mod.staticfield2depend.ClassDependsOnStaticField";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testStaticField2Depend", Design.getErrorMessage(class1, class2));
}
public void testStaticFieldDepend() {
String class1 = "mod.staticfielddepend.ClassDependsOnStaticField";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testStaticFieldDepend", Design.getErrorMessage(class1, class2));
}
public void testStaticFinalDepend() {
//This is an impossible test since javac compiles String and primitive constants into the code
//losing any reference to the class that contains the constant...In this one instance,
//verifydesign can't verify that constant imports don't violate the design!!!!
//check out mod.staticfinaldepend.ClassDependsOnStaticField to see the code
//that will pass the design even if it is violating it.
// String class1 = "mod.staticfinaldepend.ClassDependsOnConstant";
// String class2 = "mod.dummy.DummyClass";
// expectDesignCheckFailure("testStaticFinalDepend", Design.getErrorMessage(class1, class2));
}
public void testSuperDepend() {
String s = File.separator;
File f = new File("test"+s+"resources"+s+"design"+s+"build"+s+"jar"+s+"test.jar");
// executeTarget("testSuperDepend");
String class1 = "mod.superdepend.ClassDependsOnSuperMod2";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testSuperDepend", Design.getErrorMessage(class1, class2));
//jar file should have been deleted
assertTrue("jar file should not exist yet still does", !f.exists());
}
public void testWarSuccess() {
executeTarget("testWarSuccess");
}
public void testWarFailure() {
String class1 = "mod.warfailure.ClassDependsOnSuperMod2";
String class2 = "mod.dummy.DummyClass";
expectDesignCheckFailure("testWarFailure", Design.getErrorMessage(class1, class2));
}
public static void main(String[] args) {
TestSuite suite = new TestSuite();
suite.addTest(new VerifyDesignTest("testArrayDepend2"));
TestRunner.run(suite);
}
private void expectDesignCheckFailure(String target, String message) {
expectSpecificBuildException(target, "Design is broken",
"Design check failed due to previous "
+ "errors");
assertLogContaining(message);
}
}
|