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 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.comp
32 *          jdk.compiler/com.sun.tools.sjavac.options
33 * @build Wrapper
34 * @run main Wrapper OptionDecoding
35 */
36
37import static util.OptionTestUtil.assertEquals;
38import static util.OptionTestUtil.checkFilesFound;
39
40import java.io.File;
41import java.io.IOException;
42import java.nio.file.Files;
43import java.nio.file.Path;
44import java.nio.file.Paths;
45import java.util.ArrayList;
46import java.util.Arrays;
47import java.util.Collections;
48import java.util.HashMap;
49import java.util.List;
50import java.util.Map;
51
52import com.sun.tools.sjavac.CopyFile;
53import com.sun.tools.sjavac.Module;
54import com.sun.tools.sjavac.Source;
55import com.sun.tools.sjavac.client.ClientMain;
56import com.sun.tools.sjavac.comp.SjavacImpl;
57import com.sun.tools.sjavac.options.Options;
58import com.sun.tools.sjavac.options.SourceLocation;
59
60public class OptionDecoding {
61
62    public static void main(String[] args) throws IOException {
63        testPaths();
64        testDupPaths();
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 basic options
114    static void testSimpleOptions() {
115        Options options = Options.parseArgs("-j", "17", "--log=debug");
116        assertEquals(17, options.getNumCores());
117        assertEquals("debug", options.getLogLevel());
118        assertEquals(false, options.isDefaultPackagePermitted());
119        assertEquals(false, options.areUnidentifiedArtifactsPermitted());
120        assertEquals(false, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath()));
121
122        options = Options.parseArgs("--permit-unidentified-artifacts",
123                                    "--permit-artifact=bar.txt",
124                                    "--permit-sources-without-package");
125        assertEquals("info", options.getLogLevel());
126        assertEquals(true, options.isDefaultPackagePermitted());
127        assertEquals(true, options.areUnidentifiedArtifactsPermitted());
128        assertEquals(true, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath()));
129    }
130
131    // Test server configuration options
132    static void testServerConf() {
133        Options options = Options.parseArgs("--server:someServerConfiguration");
134        assertEquals("someServerConfiguration", options.getServerConf());
135        assertEquals(false, options.startServerFlag());
136
137        options = Options.parseArgs("--startserver:someServerConfiguration");
138        assertEquals("someServerConfiguration", options.getServerConf());
139        assertEquals(true, options.startServerFlag());
140    }
141
142    // Test input paths
143    static void testSearchPaths() {
144        List<String> i, x, iF, xF;
145        i = x = iF = xF = new ArrayList<>();
146
147        SourceLocation dir1 = new SourceLocation(Paths.get("dir1"), i, x);
148        SourceLocation dir2 = new SourceLocation(Paths.get("dir2"), i, x);
149        String dir1_PS_dir2 = "dir1" + File.pathSeparator + "dir2";
150
151        Options options = Options.parseArgs("--source-path", dir1_PS_dir2);
152        assertEquals(options.getSourceSearchPaths(), Arrays.asList(dir1, dir2));
153
154        options = Options.parseArgs("--module-path", dir1_PS_dir2);
155        assertEquals(options.getModuleSearchPaths(), Arrays.asList(dir1, dir2));
156
157        options = Options.parseArgs("--class-path", dir1_PS_dir2);
158        assertEquals(options.getClassSearchPath(), Arrays.asList(dir1, dir2));
159    }
160
161    // Test -tr option
162    static void testTranslationRules() {
163        Class<?> cls = com.sun.tools.sjavac.CompileJavaPackages.class;
164
165        Options options = Options.parseArgs(
166                "-tr", ".exa=" + cls.getName(),
167                "-tr", ".exb=" + cls.getName(),
168                "-copy", ".html");
169
170        assertEquals(cls, options.getTranslationRules().get(".exa").getClass());
171        assertEquals(cls, options.getTranslationRules().get(".exb").getClass());
172        assertEquals(CopyFile.class, options.getTranslationRules().get(".html").getClass());
173    }
174}
175