test/tools/sjavac/SJavac.java

Fri, 18 Jan 2013 00:16:21 +0100

author
ohrstrom
date
Fri, 18 Jan 2013 00:16:21 +0100
changeset 1504
22e417cdddee
child 1516
8943b4213f59
permissions
-rw-r--r--

8004658: Add internal smart javac wrapper to solve JEP 139
Reviewed-by: jjg

ohrstrom@1504 1 /*
ohrstrom@1504 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
ohrstrom@1504 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@1504 4 *
ohrstrom@1504 5 * This code is free software; you can redistribute it and/or modify it
ohrstrom@1504 6 * under the terms of the GNU General Public License version 2 only, as
ohrstrom@1504 7 * published by the Free Software Foundation.
ohrstrom@1504 8 *
ohrstrom@1504 9 * This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@1504 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@1504 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@1504 12 * version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@1504 13 * accompanied this code).
ohrstrom@1504 14 *
ohrstrom@1504 15 * You should have received a copy of the GNU General Public License version
ohrstrom@1504 16 * 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@1504 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@1504 18 *
ohrstrom@1504 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@1504 20 * or visit www.oracle.com if you need additional information or have any
ohrstrom@1504 21 * questions.
ohrstrom@1504 22 */
ohrstrom@1504 23
ohrstrom@1504 24 /*
ohrstrom@1504 25 * @test
ohrstrom@1504 26 * @summary Test all aspects of sjavac.
ohrstrom@1504 27 *
ohrstrom@1504 28 * @bug 8004658
ohrstrom@1504 29 * @summary Add internal smart javac wrapper to solve JEP 139
ohrstrom@1504 30 *
ohrstrom@1504 31 * @run main SJavac
ohrstrom@1504 32 */
ohrstrom@1504 33
ohrstrom@1504 34 import java.util.*;
ohrstrom@1504 35 import java.io.*;
ohrstrom@1504 36 import java.net.*;
ohrstrom@1504 37 import java.nio.file.*;
ohrstrom@1504 38 import java.nio.file.attribute.*;
ohrstrom@1504 39 import java.nio.charset.*;
ohrstrom@1504 40
ohrstrom@1504 41 import com.sun.tools.sjavac.Main;
ohrstrom@1504 42
ohrstrom@1504 43 public
ohrstrom@1504 44 class SJavac {
ohrstrom@1504 45
ohrstrom@1504 46 public static void main(String... args) throws Exception {
ohrstrom@1504 47 URL url = SJavac.class.getClassLoader().getResource("com/sun/tools/sjavac/Main.class");
ohrstrom@1504 48 if (url == null) {
ohrstrom@1504 49 // No sjavac in the classpath.
ohrstrom@1504 50 System.out.println("pass by default");
ohrstrom@1504 51 return;
ohrstrom@1504 52 }
ohrstrom@1504 53
ohrstrom@1504 54 SJavac s = new SJavac();
ohrstrom@1504 55 s.test();
ohrstrom@1504 56 }
ohrstrom@1504 57
ohrstrom@1504 58 FileSystem defaultfs = FileSystems.getDefault();
ohrstrom@1504 59
ohrstrom@1504 60 // Where to put generated sources that will
ohrstrom@1504 61 // test aspects of sjavac, ie JTWork/scratch/gensrc
ohrstrom@1504 62 Path gensrc;
ohrstrom@1504 63 // More gensrc dirs are used to test merging of serveral source roots.
ohrstrom@1504 64 Path gensrc2;
ohrstrom@1504 65 Path gensrc3;
ohrstrom@1504 66
ohrstrom@1504 67 // Where to put compiled classes.
ohrstrom@1504 68 Path bin;
ohrstrom@1504 69 // Where to put c-header files.
ohrstrom@1504 70 Path headers;
ohrstrom@1504 71
ohrstrom@1504 72 // The sjavac compiler.
ohrstrom@1504 73 Main main = new Main();
ohrstrom@1504 74
ohrstrom@1504 75 // Remember the previous bin and headers state here.
ohrstrom@1504 76 Map<String,Long> previous_bin_state;
ohrstrom@1504 77 Map<String,Long> previous_headers_state;
ohrstrom@1504 78
ohrstrom@1504 79 public void test() throws Exception {
ohrstrom@1504 80 gensrc = defaultfs.getPath("gensrc");
ohrstrom@1504 81 gensrc2 = defaultfs.getPath("gensrc2");
ohrstrom@1504 82 gensrc3 = defaultfs.getPath("gensrc3");
ohrstrom@1504 83 bin = defaultfs.getPath("bin");
ohrstrom@1504 84 headers = defaultfs.getPath("headers");
ohrstrom@1504 85
ohrstrom@1504 86 Files.createDirectory(gensrc);
ohrstrom@1504 87 Files.createDirectory(gensrc2);
ohrstrom@1504 88 Files.createDirectory(gensrc3);
ohrstrom@1504 89 Files.createDirectory(bin);
ohrstrom@1504 90 Files.createDirectory(headers);
ohrstrom@1504 91
ohrstrom@1504 92 initialCompile();
ohrstrom@1504 93 incrementalCompileNoChanges();
ohrstrom@1504 94 incrementalCompileDroppingClasses();
ohrstrom@1504 95 incrementalCompileWithChange();
ohrstrom@1504 96 incrementalCompileDropAllNatives();
ohrstrom@1504 97 incrementalCompileAddNative();
ohrstrom@1504 98 incrementalCompileChangeNative();
ohrstrom@1504 99 compileWithOverrideSource();
ohrstrom@1504 100 compileWithInvisibleSources();
ohrstrom@1504 101 compileCircularSources();
ohrstrom@1504 102
ohrstrom@1504 103 delete(gensrc);
ohrstrom@1504 104 delete(gensrc2);
ohrstrom@1504 105 delete(gensrc3);
ohrstrom@1504 106 delete(bin);
ohrstrom@1504 107 }
ohrstrom@1504 108
ohrstrom@1504 109 void initialCompile() throws Exception {
ohrstrom@1504 110 System.out.println("\nInitial compile of gensrc.");
ohrstrom@1504 111 System.out.println("----------------------------");
ohrstrom@1504 112 populate(gensrc,
ohrstrom@1504 113 "alfa/AINT.java",
ohrstrom@1504 114 "package alfa; public interface AINT { void aint(); }",
ohrstrom@1504 115
ohrstrom@1504 116 "alfa/A.java",
ohrstrom@1504 117 "package alfa; public class A implements AINT { "+
ohrstrom@1504 118 "public final static int DEFINITION = 17; public void aint() { } }",
ohrstrom@1504 119
ohrstrom@1504 120 "alfa/AA.java",
ohrstrom@1504 121 "package alfa;"+
ohrstrom@1504 122 "// A package private class, not contributing to the public api.\n"+
ohrstrom@1504 123 "class AA {"+
ohrstrom@1504 124 " // A properly nested static inner class.\n"+
ohrstrom@1504 125 " static class AAA { }\n"+
ohrstrom@1504 126 " // A properly nested inner class.\n"+
ohrstrom@1504 127 " class AAAA { }\n"+
ohrstrom@1504 128 " Runnable foo() {\n"+
ohrstrom@1504 129 " // A proper anonymous class.\n"+
ohrstrom@1504 130 " return new Runnable() { public void run() { } };\n"+
ohrstrom@1504 131 " }\n"+
ohrstrom@1504 132 " AAA aaa;\n"+
ohrstrom@1504 133 " AAAA aaaa;\n"+
ohrstrom@1504 134 " AAAAA aaaaa;\n"+
ohrstrom@1504 135 "}\n"+
ohrstrom@1504 136 "class AAAAA {\n"+
ohrstrom@1504 137 " // A bad auxiliary class, but no one is referencing it\n"+
ohrstrom@1504 138 " // from outside of this source file, therefore it is ok.\n"+
ohrstrom@1504 139 "}\n",
ohrstrom@1504 140
ohrstrom@1504 141 "beta/BINT.java",
ohrstrom@1504 142 "package beta;public interface BINT { void foo(); }",
ohrstrom@1504 143
ohrstrom@1504 144 "beta/B.java",
ohrstrom@1504 145 "package beta; import alfa.A; public class B {"+
ohrstrom@1504 146 "private int b() { return A.DEFINITION; } native void foo(); }");
ohrstrom@1504 147
ohrstrom@1504 148 compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",
ohrstrom@1504 149 "--server:portfile=testserver,background=false", "--log=debug");
ohrstrom@1504 150 previous_bin_state = collectState(bin);
ohrstrom@1504 151 previous_headers_state = collectState(headers);
ohrstrom@1504 152 }
ohrstrom@1504 153
ohrstrom@1504 154 void incrementalCompileNoChanges() throws Exception {
ohrstrom@1504 155 System.out.println("\nTesting that no change in sources implies no change in binaries.");
ohrstrom@1504 156 System.out.println("------------------------------------------------------------------");
ohrstrom@1504 157 compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",
ohrstrom@1504 158 "--server:portfile=testserver,background=false", "--log=debug");
ohrstrom@1504 159 Map<String,Long> new_bin_state = collectState(bin);
ohrstrom@1504 160 verifyEqual(new_bin_state, previous_bin_state);
ohrstrom@1504 161 Map<String,Long> new_headers_state = collectState(headers);
ohrstrom@1504 162 verifyEqual(previous_headers_state, new_headers_state);
ohrstrom@1504 163 }
ohrstrom@1504 164
ohrstrom@1504 165 void incrementalCompileDroppingClasses() throws Exception {
ohrstrom@1504 166 System.out.println("\nTesting that deleting AA.java deletes all");
ohrstrom@1504 167 System.out.println("generated inner class as well as AA.class");
ohrstrom@1504 168 System.out.println("-----------------------------------------");
ohrstrom@1504 169 removeFrom(gensrc, "alfa/AA.java");
ohrstrom@1504 170 compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",
ohrstrom@1504 171 "--server:portfile=testserver,background=false", "--log=debug");
ohrstrom@1504 172 Map<String,Long> new_bin_state = collectState(bin);
ohrstrom@1504 173 verifyThatFilesHaveBeenRemoved(previous_bin_state, new_bin_state,
ohrstrom@1504 174 "bin/alfa/AA$1.class",
ohrstrom@1504 175 "bin/alfa/AA$AAAA.class",
ohrstrom@1504 176 "bin/alfa/AA$AAA.class",
ohrstrom@1504 177 "bin/alfa/AAAAA.class",
ohrstrom@1504 178 "bin/alfa/AA.class");
ohrstrom@1504 179
ohrstrom@1504 180 previous_bin_state = new_bin_state;
ohrstrom@1504 181 Map<String,Long> new_headers_state = collectState(headers);
ohrstrom@1504 182 verifyEqual(previous_headers_state, new_headers_state);
ohrstrom@1504 183 }
ohrstrom@1504 184
ohrstrom@1504 185 void incrementalCompileWithChange() throws Exception {
ohrstrom@1504 186 System.out.println("\nNow update the A.java file with a new timestamps and");
ohrstrom@1504 187 System.out.println("new final static definition. This should trigger a recompile,");
ohrstrom@1504 188 System.out.println("not only of alfa, but also beta.");
ohrstrom@1504 189 System.out.println("But check that the generated native header was not updated!");
ohrstrom@1504 190 System.out.println("Since we did not modify the native api of B.");
ohrstrom@1504 191 System.out.println("-------------------------------------------------------------");
ohrstrom@1504 192
ohrstrom@1504 193 populate(gensrc,"alfa/A.java",
ohrstrom@1504 194 "package alfa; public class A implements AINT { "+
ohrstrom@1504 195 "public final static int DEFINITION = 18; public void aint() { } private void foo() { } }");
ohrstrom@1504 196
ohrstrom@1504 197 compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",
ohrstrom@1504 198 "--server:portfile=testserver,background=false", "--log=debug");
ohrstrom@1504 199 Map<String,Long> new_bin_state = collectState(bin);
ohrstrom@1504 200
ohrstrom@1504 201 verifyNewerFiles(previous_bin_state, new_bin_state,
ohrstrom@1504 202 "bin/alfa/A.class",
ohrstrom@1504 203 "bin/alfa/AINT.class",
ohrstrom@1504 204 "bin/beta/B.class",
ohrstrom@1504 205 "bin/beta/BINT.class",
ohrstrom@1504 206 "bin/javac_state");
ohrstrom@1504 207 previous_bin_state = new_bin_state;
ohrstrom@1504 208
ohrstrom@1504 209 Map<String,Long> new_headers_state = collectState(headers);
ohrstrom@1504 210 verifyEqual(new_headers_state, previous_headers_state);
ohrstrom@1504 211 }
ohrstrom@1504 212
ohrstrom@1504 213 void incrementalCompileDropAllNatives() throws Exception {
ohrstrom@1504 214 System.out.println("\nNow update the B.java file with one less native method,");
ohrstrom@1504 215 System.out.println("ie it has no longer any methods!");
ohrstrom@1504 216 System.out.println("Verify that beta_B.h is removed!");
ohrstrom@1504 217 System.out.println("---------------------------------------------------------");
ohrstrom@1504 218
ohrstrom@1504 219 populate(gensrc,"beta/B.java",
ohrstrom@1504 220 "package beta; import alfa.A; public class B {"+
ohrstrom@1504 221 "private int b() { return A.DEFINITION; } }");
ohrstrom@1504 222
ohrstrom@1504 223 compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",
ohrstrom@1504 224 "--server:portfile=testserver,background=false", "--log=debug");
ohrstrom@1504 225 Map<String,Long> new_bin_state = collectState(bin);
ohrstrom@1504 226 verifyNewerFiles(previous_bin_state, new_bin_state,
ohrstrom@1504 227 "bin/beta/B.class",
ohrstrom@1504 228 "bin/beta/BINT.class",
ohrstrom@1504 229 "bin/javac_state");
ohrstrom@1504 230 previous_bin_state = new_bin_state;
ohrstrom@1504 231
ohrstrom@1504 232 Map<String,Long> new_headers_state = collectState(headers);
ohrstrom@1504 233 verifyThatFilesHaveBeenRemoved(previous_headers_state, new_headers_state,
ohrstrom@1504 234 "headers/beta_B.h");
ohrstrom@1504 235 previous_headers_state = new_headers_state;
ohrstrom@1504 236 }
ohrstrom@1504 237
ohrstrom@1504 238 void incrementalCompileAddNative() throws Exception {
ohrstrom@1504 239 System.out.println("\nNow update the B.java file with a final static annotated with @Native.");
ohrstrom@1504 240 System.out.println("Verify that beta_B.h is added again!");
ohrstrom@1504 241 System.out.println("------------------------------------------------------------------------");
ohrstrom@1504 242
ohrstrom@1504 243 populate(gensrc,"beta/B.java",
ohrstrom@1504 244 "package beta; import alfa.A; public class B {"+
ohrstrom@1504 245 "private int b() { return A.DEFINITION; } "+
ohrstrom@1504 246 "@java.lang.annotation.Native final static int alfa = 42; }");
ohrstrom@1504 247
ohrstrom@1504 248 compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",
ohrstrom@1504 249 "--server:portfile=testserver,background=false", "--log=debug");
ohrstrom@1504 250 Map<String,Long> new_bin_state = collectState(bin);
ohrstrom@1504 251 verifyNewerFiles(previous_bin_state, new_bin_state,
ohrstrom@1504 252 "bin/beta/B.class",
ohrstrom@1504 253 "bin/beta/BINT.class",
ohrstrom@1504 254 "bin/javac_state");
ohrstrom@1504 255 previous_bin_state = new_bin_state;
ohrstrom@1504 256
ohrstrom@1504 257 Map<String,Long> new_headers_state = collectState(headers);
ohrstrom@1504 258 verifyThatFilesHaveBeenAdded(previous_headers_state, new_headers_state,
ohrstrom@1504 259 "headers/beta_B.h");
ohrstrom@1504 260 previous_headers_state = new_headers_state;
ohrstrom@1504 261 }
ohrstrom@1504 262
ohrstrom@1504 263 void incrementalCompileChangeNative() throws Exception {
ohrstrom@1504 264 System.out.println("\nNow update the B.java file with a new value for the final static"+
ohrstrom@1504 265 " annotated with @Native.");
ohrstrom@1504 266 System.out.println("Verify that beta_B.h is rewritten again!");
ohrstrom@1504 267 System.out.println("-------------------------------------------------------------------");
ohrstrom@1504 268
ohrstrom@1504 269 populate(gensrc,"beta/B.java",
ohrstrom@1504 270 "package beta; import alfa.A; public class B {"+
ohrstrom@1504 271 "private int b() { return A.DEFINITION; } "+
ohrstrom@1504 272 "@java.lang.annotation.Native final static int alfa = 43; }");
ohrstrom@1504 273
ohrstrom@1504 274 compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",
ohrstrom@1504 275 "--server:portfile=testserver,background=false", "--log=debug");
ohrstrom@1504 276 Map<String,Long> new_bin_state = collectState(bin);
ohrstrom@1504 277 verifyNewerFiles(previous_bin_state, new_bin_state,
ohrstrom@1504 278 "bin/beta/B.class",
ohrstrom@1504 279 "bin/beta/BINT.class",
ohrstrom@1504 280 "bin/javac_state");
ohrstrom@1504 281 previous_bin_state = new_bin_state;
ohrstrom@1504 282
ohrstrom@1504 283 Map<String,Long> new_headers_state = collectState(headers);
ohrstrom@1504 284 verifyNewerFiles(previous_headers_state, new_headers_state,
ohrstrom@1504 285 "headers/beta_B.h");
ohrstrom@1504 286 previous_headers_state = new_headers_state;
ohrstrom@1504 287 }
ohrstrom@1504 288
ohrstrom@1504 289 void compileWithOverrideSource() throws Exception {
ohrstrom@1504 290 System.out.println("\nNow verify that we can override sources to be compiled.");
ohrstrom@1504 291 System.out.println("Compile gensrc and gensrc2. However do not compile broken beta.B in gensrc,");
ohrstrom@1504 292 System.out.println("only compile ok beta.B in gensrc2.");
ohrstrom@1504 293 System.out.println("---------------------------------------------------------------------------");
ohrstrom@1504 294
ohrstrom@1504 295 delete(gensrc);
ohrstrom@1504 296 delete(gensrc2);
ohrstrom@1504 297 delete(bin);
ohrstrom@1504 298 previous_bin_state = collectState(bin);
ohrstrom@1504 299
ohrstrom@1504 300 populate(gensrc,"alfa/A.java",
ohrstrom@1504 301 "package alfa; import beta.B; import gamma.C; public class A { B b; C c; }",
ohrstrom@1504 302 "beta/B.java",
ohrstrom@1504 303 "package beta; public class B { broken",
ohrstrom@1504 304 "gamma/C.java",
ohrstrom@1504 305 "package gamma; public class C { }");
ohrstrom@1504 306
ohrstrom@1504 307 populate(gensrc2,
ohrstrom@1504 308 "beta/B.java",
ohrstrom@1504 309 "package beta; public class B { }");
ohrstrom@1504 310
ohrstrom@1504 311 compile("-x", "beta", "gensrc", "gensrc2", "-d", "bin", "-h", "headers", "-j", "1",
ohrstrom@1504 312 "--server:portfile=testserver,background=false");
ohrstrom@1504 313 Map<String,Long> new_bin_state = collectState(bin);
ohrstrom@1504 314 verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state,
ohrstrom@1504 315 "bin/alfa/A.class",
ohrstrom@1504 316 "bin/beta/B.class",
ohrstrom@1504 317 "bin/gamma/C.class",
ohrstrom@1504 318 "bin/javac_state");
ohrstrom@1504 319
ohrstrom@1504 320 System.out.println("----- Compile with exluded beta went well!");
ohrstrom@1504 321 delete(bin);
ohrstrom@1504 322 compileExpectFailure("gensrc", "gensrc2", "-d", "bin", "-h", "headers", "-j", "1",
ohrstrom@1504 323 "--server:portfile=testserver,background=false");
ohrstrom@1504 324
ohrstrom@1504 325 System.out.println("----- Compile without exluded beta failed, as expected! Good!");
ohrstrom@1504 326 delete(bin);
ohrstrom@1504 327 }
ohrstrom@1504 328
ohrstrom@1504 329 void compileWithInvisibleSources() throws Exception {
ohrstrom@1504 330 System.out.println("\nNow verify that we can make sources invisible to linking (sourcepath).");
ohrstrom@1504 331 System.out.println("Compile gensrc and link against gensrc2 and gensrc3, however");
ohrstrom@1504 332 System.out.println("gensrc2 contains broken code in beta.B, thus we must exclude that package");
ohrstrom@1504 333 System.out.println("fortunately gensrc3 contains a proper beta.B.");
ohrstrom@1504 334 System.out.println("------------------------------------------------------------------------");
ohrstrom@1504 335
ohrstrom@1504 336 // Start with a fresh gensrcs and bin.
ohrstrom@1504 337 delete(gensrc);
ohrstrom@1504 338 delete(gensrc2);
ohrstrom@1504 339 delete(gensrc3);
ohrstrom@1504 340 delete(bin);
ohrstrom@1504 341 previous_bin_state = collectState(bin);
ohrstrom@1504 342
ohrstrom@1504 343 populate(gensrc,"alfa/A.java",
ohrstrom@1504 344 "package alfa; import beta.B; import gamma.C; public class A { B b; C c; }");
ohrstrom@1504 345 populate(gensrc2,"beta/B.java",
ohrstrom@1504 346 "package beta; public class B { broken",
ohrstrom@1504 347 "gamma/C.java",
ohrstrom@1504 348 "package gamma; public class C { }");
ohrstrom@1504 349 populate(gensrc3, "beta/B.java",
ohrstrom@1504 350 "package beta; public class B { }");
ohrstrom@1504 351
ohrstrom@1504 352 compile("gensrc", "-x", "beta", "-sourcepath", "gensrc2",
ohrstrom@1504 353 "-sourcepath", "gensrc3", "-d", "bin", "-h", "headers", "-j", "1",
ohrstrom@1504 354 "--server:portfile=testserver,background=false");
ohrstrom@1504 355
ohrstrom@1504 356 System.out.println("The first compile went well!");
ohrstrom@1504 357 Map<String,Long> new_bin_state = collectState(bin);
ohrstrom@1504 358 verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state,
ohrstrom@1504 359 "bin/alfa/A.class",
ohrstrom@1504 360 "bin/javac_state");
ohrstrom@1504 361
ohrstrom@1504 362 System.out.println("----- Compile with exluded beta went well!");
ohrstrom@1504 363 delete(bin);
ohrstrom@1504 364 compileExpectFailure("gensrc", "-sourcepath", "gensrc2", "-sourcepath", "gensrc3",
ohrstrom@1504 365 "-d", "bin", "-h", "headers", "-j", "1",
ohrstrom@1504 366 "--server:portfile=testserver,background=false");
ohrstrom@1504 367
ohrstrom@1504 368 System.out.println("----- Compile without exluded beta failed, as expected! Good!");
ohrstrom@1504 369 delete(bin);
ohrstrom@1504 370 }
ohrstrom@1504 371
ohrstrom@1504 372 void compileCircularSources() throws Exception {
ohrstrom@1504 373 System.out.println("\nNow verify that circular sources split on multiple cores can be compiled.");
ohrstrom@1504 374 System.out.println("---------------------------------------------------------------------------");
ohrstrom@1504 375
ohrstrom@1504 376 // Start with a fresh gensrcs and bin.
ohrstrom@1504 377 delete(gensrc);
ohrstrom@1504 378 delete(gensrc2);
ohrstrom@1504 379 delete(gensrc3);
ohrstrom@1504 380 delete(bin);
ohrstrom@1504 381 previous_bin_state = collectState(bin);
ohrstrom@1504 382
ohrstrom@1504 383 populate(gensrc,"alfa/A.java",
ohrstrom@1504 384 "package alfa; public class A { beta.B b; }",
ohrstrom@1504 385 "beta/B.java",
ohrstrom@1504 386 "package beta; public class B { gamma.C c; }",
ohrstrom@1504 387 "gamma/C.java",
ohrstrom@1504 388 "package gamma; public class C { alfa.A a; }");
ohrstrom@1504 389
ohrstrom@1504 390 compile("gensrc", "-d", "bin", "-h", "headers", "-j", "3",
ohrstrom@1504 391 "--server:portfile=testserver,background=false","--log=debug");
ohrstrom@1504 392 Map<String,Long> new_bin_state = collectState(bin);
ohrstrom@1504 393 verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state,
ohrstrom@1504 394 "bin/alfa/A.class",
ohrstrom@1504 395 "bin/beta/B.class",
ohrstrom@1504 396 "bin/gamma/C.class",
ohrstrom@1504 397 "bin/javac_state");
ohrstrom@1504 398 delete(bin);
ohrstrom@1504 399 }
ohrstrom@1504 400
ohrstrom@1504 401 void removeFrom(Path dir, String... args) throws IOException {
ohrstrom@1504 402 for (String filename : args) {
ohrstrom@1504 403 Path p = dir.resolve(filename);
ohrstrom@1504 404 Files.delete(p);
ohrstrom@1504 405 }
ohrstrom@1504 406 }
ohrstrom@1504 407
ohrstrom@1504 408 void populate(Path src, String... args) throws IOException {
ohrstrom@1504 409 if (!Files.exists(src)) {
ohrstrom@1504 410 Files.createDirectory(src);
ohrstrom@1504 411 }
ohrstrom@1504 412 String[] a = args;
ohrstrom@1504 413 for (int i = 0; i<a.length; i+=2) {
ohrstrom@1504 414 String filename = a[i];
ohrstrom@1504 415 String content = a[i+1];
ohrstrom@1504 416 Path p = src.resolve(filename);
ohrstrom@1504 417 Files.createDirectories(p.getParent());
ohrstrom@1504 418 PrintWriter out = new PrintWriter(Files.newBufferedWriter(p,
ohrstrom@1504 419 Charset.defaultCharset()));
ohrstrom@1504 420 out.println(content);
ohrstrom@1504 421 out.close();
ohrstrom@1504 422 }
ohrstrom@1504 423 }
ohrstrom@1504 424
ohrstrom@1504 425 void delete(Path root) throws IOException {
ohrstrom@1504 426 if (!Files.exists(root)) return;
ohrstrom@1504 427 Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
ohrstrom@1504 428 @Override
ohrstrom@1504 429 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
ohrstrom@1504 430 {
ohrstrom@1504 431 Files.delete(file);
ohrstrom@1504 432 return FileVisitResult.CONTINUE;
ohrstrom@1504 433 }
ohrstrom@1504 434
ohrstrom@1504 435 @Override
ohrstrom@1504 436 public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException
ohrstrom@1504 437 {
ohrstrom@1504 438 if (e == null) {
ohrstrom@1504 439 if (!dir.equals(root)) Files.delete(dir);
ohrstrom@1504 440 return FileVisitResult.CONTINUE;
ohrstrom@1504 441 } else {
ohrstrom@1504 442 // directory iteration failed
ohrstrom@1504 443 throw e;
ohrstrom@1504 444 }
ohrstrom@1504 445 }
ohrstrom@1504 446 });
ohrstrom@1504 447 }
ohrstrom@1504 448
ohrstrom@1504 449 void compile(String... args) throws Exception {
ohrstrom@1504 450 int rc = main.go(args, System.out, System.err);
ohrstrom@1504 451 if (rc != 0) throw new Exception("Error during compile!");
ohrstrom@1504 452
ohrstrom@1504 453 // Wait a second, to get around the (temporary) problem with
ohrstrom@1504 454 // second resolution in the Java file api. But do not do this
ohrstrom@1504 455 // on windows where the timestamps work.
ohrstrom@1504 456 long in_a_sec = System.currentTimeMillis()+1000;
ohrstrom@1504 457 while (in_a_sec > System.currentTimeMillis()) {
ohrstrom@1504 458 try {
ohrstrom@1504 459 Thread.sleep(1000);
ohrstrom@1504 460 } catch (InterruptedException e) {
ohrstrom@1504 461 }
ohrstrom@1504 462 }
ohrstrom@1504 463 }
ohrstrom@1504 464
ohrstrom@1504 465 void compileExpectFailure(String... args) throws Exception {
ohrstrom@1504 466 int rc = main.go(args, System.out, System.err);
ohrstrom@1504 467 if (rc == 0) throw new Exception("Expected error during compile! Did not fail!");
ohrstrom@1504 468 }
ohrstrom@1504 469
ohrstrom@1504 470 Map<String,Long> collectState(Path dir) throws IOException
ohrstrom@1504 471 {
ohrstrom@1504 472 final Map<String,Long> files = new HashMap<>();
ohrstrom@1504 473 Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
ohrstrom@1504 474 @Override
ohrstrom@1504 475 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
ohrstrom@1504 476 throws IOException
ohrstrom@1504 477 {
ohrstrom@1504 478 files.put(file.toString(),new Long(Files.getLastModifiedTime(file).toMillis()));
ohrstrom@1504 479 return FileVisitResult.CONTINUE;
ohrstrom@1504 480 }
ohrstrom@1504 481 });
ohrstrom@1504 482 return files;
ohrstrom@1504 483 }
ohrstrom@1504 484
ohrstrom@1504 485 void verifyThatFilesHaveBeenRemoved(Map<String,Long> from,
ohrstrom@1504 486 Map<String,Long> to,
ohrstrom@1504 487 String... args) throws Exception {
ohrstrom@1504 488
ohrstrom@1504 489 Set<String> froms = from.keySet();
ohrstrom@1504 490 Set<String> tos = to.keySet();
ohrstrom@1504 491
ohrstrom@1504 492 if (froms.equals(tos)) {
ohrstrom@1504 493 throw new Exception("Expected new state to have fewer files than previous state!");
ohrstrom@1504 494 }
ohrstrom@1504 495
ohrstrom@1504 496 for (String t : tos) {
ohrstrom@1504 497 if (!froms.contains(t)) {
ohrstrom@1504 498 throw new Exception("Expected "+t+" to exist in previous state!");
ohrstrom@1504 499 }
ohrstrom@1504 500 }
ohrstrom@1504 501
ohrstrom@1504 502 for (String f : args) {
ohrstrom@1504 503 f = f.replace("/", File.separator);
ohrstrom@1504 504 if (!froms.contains(f)) {
ohrstrom@1504 505 throw new Exception("Expected "+f+" to exist in previous state!");
ohrstrom@1504 506 }
ohrstrom@1504 507 if (tos.contains(f)) {
ohrstrom@1504 508 throw new Exception("Expected "+f+" to have been removed from the new state!");
ohrstrom@1504 509 }
ohrstrom@1504 510 }
ohrstrom@1504 511
ohrstrom@1504 512 if (froms.size() - args.length != tos.size()) {
ohrstrom@1504 513 throw new Exception("There are more removed files than the expected list!");
ohrstrom@1504 514 }
ohrstrom@1504 515 }
ohrstrom@1504 516
ohrstrom@1504 517 void verifyThatFilesHaveBeenAdded(Map<String,Long> from,
ohrstrom@1504 518 Map<String,Long> to,
ohrstrom@1504 519 String... args) throws Exception {
ohrstrom@1504 520
ohrstrom@1504 521 Set<String> froms = from.keySet();
ohrstrom@1504 522 Set<String> tos = to.keySet();
ohrstrom@1504 523
ohrstrom@1504 524 if (froms.equals(tos)) {
ohrstrom@1504 525 throw new Exception("Expected new state to have more files than previous state!");
ohrstrom@1504 526 }
ohrstrom@1504 527
ohrstrom@1504 528 for (String t : froms) {
ohrstrom@1504 529 if (!tos.contains(t)) {
ohrstrom@1504 530 throw new Exception("Expected "+t+" to exist in new state!");
ohrstrom@1504 531 }
ohrstrom@1504 532 }
ohrstrom@1504 533
ohrstrom@1504 534 for (String f : args) {
ohrstrom@1504 535 f = f.replace("/", File.separator);
ohrstrom@1504 536 if (!tos.contains(f)) {
ohrstrom@1504 537 throw new Exception("Expected "+f+" to have been added to new state!");
ohrstrom@1504 538 }
ohrstrom@1504 539 if (froms.contains(f)) {
ohrstrom@1504 540 throw new Exception("Expected "+f+" to not exist in previous state!");
ohrstrom@1504 541 }
ohrstrom@1504 542 }
ohrstrom@1504 543
ohrstrom@1504 544 if (froms.size() + args.length != tos.size()) {
ohrstrom@1504 545 throw new Exception("There are more added files than the expected list!");
ohrstrom@1504 546 }
ohrstrom@1504 547 }
ohrstrom@1504 548
ohrstrom@1504 549 void verifyNewerFiles(Map<String,Long> from,
ohrstrom@1504 550 Map<String,Long> to,
ohrstrom@1504 551 String... args) throws Exception {
ohrstrom@1504 552 if (!from.keySet().equals(to.keySet())) {
ohrstrom@1504 553 throw new Exception("Expected the set of files to be identical!");
ohrstrom@1504 554 }
ohrstrom@1504 555 Set<String> files = new HashSet<String>();
ohrstrom@1504 556 for (String s : args) {
ohrstrom@1504 557 files.add(s.replace("/", File.separator));
ohrstrom@1504 558 }
ohrstrom@1504 559 for (String fn : from.keySet()) {
ohrstrom@1504 560 long f = from.get(fn);
ohrstrom@1504 561 long t = to.get(fn);
ohrstrom@1504 562 if (files.contains(fn)) {
ohrstrom@1504 563 if (t <= f) {
ohrstrom@1504 564 throw new Exception("Expected "+fn+" to have a more recent timestamp!");
ohrstrom@1504 565 }
ohrstrom@1504 566 } else {
ohrstrom@1504 567 if (t != f) {
ohrstrom@1504 568 throw new Exception("Expected "+fn+" to have the same timestamp!");
ohrstrom@1504 569 }
ohrstrom@1504 570 }
ohrstrom@1504 571 }
ohrstrom@1504 572 }
ohrstrom@1504 573
ohrstrom@1504 574 String print(Map<String,Long> m) {
ohrstrom@1504 575 StringBuilder b = new StringBuilder();
ohrstrom@1504 576 Set<String> keys = m.keySet();
ohrstrom@1504 577 for (String k : keys) {
ohrstrom@1504 578 b.append(k+" "+m.get(k)+"\n");
ohrstrom@1504 579 }
ohrstrom@1504 580 return b.toString();
ohrstrom@1504 581 }
ohrstrom@1504 582
ohrstrom@1504 583 void verifyEqual(Map<String,Long> from, Map<String,Long> to) throws Exception {
ohrstrom@1504 584 if (!from.equals(to)) {
ohrstrom@1504 585 System.out.println("FROM---"+print(from));
ohrstrom@1504 586 System.out.println("TO-----"+print(to));
ohrstrom@1504 587 throw new Exception("The dir should not differ! But it does!");
ohrstrom@1504 588 }
ohrstrom@1504 589 }
ohrstrom@1504 590 }

mercurial