test/tools/sjavac/SJavac.java

Mon, 21 Mar 2016 15:00:03 -0700

author
asaha
date
Mon, 21 Mar 2016 15:00:03 -0700
changeset 3197
837f6e6559d5
parent 1957
4300c2f5fb1b
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

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

mercurial