| 1 | /* |
| 2 | * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package net.sf.antcontrib.walls; |
| 17 | |
| 18 | import java.io.File; |
| 19 | |
| 20 | import org.apache.tools.ant.BuildFileTest; |
| 21 | |
| 22 | import junit.framework.TestSuite; |
| 23 | import junit.textui.TestRunner; |
| 24 | |
| 25 | /** |
| 26 | * BIG NOTE*************************************************** |
| 27 | * Always expect specific exceptions. Most of these test cases when |
| 28 | * first submitted were not and therefore were not testing what they said |
| 29 | * they were testing. Exceptions were being caused by other things and the |
| 30 | * tests were still passing. Now all tests expect a specific exception |
| 31 | * so if any other is thrown we will fail the test case. |
| 32 | * ************************************************************ |
| 33 | * |
| 34 | * Testcase for <propertycopy>. |
| 35 | */ |
| 36 | public class CompileWithWallsTest extends BuildFileTest { |
| 37 | |
| 38 | private String baseDir = "test"+File.separator |
| 39 | +"resources"+File.separator |
| 40 | +"walls"+File.separator; |
| 41 | private String c = File.separator; |
| 42 | |
| 43 | public CompileWithWallsTest(String name) { |
| 44 | super(name); |
| 45 | } |
| 46 | |
| 47 | public void setUp() { |
| 48 | |
| 49 | configureProject("test/resources/walls/compilewithwalls.xml"); |
| 50 | // project.addBuildListener(new LogListener()); |
| 51 | } |
| 52 | // protected class LogListener implements BuildListener { |
| 53 | // |
| 54 | // /* (non-Javadoc) |
| 55 | // * @see org.apache.tools.ant.BuildListener#buildStarted(org.apache.tools.ant.BuildEvent) |
| 56 | // */ |
| 57 | // public void buildStarted(BuildEvent event) { |
| 58 | // // TODO Auto-generated method stub |
| 59 | // |
| 60 | // } |
| 61 | // |
| 62 | // /* (non-Javadoc) |
| 63 | // * @see org.apache.tools.ant.BuildListener#buildFinished(org.apache.tools.ant.BuildEvent) |
| 64 | // */ |
| 65 | // public void buildFinished(BuildEvent event) { |
| 66 | // // TODO Auto-generated method stub |
| 67 | // |
| 68 | // } |
| 69 | // |
| 70 | // /* (non-Javadoc) |
| 71 | // * @see org.apache.tools.ant.BuildListener#targetStarted(org.apache.tools.ant.BuildEvent) |
| 72 | // */ |
| 73 | // public void targetStarted(BuildEvent event) { |
| 74 | // // TODO Auto-generated method stub |
| 75 | // |
| 76 | // } |
| 77 | // |
| 78 | // /* (non-Javadoc) |
| 79 | // * @see org.apache.tools.ant.BuildListener#targetFinished(org.apache.tools.ant.BuildEvent) |
| 80 | // */ |
| 81 | // public void targetFinished(BuildEvent event) { |
| 82 | // // TODO Auto-generated method stub |
| 83 | // |
| 84 | // } |
| 85 | // |
| 86 | // /* (non-Javadoc) |
| 87 | // * @see org.apache.tools.ant.BuildListener#taskStarted(org.apache.tools.ant.BuildEvent) |
| 88 | // */ |
| 89 | // public void taskStarted(BuildEvent event) { |
| 90 | // // TODO Auto-generated method stub |
| 91 | // |
| 92 | // } |
| 93 | // |
| 94 | // /* (non-Javadoc) |
| 95 | // * @see org.apache.tools.ant.BuildListener#taskFinished(org.apache.tools.ant.BuildEvent) |
| 96 | // */ |
| 97 | // public void taskFinished(BuildEvent event) { |
| 98 | // // TODO Auto-generated method stub |
| 99 | // |
| 100 | // } |
| 101 | // |
| 102 | // /* (non-Javadoc) |
| 103 | // * @see org.apache.tools.ant.BuildListener#messageLogged(org.apache.tools.ant.BuildEvent) |
| 104 | // */ |
| 105 | // public void messageLogged(BuildEvent event) { |
| 106 | // |
| 107 | // System.out.println(event.getException()); |
| 108 | // System.out.println("aaa"); |
| 109 | // } |
| 110 | // |
| 111 | // } |
| 112 | |
| 113 | public void tearDown() { |
| 114 | executeTarget("cleanup"); |
| 115 | |
| 116 | // System.out.println(getFullLog()); |
| 117 | // System.out.println("std out. from ant build begin--------------"); |
| 118 | // System.out.println(getOutput()); |
| 119 | // System.out.println("std.out. from ant build end----------------"); |
| 120 | // System.out.println("std err. from ant build begin--------------"); |
| 121 | // System.out.println(getError()); |
| 122 | // System.out.println("std.err. from ant build end----------------"); |
| 123 | } |
| 124 | |
| 125 | public void testTooManyNestedWallElements() { |
| 126 | expectSpecificBuildException("testTooManyNestedWallElements" |
| 127 | , "TooManyNestedWallElements" |
| 128 | , "compilewithwalls task only supports one nested walls element or one walls attribute"); |
| 129 | } |
| 130 | |
| 131 | public void testFakeTest() { |
| 132 | //this is being deprecated, tests no longer really needed. |
| 133 | } |
| 134 | // public void testTooManyNestedJavacElements() { |
| 135 | // expectSpecificBuildException("testTooManyNestedJavacElements" |
| 136 | // , "TooManyNestedJavacElements" |
| 137 | // , "compilewithwalls task only supports one nested javac element"); |
| 138 | // } |
| 139 | // |
| 140 | // public void testNoWallElement() { |
| 141 | // expectSpecificBuildException("testNoWallElement" |
| 142 | // , "NoWallElement" |
| 143 | // , "There must be a nested walls element"); |
| 144 | // } |
| 145 | // |
| 146 | // public void testNoJavacElement() { |
| 147 | // expectSpecificBuildException("testNoJavacElement" |
| 148 | // , "NoJavacElement" |
| 149 | // , "There must be a nested javac element"); |
| 150 | // } |
| 151 | // |
| 152 | // public void testMoreThanOneSrcDirInJavac() { |
| 153 | // executeTarget("testMoreThanOneSrcDirInJavac"); |
| 154 | // } |
| 155 | // |
| 156 | // public void testNoSrcDirInJavac() { |
| 157 | // expectSpecificBuildException("testNoSrcDirInJavac" |
| 158 | // , "NoSrcDirInJavac" |
| 159 | // , "Javac inside compilewithwalls must have a srcdir specified"); |
| 160 | // } |
| 161 | // |
| 162 | // public void testIntermediaryDirAndDestDirSame() { |
| 163 | // expectSpecificBuildException("testIntermediaryDirAndDestDirSame" |
| 164 | // , "IntermediaryDirAndDestDirSame" |
| 165 | // , "intermediaryBuildDir attribute cannot be specified\n" |
| 166 | // +"to be the same as destdir or inside desdir of the javac task.\n" |
| 167 | // +"This is an intermediary build directory only used by the\n" |
| 168 | // +"compilewithwalls task, not the class file output directory.\n" |
| 169 | // +"The class file output directory is specified in javac's destdir attribute"); |
| 170 | // } |
| 171 | // |
| 172 | // public void testIntermediaryDirInsideDestDir() { |
| 173 | // expectSpecificBuildException("testIntermediaryDirInsideDestDir" |
| 174 | // , "IntermediaryDirInsideDestDir" |
| 175 | // , "intermediaryBuildDir attribute cannot be specified\n" |
| 176 | // +"to be the same as destdir or inside desdir of the javac task.\n" |
| 177 | // +"This is an intermediary build directory only used by the\n" |
| 178 | // +"compilewithwalls task, not the class file output directory.\n" |
| 179 | // +"The class file output directory is specified in javac's destdir attribute"); |
| 180 | // } |
| 181 | // |
| 182 | // public void testPackageDoesntEndWithStar() { |
| 183 | // expectSpecificBuildException("testPackageDoesntEndWithStar" |
| 184 | // , "PackageDoesntEndWithStar" |
| 185 | // , "The package='biz.xsoftware' must end with " |
| 186 | // + ".* or .** such as biz.xsoftware.* or " |
| 187 | // + "biz.xsoftware.**" ); |
| 188 | // } |
| 189 | // |
| 190 | // public void testPackageDoesntHaveSlash() { |
| 191 | // expectSpecificBuildException("testPackageDoesntHaveSlash" |
| 192 | // , "PackageDoesntHaveSlash" |
| 193 | // ,"A package name cannot contain '\\' or '/' like package=" |
| 194 | // + "biz/xsoftware.*\nIt must look like biz.xsoftware.* for example"); |
| 195 | // } |
| 196 | // |
| 197 | // public void testDependsOnNonExistPackage() { |
| 198 | // expectSpecificBuildException("testDependsOnNonExistPackage" |
| 199 | // , "DependsOnNonExistPackage" |
| 200 | // , "package name=modA did not have modB" |
| 201 | // + " listed before it and cannot compile without it"); |
| 202 | // } |
| 203 | // |
| 204 | // public void testDependsOnPackageAfter() { |
| 205 | // expectSpecificBuildException("testDependsOnPackageAfter" |
| 206 | // , "DependsOnPackageAfter" |
| 207 | // , "package name=modA did not have modB" |
| 208 | // + " listed before it and cannot compile without it"); |
| 209 | // } |
| 210 | // |
| 211 | // public void testPackageABreakingWhenAIsCompiledFirst() { |
| 212 | // expectSpecificBuildException("testPackageABreakingWhenAIsCompiledFirst" |
| 213 | // , "PackageABreakingWhenAIsCompiledFirst" |
| 214 | // , "Compile failed; see the compiler error output for details."); |
| 215 | // } |
| 216 | // |
| 217 | // |
| 218 | // /** |
| 219 | // * This test case tests when modB depends on modA but it was |
| 220 | // * not specified in the walls so modA is not in modB's path. |
| 221 | // * The build should then fail until they change the build.xml |
| 222 | // * so modB depends on modA in the walls element. |
| 223 | // */ |
| 224 | // public void testPackageBBreakingWhenAIsCompiledFirst() { |
| 225 | // |
| 226 | // expectSpecificBuildException("testPackageBBreakingWhenAIsCompiledFirst" |
| 227 | // , "PackageBBreakingWhenAIsCompiledFirst" |
| 228 | // , "Compile failed; see the compiler error output for details."); |
| 229 | // |
| 230 | // //modA should have been compiled successfully, it is only modB that |
| 231 | // //fails. It is very important we make sure A got compiled otherwise |
| 232 | // //we are not testing the correct behavior and the test would be wrong. |
| 233 | // ensureClassFileExists("testB"+c+"mod"+c+"modA"+c+"ModuleA.class", true); |
| 234 | // ensureClassFileExists("testB"+c+"mod"+c+"modB"+c+"ModuleB.class", false); |
| 235 | // } |
| 236 | // |
| 237 | // public void testCompileOfAllUsingDepends() { |
| 238 | // ensureClassFileExists("testC"+c+"mod"+c+"Module.class", false); |
| 239 | // //make sure we are testing the correct thing and Module.java exists! |
| 240 | // ensureJavaFileExists("testC"+c+"mod"+c+"Module.java", true); |
| 241 | // |
| 242 | // executeTarget("testCompileOfAllUsingDepends"); |
| 243 | // |
| 244 | // //must test class files were actually created afterwards. |
| 245 | // //The build might pass with no class files if the task is |
| 246 | // //messed up. |
| 247 | // ensureClassFileExists("testC"+c+"mod"+c+"Module.class", true); |
| 248 | // |
| 249 | // } |
| 250 | ////--------------------------------------------------------- |
| 251 | //// |
| 252 | //// The following tests are all just repeats of some of the above tests |
| 253 | //// except the below tests use External walls file and the above tests |
| 254 | //// don't. |
| 255 | //// |
| 256 | ////--------------------------------------------------------- |
| 257 | // |
| 258 | // public void testDependsOnPackageAfterExternalWalls() { |
| 259 | // expectSpecificBuildException("testDependsOnPackageAfterExternalWalls" |
| 260 | // , "DependsOnPackageAfterExternalWalls" |
| 261 | // , "package name=modA did not have modB" |
| 262 | // + " listed before it and cannot compile without it"); |
| 263 | // } |
| 264 | // |
| 265 | // /** |
| 266 | // * This test case tests when modB depends on modA but it was |
| 267 | // * not specified in the walls so modA is not in modB's path. |
| 268 | // * The build should then fail until they change the build.xml |
| 269 | // * so modB depends on modA in the walls element. |
| 270 | // */ |
| 271 | // public void testPackageBBreakingWhenAIsCompiledFirstExternalWalls() { |
| 272 | // ensureClassFileExists("testB"+c+"mod"+c+"modA"+c+"ModuleA.class", false); |
| 273 | // ensureJavaFileExists("testB"+c+"mod"+c+"modB"+c+"ModuleB.java", true); |
| 274 | // |
| 275 | // expectSpecificBuildException("testPackageBBreakingWhenAIsCompiledFirst" |
| 276 | // , "PackageBBreakingWhenAIsCompiledFirst" |
| 277 | // , "Compile failed; see the compiler error output for details."); |
| 278 | // |
| 279 | // //modA should have been compiled successfully, it is only modB that |
| 280 | // //fails. It is very important we make sure A got compiled otherwise |
| 281 | // //we are not testing the correct behavior and the test would be wrong. |
| 282 | // ensureClassFileExists("testB"+c+"mod"+c+"modA"+c+"ModuleA.class", true); |
| 283 | // ensureClassFileExists("testB"+c+"mod"+c+"modB"+c+"ModuleB.class", false); |
| 284 | // } |
| 285 | // |
| 286 | // public void testCompileOfAllUsingDependsExternalWalls() { |
| 287 | // ensureClassFileExists("testC"+c+"mod"+c+"Module.class", false); |
| 288 | // ensureJavaFileExists("testC"+c+"mod"+c+"Module.java", true); |
| 289 | // executeTarget("testCompileOfAllUsingDependsExternalWalls"); |
| 290 | // //must test class files were actually created afterwards. |
| 291 | // //The build might pass with no class files if the task is |
| 292 | // //messed up. |
| 293 | // ensureClassFileExists("testC"+c+"mod"+c+"Module.class", true); |
| 294 | // } |
| 295 | |
| 296 | private void ensureJavaFileExists(String file, boolean shouldExist) { |
| 297 | |
| 298 | //must test that it is testing the correct directory. |
| 299 | //It wasn't before. |
| 300 | String javaFile = baseDir+file; |
| 301 | File f1 = new File(javaFile); |
| 302 | if(shouldExist) |
| 303 | assertTrue("The java file="+f1.getAbsolutePath()+" didn't exist, we can't run this test. It will pass with false results", |
| 304 | f1.exists()); |
| 305 | else |
| 306 | assertTrue("The java file="+f1.getAbsolutePath()+" exists and shouldn't, we can't run this test. It will pass with false results", |
| 307 | !f1.exists()); |
| 308 | } |
| 309 | |
| 310 | private void ensureClassFileExists(String file, boolean shouldExist) { |
| 311 | |
| 312 | String classFile = baseDir |
| 313 | +"compilewithwalls"+File.separator |
| 314 | +"classes"+File.separator |
| 315 | +file; |
| 316 | |
| 317 | File f1 = new File(classFile); |
| 318 | if(shouldExist) |
| 319 | assertTrue("The class file="+f1.getAbsolutePath()+" didn't get created, No build exception\nwas thrown, but the build failed because a class\nfile should have been created", |
| 320 | f1.exists()); |
| 321 | else |
| 322 | assertTrue("The class file="+f1.getAbsolutePath()+" exists and shouldn't\nTest may be inaccurate if this file already exists...correct the test", |
| 323 | !f1.exists()); |
| 324 | } |
| 325 | |
| 326 | public static void main(String[] args) { |
| 327 | TestSuite suite = new TestSuite(CompileWithWallsTest.class); |
| 328 | TestRunner.run(suite); |
| 329 | } |
| 330 | } |