OptionDecoding.java revision 3573:c4a18ee691c4
130581Sjmb/*
230581Sjmb * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
330581Sjmb * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
430581Sjmb *
530581Sjmb * This code is free software; you can redistribute it and/or modify it
630581Sjmb * under the terms of the GNU General Public License version 2 only, as
730581Sjmb * published by the Free Software Foundation.
830581Sjmb *
930581Sjmb * This code is distributed in the hope that it will be useful, but WITHOUT
1030581Sjmb * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1130581Sjmb * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1230581Sjmb * version 2 for more details (a copy is included in the LICENSE file that
1330581Sjmb * accompanied this code).
1430581Sjmb *
1530581Sjmb * You should have received a copy of the GNU General Public License version
1630581Sjmb * 2 along with this work; if not, write to the Free Software Foundation,
1730581Sjmb * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1830581Sjmb *
1930581Sjmb * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2030581Sjmb * or visit www.oracle.com if you need additional information or have any
2130581Sjmb * questions.
2230581Sjmb */
2330581Sjmb
2430581Sjmb/*
2530581Sjmb * @test
2630581Sjmb * @bug 8035063 8054465
2730581Sjmb * @summary Tests decoding of String[] into Options.
2830581Sjmb *
2930581Sjmb * @modules jdk.compiler/com.sun.tools.sjavac
3030581Sjmb *          jdk.compiler/com.sun.tools.sjavac.client
3130581Sjmb *          jdk.compiler/com.sun.tools.sjavac.comp
3230581Sjmb *          jdk.compiler/com.sun.tools.sjavac.options
3330581Sjmb * @build Wrapper
3430581Sjmb * @run main Wrapper OptionDecoding
3530581Sjmb */
3630581Sjmb
3730581Sjmbimport static util.OptionTestUtil.assertEquals;
3830581Sjmbimport static util.OptionTestUtil.checkFilesFound;
3930581Sjmb
4030581Sjmbimport java.io.File;
4130581Sjmbimport java.io.IOException;
4230581Sjmbimport java.nio.file.Files;
4330581Sjmbimport java.nio.file.Path;
4430581Sjmbimport java.nio.file.Paths;
4530581Sjmbimport java.util.ArrayList;
4630581Sjmbimport java.util.Arrays;
4730581Sjmbimport java.util.Collections;
4830581Sjmbimport java.util.HashMap;
4930581Sjmbimport java.util.List;
5030581Sjmbimport java.util.Map;
5130581Sjmb
5230581Sjmbimport com.sun.tools.sjavac.CopyFile;
5330581Sjmbimport com.sun.tools.sjavac.Module;
5430581Sjmbimport com.sun.tools.sjavac.Source;
5530581Sjmbimport com.sun.tools.sjavac.client.ClientMain;
5630581Sjmbimport com.sun.tools.sjavac.comp.SjavacImpl;
5730581Sjmbimport com.sun.tools.sjavac.options.Options;
5830581Sjmbimport com.sun.tools.sjavac.options.SourceLocation;
5930581Sjmb
6030581Sjmbpublic class OptionDecoding {
6130581Sjmb
6230581Sjmb    public static void main(String[] args) throws IOException {
6330581Sjmb        testPaths();
6430581Sjmb        testDupPaths();
6530581Sjmb        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