1 /*
   2  * Copyright (c) 2015, 2016, 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  * @test
  26  * @summary tests for output directory
  27  * @library /tools/lib
  28  * @modules
  29  *      jdk.compiler/com.sun.tools.javac.api
  30  *      jdk.compiler/com.sun.tools.javac.main
  31  * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
  32  * @run main OutputDirTest
  33  */
  34 
  35 import java.io.IOException;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;
  39 
  40 import toolbox.JavacTask;
  41 import toolbox.Task;
  42 import toolbox.ToolBox;
  43 
  44 public class OutputDirTest extends ModuleTestBase {
  45     public static void main(String... args) throws Exception {
  46         new OutputDirTest().run();
  47     }
  48 
  49     Path src;
  50 
  51     void run() throws Exception {
  52         tb = new ToolBox();
  53 
  54         src = Paths.get("src");
  55         tb.writeJavaFiles(src.resolve("m"),
  56                 "module m { }",
  57                 "package p; class C { }");
  58 
  59         runTests();
  60     }
  61 
  62     @Test
  63     public void testError(Path base) throws Exception {
  64         String log = new JavacTask(tb)
  65                 .options("-XDrawDiagnostics",
  66                         "-modulesourcepath", src.toString())
  67                 .files(findJavaFiles(src))
  68                 .run(Task.Expect.FAIL)
  69                 .writeAll()
  70                 .getOutput(Task.OutputKind.DIRECT);
  71 
  72         if (!log.contains("- compiler.err.no.output.dir"))
  73             throw new Exception("expected output not found");
  74     }
  75 
  76     @Test
  77     public void testProcOnly(Path base) throws IOException {
  78         new JavacTask(tb)
  79                 .options("-XDrawDiagnostics",
  80                         "-proc:only",
  81                         "-modulesourcepath", src.toString())
  82                 .files(findJavaFiles(src))
  83                 .run(Task.Expect.SUCCESS)
  84                 .writeAll();
  85     }
  86 
  87     @Test
  88     public void testClassOutDir(Path base) throws IOException {
  89         Path classes = base.resolve("classes");
  90         new JavacTask(tb)
  91                 .options("-XDrawDiagnostics",
  92                         "-d", classes.toString(),
  93                         "-modulesourcepath", src.toString())
  94                 .files(findJavaFiles(src))
  95                 .run(Task.Expect.SUCCESS)
  96                 .writeAll();
  97     }
  98 
  99     @Test
 100     public void testExplodedOutDir(Path base) throws Exception {
 101         Path modSrc = base.resolve("modSrc");
 102         tb.writeJavaFiles(modSrc,
 103                 "module m1 { exports p; }",
 104                 "package p; public class CC { }");
 105         Path modClasses = base.resolve("modClasses");
 106         Files.createDirectories(modClasses);
 107 
 108         new JavacTask(tb, Task.Mode.CMDLINE)
 109                 .outdir(modClasses)
 110                 .files(findJavaFiles(modSrc))
 111                 .run()
 112                 .writeAll();
 113 
 114         Path src = base.resolve("src");
 115         Path src_m = src.resolve("m");
 116         tb.writeJavaFiles(src_m,
 117                 "module m { requires m1 ; }",
 118                 "class C { }");
 119 
 120         String log = new JavacTask(tb, Task.Mode.CMDLINE)
 121                 .outdir(modClasses) // an exploded module
 122                 .options("-XDrawDiagnostics",
 123                         "-modulesourcepath", src.toString())
 124                 .files(findJavaFiles(src))
 125                 .run(Task.Expect.FAIL)
 126                 .writeAll()
 127                 .getOutput(Task.OutputKind.DIRECT);
 128 
 129         if (!log.contains("- compiler.err.multi-module.outdir.cannot.be.exploded.module: " + modClasses.toString()))
 130             throw new Exception("expected output not found");
 131     }
 132 
 133     @Test
 134     public void testInExplodedOutDir(Path base) throws Exception {
 135         Path modSrc = base.resolve("modSrc");
 136         tb.writeJavaFiles(modSrc,
 137                 "module m1 { exports p; }",
 138                 "package p; public class CC { }");
 139         Path modClasses = base.resolve("modClasses");
 140         Files.createDirectories(modClasses);
 141 
 142         new JavacTask(tb, Task.Mode.CMDLINE)
 143                 .outdir(modClasses)
 144                 .files(findJavaFiles(modSrc))
 145                 .run()
 146                 .writeAll();
 147 
 148         Path src = base.resolve("src");
 149         tb.writeJavaFiles(src,
 150                 "module m { requires m1 ; }",
 151                 "class C { }");
 152 
 153         Path classes = modClasses.resolve("m");
 154         Files.createDirectories(classes);
 155 
 156         String log = new JavacTask(tb, Task.Mode.CMDLINE)
 157                 .outdir(classes) // within an exploded module
 158                 .options("-XDrawDiagnostics",
 159                         "-Xlint", "-Werror",
 160                         "-modulepath", modClasses.toString())
 161                 .files(findJavaFiles(src))
 162                 .run(Task.Expect.FAIL)
 163                 .writeAll()
 164                 .getOutput(Task.OutputKind.DIRECT);
 165 
 166         if (!log.contains("- compiler.warn.outdir.is.in.exploded.module: " + classes.toString()))
 167             throw new Exception("expected output not found");
 168     }
 169 }