OptionDecoding.java revision 2721:f7ce2cfa4cdb
1/* 2 * Copyright (c) 2014, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26/* 27 * @test 28 * @bug 8035063 8054465 29 * @summary Tests decoding of String[] into Options. 30 * 31 * @build Wrapper 32 * @run main Wrapper OptionDecoding 33 */ 34 35import static util.OptionTestUtil.assertEquals; 36import static util.OptionTestUtil.checkFilesFound; 37 38import java.io.File; 39import java.io.IOException; 40import java.nio.file.Files; 41import java.nio.file.Path; 42import java.nio.file.Paths; 43import java.util.ArrayList; 44import java.util.Arrays; 45import java.util.Collections; 46import java.util.HashMap; 47import java.util.List; 48import java.util.Map; 49 50import com.sun.tools.sjavac.CopyFile; 51import com.sun.tools.sjavac.Main; 52import com.sun.tools.sjavac.Module; 53import com.sun.tools.sjavac.Source; 54import com.sun.tools.sjavac.client.ClientMain; 55import com.sun.tools.sjavac.options.Options; 56import com.sun.tools.sjavac.options.SourceLocation; 57 58public class OptionDecoding { 59 60 public static void main(String[] args) throws IOException { 61 62 testPaths(); 63 testDupPaths(); 64 testSourceLocations(); 65 testSimpleOptions(); 66 testServerConf(); 67 testSearchPaths(); 68 testTranslationRules(); 69 70 } 71 72 // Test decoding of output paths 73 static void testPaths() throws IOException { 74 75 final String H = "headers"; 76 final String G = "gensrc"; 77 final String D = "dest"; 78 final String CMP = "srcRefList.txt"; 79 80 Options options = Options.parseArgs("-h", H, "-s", G, "-d", D, 81 "--compare-found-sources", CMP); 82 83 assertEquals(Paths.get(H).toAbsolutePath(), options.getHeaderDir()); 84 assertEquals(Paths.get(G).toAbsolutePath(), options.getGenSrcDir()); 85 assertEquals(Paths.get(D).toAbsolutePath(), options.getDestDir()); 86 assertEquals(Paths.get(CMP), options.getSourceReferenceList()); 87 88 } 89 90 // Providing duplicate header / dest / gensrc paths should produce an error. 91 static void testDupPaths() throws IOException { 92 93 try { 94 Options.parseArgs("-h", "dir1", "-h", "dir2"); 95 throw new RuntimeException("Duplicate header directories should fail."); 96 } catch (IllegalArgumentException iae) { 97 // Expected 98 } 99 100 try { 101 Options.parseArgs("-s", "dir1", "-s", "dir2"); 102 throw new RuntimeException("Duplicate paths for generated sources should fail."); 103 } catch (IllegalArgumentException iae) { 104 // Expected 105 } 106 107 try { 108 Options.parseArgs("-d", "dir1", "-d", "dir2"); 109 throw new RuntimeException("Duplicate destination directories should fail."); 110 } catch (IllegalArgumentException iae) { 111 // Expected 112 } 113 114 } 115 116 // Test source locations and -x, -i, -xf, -if filters 117 static void testSourceLocations() throws IOException { 118 119 Path a1 = Paths.get("root/pkg1/ClassA1.java"); 120 Path a2 = Paths.get("root/pkg1/ClassA2.java"); 121 Path b1 = Paths.get("root/pkg1/pkg2/ClassB1.java"); 122 Path b2 = Paths.get("root/pkg1/pkg2/ClassB2.java"); 123 Path c1 = Paths.get("root/pkg3/ClassC1.java"); 124 Path c2 = Paths.get("root/pkg3/ClassC2.java"); 125 126 for (Path p : Arrays.asList(a1, a2, b1, b2, c1, c2)) { 127 Files.createDirectories(p.getParent()); 128 Files.createFile(p); 129 } 130 131 // Test -if 132 { 133 Options options = Options.parseArgs("-if", "root/pkg1/ClassA1.java", "root"); 134 135 Map<String, Source> foundFiles = new HashMap<>(); 136 ClientMain.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles, 137 new HashMap<String, Module>(), new Module("", ""), false, true); 138 139 checkFilesFound(foundFiles.keySet(), a1); 140 } 141 142 // Test -i 143 System.out.println("--------------------------- CHECKING -i ----------------"); 144 { 145 Options options = Options.parseArgs("-i", "pkg1/*", "root"); 146 147 Map<String, Source> foundFiles = new HashMap<>(); 148 ClientMain.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles, 149 new HashMap<String, Module>(), new Module("", ""), false, true); 150 151 checkFilesFound(foundFiles.keySet(), a1, a2, b1, b2); 152 } 153 System.out.println("--------------------------------------------------------"); 154 155 // Test -xf 156 { 157 Options options = Options.parseArgs("-xf", "root/pkg1/ClassA1.java", "root"); 158 159 Map<String, Source> foundFiles = new HashMap<>(); 160 ClientMain.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles, 161 new HashMap<String, Module>(), new Module("", ""), false, true); 162 163 checkFilesFound(foundFiles.keySet(), a2, b1, b2, c1, c2); 164 } 165 166 // Test -x 167 { 168 Options options = Options.parseArgs("-i", "pkg1/*", "root"); 169 170 Map<String, Source> foundFiles = new HashMap<>(); 171 ClientMain.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles, 172 new HashMap<String, Module>(), new Module("", ""), false, true); 173 174 checkFilesFound(foundFiles.keySet(), a1, a2, b1, b2); 175 } 176 177 // Test -x and -i 178 { 179 Options options = Options.parseArgs("-i", "pkg1/*", "-x", "pkg1/pkg2/*", "root"); 180 181 Map<String, Source> foundFiles = new HashMap<>(); 182 ClientMain.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles, 183 new HashMap<String, Module>(), new Module("", ""), false, true); 184 185 checkFilesFound(foundFiles.keySet(), a1, a2); 186 } 187 188 } 189 190 // Test basic options 191 static void testSimpleOptions() { 192 193 Options options = Options.parseArgs("-j", "17", "--log=debug"); 194 assertEquals(17, options.getNumCores()); 195 assertEquals("debug", options.getLogLevel()); 196 assertEquals(false, options.isDefaultPackagePermitted()); 197 assertEquals(false, options.areUnidentifiedArtifactsPermitted()); 198 assertEquals(false, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath())); 199 200 options = Options.parseArgs("--permit-unidentified-artifacts", 201 "--permit-artifact=bar.txt", 202 "--permit-sources-without-package"); 203 assertEquals("info", options.getLogLevel()); 204 assertEquals(true, options.isDefaultPackagePermitted()); 205 assertEquals(true, options.areUnidentifiedArtifactsPermitted()); 206 assertEquals(true, options.isUnidentifiedArtifactPermitted(Paths.get("bar.txt").toFile().getAbsolutePath())); 207 } 208 209 // Test server configuration options 210 static void testServerConf() { 211 Options options = Options.parseArgs("--server:someServerConfiguration"); 212 assertEquals("someServerConfiguration", options.getServerConf()); 213 assertEquals(false, options.startServerFlag()); 214 215 options = Options.parseArgs("--startserver:someServerConfiguration"); 216 assertEquals("someServerConfiguration", options.getServerConf()); 217 assertEquals(true, options.startServerFlag()); 218 } 219 220 // Test input paths 221 static void testSearchPaths() { 222 List<String> i, x, iF, xF; 223 i = x = iF = xF = new ArrayList<>(); 224 225 SourceLocation dir1 = new SourceLocation(Paths.get("dir1"), i, x, iF, xF); 226 SourceLocation dir2 = new SourceLocation(Paths.get("dir2"), i, x, iF, xF); 227 String dir1_PS_dir2 = "dir1" + File.pathSeparator + "dir2"; 228 229 Options options = Options.parseArgs("-sourcepath", dir1_PS_dir2); 230 assertEquals(options.getSourceSearchPaths(), Arrays.asList(dir1, dir2)); 231 232 options = Options.parseArgs("-modulepath", dir1_PS_dir2); 233 assertEquals(options.getModuleSearchPaths(), Arrays.asList(dir1, dir2)); 234 235 options = Options.parseArgs("-classpath", dir1_PS_dir2); 236 assertEquals(options.getClassSearchPath(), Arrays.asList(dir1, dir2)); 237 } 238 239 // Test -tr option 240 static void testTranslationRules() { 241 242 Class<?> cls = com.sun.tools.sjavac.CompileJavaPackages.class; 243 244 Options options = Options.parseArgs( 245 "-tr", ".exa=" + cls.getName(), 246 "-tr", ".exb=" + cls.getName(), 247 "-copy", ".html"); 248 249 assertEquals(cls, options.getTranslationRules().get(".exa").getClass()); 250 assertEquals(cls, options.getTranslationRules().get(".exb").getClass()); 251 assertEquals(CopyFile.class, options.getTranslationRules().get(".html").getClass()); 252 253 } 254} 255