test/tools/javac/lib/ToolBox.java

changeset 1637
2e21ecd7a5ad
parent 1591
dc8b7aa7cef3
child 2525
2eb010b6cb22
equal deleted inserted replaced
1636:82dc1e827c2a 1637:2e21ecd7a5ad
36 import java.nio.file.Paths; 36 import java.nio.file.Paths;
37 import java.nio.file.StandardOpenOption; 37 import java.nio.file.StandardOpenOption;
38 import java.util.ArrayList; 38 import java.util.ArrayList;
39 import java.util.Arrays; 39 import java.util.Arrays;
40 import java.util.Collection; 40 import java.util.Collection;
41 import java.util.Collections;
41 import java.util.EnumSet; 42 import java.util.EnumSet;
42 import java.util.List; 43 import java.util.List;
43 import java.util.Map; 44 import java.util.Map;
44 import java.util.Set; 45 import java.util.Set;
45 import java.util.regex.Matcher; 46 import java.util.regex.Matcher;
63 64
64 public class ToolBox { 65 public class ToolBox {
65 66
66 public static final String lineSeparator = System.getProperty("line.separator"); 67 public static final String lineSeparator = System.getProperty("line.separator");
67 public static final String jdkUnderTest = System.getProperty("test.jdk"); 68 public static final String jdkUnderTest = System.getProperty("test.jdk");
68 public static final String testVMOpts = System.getProperty("test.tool.vm.opts"); 69 public static final Path javaBinary = Paths.get(jdkUnderTest, "bin", "java");
69 public static final String javaBinary = Paths.get(jdkUnderTest, "bin", "java").toString(); 70 public static final Path javacBinary = Paths.get(jdkUnderTest, "bin", "javac");
70 //why this one private. Because the function which provide also the test options should be used 71
71 private static final String javacBinary = Paths.get(jdkUnderTest, "bin", "javac").toString(); 72 public static final List<String> testToolVMOpts;
73 public static final List<String> testVMOpts;
72 74
73 private static final Charset defaultCharset = Charset.defaultCharset(); 75 private static final Charset defaultCharset = Charset.defaultCharset();
74 76
75 static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 77 static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
78
79 static {
80 String sysProp = System.getProperty("test.tool.vm.opts");
81 if (sysProp != null && sysProp.length() > 0) {
82 testToolVMOpts = Arrays.asList(sysProp.split("\\s+"));
83 } else {
84 testToolVMOpts = Collections.<String>emptyList();
85 }
86
87 sysProp = System.getProperty("test.vm.opts");
88 if (sysProp != null && sysProp.length() > 0) {
89 testVMOpts = Arrays.asList(sysProp.split("\\s+"));
90 } else {
91 testVMOpts = Collections.<String>emptyList();
92 }
93 }
76 94
77 /** 95 /**
78 * The expected result of command-like method execution. 96 * The expected result of command-like method execution.
79 */ 97 */
80 public enum Expect {SUCCESS, FAIL} 98 public enum Expect {SUCCESS, FAIL}
197 EnumSet.<AcceptedParams>noneOf(AcceptedParams.class); 215 EnumSet.<AcceptedParams>noneOf(AcceptedParams.class);
198 216
199 protected Expect whatToExpect; 217 protected Expect whatToExpect;
200 protected WriterHelper stdOutput; 218 protected WriterHelper stdOutput;
201 protected WriterHelper errOutput; 219 protected WriterHelper errOutput;
202 protected List<String> options; 220 protected List<String> args = new ArrayList<>();
203 protected String[] optionsArr; 221 protected String[] argsArr;
204 222
205 protected GenericArgs() { 223 protected GenericArgs() {
206 set(Expect.SUCCESS); 224 set(Expect.SUCCESS);
207 } 225 }
208 226
236 return (T)this; 254 return (T)this;
237 } 255 }
238 256
239 public T setAllArgs(String... args) { 257 public T setAllArgs(String... args) {
240 currentParams.add(AcceptedParams.OPTIONS); 258 currentParams.add(AcceptedParams.OPTIONS);
241 this.optionsArr = args; 259 this.argsArr = args;
260 return (T) this;
261 }
262
263
264 public T appendArgs(String... args) {
265 appendArgs(Arrays.asList(args));
266 return (T)this;
267 }
268
269 public T appendArgs(Path... args) {
270 if (args != null) {
271 List<String> list = new ArrayList<>();
272 for (int i = 0; i < args.length; i++) {
273 if (args[i] != null) {
274 list.add(args[i].toString());
275 }
276 }
277 appendArgs(list);
278 }
279 return (T)this;
280 }
281
282 public T appendArgs(List<String> args) {
283 if (args != null && args.size() > 0) {
284 currentParams.add(AcceptedParams.OPTIONS);
285 for (int i = 0; i < args.size(); i++) {
286 if (args.get(i) != null) {
287 this.args.add(args.get(i));
288 }
289 }
290 }
242 return (T)this; 291 return (T)this;
243 } 292 }
244 293
245 public T setOptions(List<String> options) { 294 public T setOptions(List<String> options) {
246 currentParams.add(AcceptedParams.OPTIONS); 295 currentParams.add(AcceptedParams.OPTIONS);
247 this.options = options; 296 this.args = options;
248 return (T)this; 297 return (T)this;
249 } 298 }
250 299
251 public T setOptions(String... options) { 300 public T setOptions(String... options) {
252 currentParams.add(AcceptedParams.OPTIONS); 301 currentParams.add(AcceptedParams.OPTIONS);
253 this.options = Arrays.asList(options); 302 this.args = Arrays.asList(options);
254 return (T)this; 303 return (T)this;
255 } 304 }
256 305
257 public boolean hasMinParams() { 306 public boolean hasMinParams() {
258 for (Set<AcceptedParams> minSet : minAcceptedParams) { 307 for (Set<AcceptedParams> minSet : minAcceptedParams) {
363 412
364 /** 413 /**
365 * Custom exception for not equal resources. 414 * Custom exception for not equal resources.
366 */ 415 */
367 public static class ResourcesNotEqualException extends Exception { 416 public static class ResourcesNotEqualException extends Exception {
368 public ResourcesNotEqualException() { 417 public ResourcesNotEqualException(List<String> res1, List<String> res2) {
369 super("The resources provided for comparison are different"); 418 super(createMessage(res1, res2));
419 }
420
421 public ResourcesNotEqualException(String line1, String line2) {
422 super(createMessage(line1, line2));
370 } 423 }
371 424
372 public ResourcesNotEqualException(Path path1, Path path2) { 425 public ResourcesNotEqualException(Path path1, Path path2) {
373 super(createMessage(path1, path2)); 426 super(createMessage(path1, path2));
374 } 427 }
377 return new StringBuilder() 430 return new StringBuilder()
378 .append("The resources provided for comparison in paths \n") 431 .append("The resources provided for comparison in paths \n")
379 .append(path1.toString()).append(" and \n") 432 .append(path1.toString()).append(" and \n")
380 .append(path2.toString()).append("are different").toString(); 433 .append(path2.toString()).append("are different").toString();
381 } 434 }
382 } 435
383 436 private static String createMessage(String line1, String line2) {
384 /** 437 return new StringBuilder()
385 * Method to get the a path to the javac command available at the jdk being 438 .append("The resources provided for comparison are different at lines: \n")
386 * tested along with the test vm options. 439 .append(line1).append(" and \n")
387 * @return a String[] with the two components mentioned. 440 .append(line2).toString();
388 */ 441 }
389 public static String[] getJavacBin() { 442
390 return new String[]{javacBinary, testVMOpts}; 443 private static String createMessage(List<String> res1, List<String> res2) {
444 return new StringBuilder()
445 .append("The resources provided for comparison are different: \n")
446 .append("Resource 1 is: ").append(res1).append("\n and \n")
447 .append("Resource 2 is: ").append(res2).append("\n").toString();
448 }
391 } 449 }
392 450
393 /** 451 /**
394 * A javac compiler caller method. 452 * A javac compiler caller method.
395 */ 453 */
396 public static int javac(JavaToolArgs params) 454 public static int javac(JavaToolArgs params)
397 throws CommandExecutionException, IOException { 455 throws CommandExecutionException, IOException {
398 if (params.hasMinParams()) { 456 if (params.hasMinParams()) {
399 if (params.optionsArr != null) { 457 if (params.argsArr != null) {
400 return genericJavaCMD(JavaCMD.JAVAC, params); 458 return genericJavaCMD(JavaCMD.JAVAC, params);
401 } else { 459 } else {
402 return genericJavaCMD(JavaCMD.JAVAC_API, params); 460 return genericJavaCMD(JavaCMD.JAVAC_API, params);
403 } 461 }
404 } 462 }
435 */ 493 */
436 enum JavaCMD { 494 enum JavaCMD {
437 JAVAC { 495 JAVAC {
438 @Override 496 @Override
439 int run(JavaToolArgs params, PrintWriter pw) { 497 int run(JavaToolArgs params, PrintWriter pw) {
440 return com.sun.tools.javac.Main.compile(params.optionsArr, pw); 498 return com.sun.tools.javac.Main.compile(params.argsArr, pw);
441 } 499 }
442 }, 500 },
443 JAVAC_API { 501 JAVAC_API {
444 @Override 502 @Override
445 int run(JavaToolArgs params, PrintWriter pw) { 503 int run(JavaToolArgs params, PrintWriter pw) {
446 JavacTask ct = (JavacTask)comp.getTask(pw, null, null, 504 JavacTask ct = (JavacTask)comp.getTask(pw, null, null,
447 params.options, null, params.sources); 505 params.args, null, params.sources);
448 return ((JavacTaskImpl)ct).doCall().exitCode; 506 return ((JavacTaskImpl)ct).doCall().exitCode;
449 } 507 }
450 508
451 @Override 509 @Override
452 String getName() { 510 String getName() {
465 } 523 }
466 }, 524 },
467 JAVAH { 525 JAVAH {
468 @Override 526 @Override
469 int run(JavaToolArgs params, PrintWriter pw) { 527 int run(JavaToolArgs params, PrintWriter pw) {
470 return com.sun.tools.javah.Main.run(params.optionsArr, pw); 528 return com.sun.tools.javah.Main.run(params.argsArr, pw);
471 } 529 }
472 }, 530 },
473 JAVAP { 531 JAVAP {
474 @Override 532 @Override
475 int run(JavaToolArgs params, PrintWriter pw) { 533 int run(JavaToolArgs params, PrintWriter pw) {
476 return com.sun.tools.javap.Main.run(params.optionsArr, pw); 534 return com.sun.tools.javap.Main.run(params.argsArr, pw);
477 } 535 }
478 }; 536 };
479 537
480 abstract int run(JavaToolArgs params, PrintWriter pw); 538 abstract int run(JavaToolArgs params, PrintWriter pw);
481 539
484 } 542 }
485 543
486 List<String> getExceptionMsgContent(JavaToolArgs params) { 544 List<String> getExceptionMsgContent(JavaToolArgs params) {
487 List<String> result = new ArrayList<>(); 545 List<String> result = new ArrayList<>();
488 result.add(getName()); 546 result.add(getName());
489 result.addAll(params.optionsArr != null ? 547 result.addAll(params.argsArr != null ?
490 Arrays.asList(params.optionsArr) : 548 Arrays.asList(params.argsArr) :
491 params.options); 549 params.args);
492 return result; 550 return result;
493 } 551 }
494 } 552 }
495 553
496 /** 554 /**
507 rc = cmd.run(params, pw); 565 rc = cmd.run(params, pw);
508 } 566 }
509 String out = (sw == null) ? null : sw.toString(); 567 String out = (sw == null) ? null : sw.toString();
510 568
511 if (params.errOutput != null && (out != null) && !out.isEmpty()) { 569 if (params.errOutput != null && (out != null) && !out.isEmpty()) {
512 params.errOutput.addAll(splitLines(out)); 570 params.errOutput.addAll(splitLines(out, lineSeparator));
513 } 571 }
514 572
515 if ( (rc == 0 && params.whatToExpect == Expect.SUCCESS) || 573 if ( (rc == 0 && params.whatToExpect == Expect.SUCCESS) ||
516 (rc != 0 && params.whatToExpect == Expect.FAIL) ) { 574 (rc != 0 && params.whatToExpect == Expect.FAIL) ) {
517 return rc; 575 return rc;
540 * A general command calling method. 598 * A general command calling method.
541 */ 599 */
542 public static int executeCommand(AnyToolArgs params) 600 public static int executeCommand(AnyToolArgs params)
543 throws CommandExecutionException, IOException, InterruptedException { 601 throws CommandExecutionException, IOException, InterruptedException {
544 if (params.hasMinParams()) { 602 if (params.hasMinParams()) {
545 List<String> cmd = (params.options != null) ? 603 List<String> cmd = (params.args != null) ?
546 params.options : 604 params.args :
547 Arrays.asList(params.optionsArr); 605 Arrays.asList(params.argsArr);
548 return executeCommand(cmd, params.extraEnv, params.stdOutput, 606 return executeCommand(cmd, params.extraEnv, params.stdOutput,
549 params.errOutput, params.whatToExpect); 607 params.errOutput, params.whatToExpect);
550 } 608 }
551 throw new AssertionError("command has been invoked with less parameters than needed"); 609 throw new AssertionError("command has been invoked with less parameters than needed");
552 } 610 }
628 686
629 public static void compareLines(List<String> list1, 687 public static void compareLines(List<String> list1,
630 List<String> list2, boolean trim) throws ResourcesNotEqualException { 688 List<String> list2, boolean trim) throws ResourcesNotEqualException {
631 if ((list1 == list2) || (list1 == null && list2 == null)) return; 689 if ((list1 == list2) || (list1 == null && list2 == null)) return;
632 if (list1.size() != list2.size()) 690 if (list1.size() != list2.size())
633 throw new ResourcesNotEqualException(); 691 throw new ResourcesNotEqualException(list1, list2);
634 int i = 0; 692 int i = 0;
635 int j = 0; 693 int j = 0;
636 while (i < list1.size() && 694 while (i < list1.size() &&
637 j < list2.size() && 695 j < list2.size() &&
638 equals(list1.get(i), list2.get(j), trim)) { 696 equals(list1.get(i), list2.get(j), trim)) {
639 i++; j++; 697 i++; j++;
640 } 698 }
641 if (!(i == list1.size() && j == list2.size())) 699 if (!(i == list1.size() && j == list2.size()))
642 throw new ResourcesNotEqualException(); 700 throw new ResourcesNotEqualException(list1, list2);
643 } 701 }
644 702
645 private static boolean equals(String s1, String s2, boolean trim) { 703 private static boolean equals(String s1, String s2, boolean trim) {
646 return (trim ? s1.trim().equals(s2.trim()) : s1.equals(s2)); 704 return (trim ? s1.trim().equals(s2.trim()) : s1.equals(s2));
647 } 705 }
650 * A set of simple grep-like methods, looks for regExpr in text. 708 * A set of simple grep-like methods, looks for regExpr in text.
651 * The content of text is split using the new line character as a pattern 709 * The content of text is split using the new line character as a pattern
652 * and later the regExpr is seek in every split line. If a match is found, 710 * and later the regExpr is seek in every split line. If a match is found,
653 * the whole line is added to the result. 711 * the whole line is added to the result.
654 */ 712 */
655 public static List<String> grep(String regExpr, String text) { 713 public static List<String> grep(String regExpr, String text, String sep) {
656 return grep(regExpr, splitLines(text)); 714 return grep(regExpr, splitLines(text, sep));
657 } 715 }
658 716
659 public static List<String> grep(String regExpr, List<String> text) { 717 public static List<String> grep(String regExpr, List<String> text) {
660 List<String> result = new ArrayList<>(); 718 List<String> result = new ArrayList<>();
661 Pattern pattern = Pattern.compile(regExpr); 719 Pattern pattern = Pattern.compile(regExpr);
863 } 921 }
864 922
865 /** 923 /**
866 * Splits a String using the System's line separator character as splitting point. 924 * Splits a String using the System's line separator character as splitting point.
867 */ 925 */
868 public static List<String> splitLines(String lines) { 926 public static List<String> splitLines(String lines, String sep) {
869 return Arrays.asList(lines.split(lineSeparator)); 927 return Arrays.asList(lines.split(sep));
870 } 928 }
871 929
872 /** 930 /**
873 * Converts a String list into one String by appending the System's line separator 931 * Converts a String list into one String by appending the System's line separator
874 * character after each component. 932 * character after each component.
877 StringBuilder sb = new StringBuilder(); 935 StringBuilder sb = new StringBuilder();
878 for (String s : lines) { 936 for (String s : lines) {
879 sb.append(s).append(lineSeparator); 937 sb.append(s).append(lineSeparator);
880 } 938 }
881 return sb.toString(); 939 return sb.toString();
940 }
941
942 /**
943 * Returns true if the OS is a Windows version.
944 */
945 public static boolean isWindows() {
946 String osName = System.getProperty("os.name");
947 return osName.toUpperCase().startsWith("WINDOWS");
882 } 948 }
883 949
884 /** 950 /**
885 * Class representing an in-memory java source file. It is able to extract 951 * Class representing an in-memory java source file. It is able to extract
886 * the file name from simple source codes using regular expressions. 952 * the file name from simple source codes using regular expressions.

mercurial