| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.design; |
| 17 |
|
| 18 |
import java.io.File; |
| 19 |
|
| 20 |
import junit.framework.TestSuite; |
| 21 |
import junit.textui.TestRunner; |
| 22 |
|
| 23 |
import org.apache.tools.ant.BuildFileTest; |
| 24 |
import org.apache.tools.ant.util.JavaEnvUtils; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class VerifyDesignTest extends BuildFileTest { |
| 38 |
|
| 39 |
private final static String REASON = "Build should have failed with proper message and did not"; |
| 40 |
private String baseDir = "test"+File.separator |
| 41 |
+"resources"+File.separator |
| 42 |
+"design"+File.separator; |
| 43 |
private String c = File.separator; |
| 44 |
|
| 45 |
public VerifyDesignTest(String name) { |
| 46 |
super(name); |
| 47 |
} |
| 48 |
|
| 49 |
public void setUp() { |
| 50 |
|
| 51 |
configureProject("test/resources/design/verifydesign.xml"); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
private static String s = ""; |
| 56 |
|
| 57 |
public static void log(String msg) { |
| 58 |
s += msg+"\n"; |
| 59 |
} |
| 60 |
|
| 61 |
public void tearDown() { |
| 62 |
executeTarget("cleanup"); |
| 63 |
|
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
public void testArrayDepend() { |
| 68 |
String class1 = "mod.arraydepend.ClassDependsOnArray"; |
| 69 |
String class2 = "mod.dummy.DummyClass"; |
| 70 |
expectDesignCheckFailure("testArrayDepend", Design.getErrorMessage(class1, class2)); |
| 71 |
} |
| 72 |
|
| 73 |
public void testArrayDepend2() { |
| 74 |
executeTarget("testArrayDepend2"); |
| 75 |
} |
| 76 |
|
| 77 |
public void testArrayDepend3() { |
| 78 |
String class1 = "mod.arraydepend3.ClassDependsOnArray"; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
String class2 = "mod.dummy.DummyClass;"; |
| 84 |
expectDesignCheckFailure("testArrayDepend3", Design.getErrorMessage(class1, class2)); |
| 85 |
} |
| 86 |
|
| 87 |
public void testBadXML() { |
| 88 |
File designFile = new File("test/resources/design/designfiles/badxml.xml"); |
| 89 |
String msg = "\nProblem parsing design file='" |
| 90 |
+ designFile.getAbsolutePath() + "'. \nline=3 column=1 Reason:\nElement type \"design\" must be followed by either attribute specifications, \">\" or \"/>\".\n"; |
| 91 |
expectSpecificBuildException("testBadXML", REASON, msg); |
| 92 |
} |
| 93 |
|
| 94 |
public void testCastDepend() { |
| 95 |
String class1 = "mod.castdepend.ClassDependsOnCast"; |
| 96 |
String class2 = "mod.dummy.DummyInterface"; |
| 97 |
expectDesignCheckFailure("testCastDepend", Design.getErrorMessage(class1, class2)); |
| 98 |
} |
| 99 |
|
| 100 |
public void testCatchDepend() { |
| 101 |
String class1 = "mod.catchdepend.ClassDependsOnCatch"; |
| 102 |
String class2 = "mod.dummy.DummyRuntimeException"; |
| 103 |
expectDesignCheckFailure("testCatchDepend", Design.getErrorMessage(class1, class2)); |
| 104 |
} |
| 105 |
|
| 106 |
public void testClassFiles() { |
| 107 |
String class1 = "mod.castdepend.ClassDependsOnCast"; |
| 108 |
String class2 = "mod.dummy.DummyInterface"; |
| 109 |
expectDesignCheckFailure("testClassFiles", Design.getErrorMessage(class1, class2)); |
| 110 |
} |
| 111 |
|
| 112 |
public void testDeclareJavaUtil() { |
| 113 |
executeTarget("testDeclareJavaUtil"); |
| 114 |
} |
| 115 |
|
| 116 |
public void testDeclareJavaUtilFail() { |
| 117 |
String class1 = "mod.declarejavautil.ClassDependsOnJavaUtil"; |
| 118 |
String class2 = "java.util.List"; |
| 119 |
expectDesignCheckFailure("testDeclareJavaUtilFail", Design.getErrorMessage(class1, class2)); |
| 120 |
} |
| 121 |
|
| 122 |
public void testDeclareJavax() { |
| 123 |
String class1 = "mod.declarejavax.ClassDependsOnJavax"; |
| 124 |
String class2 = "javax.swing.JButton"; |
| 125 |
expectDesignCheckFailure("testDeclareJavax", Design.getErrorMessage(class1, class2)); |
| 126 |
} |
| 127 |
|
| 128 |
public void testDeclareJavaxPass() { |
| 129 |
executeTarget("testDeclareJavaxPass"); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public void testFieldDepend() { |
| 142 |
String class1 = "mod.fielddepend.ClassDependsOnField"; |
| 143 |
String class2 = "mod.dummy.DummyClass"; |
| 144 |
expectDesignCheckFailure("testFieldDepend", Design.getErrorMessage(class1, class2)); |
| 145 |
} |
| 146 |
|
| 147 |
public void testFieldRefDepend() { |
| 148 |
if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1) |
| 149 |
|| JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2) |
| 150 |
|| JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
String class1 = "mod.fieldrefdepend.ClassDependsOnReferenceInFieldDeclaration"; |
| 155 |
String class2 = "mod.dummy.DummyClass"; |
| 156 |
expectDesignCheckFailure("testFieldRefDepend", Design.getErrorMessage(class1, class2)); |
| 157 |
} |
| 158 |
|
| 159 |
public void testInnerClassDepend() { |
| 160 |
String class1 = "mod.innerclassdepend.InnerClassDependsOnSuper$Inner"; |
| 161 |
String class2 = "mod.dummy.DummyClass"; |
| 162 |
expectDesignCheckFailure("testInnerClassDepend", Design.getErrorMessage(class1, class2)); |
| 163 |
} |
| 164 |
|
| 165 |
public void testInstanceOfDepend() { |
| 166 |
String class1 = "mod.instanceofdepend.ClassDependsOnInstanceOf"; |
| 167 |
String class2 = "mod.dummy.DummyInterface"; |
| 168 |
expectDesignCheckFailure("testInstanceOfDepend", Design.getErrorMessage(class1, class2)); |
| 169 |
} |
| 170 |
|
| 171 |
public void testInterfaceDepend() { |
| 172 |
String class1 = "mod.interfacedepend.ClassDependsOnInterfaceMod2"; |
| 173 |
String class2 = "mod.dummy.DummyInterface"; |
| 174 |
expectDesignCheckFailure("testInterfaceDepend", Design.getErrorMessage(class1, class2)); |
| 175 |
} |
| 176 |
|
| 177 |
public void testLocalVarDepend() { |
| 178 |
String class1 = "mod.localvardepend.ClassDependsOnLocalVar"; |
| 179 |
String class2 = "mod.dummy.DummyClass"; |
| 180 |
expectDesignCheckFailure("testLocalVarDepend", Design.getErrorMessage(class1, class2)); |
| 181 |
} |
| 182 |
|
| 183 |
public void testLocalVarRefDepend() { |
| 184 |
if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1) |
| 185 |
|| JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2) |
| 186 |
|| JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) { |
| 187 |
return; |
| 188 |
} |
| 189 |
String class1 = "mod.localvarrefdepend.ClassDependsOnLocalVariableReference"; |
| 190 |
String class2 = "mod.dummy.DummyInterface"; |
| 191 |
expectDesignCheckFailure("testLocalVarRefDepend", Design.getErrorMessage(class1, class2)); |
| 192 |
} |
| 193 |
|
| 194 |
public void testMissingAttribute() { |
| 195 |
File designFile = new File("test/resources/design/designfiles/missingattribute.xml"); |
| 196 |
String msg = "\nProblem parsing design file='" |
| 197 |
+ designFile.getAbsolutePath() + "'. \nline=3 column=31 Reason:\nError in file=" |
| 198 |
+ designFile.getAbsolutePath() + ", package element must contain the 'name' attribute\n"; |
| 199 |
expectSpecificBuildException("testMissingAttribute", REASON, msg); |
| 200 |
} |
| 201 |
|
| 202 |
public void testMultipleErrors() { |
| 203 |
File jarFile = new File(baseDir+File.separator+"build"+File.separator+"jar" |
| 204 |
+File.separator+"test.jar"); |
| 205 |
String class1 = "mod.arraydepend.ClassDependsOnArray"; |
| 206 |
String class2 = "mod.dummy.DummyClass"; |
| 207 |
|
| 208 |
String error1 = Design.getWrapperMsg(jarFile, Design.getErrorMessage(class1, class2)); |
| 209 |
class1 = "mod.castdepend.ClassDependsOnCast"; |
| 210 |
String error2 = Design.getWrapperMsg(jarFile, Design.getNoDefinitionError(class1)); |
| 211 |
class1 = "mod.catchdepend.ClassDependsOnCatch"; |
| 212 |
class2 = "mod.dummy.DummyRuntimeException"; |
| 213 |
String error3 = Design.getWrapperMsg(jarFile, Design.getErrorMessage(class1, class2)); |
| 214 |
String s = "\nEvaluating package=mod.arraydepend"+error1; |
| 215 |
s += "\nEvaluating package=mod.castdepend"+error2; |
| 216 |
s += "\nEvaluating package=mod.catchdepend"+error3; |
| 217 |
s += "\nEvaluating package=mod.dummy"; |
| 218 |
|
| 219 |
|
| 220 |
expectDesignCheckFailure("testMultipleErrors", s); |
| 221 |
} |
| 222 |
|
| 223 |
public void testNewDepend() { |
| 224 |
String class1 = "mod.newdepend.ClassDependsOnNew"; |
| 225 |
String class2 = "mod.dummy.DummyClass"; |
| 226 |
expectDesignCheckFailure("testNewDepend", Design.getErrorMessage(class1, class2)); |
| 227 |
} |
| 228 |
|
| 229 |
public void testNewDepend2() { |
| 230 |
String class1 = "mod.newdepend2.ClassDependsOnNewInField"; |
| 231 |
String class2 = "mod.dummy.DummyClass"; |
| 232 |
expectDesignCheckFailure("testNewDepend2", Design.getErrorMessage(class1, class2)); |
| 233 |
} |
| 234 |
|
| 235 |
public void testNoDebugOption() { |
| 236 |
String class1 = "mod.nodebugoption.ClassDependsOnLocalVar"; |
| 237 |
expectDesignCheckFailure("testNoDebugOption", VisitorImpl.getNoDebugMsg(class1)); |
| 238 |
} |
| 239 |
public void testNoJar() { |
| 240 |
File jar = new File("test/resources/design/build/jar/test.jar"); |
| 241 |
expectSpecificBuildException("testNoJar", REASON, VisitorImpl.getNoFileMsg(jar)); |
| 242 |
} |
| 243 |
|
| 244 |
public void testParamDepend() { |
| 245 |
String class1 = "mod.paramdepend.ClassDependsOnParameter"; |
| 246 |
String class2 = "mod.dummy.DummyClass"; |
| 247 |
expectDesignCheckFailure("testParamDepend", Design.getErrorMessage(class1, class2)); |
| 248 |
} |
| 249 |
|
| 250 |
public void testPassLocalDepend() { |
| 251 |
executeTarget("testPassLocalDepend"); |
| 252 |
} |
| 253 |
|
| 254 |
public void testPathElementLocation() { |
| 255 |
executeTarget("testPathElementLocation"); |
| 256 |
} |
| 257 |
|
| 258 |
public void testPathElementPath() { |
| 259 |
executeTarget("testPathElementPath"); |
| 260 |
} |
| 261 |
|
| 262 |
public void testPutStatic() { |
| 263 |
executeTarget("testPutStatic"); |
| 264 |
} |
| 265 |
|
| 266 |
public void testRecursion() { |
| 267 |
executeTarget("testRecursion"); |
| 268 |
} |
| 269 |
|
| 270 |
public void testRecursion2() { |
| 271 |
executeTarget("testRecursion2"); |
| 272 |
} |
| 273 |
|
| 274 |
public void testRecursion3() { |
| 275 |
executeTarget("testRecursion3"); |
| 276 |
} |
| 277 |
|
| 278 |
public void testReturnValDepend() { |
| 279 |
String class1 = "mod.returnvaldepend.ClassDependsOnReturnValue"; |
| 280 |
String class2 = "mod.dummy.DummyInterface"; |
| 281 |
expectDesignCheckFailure("testReturnValDepend", Design.getErrorMessage(class1, class2)); |
| 282 |
} |
| 283 |
|
| 284 |
public void testSignatureExceptionDepend() { |
| 285 |
String class1 = "mod.signatureexceptiondepend.ClassDependsOnExceptionInMethodSignature"; |
| 286 |
String class2 = "mod.dummy.DummyException"; |
| 287 |
expectDesignCheckFailure("testSignatureExceptionDepend", Design.getErrorMessage(class1, class2)); |
| 288 |
} |
| 289 |
|
| 290 |
public void testStaticDepend() { |
| 291 |
String class1 = "mod.staticdepend.ClassDependsOnStatic"; |
| 292 |
String class2 = "mod.dummy.DummyClass"; |
| 293 |
expectDesignCheckFailure("testStaticDepend", Design.getErrorMessage(class1, class2)); |
| 294 |
} |
| 295 |
|
| 296 |
public void testStaticField2Depend() { |
| 297 |
String class1 = "mod.staticfield2depend.ClassDependsOnStaticField"; |
| 298 |
String class2 = "mod.dummy.DummyClass"; |
| 299 |
expectDesignCheckFailure("testStaticField2Depend", Design.getErrorMessage(class1, class2)); |
| 300 |
} |
| 301 |
|
| 302 |
public void testStaticFieldDepend() { |
| 303 |
String class1 = "mod.staticfielddepend.ClassDependsOnStaticField"; |
| 304 |
String class2 = "mod.dummy.DummyClass"; |
| 305 |
expectDesignCheckFailure("testStaticFieldDepend", Design.getErrorMessage(class1, class2)); |
| 306 |
} |
| 307 |
|
| 308 |
public void testStaticFinalDepend() { |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
} |
| 318 |
|
| 319 |
public void testSuperDepend() { |
| 320 |
String s = File.separator; |
| 321 |
File f = new File("test"+s+"resources"+s+"design"+s+"build"+s+"jar"+s+"test.jar"); |
| 322 |
|
| 323 |
|
| 324 |
String class1 = "mod.superdepend.ClassDependsOnSuperMod2"; |
| 325 |
String class2 = "mod.dummy.DummyClass"; |
| 326 |
expectDesignCheckFailure("testSuperDepend", Design.getErrorMessage(class1, class2)); |
| 327 |
|
| 328 |
|
| 329 |
assertTrue("jar file should not exist yet still does", !f.exists()); |
| 330 |
} |
| 331 |
|
| 332 |
public void testWarSuccess() { |
| 333 |
executeTarget("testWarSuccess"); |
| 334 |
} |
| 335 |
|
| 336 |
public void testWarFailure() { |
| 337 |
String class1 = "mod.warfailure.ClassDependsOnSuperMod2"; |
| 338 |
String class2 = "mod.dummy.DummyClass"; |
| 339 |
expectDesignCheckFailure("testWarFailure", Design.getErrorMessage(class1, class2)); |
| 340 |
} |
| 341 |
|
| 342 |
public static void main(String[] args) { |
| 343 |
TestSuite suite = new TestSuite(); |
| 344 |
suite.addTest(new VerifyDesignTest("testArrayDepend2")); |
| 345 |
TestRunner.run(suite); |
| 346 |
} |
| 347 |
|
| 348 |
private void expectDesignCheckFailure(String target, String message) { |
| 349 |
expectSpecificBuildException(target, "Design is broken", |
| 350 |
"Design check failed due to previous " |
| 351 |
+ "errors"); |
| 352 |
assertLogContaining(message); |
| 353 |
} |
| 354 |
} |