|
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 import static util.OptionTestUtil.assertEquals; |
|
27 import static util.OptionTestUtil.checkFilesFound; |
|
28 |
|
29 import java.io.IOException; |
|
30 import java.nio.file.Files; |
|
31 import java.nio.file.Path; |
|
32 import java.nio.file.Paths; |
|
33 import java.util.ArrayList; |
|
34 import java.util.Arrays; |
|
35 import java.util.Collections; |
|
36 import java.util.HashMap; |
|
37 import java.util.List; |
|
38 import java.util.Map; |
|
39 |
|
40 import com.sun.tools.sjavac.CopyFile; |
|
41 import com.sun.tools.sjavac.Main; |
|
42 import com.sun.tools.sjavac.Module; |
|
43 import com.sun.tools.sjavac.Source; |
|
44 import com.sun.tools.sjavac.options.Options; |
|
45 import com.sun.tools.sjavac.options.SourceLocation; |
|
46 |
|
47 public class OptionDecoding { |
|
48 |
|
49 public static void main(String[] args) throws IOException { |
|
50 |
|
51 testPaths(); |
|
52 testDupPaths(); |
|
53 testSourceLocations(); |
|
54 testSimpleOptions(); |
|
55 testServerConf(); |
|
56 testSearchPaths(); |
|
57 testTranslationRules(); |
|
58 |
|
59 } |
|
60 |
|
61 // Test decoding of output paths |
|
62 static void testPaths() throws IOException { |
|
63 |
|
64 final String H = "headers"; |
|
65 final String G = "gensrc"; |
|
66 final String D = "dest"; |
|
67 final String CMP = "srcRefList.txt"; |
|
68 |
|
69 Options options = Options.parseArgs("-h", H, "-s", G, "-d", D, |
|
70 "--compare-found-sources", CMP); |
|
71 |
|
72 assertEquals(Paths.get(H).toAbsolutePath(), options.getHeaderDir()); |
|
73 assertEquals(Paths.get(G).toAbsolutePath(), options.getGenSrcDir()); |
|
74 assertEquals(Paths.get(D).toAbsolutePath(), options.getDestDir()); |
|
75 assertEquals(Paths.get(CMP), options.getSourceReferenceList()); |
|
76 |
|
77 } |
|
78 |
|
79 // Providing duplicate header / dest / gensrc paths should produce an error. |
|
80 static void testDupPaths() throws IOException { |
|
81 |
|
82 try { |
|
83 Options.parseArgs("-h", "dir1", "-h", "dir2"); |
|
84 throw new RuntimeException("Duplicate header directories should fail."); |
|
85 } catch (IllegalArgumentException iae) { |
|
86 // Expected |
|
87 } |
|
88 |
|
89 try { |
|
90 Options.parseArgs("-s", "dir1", "-s", "dir2"); |
|
91 throw new RuntimeException("Duplicate paths for generated sources should fail."); |
|
92 } catch (IllegalArgumentException iae) { |
|
93 // Expected |
|
94 } |
|
95 |
|
96 try { |
|
97 Options.parseArgs("-d", "dir1", "-d", "dir2"); |
|
98 throw new RuntimeException("Duplicate destination directories should fail."); |
|
99 } catch (IllegalArgumentException iae) { |
|
100 // Expected |
|
101 } |
|
102 |
|
103 } |
|
104 |
|
105 // Test source locations and -x, -i, -xf, -if filters |
|
106 static void testSourceLocations() throws IOException { |
|
107 |
|
108 Path a1 = Paths.get("root/pkg1/ClassA1.java"); |
|
109 Path a2 = Paths.get("root/pkg1/ClassA2.java"); |
|
110 Path b1 = Paths.get("root/pkg1/pkg2/ClassB1.java"); |
|
111 Path b2 = Paths.get("root/pkg1/pkg2/ClassB2.java"); |
|
112 Path c1 = Paths.get("root/pkg3/ClassC1.java"); |
|
113 Path c2 = Paths.get("root/pkg3/ClassC2.java"); |
|
114 |
|
115 for (Path p : Arrays.asList(a1, a2, b1, b2, c1, c2)) { |
|
116 Files.createDirectories(p.getParent()); |
|
117 Files.createFile(p); |
|
118 } |
|
119 |
|
120 // Test -if |
|
121 { |
|
122 Options options = Options.parseArgs("-if", "root/pkg1/ClassA1.java", "root"); |
|
123 |
|
124 Map<String, Source> foundFiles = new HashMap<>(); |
|
125 Main.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles, |
|
126 new HashMap<String, Module>(), new Module("", ""), false, true); |
|
127 |
|
128 checkFilesFound(foundFiles.keySet(), a1); |
|
129 } |
|
130 |
|
131 // Test -i |
|
132 System.out.println("--------------------------- CHECKING -i ----------------"); |
|
133 { |
|
134 Options options = Options.parseArgs("-i", "pkg1/*", "root"); |
|
135 |
|
136 Map<String, Source> foundFiles = new HashMap<>(); |
|
137 Main.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles, |
|
138 new HashMap<String, Module>(), new Module("", ""), false, true); |
|
139 |
|
140 checkFilesFound(foundFiles.keySet(), a1, a2, b1, b2); |
|
141 } |
|
142 System.out.println("--------------------------------------------------------"); |
|
143 |
|
144 // Test -xf |
|
145 { |
|
146 Options options = Options.parseArgs("-xf", "root/pkg1/ClassA1.java", "root"); |
|
147 |
|
148 Map<String, Source> foundFiles = new HashMap<>(); |
|
149 Main.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles, |
|
150 new HashMap<String, Module>(), new Module("", ""), false, true); |
|
151 |
|
152 checkFilesFound(foundFiles.keySet(), a2, b1, b2, c1, c2); |
|
153 } |
|
154 |
|
155 // Test -x |
|
156 { |
|
157 Options options = Options.parseArgs("-i", "pkg1/*", "root"); |
|
158 |
|
159 Map<String, Source> foundFiles = new HashMap<>(); |
|
160 Main.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles, |
|
161 new HashMap<String, Module>(), new Module("", ""), false, true); |
|
162 |
|
163 checkFilesFound(foundFiles.keySet(), a1, a2, b1, b2); |
|
164 } |
|
165 |
|
166 // Test -x and -i |
|
167 { |
|
168 Options options = Options.parseArgs("-i", "pkg1/*", "-x", "pkg1/pkg2/*", "root"); |
|
169 |
|
170 Map<String, Source> foundFiles = new HashMap<>(); |
|
171 Main.findSourceFiles(options.getSources(), Collections.singleton(".java"), foundFiles, |
|
172 new HashMap<String, Module>(), new Module("", ""), false, true); |
|
173 |
|
174 checkFilesFound(foundFiles.keySet(), a1, a2); |
|
175 } |
|
176 |
|
177 } |
|
178 |
|
179 // Test basic options |
|
180 static void testSimpleOptions() { |
|
181 |
|
182 Options options = Options.parseArgs("-j", "17", "--log=debug"); |
|
183 assertEquals(17, options.getNumCores()); |
|
184 assertEquals("debug", options.getLogLevel()); |
|
185 assertEquals(false, options.isDefaultPackagePermitted()); |
|
186 assertEquals(false, options.isUnidentifiedArtifactPermitted()); |
|
187 |
|
188 options = Options.parseArgs("--permit-unidentified-artifacts", |
|
189 "--permit-sources-without-package"); |
|
190 assertEquals("info", options.getLogLevel()); |
|
191 assertEquals(true, options.isDefaultPackagePermitted()); |
|
192 assertEquals(true, options.isUnidentifiedArtifactPermitted()); |
|
193 } |
|
194 |
|
195 // Test server configuration options |
|
196 static void testServerConf() { |
|
197 Options options = Options.parseArgs("--server:someServerConfiguration"); |
|
198 assertEquals("someServerConfiguration", options.getServerConf()); |
|
199 assertEquals(false, options.startServerFlag()); |
|
200 |
|
201 options = Options.parseArgs("--startserver:someServerConfiguration"); |
|
202 assertEquals("someServerConfiguration", options.getServerConf()); |
|
203 assertEquals(true, options.startServerFlag()); |
|
204 } |
|
205 |
|
206 // Test input paths |
|
207 static void testSearchPaths() { |
|
208 |
|
209 List<String> i, x, iF, xF; |
|
210 i = x = iF = xF = new ArrayList<>(); |
|
211 |
|
212 SourceLocation dir1 = new SourceLocation(Paths.get("dir1"), i, x, iF, xF); |
|
213 SourceLocation dir2 = new SourceLocation(Paths.get("dir2"), i, x, iF, xF); |
|
214 |
|
215 Options options = Options.parseArgs("-sourcepath", "dir1:dir2"); |
|
216 assertEquals(options.getSourceSearchPaths(), Arrays.asList(dir1, dir2)); |
|
217 |
|
218 options = Options.parseArgs("-modulepath", "dir1:dir2"); |
|
219 assertEquals(options.getModuleSearchPaths(), Arrays.asList(dir1, dir2)); |
|
220 |
|
221 options = Options.parseArgs("-classpath", "dir1:dir2"); |
|
222 assertEquals(options.getClassSearchPath(), Arrays.asList(dir1, dir2)); |
|
223 } |
|
224 |
|
225 // Test -tr option |
|
226 static void testTranslationRules() { |
|
227 |
|
228 Class<?> cls = com.sun.tools.sjavac.CompileJavaPackages.class; |
|
229 |
|
230 Options options = Options.parseArgs( |
|
231 "-tr", ".exa=" + cls.getName(), |
|
232 "-tr", ".exb=" + cls.getName(), |
|
233 "-copy", ".html"); |
|
234 |
|
235 assertEquals(cls, options.getTranslationRules().get(".exa").getClass()); |
|
236 assertEquals(cls, options.getTranslationRules().get(".exb").getClass()); |
|
237 assertEquals(CopyFile.class, options.getTranslationRules().get(".html").getClass()); |
|
238 |
|
239 } |
|
240 } |