Path: blob/master/test/langtools/tools/sjavac/JavacOptionPrep.java
41144 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 803506326* @summary Tests the preparation of javac-arguments.27*28* @modules jdk.compiler/com.sun.tools.sjavac.options29* @build Wrapper30* @run main Wrapper JavacOptionPrep31*/3233import java.io.File;34import java.io.IOException;35import java.nio.file.Files;36import java.nio.file.Paths;37import java.util.Arrays;38import java.util.Iterator;3940import com.sun.tools.sjavac.options.Options;414243public class JavacOptionPrep {4445enum TestPath {46CP1, CP2, SRC1, SRC2, SOURCEPATH1, SOURCEPATH2;4748public String toString() {49return name().toLowerCase();50}51}5253private final static String SEP = File.pathSeparator;5455public static void main(String[] unused) throws IOException {5657for (TestPath p : TestPath.values())58Files.createDirectory(Paths.get(p.toString()));5960// Test some various cases:61// - Paths combined with File.pathSeparator (CP1 / CP2)62// - Paths given as duplicate options (SOURCEPATH1 / SOURCEPATH2)63// - Sources provided by -src (SRC1)64// - Sources provided without preceding option (SRC2)65// - An unrecognized option which is to be passed on to javac66String sjavacArgs = "-cp " + TestPath.CP1 + SEP + TestPath.CP2 +67" -d dest" +68" -h header" +69" -sourcepath " + TestPath.SOURCEPATH1 +70" -src " + TestPath.SRC1 +71" -s gensrc" +72" -sourcepath " + TestPath.SOURCEPATH2 +73" " + TestPath.SRC2 +74" -unrecognized";7576Options options = Options.parseArgs(sjavacArgs.split(" "));7778// Extract javac-options79String[] javacArgs = options.prepJavacArgs();8081// Check the result82boolean destDirFound = false;83boolean userPathsFirst = false;84boolean headerDirFound = false;85boolean gensrcDirFound = false;86boolean classPathFound = false;87boolean sourcePathFound = false;88boolean unrecognizedFound = false;89boolean implicitNoneFound = false;9091Iterator<String> javacArgIter = Arrays.asList(javacArgs).iterator();92while (javacArgIter.hasNext()) {9394String option = javacArgIter.next();9596// Ignore this option for now. When the file=... requirement goes97// away, this will be easier to handle.98if (option.startsWith("--debug=completionDeps"))99continue;100101switch (option) {102case "-classpath":103case "-cp":104classPathFound = true;105assertEquals(TestPath.CP1 + SEP + TestPath.CP2,106javacArgIter.next());107break;108109case "-d":110destDirFound = true;111assertEquals(Paths.get("dest").toAbsolutePath().toString(),112javacArgIter.next());113break;114115case "-h":116headerDirFound = true;117assertEquals(Paths.get("header").toAbsolutePath().toString(),118javacArgIter.next());119break;120121case "-s":122gensrcDirFound = true;123assertEquals(Paths.get("gensrc").toAbsolutePath().toString(),124javacArgIter.next());125break;126127case "-sourcepath":128sourcePathFound = true;129assertEquals(TestPath.SRC1 + SEP +130TestPath.SRC2 + SEP +131TestPath.SOURCEPATH1 + SEP +132TestPath.SOURCEPATH2,133javacArgIter.next());134break;135136case "-unrecognized":137unrecognizedFound = true;138break;139140case "-implicit:none":141implicitNoneFound = true;142break;143144// Note that *which* files to actually compile is not dealt145// with by prepJavacArgs.146147default:148throw new AssertionError("Unexpected option found: " + option);149}150}151152if (!destDirFound)153throw new AssertionError("Dest directory not found.");154155if (!headerDirFound)156throw new AssertionError("Header directory not found.");157158if (!gensrcDirFound)159throw new AssertionError("Generated source directory not found.");160161if (!classPathFound)162throw new AssertionError("Class path not found.");163164if (!sourcePathFound)165throw new AssertionError("Source path not found.");166167if (!unrecognizedFound)168throw new AssertionError("\"-unrecognized\" not found.");169170if (!implicitNoneFound)171throw new AssertionError("\"-implicit:none\" not found.");172}173174static void assertEquals(Object expected, Object actual) {175if (!expected.equals(actual))176throw new AssertionError("Expected " + expected + " but got " + actual);177}178}179180181