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 29 * @summary Tests the preparation of javac-arguments. 30 * 31 * @build Wrapper 32 * @run main Wrapper JavacOptionPrep 33 */ 34 35 import java.io.File; 36 import java.io.IOException; 37 import java.nio.file.Files; 38 import java.nio.file.Paths; 39 import java.util.Arrays; 40 import java.util.Iterator; 41 42 import com.sun.tools.sjavac.options.Options; 43 44 45 public class JavacOptionPrep { 46 47 enum TestPath { 48 CP1, CP2, SRC1, SRC2, SOURCEPATH1, SOURCEPATH2; 49 50 public String toString() { 51 return name().toLowerCase(); 52 } 53 } 54 55 private final static String SEP = File.pathSeparator; 56 57 public static void main(String[] unused) throws IOException { 58 59 for (TestPath p : TestPath.values()) 60 Files.createDirectory(Paths.get(p.toString())); 61 62 // Test some various cases: 63 // - Paths combined with File.pathSeparator (CP1 / CP2) 64 // - Paths given as duplicate options (SOURCEPATH1 / SOURCEPATH2) 65 // - Sources provided by -src (SRC1) 66 // - Sources provided without preceding option (SRC2) 67 // - An unrecognized option which is to be passed on to javac 68 String sjavacArgs = "-cp " + TestPath.CP1 + SEP + TestPath.CP2 + 69 " -d dest " + 70 " -h header" + 71 " -sourcepath " + TestPath.SOURCEPATH1 + 72 " -src " + TestPath.SRC1 + 73 " -s gensrc" + 74 " -sourcepath " + TestPath.SOURCEPATH2 + 75 " " + TestPath.SRC2 + 76 " -unrecognized"; 77 78 Options options = Options.parseArgs(sjavacArgs.split(" ")); 79 80 // Extract javac-options 81 String[] javacArgs = options.prepJavacArgs(); 82 83 // Check the result 84 boolean destDirFound = false; 85 boolean userPathsFirst = false; 86 boolean headerDirFound = false; 87 boolean gensrcDirFound = false; 88 boolean classPathFound = false; 89 boolean sourcePathFound = false; 90 boolean unrecognizedFound = false; 91 boolean implicitNoneFound = false; 92 93 Iterator<String> javacArgIter = Arrays.asList(javacArgs).iterator(); 94 while (javacArgIter.hasNext()) { 95 96 String option = javacArgIter.next(); 97 98 switch (option) { 99 case "-classpath": 100 case "-cp": 101 classPathFound = true; 102 assertEquals(TestPath.CP1 + SEP + TestPath.CP2, 103 javacArgIter.next()); 104 break; 105 106 case "-d": 107 destDirFound = true; 108 assertEquals(Paths.get("dest").toAbsolutePath().toString(), 109 javacArgIter.next()); 110 break; 111 112 case "-h": 113 headerDirFound = true; 114 assertEquals(Paths.get("header").toAbsolutePath().toString(), 115 javacArgIter.next()); 116 break; 117 118 case "-s": 119 gensrcDirFound = true; 120 assertEquals(Paths.get("gensrc").toAbsolutePath().toString(), 121 javacArgIter.next()); 122 break; 123 124 case "-sourcepath": 125 sourcePathFound = true; 126 assertEquals(TestPath.SRC1 + SEP + 127 TestPath.SRC2 + SEP + 128 TestPath.SOURCEPATH1 + SEP + 129 TestPath.SOURCEPATH2, 130 javacArgIter.next()); 131 break; 132 133 case "-unrecognized": 134 unrecognizedFound = true; 135 break; 136 137 case "-implicit:none": 138 implicitNoneFound = true; 139 break; 140 141 // Note that *which* files to actually compile is not dealt 142 // with by prepJavacArgs. 143 144 default: 145 throw new AssertionError("Unexpected option found: " + option); 146 } 147 } 148 149 if (!destDirFound) 150 throw new AssertionError("Dest directory not found."); 151 152 if (!headerDirFound) 153 throw new AssertionError("Header directory not found."); 154 155 if (!gensrcDirFound) 156 throw new AssertionError("Generated source directory not found."); 157 158 if (!classPathFound) 159 throw new AssertionError("Class path not found."); 160 161 if (!sourcePathFound) 162 throw new AssertionError("Source path not found."); 163 164 if (!unrecognizedFound) 165 throw new AssertionError("\"-unrecognized\" not found."); 166 167 if (!implicitNoneFound) 168 throw new AssertionError("\"-implicit:none\" not found."); 169 } 170 171 static void assertEquals(Object expected, Object actual) { 172 if (!expected.equals(actual)) 173 throw new AssertionError("Expected " + expected + " but got " + actual); 174 } 175 }