Path: blob/master/test/langtools/tools/sjavac/OptionDecoding.java
41144 views
/*1* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8035063 805446526* @summary Tests decoding of String[] into Options.27*28* @modules jdk.compiler/com.sun.tools.sjavac29* jdk.compiler/com.sun.tools.sjavac.client30* jdk.compiler/com.sun.tools.sjavac.comp31* jdk.compiler/com.sun.tools.sjavac.options32* @build Wrapper33* @run main Wrapper OptionDecoding34*/3536import static util.OptionTestUtil.assertEquals;37import static util.OptionTestUtil.checkFilesFound;3839import 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;5051import 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;5859public class OptionDecoding {6061public static void main(String[] args) throws IOException {62testPaths();63testDupPaths();64testSimpleOptions();65testServerConf();66testSearchPaths();67testTranslationRules();68}6970// Test decoding of output paths71static void testPaths() throws IOException {72final String H = "headers";73final String G = "gensrc";74final String D = "dest";75final String stateDir = "stateDir";76final String CMP = "srcRefList.txt";7778Options options = Options.parseArgs("-h", H, "-s", G, "-d", D, "--state-dir=" + stateDir,79"--compare-found-sources", CMP);8081assertEquals(Paths.get(H).toAbsolutePath(), options.getHeaderDir());82assertEquals(Paths.get(G).toAbsolutePath(), options.getGenSrcDir());83assertEquals(Paths.get(D).toAbsolutePath(), options.getDestDir());84assertEquals(Paths.get(stateDir).toAbsolutePath(), options.getStateDir());85assertEquals(Paths.get(CMP), options.getSourceReferenceList());86}8788// Providing duplicate header / dest / gensrc paths should produce an error.89static void testDupPaths() throws IOException {90try {91Options.parseArgs("-h", "dir1", "-h", "dir2");92throw new RuntimeException("Duplicate header directories should fail.");93} catch (IllegalArgumentException iae) {94// Expected95}9697try {98Options.parseArgs("-s", "dir1", "-s", "dir2");99throw new RuntimeException("Duplicate paths for generated sources should fail.");100} catch (IllegalArgumentException iae) {101// Expected102}103104try {105Options.parseArgs("-d", "dir1", "-d", "dir2");106throw new RuntimeException("Duplicate destination directories should fail.");107} catch (IllegalArgumentException iae) {108// Expected109}110}111112// Test basic options113static void testSimpleOptions() {114Options options = Options.parseArgs("-j", "17", "--log=debug");115assertEquals(17, options.getNumCores());116assertEquals("debug", options.getLogLevel());117assertEquals(false, options.isDefaultPackagePermitted());118assertEquals(false, options.areUnidentifiedArtifactsPermitted());119assertEquals(false, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath()));120121options = Options.parseArgs("--permit-unidentified-artifacts",122"--permit-artifact=bar.txt",123"--permit-sources-without-package");124assertEquals("info", options.getLogLevel());125assertEquals(true, options.isDefaultPackagePermitted());126assertEquals(true, options.areUnidentifiedArtifactsPermitted());127assertEquals(true, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath()));128}129130// Test server configuration options131static void testServerConf() {132Options options = Options.parseArgs("--server:someServerConfiguration");133assertEquals("someServerConfiguration", options.getServerConf());134assertEquals(false, options.startServerFlag());135136options = Options.parseArgs("--startserver:someServerConfiguration");137assertEquals("someServerConfiguration", options.getServerConf());138assertEquals(true, options.startServerFlag());139}140141// Test input paths142static void testSearchPaths() {143List<String> i, x, iF, xF;144i = x = iF = xF = new ArrayList<>();145146SourceLocation dir1 = new SourceLocation(Paths.get("dir1"), i, x);147SourceLocation dir2 = new SourceLocation(Paths.get("dir2"), i, x);148String dir1_PS_dir2 = "dir1" + File.pathSeparator + "dir2";149150Options options = Options.parseArgs("--source-path", dir1_PS_dir2);151assertEquals(options.getSourceSearchPaths(), Arrays.asList(dir1, dir2));152153options = Options.parseArgs("--module-path", dir1_PS_dir2);154assertEquals(options.getModuleSearchPaths(), Arrays.asList(dir1, dir2));155156options = Options.parseArgs("--class-path", dir1_PS_dir2);157assertEquals(options.getClassSearchPath(), Arrays.asList(dir1, dir2));158}159160// Test -tr option161static void testTranslationRules() {162Class<?> cls = com.sun.tools.sjavac.CompileJavaPackages.class;163164Options options = Options.parseArgs(165"-tr", ".exa=" + cls.getName(),166"-tr", ".exb=" + cls.getName(),167"-copy", ".html");168169assertEquals(cls, options.getTranslationRules().get(".exa").getClass());170assertEquals(cls, options.getTranslationRules().get(".exb").getClass());171assertEquals(CopyFile.class, options.getTranslationRules().get(".html").getClass());172}173}174175176