1 /*
   2  * Copyright (c) 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @bug 8035063
  29  * @summary Tests the preparation of javac-arguments.
  30  *
  31  * @build Wrapper
  32  * @run main Wrapper JavacOptionPrep
  33  */
  34 
  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.nio.file.Files;
  38 import java.nio.file.Paths;
  39 import java.util.Arrays;
  40 import java.util.Iterator;
  41 
  42 import com.sun.tools.sjavac.options.Options;
  43 
  44 
  45 public class JavacOptionPrep {
  46 
  47     enum TestPath {
  48         CP1, CP2, SRC1, SRC2, SOURCEPATH1, SOURCEPATH2;
  49 
  50         public String toString() {
  51             return name().toLowerCase();
  52         }
  53     }
  54 
  55     private final static String SEP = File.pathSeparator;
  56 
  57     public static void main(String[] unused) throws IOException {
  58 
  59         for (TestPath p : TestPath.values())
  60             Files.createDirectory(Paths.get(p.toString()));
  61 
  62         // Test some various cases:
  63         //  - Paths combined with File.pathSeparator (CP1 / CP2)
  64         //  - Paths given as duplicate options (SOURCEPATH1 / SOURCEPATH2)
  65         //  - Sources provided by -src (SRC1)
  66         //  - Sources provided without preceding option (SRC2)
  67         //  - An unrecognized option which is to be passed on to javac
  68         String sjavacArgs = "-cp " + TestPath.CP1 + SEP + TestPath.CP2 +
  69                             " -d dest " +
  70                             " -h header" +
  71                             " -sourcepath " + TestPath.SOURCEPATH1 +
  72                             " -src " + TestPath.SRC1 +
  73                             " -s gensrc" +
  74                             " -sourcepath " + TestPath.SOURCEPATH2 +
  75                             " " + TestPath.SRC2 +
  76                             " -unrecognized";
  77 
  78         Options options = Options.parseArgs(sjavacArgs.split(" "));
  79 
  80         // Extract javac-options
  81         String[] javacArgs = options.prepJavacArgs();
  82 
  83         // Check the result
  84         boolean destDirFound = false;
  85         boolean headerDirFound = false;
  86         boolean gensrcDirFound = false;
  87         boolean classPathFound = false;
  88         boolean sourcePathFound = false;
  89         boolean unrecognizedFound = false;
  90         boolean implicitNoneFound = false;
  91 
  92         Iterator<String> javacArgIter = Arrays.asList(javacArgs).iterator();
  93         while (javacArgIter.hasNext()) {
  94 
  95             String option = javacArgIter.next();
  96 
  97             switch (option) {
  98             case "-classpath":
  99             case "-cp":
 100                 classPathFound = true;
 101                 assertEquals(TestPath.CP1 + SEP + TestPath.CP2,
 102                              javacArgIter.next());
 103                 break;
 104 
 105             case "-d":
 106                 destDirFound = true;
 107                 assertEquals(Paths.get("dest").toAbsolutePath().toString(),
 108                              javacArgIter.next());
 109                 break;
 110 
 111             case "-h":
 112                 headerDirFound = true;
 113                 assertEquals(Paths.get("header").toAbsolutePath().toString(),
 114                              javacArgIter.next());
 115                 break;
 116 
 117             case "-s":
 118                 gensrcDirFound = true;
 119                 assertEquals(Paths.get("gensrc").toAbsolutePath().toString(),
 120                              javacArgIter.next());
 121                 break;
 122 
 123             case "-sourcepath":
 124                 sourcePathFound = true;
 125                 assertEquals(TestPath.SRC1 + SEP +
 126                              TestPath.SRC2 + SEP +
 127                              TestPath.SOURCEPATH1 + SEP +
 128                              TestPath.SOURCEPATH2,
 129                              javacArgIter.next());
 130                 break;
 131 
 132             case "-unrecognized":
 133                 unrecognizedFound = true;
 134                 break;
 135 
 136             case "-implicit:none":
 137                 implicitNoneFound = true;
 138                 break;
 139 
 140                 // Note that *which* files to actually compile is not dealt
 141                 // with by prepJavacArgs.
 142 
 143             default:
 144                 throw new AssertionError("Unexpected option found: " + option);
 145             }
 146         }
 147 
 148         if (!destDirFound)
 149             throw new AssertionError("Dest directory not found.");
 150 
 151         if (!headerDirFound)
 152             throw new AssertionError("Header directory not found.");
 153 
 154         if (!gensrcDirFound)
 155             throw new AssertionError("Generated source directory not found.");
 156 
 157         if (!classPathFound)
 158             throw new AssertionError("Class path not found.");
 159 
 160         if (!sourcePathFound)
 161             throw new AssertionError("Source path not found.");
 162 
 163         if (!unrecognizedFound)
 164             throw new AssertionError("\"-unrecognized\" not found.");
 165 
 166         if (!implicitNoneFound)
 167             throw new AssertionError("\"-implicit:none\" not found.");
 168 
 169     }
 170 
 171     static void assertEquals(Object expected, Object actual) {
 172         if (!expected.equals(actual))
 173             throw new AssertionError("Expected " + expected + " but got " + actual);
 174     }
 175 }