JavacOptionPrep.java revision 3528:5538ba41cb97
1/*
2 * Copyright (c) 2014, 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 * @bug 8035063
27 * @summary Tests the preparation of javac-arguments.
28 *
29 * @modules jdk.compiler/com.sun.tools.sjavac.options
30 * @build Wrapper
31 * @run main Wrapper JavacOptionPrep
32 */
33
34import java.io.File;
35import java.io.IOException;
36import java.nio.file.Files;
37import java.nio.file.Paths;
38import java.util.Arrays;
39import java.util.Iterator;
40
41import com.sun.tools.sjavac.options.Options;
42
43
44public class JavacOptionPrep {
45
46    enum TestPath {
47        CP1, CP2, SRC1, SRC2, SOURCEPATH1, SOURCEPATH2;
48
49        public String toString() {
50            return name().toLowerCase();
51        }
52    }
53
54    private final static String SEP = File.pathSeparator;
55
56    public static void main(String[] unused) throws IOException {
57
58        for (TestPath p : TestPath.values())
59            Files.createDirectory(Paths.get(p.toString()));
60
61        // Test some various cases:
62        //  - Paths combined with File.pathSeparator (CP1 / CP2)
63        //  - Paths given as duplicate options (SOURCEPATH1 / SOURCEPATH2)
64        //  - Sources provided by -src (SRC1)
65        //  - Sources provided without preceding option (SRC2)
66        //  - An unrecognized option which is to be passed on to javac
67        String sjavacArgs = "-cp " + TestPath.CP1 + SEP + TestPath.CP2 +
68                            " -d dest" +
69                            " -h header" +
70                            " -sourcepath " + TestPath.SOURCEPATH1 +
71                            " -src " + TestPath.SRC1 +
72                            " -s gensrc" +
73                            " -sourcepath " + TestPath.SOURCEPATH2 +
74                            " " + TestPath.SRC2 +
75                            " -unrecognized";
76
77        Options options = Options.parseArgs(sjavacArgs.split(" "));
78
79        // Extract javac-options
80        String[] javacArgs = options.prepJavacArgs();
81
82        // Check the result
83        boolean destDirFound = false;
84        boolean userPathsFirst = 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            // Ignore this option for now. When the file=... requirement goes
98            // away, this will be easier to handle.
99            if (option.startsWith("-Xdebug:completionDeps"))
100                continue;
101
102            switch (option) {
103            case "-classpath":
104            case "-cp":
105                classPathFound = true;
106                assertEquals(TestPath.CP1 + SEP + TestPath.CP2,
107                             javacArgIter.next());
108                break;
109
110            case "-d":
111                destDirFound = true;
112                assertEquals(Paths.get("dest").toAbsolutePath().toString(),
113                             javacArgIter.next());
114                break;
115
116            case "-h":
117                headerDirFound = true;
118                assertEquals(Paths.get("header").toAbsolutePath().toString(),
119                             javacArgIter.next());
120                break;
121
122            case "-s":
123                gensrcDirFound = true;
124                assertEquals(Paths.get("gensrc").toAbsolutePath().toString(),
125                             javacArgIter.next());
126                break;
127
128            case "-sourcepath":
129                sourcePathFound = true;
130                assertEquals(TestPath.SRC1 + SEP +
131                             TestPath.SRC2 + SEP +
132                             TestPath.SOURCEPATH1 + SEP +
133                             TestPath.SOURCEPATH2,
134                             javacArgIter.next());
135                break;
136
137            case "-unrecognized":
138                unrecognizedFound = true;
139                break;
140
141            case "-implicit:none":
142                implicitNoneFound = true;
143                break;
144
145                // Note that *which* files to actually compile is not dealt
146                // with by prepJavacArgs.
147
148            default:
149                throw new AssertionError("Unexpected option found: " + option);
150            }
151        }
152
153        if (!destDirFound)
154            throw new AssertionError("Dest directory not found.");
155
156        if (!headerDirFound)
157            throw new AssertionError("Header directory not found.");
158
159        if (!gensrcDirFound)
160            throw new AssertionError("Generated source directory not found.");
161
162        if (!classPathFound)
163            throw new AssertionError("Class path not found.");
164
165        if (!sourcePathFound)
166            throw new AssertionError("Source path not found.");
167
168        if (!unrecognizedFound)
169            throw new AssertionError("\"-unrecognized\" not found.");
170
171        if (!implicitNoneFound)
172            throw new AssertionError("\"-implicit:none\" not found.");
173    }
174
175    static void assertEquals(Object expected, Object actual) {
176        if (!expected.equals(actual))
177            throw new AssertionError("Expected " + expected + " but got " + actual);
178    }
179}
180