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