OptionDecoding.java revision 3034:c8206f440046
1/*
2 * Copyright (c) 2014, 2015, 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 8054465
27 * @summary Tests decoding of String[] into Options.
28 *
29 * @modules jdk.compiler/com.sun.tools.sjavac
30 *          jdk.compiler/com.sun.tools.sjavac.client
31 *          jdk.compiler/com.sun.tools.sjavac.options
32 * @build Wrapper
33 * @run main Wrapper OptionDecoding
34 */
35
36import static util.OptionTestUtil.assertEquals;
37import static util.OptionTestUtil.checkFilesFound;
38
39import java.io.File;
40import java.io.IOException;
41import java.nio.file.Files;
42import java.nio.file.Path;
43import java.nio.file.Paths;
44import java.util.ArrayList;
45import java.util.Arrays;
46import java.util.Collections;
47import java.util.HashMap;
48import java.util.List;
49import java.util.Map;
50
51import com.sun.tools.sjavac.CopyFile;
52import com.sun.tools.sjavac.Module;
53import com.sun.tools.sjavac.Source;
54import com.sun.tools.sjavac.client.ClientMain;
55import com.sun.tools.sjavac.comp.SjavacImpl;
56import com.sun.tools.sjavac.options.Options;
57import com.sun.tools.sjavac.options.SourceLocation;
58
59public class OptionDecoding {
60
61    public static void main(String[] args) throws IOException {
62        testPaths();
63        testDupPaths();
64        testSourceLocations();
65        testSimpleOptions();
66        testServerConf();
67        testSearchPaths();
68        testTranslationRules();
69    }
70
71    // Test decoding of output paths
72    static void testPaths() throws IOException {
73        final String H = "headers";
74        final String G = "gensrc";
75        final String D = "dest";
76        final String stateDir = "stateDir";
77        final String CMP = "srcRefList.txt";
78
79        Options options = Options.parseArgs("-h", H, "-s", G, "-d", D, "--state-dir=" + stateDir,
80                                            "--compare-found-sources", CMP);
81
82        assertEquals(Paths.get(H).toAbsolutePath(), options.getHeaderDir());
83        assertEquals(Paths.get(G).toAbsolutePath(), options.getGenSrcDir());
84        assertEquals(Paths.get(D).toAbsolutePath(), options.getDestDir());
85        assertEquals(Paths.get(stateDir).toAbsolutePath(), options.getStateDir());
86        assertEquals(Paths.get(CMP), options.getSourceReferenceList());
87    }
88
89    // Providing duplicate header / dest / gensrc paths should produce an error.
90    static void testDupPaths() throws IOException {
91        try {
92            Options.parseArgs("-h", "dir1", "-h", "dir2");
93            throw new RuntimeException("Duplicate header directories should fail.");
94        } catch (IllegalArgumentException iae) {
95            // Expected
96        }
97
98        try {
99            Options.parseArgs("-s", "dir1", "-s", "dir2");
100            throw new RuntimeException("Duplicate paths for generated sources should fail.");
101        } catch (IllegalArgumentException iae) {
102            // Expected
103        }
104
105        try {
106            Options.parseArgs("-d", "dir1", "-d", "dir2");
107            throw new RuntimeException("Duplicate destination directories should fail.");
108        } catch (IllegalArgumentException iae) {
109            // Expected
110        }
111    }
112
113    // Test source locations and -x, -i, -xf, -if filters
114    static void testSourceLocations() throws IOException {
115        Path a1 = Paths.get("root/pkg1/ClassA1.java");
116        Path a2 = Paths.get("root/pkg1/ClassA2.java");
117        Path b1 = Paths.get("root/pkg1/pkg2/ClassB1.java");
118        Path b2 = Paths.get("root/pkg1/pkg2/ClassB2.java");
119        Path c1 = Paths.get("root/pkg3/ClassC1.java");
120        Path c2 = Paths.get("root/pkg3/ClassC2.java");
121
122        for (Path p : Arrays.asList(a1, a2, b1, b2, c1, c2)) {
123            Files.createDirectories(p.getParent());
124            Files.createFile(p);
125        }
126
127        // Test -if
128        {
129            Options options = Options.parseArgs("-if", "root/pkg1/ClassA1.java", "root");
130
131            Map<String, Source> foundFiles = new HashMap<>();
132            SjavacImpl.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles,
133                    new HashMap<String, Module>(), new Module("", ""), false, true);
134
135            checkFilesFound(foundFiles.keySet(), a1);
136        }
137
138        // Test -i
139        System.out.println("--------------------------- CHECKING -i ----------------");
140        {
141            Options options = Options.parseArgs("-i", "pkg1/*", "root");
142
143            Map<String, Source> foundFiles = new HashMap<>();
144            SjavacImpl.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles,
145                    new HashMap<String, Module>(), new Module("", ""), false, true);
146
147            checkFilesFound(foundFiles.keySet(), a1, a2, b1, b2);
148        }
149        System.out.println("--------------------------------------------------------");
150
151        // Test -xf
152        {
153            Options options = Options.parseArgs("-xf", "root/pkg1/ClassA1.java", "root");
154
155            Map<String, Source> foundFiles = new HashMap<>();
156            SjavacImpl.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles,
157                    new HashMap<String, Module>(), new Module("", ""), false, true);
158
159            checkFilesFound(foundFiles.keySet(), a2, b1, b2, c1, c2);
160        }
161
162        // Test -x
163        {
164            Options options = Options.parseArgs("-i", "pkg1/*", "root");
165
166            Map<String, Source> foundFiles = new HashMap<>();
167            SjavacImpl.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles,
168                    new HashMap<String, Module>(), new Module("", ""), false, true);
169
170            checkFilesFound(foundFiles.keySet(), a1, a2, b1, b2);
171        }
172
173        // Test -x and -i
174        {
175            Options options = Options.parseArgs("-i", "pkg1/*", "-x", "pkg1/pkg2/*", "root");
176
177            Map<String, Source> foundFiles = new HashMap<>();
178            SjavacImpl.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles,
179                    new HashMap<String, Module>(), new Module("", ""), false, true);
180
181            checkFilesFound(foundFiles.keySet(), a1, a2);
182        }
183    }
184
185    // Test basic options
186    static void testSimpleOptions() {
187        Options options = Options.parseArgs("-j", "17", "--log=debug");
188        assertEquals(17, options.getNumCores());
189        assertEquals("debug", options.getLogLevel());
190        assertEquals(false, options.isDefaultPackagePermitted());
191        assertEquals(false, options.areUnidentifiedArtifactsPermitted());
192        assertEquals(false, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath()));
193
194        options = Options.parseArgs("--permit-unidentified-artifacts",
195                                    "--permit-artifact=bar.txt",
196                                    "--permit-sources-without-package");
197        assertEquals("info", options.getLogLevel());
198        assertEquals(true, options.isDefaultPackagePermitted());
199        assertEquals(true, options.areUnidentifiedArtifactsPermitted());
200        assertEquals(true, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath()));
201    }
202
203    // Test server configuration options
204    static void testServerConf() {
205        Options options = Options.parseArgs("--server:someServerConfiguration");
206        assertEquals("someServerConfiguration", options.getServerConf());
207        assertEquals(false, options.startServerFlag());
208
209        options = Options.parseArgs("--startserver:someServerConfiguration");
210        assertEquals("someServerConfiguration", options.getServerConf());
211        assertEquals(true, options.startServerFlag());
212    }
213
214    // Test input paths
215    static void testSearchPaths() {
216        List<String> i, x, iF, xF;
217        i = x = iF = xF = new ArrayList<>();
218
219        SourceLocation dir1 = new SourceLocation(Paths.get("dir1"), i, x, iF, xF);
220        SourceLocation dir2 = new SourceLocation(Paths.get("dir2"), i, x, iF, xF);
221        String dir1_PS_dir2 = "dir1" + File.pathSeparator + "dir2";
222
223        Options options = Options.parseArgs("-sourcepath", dir1_PS_dir2);
224        assertEquals(options.getSourceSearchPaths(), Arrays.asList(dir1, dir2));
225
226        options = Options.parseArgs("-modulepath", dir1_PS_dir2);
227        assertEquals(options.getModuleSearchPaths(), Arrays.asList(dir1, dir2));
228
229        options = Options.parseArgs("-classpath", dir1_PS_dir2);
230        assertEquals(options.getClassSearchPath(), Arrays.asList(dir1, dir2));
231    }
232
233    // Test -tr option
234    static void testTranslationRules() {
235        Class<?> cls = com.sun.tools.sjavac.CompileJavaPackages.class;
236
237        Options options = Options.parseArgs(
238                "-tr", ".exa=" + cls.getName(),
239                "-tr", ".exb=" + cls.getName(),
240                "-copy", ".html");
241
242        assertEquals(cls, options.getTranslationRules().get(".exa").getClass());
243        assertEquals(cls, options.getTranslationRules().get(".exb").getClass());
244        assertEquals(CopyFile.class, options.getTranslationRules().get(".html").getClass());
245    }
246}
247