test/tools/sjavac/SJavac.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial