test/tools/sjavac/SJavac.java

Tue, 22 Jan 2013 18:43:22 -0800

author
jjg
date
Tue, 22 Jan 2013 18:43:22 -0800
changeset 1516
8943b4213f59
parent 1504
22e417cdddee
child 1953
71b0089b146f
permissions
-rw-r--r--

8006723: sjavac test fails to compile on clean build
Reviewed-by: ksrini

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

mercurial