OptionDecoding.java revision 3012:adba44f6b471
11592Srgrimes/*
21592Srgrimes * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
31592Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41592Srgrimes *
51592Srgrimes * This code is free software; you can redistribute it and/or modify it
61592Srgrimes * under the terms of the GNU General Public License version 2 only, as
71592Srgrimes * published by the Free Software Foundation.  Oracle designates this
81592Srgrimes * particular file as subject to the "Classpath" exception as provided
91592Srgrimes * by Oracle in the LICENSE file that accompanied this code.
101592Srgrimes *
111592Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
121592Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
131592Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
141592Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
151592Srgrimes * accompanied this code).
161592Srgrimes *
171592Srgrimes * You should have received a copy of the GNU General Public License version
181592Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
191592Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
201592Srgrimes *
211592Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
221592Srgrimes * or visit www.oracle.com if you need additional information or have any
231592Srgrimes * questions.
241592Srgrimes */
251592Srgrimes
261592Srgrimes/*
271592Srgrimes * @test
281592Srgrimes * @bug 8035063 8054465
291592Srgrimes * @summary Tests decoding of String[] into Options.
301592Srgrimes *
311592Srgrimes * @modules jdk.compiler/com.sun.tools.sjavac
321592Srgrimes *          jdk.compiler/com.sun.tools.sjavac.client
331592Srgrimes *          jdk.compiler/com.sun.tools.sjavac.options
341592Srgrimes * @build Wrapper
351592Srgrimes * @run main Wrapper OptionDecoding
361592Srgrimes */
371592Srgrimes
381592Srgrimesimport static util.OptionTestUtil.assertEquals;
391592Srgrimesimport static util.OptionTestUtil.checkFilesFound;
401592Srgrimes
411592Srgrimesimport java.io.File;
421592Srgrimesimport java.io.IOException;
431592Srgrimesimport java.nio.file.Files;
441592Srgrimesimport java.nio.file.Path;
451592Srgrimesimport java.nio.file.Paths;
461592Srgrimesimport java.util.ArrayList;
471592Srgrimesimport java.util.Arrays;
481592Srgrimesimport java.util.Collections;
491592Srgrimesimport java.util.HashMap;
501592Srgrimesimport java.util.List;
511592Srgrimesimport java.util.Map;
521592Srgrimes
531592Srgrimesimport com.sun.tools.sjavac.CopyFile;
541592Srgrimesimport com.sun.tools.sjavac.Module;
551592Srgrimesimport com.sun.tools.sjavac.Source;
561592Srgrimesimport com.sun.tools.sjavac.client.ClientMain;
571592Srgrimesimport com.sun.tools.sjavac.comp.SjavacImpl;
581592Srgrimesimport com.sun.tools.sjavac.options.Options;
591592Srgrimesimport com.sun.tools.sjavac.options.SourceLocation;
601592Srgrimes
611592Srgrimespublic class OptionDecoding {
621592Srgrimes
631592Srgrimes    public static void main(String[] args) throws IOException {
641592Srgrimes
651592Srgrimes        testPaths();
661592Srgrimes        testDupPaths();
671592Srgrimes        testSourceLocations();
681592Srgrimes        testSimpleOptions();
691592Srgrimes        testServerConf();
701592Srgrimes        testSearchPaths();
711592Srgrimes        testTranslationRules();
721592Srgrimes
731592Srgrimes    }
741592Srgrimes
751592Srgrimes    // Test decoding of output paths
761592Srgrimes    static void testPaths() throws IOException {
771592Srgrimes
781592Srgrimes        final String H = "headers";
791592Srgrimes        final String G = "gensrc";
801592Srgrimes        final String D = "dest";
811592Srgrimes        final String CMP = "srcRefList.txt";
821592Srgrimes
831592Srgrimes        Options options = Options.parseArgs("-h", H, "-s", G, "-d", D,
841592Srgrimes                                            "--compare-found-sources", CMP);
851592Srgrimes
861592Srgrimes        assertEquals(Paths.get(H).toAbsolutePath(), options.getHeaderDir());
871592Srgrimes        assertEquals(Paths.get(G).toAbsolutePath(), options.getGenSrcDir());
881592Srgrimes        assertEquals(Paths.get(D).toAbsolutePath(), options.getDestDir());
891592Srgrimes        assertEquals(Paths.get(CMP), options.getSourceReferenceList());
901592Srgrimes
911592Srgrimes    }
921592Srgrimes
931592Srgrimes    // Providing duplicate header / dest / gensrc paths should produce an error.
941592Srgrimes    static void testDupPaths() throws IOException {
951592Srgrimes
961592Srgrimes        try {
971592Srgrimes            Options.parseArgs("-h", "dir1", "-h", "dir2");
981592Srgrimes            throw new RuntimeException("Duplicate header directories should fail.");
991592Srgrimes        } catch (IllegalArgumentException iae) {
1001592Srgrimes            // Expected
1011592Srgrimes        }
1021592Srgrimes
1031592Srgrimes        try {
1041592Srgrimes            Options.parseArgs("-s", "dir1", "-s", "dir2");
1051592Srgrimes            throw new RuntimeException("Duplicate paths for generated sources should fail.");
1061592Srgrimes        } catch (IllegalArgumentException iae) {
1071592Srgrimes            // Expected
1081592Srgrimes        }
1091592Srgrimes
1101592Srgrimes        try {
1111592Srgrimes            Options.parseArgs("-d", "dir1", "-d", "dir2");
1121592Srgrimes            throw new RuntimeException("Duplicate destination directories should fail.");
1131592Srgrimes        } catch (IllegalArgumentException iae) {
1141592Srgrimes            // Expected
1151592Srgrimes        }
1161592Srgrimes
1171592Srgrimes    }
1181592Srgrimes
1191592Srgrimes    // Test source locations and -x, -i, -xf, -if filters
1201592Srgrimes    static void testSourceLocations() throws IOException {
1211592Srgrimes
1221592Srgrimes        Path a1 = Paths.get("root/pkg1/ClassA1.java");
1231592Srgrimes        Path a2 = Paths.get("root/pkg1/ClassA2.java");
1241592Srgrimes        Path b1 = Paths.get("root/pkg1/pkg2/ClassB1.java");
1251592Srgrimes        Path b2 = Paths.get("root/pkg1/pkg2/ClassB2.java");
1261592Srgrimes        Path c1 = Paths.get("root/pkg3/ClassC1.java");
1271592Srgrimes        Path c2 = Paths.get("root/pkg3/ClassC2.java");
1281592Srgrimes
1291592Srgrimes        for (Path p : Arrays.asList(a1, a2, b1, b2, c1, c2)) {
1301592Srgrimes            Files.createDirectories(p.getParent());
1311592Srgrimes            Files.createFile(p);
1321592Srgrimes        }
1331592Srgrimes
1341592Srgrimes        // Test -if
1351592Srgrimes        {
1361592Srgrimes            Options options = Options.parseArgs("-if", "root/pkg1/ClassA1.java", "root");
1371592Srgrimes
1381592Srgrimes            Map<String, Source> foundFiles = new HashMap<>();
1391592Srgrimes            SjavacImpl.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles,
1401592Srgrimes                    new HashMap<String, Module>(), new Module("", ""), false, true);
1411592Srgrimes
1421592Srgrimes            checkFilesFound(foundFiles.keySet(), a1);
1431592Srgrimes        }
1441592Srgrimes
1451592Srgrimes        // Test -i
1461592Srgrimes        System.out.println("--------------------------- CHECKING -i ----------------");
1471592Srgrimes        {
1481592Srgrimes            Options options = Options.parseArgs("-i", "pkg1/*", "root");
1491592Srgrimes
1501592Srgrimes            Map<String, Source> foundFiles = new HashMap<>();
1511592Srgrimes            SjavacImpl.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles,
1521592Srgrimes                    new HashMap<String, Module>(), new Module("", ""), false, true);
1531592Srgrimes
1541592Srgrimes            checkFilesFound(foundFiles.keySet(), a1, a2, b1, b2);
1551592Srgrimes        }
1561592Srgrimes        System.out.println("--------------------------------------------------------");
1571592Srgrimes
1581592Srgrimes        // Test -xf
1591592Srgrimes        {
1601592Srgrimes            Options options = Options.parseArgs("-xf", "root/pkg1/ClassA1.java", "root");
1611592Srgrimes
1621592Srgrimes            Map<String, Source> foundFiles = new HashMap<>();
1631592Srgrimes            SjavacImpl.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles,
1641592Srgrimes                    new HashMap<String, Module>(), new Module("", ""), false, true);
1651592Srgrimes
1661592Srgrimes            checkFilesFound(foundFiles.keySet(), a2, b1, b2, c1, c2);
1671592Srgrimes        }
1681592Srgrimes
1691592Srgrimes        // Test -x
1701592Srgrimes        {
1711592Srgrimes            Options options = Options.parseArgs("-i", "pkg1/*", "root");
1721592Srgrimes
1731592Srgrimes            Map<String, Source> foundFiles = new HashMap<>();
1741592Srgrimes            SjavacImpl.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles,
1751592Srgrimes                    new HashMap<String, Module>(), new Module("", ""), false, true);
1761592Srgrimes
1771592Srgrimes            checkFilesFound(foundFiles.keySet(), a1, a2, b1, b2);
1781592Srgrimes        }
1791592Srgrimes
1801592Srgrimes        // Test -x and -i
1811592Srgrimes        {
1821592Srgrimes            Options options = Options.parseArgs("-i", "pkg1/*", "-x", "pkg1/pkg2/*", "root");
1831592Srgrimes
1841592Srgrimes            Map<String, Source> foundFiles = new HashMap<>();
1851592Srgrimes            SjavacImpl.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles,
1861592Srgrimes                    new HashMap<String, Module>(), new Module("", ""), false, true);
1871592Srgrimes
1881592Srgrimes            checkFilesFound(foundFiles.keySet(), a1, a2);
1891592Srgrimes        }
1901592Srgrimes
1911592Srgrimes    }
1921592Srgrimes
1931592Srgrimes    // Test basic options
1941592Srgrimes    static void testSimpleOptions() {
1951592Srgrimes
1961592Srgrimes        Options options = Options.parseArgs("-j", "17", "--log=debug");
1971592Srgrimes        assertEquals(17, options.getNumCores());
1981592Srgrimes        assertEquals("debug", options.getLogLevel());
1991592Srgrimes        assertEquals(false, options.isDefaultPackagePermitted());
2001592Srgrimes        assertEquals(false, options.areUnidentifiedArtifactsPermitted());
2011592Srgrimes        assertEquals(false, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath()));
2021592Srgrimes
2031592Srgrimes        options = Options.parseArgs("--permit-unidentified-artifacts",
2041592Srgrimes                                    "--permit-artifact=bar.txt",
2051592Srgrimes                                    "--permit-sources-without-package");
2061592Srgrimes        assertEquals("info", options.getLogLevel());
2071592Srgrimes        assertEquals(true, options.isDefaultPackagePermitted());
2081592Srgrimes        assertEquals(true, options.areUnidentifiedArtifactsPermitted());
2091592Srgrimes        assertEquals(true, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath()));
2101592Srgrimes    }
2111592Srgrimes
2121592Srgrimes    // Test server configuration options
2131592Srgrimes    static void testServerConf() {
2141592Srgrimes        Options options = Options.parseArgs("--server:someServerConfiguration");
2151592Srgrimes        assertEquals("someServerConfiguration", options.getServerConf());
2161592Srgrimes        assertEquals(false, options.startServerFlag());
2171592Srgrimes
2181592Srgrimes        options = Options.parseArgs("--startserver:someServerConfiguration");
2191592Srgrimes        assertEquals("someServerConfiguration", options.getServerConf());
2201592Srgrimes        assertEquals(true, options.startServerFlag());
2211592Srgrimes    }
2221592Srgrimes
2231592Srgrimes    // Test input paths
2241592Srgrimes    static void testSearchPaths() {
2251592Srgrimes        List<String> i, x, iF, xF;
2261592Srgrimes        i = x = iF = xF = new ArrayList<>();
2271592Srgrimes
2281592Srgrimes        SourceLocation dir1 = new SourceLocation(Paths.get("dir1"), i, x, iF, xF);
2291592Srgrimes        SourceLocation dir2 = new SourceLocation(Paths.get("dir2"), i, x, iF, xF);
2301592Srgrimes        String dir1_PS_dir2 = "dir1" + File.pathSeparator + "dir2";
2311592Srgrimes
2321592Srgrimes        Options options = Options.parseArgs("-sourcepath", dir1_PS_dir2);
2331592Srgrimes        assertEquals(options.getSourceSearchPaths(), Arrays.asList(dir1, dir2));
2341592Srgrimes
2351592Srgrimes        options = Options.parseArgs("-modulepath", dir1_PS_dir2);
2361592Srgrimes        assertEquals(options.getModuleSearchPaths(), Arrays.asList(dir1, dir2));
2371592Srgrimes
2381592Srgrimes        options = Options.parseArgs("-classpath", dir1_PS_dir2);
2391592Srgrimes        assertEquals(options.getClassSearchPath(), Arrays.asList(dir1, dir2));
2401592Srgrimes    }
2411592Srgrimes
2421592Srgrimes    // Test -tr option
2431592Srgrimes    static void testTranslationRules() {
2441592Srgrimes
2451592Srgrimes        Class<?> cls = com.sun.tools.sjavac.CompileJavaPackages.class;
2461592Srgrimes
2471592Srgrimes        Options options = Options.parseArgs(
2481592Srgrimes                "-tr", ".exa=" + cls.getName(),
2491592Srgrimes                "-tr", ".exb=" + cls.getName(),
2501592Srgrimes                "-copy", ".html");
2511592Srgrimes
2521592Srgrimes        assertEquals(cls, options.getTranslationRules().get(".exa").getClass());
2531592Srgrimes        assertEquals(cls, options.getTranslationRules().get(".exb").getClass());
2541592Srgrimes        assertEquals(CopyFile.class, options.getTranslationRules().get(".html").getClass());
2551592Srgrimes
2561592Srgrimes    }
2571592Srgrimes}
2581592Srgrimes