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 
  36 import static util.OptionTestUtil.assertEquals;
  37 import static util.OptionTestUtil.checkFilesFound;
  38 
  39 import java.io.File;
  40 import java.io.IOException;
  41 import java.nio.file.Files;
  42 import java.nio.file.Path;
  43 import java.nio.file.Paths;
  44 import java.util.ArrayList;
  45 import java.util.Arrays;
  46 import java.util.Collections;
  47 import java.util.HashMap;
  48 import java.util.List;
  49 import java.util.Map;
  50 
  51 import com.sun.tools.sjavac.CopyFile;
  52 import com.sun.tools.sjavac.Module;
  53 import com.sun.tools.sjavac.Source;
  54 import com.sun.tools.sjavac.client.ClientMain;
  55 import com.sun.tools.sjavac.comp.SjavacImpl;
  56 import com.sun.tools.sjavac.options.Options;
  57 import com.sun.tools.sjavac.options.SourceLocation;
  58 
  59 public class OptionDecoding {
  60 
  61     public static void main(String[] args) throws IOException {
  62         testPaths();
  63         testDupPaths();
  64         testSimpleOptions();
  65         testServerConf();
  66         testSearchPaths();
  67         testTranslationRules();
  68     }
  69 
  70     // Test decoding of output paths
  71     static void testPaths() throws IOException {
  72         final String H = "headers";
  73         final String G = "gensrc";
  74         final String D = "dest";
  75         final String stateDir = "stateDir";
  76         final String CMP = "srcRefList.txt";
  77 
  78         Options options = Options.parseArgs("-h", H, "-s", G, "-d", D, "--state-dir=" + stateDir,
  79                                             "--compare-found-sources", CMP);
  80 
  81         assertEquals(Paths.get(H).toAbsolutePath(), options.getHeaderDir());
  82         assertEquals(Paths.get(G).toAbsolutePath(), options.getGenSrcDir());
  83         assertEquals(Paths.get(D).toAbsolutePath(), options.getDestDir());
  84         assertEquals(Paths.get(stateDir).toAbsolutePath(), options.getStateDir());
  85         assertEquals(Paths.get(CMP), options.getSourceReferenceList());
  86     }
  87 
  88     // Providing duplicate header / dest / gensrc paths should produce an error.
  89     static void testDupPaths() throws IOException {
  90         try {
  91             Options.parseArgs("-h", "dir1", "-h", "dir2");
  92             throw new RuntimeException("Duplicate header directories should fail.");
  93         } catch (IllegalArgumentException iae) {
  94             // Expected
  95         }
  96 
  97         try {
  98             Options.parseArgs("-s", "dir1", "-s", "dir2");
  99             throw new RuntimeException("Duplicate paths for generated sources should fail.");
 100         } catch (IllegalArgumentException iae) {
 101             // Expected
 102         }
 103 
 104         try {
 105             Options.parseArgs("-d", "dir1", "-d", "dir2");
 106             throw new RuntimeException("Duplicate destination directories should fail.");
 107         } catch (IllegalArgumentException iae) {
 108             // Expected
 109         }
 110     }
 111 
 112     // Test basic options
 113     static void testSimpleOptions() {
 114         Options options = Options.parseArgs("-j", "17", "--log=debug");
 115         assertEquals(17, options.getNumCores());
 116         assertEquals("debug", options.getLogLevel());
 117         assertEquals(false, options.isDefaultPackagePermitted());
 118         assertEquals(false, options.areUnidentifiedArtifactsPermitted());
 119         assertEquals(false, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath()));
 120 
 121         options = Options.parseArgs("--permit-unidentified-artifacts",
 122                                     "--permit-artifact=bar.txt",
 123                                     "--permit-sources-without-package");
 124         assertEquals("info", options.getLogLevel());
 125         assertEquals(true, options.isDefaultPackagePermitted());
 126         assertEquals(true, options.areUnidentifiedArtifactsPermitted());
 127         assertEquals(true, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath()));
 128     }
 129 
 130     // Test server configuration options
 131     static void testServerConf() {
 132         Options options = Options.parseArgs("--server:someServerConfiguration");
 133         assertEquals("someServerConfiguration", options.getServerConf());
 134         assertEquals(false, options.startServerFlag());
 135 
 136         options = Options.parseArgs("--startserver:someServerConfiguration");
 137         assertEquals("someServerConfiguration", options.getServerConf());
 138         assertEquals(true, options.startServerFlag());
 139     }
 140 
 141     // Test input paths
 142     static void testSearchPaths() {
 143         List<String> i, x, iF, xF;
 144         i = x = iF = xF = new ArrayList<>();
 145 
 146         SourceLocation dir1 = new SourceLocation(Paths.get("dir1"), i, x);
 147         SourceLocation dir2 = new SourceLocation(Paths.get("dir2"), i, x);
 148         String dir1_PS_dir2 = "dir1" + File.pathSeparator + "dir2";
 149 
 150         Options options = Options.parseArgs("-sourcepath", dir1_PS_dir2);
 151         assertEquals(options.getSourceSearchPaths(), Arrays.asList(dir1, dir2));
 152 
 153         options = Options.parseArgs("-modulepath", dir1_PS_dir2);
 154         assertEquals(options.getModuleSearchPaths(), Arrays.asList(dir1, dir2));
 155 
 156         options = Options.parseArgs("-classpath", dir1_PS_dir2);
 157         assertEquals(options.getClassSearchPath(), Arrays.asList(dir1, dir2));
 158     }
 159 
 160     // Test -tr option
 161     static void testTranslationRules() {
 162         Class<?> cls = com.sun.tools.sjavac.CompileJavaPackages.class;
 163 
 164         Options options = Options.parseArgs(
 165                 "-tr", ".exa=" + cls.getName(),
 166                 "-tr", ".exb=" + cls.getName(),
 167                 "-copy", ".html");
 168 
 169         assertEquals(cls, options.getTranslationRules().get(".exa").getClass());
 170         assertEquals(cls, options.getTranslationRules().get(".exb").getClass());
 171         assertEquals(CopyFile.class, options.getTranslationRules().get(".html").getClass());
 172     }
 173 }