1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
|
// Copyright (c) 2006, 2007, 2012 Red Hat, Inc.
// Written by Anthony Balkissoon <abalkiss@redhat.com>
// This file is part of Mauve.
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301 USA.
/*
* See the README file for information on how to use this
* file and what it is designed to do.
*/
import gnu.testlet.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
/**
* The Mauve Harness. This class parses command line input and standard
* input for tests to run and runs them in a separate process. It detects
* when that separate process is hung and restarts the process.
* @author Anthony Balkissoon abalkiss at redhat dot com
*
*/
public class Harness
{
// The compile method for the embedded ecj
private static Method ecjMethod = null;
// The string that will be passed to the compiler containing the options
// and the file(s) to compile
private static String compileString = null;
// The options to pass to the compiler, needs to be augmented by the
// bootclasspath, which should be the classpath installation directory
private static String compileStringBase = "-proceedOnError -nowarn -1.5 -d " + config.builddir;
// Options specified in a test which is passed to a compiler
private static String compileOptions = "";
// The writers for ecj's out and err streams.
private static PrintWriter ecjWriterOut = null;
private static PrintWriter ecjWriterErr = null;
// The name of the most recent test that failed to compile.
private static String lastFailingCompile = "";
// The number of compile fails in the current folder.
private static int numCompileFailsInFolder = 0;
// The constructor for the embedded ecj
private static Constructor<?> ecjConstructor = null;
// The classpath installation location, used for the compiler's bootcalsspath
private static String classpathInstallDir = null;
// The location of the eclipse-ecj.jar file
private static String ecjJarLocation = null;
// How long a test may run before it is considered hung
private static long runner_timeout = 60000;
// The command to invoke for the VM on which we will run the tests.
private static String vmCommand = null;
// A command that is prepended to the test commandline (e.g. strace, gdb, time)
private static String vmPrefix = null;
// Arguments to be passed to the VM
private static String vmArgs = "";
// Whether or not we should recurse into directories when a folder is
// specified to be tested
private static boolean recursion = true;
// Whether we should run in noisy mode
private static boolean verbose = false;
// Whether we should display one-line summaries for passing tests
private static boolean showPasses = false;
// Whether we should compile tests before running them
private static boolean compileTests = true;
// The total number of tests run
private static int total_tests = 0;
// The total number of failing tests (not harness.check() calls)
private static int total_test_fails = 0;
// The total number of harness.check() calls that fail
private static int total_check_fails = 0;
// All the tests that were specified on the command line rather than
// through standard input or an input file
private static List<String> commandLineTests = null;
// The input file (possibly) supplied by the user
private static String inputFile = null;
// All the tests that were explicitly excluded via the -exclude option
private static List<String> excludeTests = new ArrayList<String>();
// A way to speak to the runner process
private static PrintWriter runner_out = null;
// A way to listen to the runner process
private static BufferedReader runner_in = null;
// A thread listening to the error stream of the RunnerProcess
private static ErrorStreamPrinter runner_esp = null;
// A flag indicating whether or not we shoudl restart the error stream
// printer when we enter the runTest method
private static boolean restartESP = false;
// The process that will run the tests for us
private static Process runnerProcess = null;
// A watcher to determine if runnerProcess is hung
private static TimeoutWatcher runner_watcher = null;
// The arguments used when this Harness was invoked, we use this to create an
// appropriate RunnerProcess
private static String[] harnessArgs = null;
// A convenience String for ensuring tests all have the same name format
private static final String gnuTestletHeader1 = "gnu" + File.separatorChar
+ "testlet";
// A convenience String for ensuring tests all have the same name format
private static final String gnuTestletHeader2 = gnuTestletHeader1
+ File.separatorChar;
// The usual name of the CVS project containing this resource surrounded
// with file-separator strings
private static final String MAUVE = File.separator
+ System.getenv("MAUVE_PROJECT_NAME")
+ File.separator;
// When a folder is selected from Eclipse this is what usually gets
// prepended to the folder name
private static final String MAUVE_GNU_TESTLET = MAUVE + gnuTestletHeader2;
/**
* The main method for the Harness. Parses through the compile line
* options and sets up the internals, sets up the compiler options,
* and then runs all the tests. Finally, prints out a summary
* of the test run.
*
* @param args the compile line options
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
// Create a new Harness and set it up based on args.
Harness harness = new Harness();
harness.setupHarness(args);
// Start the runner process and run all the tests.
initProcess(args);
runAllTests();
// If more than one test was run, print a summary.
if (total_tests > 0)
System.out.println("\nTEST RESULTS:\n" + total_test_fails + " of "
+ total_tests + " tests failed. " + total_check_fails
+ " total calls to harness.check() failed.");
else
{
// If no tests were run, try to help the user out by suggesting what
// the problem might have been.
System.out.println ("No tests were run. Possible reasons " +
"may be listed below.");
if (compileTests == false)
{
System.out.println("Autocompilation is not enabled, so the " +
"tests need to be compiled manually. You can enable " +
"autocompilation via configure, see the README for more " +
"info.\n");
}
else if (recursion == false)
{
System.out.println ("-norecursion was specified, did you " +
"specify a folder that had no tests in it?\n");
}
else if (excludeTests != null && excludeTests.size() > 0)
{
System.out.println ("Some tests were excluded.\nDid you use " +
"-exclude and exclude all tests (or all specified " +
"tests)? \n");
}
else
{
System.out.println ("Did you specify a test that " +
"doesn't exist or a folder that contains " +
"no tests? \n");
}
}
harness.finalize();
System.exit(total_test_fails > 0 ? 1 : 0);
}
/**
* Sets up the harness internals before the tests are run. Parses through
* the compile line options and then sets up the compiler options.
* @param args
* @throws Exception
*/
private void setupHarness(String[] args) throws Exception
{
// Save the arguments, we'll pass them to the RunnerProcess so it can
// set up its internal properties.
harnessArgs = args;
// Find out from configuration whether auto-compilation is enabled or not.
// This can be changed via the options to Harness (-compile true or
// -compile false).
compileTests = config.autoCompile.equals("yes");
// Find out from configuration which VM we're testing. This can be changed
// via the options to Harness (-vm VM_TO_TEST).
vmCommand = config.testJava;
// Now parse all the options to Harness and set the appropriate internal
// properties.
for (int i = 0; i < args.length; i++)
{
if (args[i].equals("-norecursion"))
recursion = false;
else if (args[i].equals("-verbose"))
verbose = true;
else if (args[i].equals("-showpasses"))
showPasses = true;
else if (args[i].equals("-compile"))
{
// User wants to use an input file to specify which tests to run.
if (++i >= args.length)
throw new RuntimeException("No file path after '-file'. Exit");
if (args[i].equals("yes") || args[i].equals("true"))
compileTests = true;
else if (args[i].equals("no") || args[i].equals("false"))
compileTests = false;
}
else if (args[i].equals("-help") || args[i].equals("--help")
|| args[i].equals("-h"))
printHelpMessage();
else if (args[i].equalsIgnoreCase("-file"))
{
// User wants to use an input file to specify which tests to run.
if (++i >= args.length)
throw new RuntimeException("No file path after '-file'. Exit");
inputFile = args[i];
}
else if (args[i].equalsIgnoreCase("-bootclasspath"))
{
// User is specifying the classpath installation folder to use
// as the compiler's bootclasspath.
if (++i >= args.length)
throw new RuntimeException("No file path " +
"after '-bootclasspath'. Exit");
classpathInstallDir = args[i];
}
else if (args[i].equalsIgnoreCase("-vmarg"))
{
// User is specifying arguments to be passed to the VM of the
// RunnerProcess.
if (++i >= args.length)
throw new RuntimeException("No argument after -vmarg. Exit");
{
vmArgs += " " + args[i];
}
}
else if (args[i].equalsIgnoreCase("-ecj-jar"))
{
// User is specifying the location of the eclipse-ecj.jar file
// to use for compilation.
if (++i >= args.length)
throw new RuntimeException("No file path " +
"after '-ecj-jar'. Exit");
ecjJarLocation = args[i];
}
else if (args[i].equals("-exclude"))
{
// User wants to exclude some tests from the run.
if (++i >= args.length)
throw new RuntimeException ("No test or directory " +
"given after '-exclude'. Exit");
excludeTests.add(startingFormat(args[i]));
}
else if (args[i].equals("-vm"))
{
// User wants to exclude some tests from the run.
if (++i >= args.length)
throw new RuntimeException ("No VMPATH" +
"given after '-vm'. Exit");
vmCommand = args[i];
}
else if (args[i].equals("-vmprefix"))
{
// User wants to prepend a certain command.
if (++i >= args.length)
throw new RuntimeException ("No file" +
"given after '-vmprefix'. Exit");
vmPrefix = args[i] + " ";
}
else if (args[i].equals("-timeout"))
{
// User wants to change the timeout value.
if (++i >= args.length)
throw new RuntimeException ("No timeout value given " +
"after '-timeout'. Exit");
runner_timeout = Long.parseLong(args[i]);
}
else if (args[i].equals("-xmlout"))
{
// User wants output in an xml file
if (++i >= args.length)
throw new RuntimeException ("No file " +
"given after '-xmlout'. Exit");
// the filename is used directly from args
}
else if (args[i].equals("-autoxml"))
{
// Path to store xml files
if (++i >= args.length)
throw new RuntimeException ("No path " +
"specified after '-autoxml'. Exit");
// the dirname is used directly from args
}
else if (args[i].charAt(0) == '-')
{
// One of the ignored options (handled by RunnerProcess)
// such as -debug. Do nothing here but don't let it fall
// through to the next branch which would consider it a
// test or folder name
}
else if (args[i] != null)
{
// This is a command-line (not standard input) test or directory.
if (commandLineTests == null)
commandLineTests = new ArrayList<String>();
commandLineTests.add(startingFormat(args[i]));
}
}
// If ecj-jar wasn't specified, use the configuration value.
if (ecjJarLocation == null)
ecjJarLocation = config.ecjJar;
// If auto-compilation is enabled, verify that the ecj-jar location is
// valid.
if (compileTests)
{
if (ecjJarLocation == null || ecjJarLocation.equals(""))
compileTests = false;
else
{
File testECJ = new File(ecjJarLocation);
if (!testECJ.exists())
compileTests = false;
}
}
// If auto-compilation is enabled and the ecj-jar location was fine,
// set up the compiler options and PrintWriters
if (compileTests)
setupCompiler();
// If vmCommand is "java" it is likely that it defaulted to this value,
// so it wasn't set in configure (--with-vm) and it wasn't set
// on the command line (-vm TESTVM), so we should print a warning.
if (vmCommand.equals("java"))
System.out.println("WARNING: running tests on 'java'. To set the " +
"test VM, use --with-vm when\nconfiguring " +
"or specify -vm when running the Harness.\n");
}
/**
* Sets up the compiler by reflection, sets up the compiler options,
* and the PrintWriters to get error messages from the compiler.
*
* @throws Exception
*/
private void setupCompiler() throws Exception
{
String classname = "org.eclipse.jdt.internal.compiler.batch.Main";
Class<?> klass = null;
try
{
klass = Class.forName(classname);
}
catch (ClassNotFoundException e)
{
File jar = new File(ecjJarLocation);
if (! jar.exists() || ! jar.canRead())
throw e;
ClassLoader loader = new URLClassLoader(new URL[] { jar.toURL() });
try
{
klass = loader.loadClass(classname);
}
catch (ClassNotFoundException f)
{
throw e;
}
}
// Set up the compiler and the PrintWriters for the compile errors.
ecjConstructor =
klass.getConstructor
(PrintWriter.class, PrintWriter.class, Boolean.TYPE);
ecjMethod =
klass.getMethod
("compile", String.class, PrintWriter.class, PrintWriter.class );
ecjWriterErr = new CompilerErrorWriter(System.out);
ecjWriterOut = new PrintWriter(System.out);
// Set up the compiler options now that we know whether or not we are
// compiling.
compileStringBase += getClasspathInstallString();
}
/**
* Removes the config.srcdir + File.separatorChar from the start of
* a String.
* @param val the String
* @return the String with config.srcdir + File.separatorChar
* removed
*/
private static String stripSourcePath(String val)
{
if (val.startsWith(config.srcdir + File.separatorChar)
|| val.startsWith(config.srcdir.replace('/', '.') + "."))
val = val.substring(config.srcdir.length() + ".".length());
return val;
}
/**
* Removes the "gnu.testlet." from the start of a String.
* @param val the String
* @return the String with "gnu.testlet." removed
*/
private static String stripPrefix(String val)
{
if (val.startsWith("gnu" + File.separatorChar + "testlet")
|| val.startsWith("gnu.testlet."))
val = val.substring("gnu.testlet.".length());
return val;
}
/**
* Get the bootclasspath from the configuration so it can be added
* to the string passed to the compiler.
* @return the bootclasspath for the compiler, in String format
*/
private static String getClasspathInstallString()
{
String temp = classpathInstallDir;
// If classpathInstallDir is null that means no bootclasspath was
// specified on the command line using -bootclasspath. In this case
// auto-detect the bootclasspath.
if (temp == null)
{
temp = getBootClassPath();
// If auto-detect returned null we cannot auto-detect the
// bootclasspath and we should try invoking the compiler without
// specifying the bootclasspath. Otherwise, we should add
// " -bootclasspath " followed by the detected path.
if (temp != null)
return " -bootclasspath " + temp;
return "";
}
// This section is for bootclasspath's specified with
// -bootclasspath or --with-bootclasspath (in configure), we need
// to add "/share/classpath/glibj.zip" onto the end and
// " -bootclasspath onto the start".
temp = " -bootclasspath " + temp;
if (!temp.endsWith(File.separator))
temp += File.separator;
temp += "share" + File.separator + "classpath";
// If (for some reason) there is no glibj.zip file in the specified
// folder, just use the folder as the bootclasspath, perhaps the folder
// contains an expanded view of the resources.
File f = new File (temp.substring(16) + File.separator + "glibj.zip");
if (f.exists())
temp += File.separator + "glibj.zip";
return temp;
}
/**
* Forks a process to run DetectBootclasspath on the VM that is
* being tested. This program detects the bootclasspath so we can use
* it for the compiler's bootclasspath.
* @return the bootclasspath as found, or null if none could be found.
*/
private static String getBootClassPath()
{
try
{
String c = vmCommand + vmArgs + " Harness$DetectBootclasspath";
Process p = Runtime.getRuntime().exec(c);
BufferedReader br =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String bcpOutput = null;
while (true)
{
// This readLine() is a blocking call. This will hang if the
// bootclasspath finder hangs.
bcpOutput = br.readLine();
if (bcpOutput.equals("BCP_FINDER:can't_find_bcp_"))
{
// This means the auto-detection failed.
return null;
}
else if (bcpOutput.startsWith("BCP_FINDER:"))
{
return bcpOutput.substring(11);
}
else
System.out.println(bcpOutput);
}
}
catch (IOException ioe)
{
// Couldn't auto-fetch the bootclasspath.
return null;
}
}
/**
* This method takes a String and puts it into a consistent format so we can
* deal with all test names in the same way. It ensures that tests start with
* "gnu/testlet" and that '.' are replaced by the file separator character.
* It also strips the .java or .class extensions if they are present,
* and removes single trailing dots.
*
* @param val the input String
* @return the formatted String
*/
private static String startingFormat(String val)
{
if (val != null)
{
if (val.endsWith(".class"))
val = val.substring(0, val.length() - 6);
if (val.endsWith(".java"))
val = val.substring(0, val.length() - 5);
val = val.replace('.', File.separatorChar);
if (val.endsWith("" + File.separatorChar))
val = val.substring(0, val.length() - 1);
if (val.startsWith(MAUVE_GNU_TESTLET))
val = val.substring(MAUVE.length());
else if (! val.startsWith(gnuTestletHeader1))
val = gnuTestletHeader2 + val;
}
return val;
}
/**
* This method prints a help screen to the console and then exits.
*/
static void printHelpMessage()
{
String align = "\n ";
String message =
"This is the Mauve Harness. Usage:\n\n" +
" JAVA Harness <options> <testcase | folder>\n" +
" If no testcase or folder is given, all the tests will be run. \n" +
// Example invocation.
"\nExample: 'jamvm Harness -vm jamvm -showpasses javax.swing'\n" +
" will use jamvm (located in your path) to run all the tests in the\n" +
" gnu.testlet.javax.swing folder and will display PASSES\n" +
" as well as FAILS.\n\nOPTIONS:\n\n" +
// Test Run Options.
"Test Run Options:\n" +
" -vm [vmpath]: specify the vm on which to run the tests." +
"It is strongly recommended" + align + "that you use this option or " +
"use the --with-vm option when running" + align + "configure. " +
"See the README file for more details.\n" +
" -compile [yes|no]: specify whether or not to compile the " +
"tests before running them. This" + align + "overrides the configure" +
"option --disable-auto-compilation but requires an ecj jar" + align +
"to be in /usr/share/java/eclipse-ecj.jar or specified via the " +
"--with-ecj-jar" + align + "option to configure. See the README" +
" file for more details.\n" +
" -timeout [millis]: specifies a timeout value for the tests " +
"(default is 60000 milliseconds)" +
// Testcase Selection Options.
"\n\nTestcase Selection Options:\n" +
" -exclude [test|folder]: specifies a test or a folder to exclude " +
"from the run\n" +
" -norecursion: if a folder is specified to be run, don't " +
"run the tests in its subfolders\n" +
" -file [filename]: specifies a file that contains the names " +
"of tests to be run (one per line)\n" +
" -interactive: only run interactive tests, if not set, " +
"only run non-interactive tests\n" +
// Output Options.
"\n\nOutput Options:\n" +
" -showpasses: display passing tests as well as failing " +
"ones\n" +
" -hidecompilefails: hide errors from the compiler when " +
"tests fail to compile\n" +
" -noexceptions: suppress stack traces for uncaught " +
"exceptions\n" +
" -verbose: run in noisy mode, displaying extra " +
"information\n" +
" -debug: displays some extra information for " +
"failing tests that " +
"use the" + align + "harness.check(Object, Object) method\n" +
" -xmlout [filename]: specifies a file to use for xml output\n" +
" -autoxml [folder]: generate individual xml output, for each test case \n" +
"\nOther Options:\n" +
" -help: display this help message\n";
System.out.println(message);
System.exit(0);
}
protected void finalize()
{
//Clean up
try
{
runTest("_dump_data_");
runnerProcess.destroy();
runner_in.close();
runner_out.close();
}
catch (IOException e)
{
System.err.println("Could not close the interprocess pipes.");
System.exit(-1);
}
}
/**
* This method sets up our runner process - the process that actually
* runs the tests. This needs to be done once initially and also
* every time a test hangs.
* @param args the compile line options for Harness
*/
private static void initProcess(String[] args)
{
StringBuffer sb = new StringBuffer(" RunnerProcess");
for (int i = 0; i < args.length; i++)
sb.append(" " + args[i]);
if (vmPrefix != null)
sb.insert(0, vmPrefix + vmCommand + vmArgs);
else
sb.insert(0, vmCommand + vmArgs);
try
{
// Exec the process and set up in/out communications with it.
runnerProcess = Runtime.getRuntime().exec(sb.toString());
runner_out = new PrintWriter(runnerProcess.getOutputStream(), true);
runner_in =
new BufferedReader
(new InputStreamReader(runnerProcess.getInputStream()));
runner_esp = new ErrorStreamPrinter(runnerProcess.getErrorStream());
InputPiperThread pipe = new InputPiperThread(System.in,
runnerProcess.getOutputStream());
pipe.start();
runner_esp.start();
}
catch (IOException e)
{
System.err.println("Problems invoking RunnerProcess: " + e);
System.exit(1);
}
// Create a timer to watch this new process. After confirming the
// RunnerProcess started properly, we create a new runner_watcher
// because it may be a while before we run the next test (due to
// preprocessing and compilation) and we don't want the runner_watcher
// to time out.
if (runner_watcher != null)
runner_watcher.stop();
runner_watcher = new TimeoutWatcher(runner_timeout, runnerProcess);
runTest("_confirm_startup_");
runner_watcher.stop();
runner_watcher = new TimeoutWatcher(runner_timeout, runnerProcess);
}
/**
* This method runs all the tests, both from the command line and from
* standard input. This is so the legacy method of running tests by
* echoing the classname and piping it to the Harness works, but so does
* a more natural "jamvm Harness <TESTNAME>".
*/
private static void runAllTests()
{
// Run the commandLine tests. These were assembled into
// <code>commandLineTests</code> in the setupHarness method.
if (commandLineTests != null)
{
for (int i = 0; i < commandLineTests.size(); i++)
{
String cname = null;
cname = commandLineTests.get(i);
if (cname == null)
break;
processTest(cname);
}
}
// Now run the standard input tests. First we determine if the input is
// coming from a file (if the -file option was used) or from stdin.
BufferedReader r = null;
if (inputFile != null)
// The -file option was used, so set up our BufferedReader to use the
// input file.
try
{
r = new BufferedReader(new FileReader(inputFile));
}
catch (FileNotFoundException x)
{
throw new
RuntimeException("Cannot find \"" + inputFile + "\". Exit");
}
else
{
// The -file option was not used, so use stdin instead.
r = new BufferedReader(new InputStreamReader(System.in));
try
{
if (! r.ready())
{
// If no tests were specified to be run, we will run all the
// tests (except those explicitly excluded).
if (commandLineTests == null || commandLineTests.size() == 0)
processTest("gnu/testlet");
return;
}
}
catch (IOException ioe)
{
}
}
// Now process all the tests specified in the file or from stdin.
while (true)
{
String cname = null;
try
{
cname = r.readLine();
if (cname == null)
break;
}
catch (IOException x)
{
// Nothing.
}
processTest(startingFormat(cname));
}
}
/**
* This method runs a single test in a new Harness and increments the
* total tests run and total failures, if the test fails. Prints
* PASS and adds to the report, if the appropriate options are enabled.
* @param testName the name of the test
*/
private static void runTest(String testName)
{
String tn = stripPrefix(testName.replace(File.separatorChar, '.'));
String outputFromTest;
boolean invalidTest = false;
int temp;
// Restart the error stream printer if necessary
if (restartESP)
{
restartESP = false;
runner_esp = new ErrorStreamPrinter(runnerProcess.getErrorStream());
}
// (Re)start the timeout watcher
runner_watcher.reset();
// Tell the RunnerProcess to run test with name testName
runner_out.println(testName);
while (true)
{
// Continue getting output from the RunnerProcess until it
// signals the test completed or was invalid, or until the
// TimeoutWatcher stops the RunnerProcess forcefully.
try
{
outputFromTest = runner_in.readLine();
if (outputFromTest == null)
{
// This means the test hung.
initProcess(harnessArgs);
temp = - 1;
break;
}
else if (outputFromTest.startsWith("RunnerProcess:"))
{
invalidTest = false;
// This means the RunnerProcess has sent us back some
// information. This could be telling us that a check() call
// was made and we should reset the timer, or that the
// test passed, or failed, or that it wasn't a test.
if (outputFromTest.endsWith("restart-timer"))
runner_watcher.reset();
else if (outputFromTest.endsWith("pass"))
{
temp = 0;
break;
}
else if (outputFromTest.indexOf("fail-loading") != -1)
{
temp = 1;
System.out.println(outputFromTest.substring(27));
}
else if (outputFromTest.indexOf("fail-") != - 1)
{
total_check_fails += Integer.parseInt(outputFromTest.substring(19));
temp = 1;
break;
}
else if (outputFromTest.endsWith("not-a-test"))
{
// Temporarily decrease the total number of tests,
// because it will be incremented later even
// though the test was not a real test.
invalidTest = true;
total_tests--;
temp = 0;
break;
}
}
else if (outputFromTest.equals("_startup_okay_")
|| outputFromTest.equals("_data_dump_okay_"))
return;
else
// This means it was just output from the test, like a
// System.out.println within the test itself, we should
// pass these on to stdout.
System.out.println(outputFromTest);
}
catch (IOException e)
{
initProcess(harnessArgs);
temp = -1;
break;
}
}
if (temp == -1)
{
// This means the watcher thread had to stop the process
// from running. So this is a fail.
if (verbose)
System.out.println(" FAIL: timed out. \nTEST FAILED: timeout " +
tn);
else
System.out.println("FAIL: " + tn
+ "\n Test timed out. Use -timeout [millis] " +
"option to change the timeout value.");
total_test_fails++;
}
else
total_test_fails += temp;
total_tests ++;
// If the test passed and the user wants to know about passes, tell them.
if (showPasses && temp == 0 && !verbose && !invalidTest)
System.out.println ("PASS: "+tn);
}
/**
* This method handles the input, whether it is a single test or a folder
* and calls runTest on the appropriate .class files. Will also compile
* tests that haven't been compiled or that have been changed since last
* being compiled.
* @param cname the input file name - may be a directory
*/
private static void processTest(String cname)
{
if (cname.equals("CVS") || cname.endsWith(File.separatorChar + "CVS")
|| excludeTests.contains(cname)
|| (cname.lastIndexOf("$") > cname.lastIndexOf(File.separator)))
return;
// If processSingleTest returns -1 then this test was explicitly
// excluded with the -exclude option, and if it returns 0 then
// the test was successfully run and we should stop here. Only
// if it returns 1 should we try to process cname as a directory.
if (processSingleTest(cname) == 1)
processFolder(cname);
}
/**
* Checks if the corresponding classfile for the given test needs to
* be compiled, or exists and needs to be updated.
*
* @param test name or path of the test
* @return true if the classfile needs to be compiled
*/
private static boolean testNeedsToBeCompiled(String testname)
{
String filename = stripSourcePath(testname);
if (filename.endsWith(".java"))
filename =
filename.substring(0, filename.length() - ".java".length());
String sourcefile =
config.srcdir + File.separatorChar + filename + ".java";
String classfile =
config.builddir + File.separatorChar + filename + ".class";
File sf = new File(sourcefile);
File cf = new File(classfile);
if (!sf.exists())
throw new RuntimeException(sourcefile + " does not exists!");
if (!cf.exists())
return true;
return (sf.lastModified() > cf.lastModified());
}
/**
* Parse and process tags in the source file.
*
* @param sourcefile path of the source file
* @param filesToCompile LinkedHashSet of the files to compile
*
* @return true on success, false on error
*/
private static boolean parseTags(String sourcefile, LinkedHashSet<String> filesToCompile,
LinkedHashSet<String> filesToCopy, LinkedHashSet<String> testsToRun)
{
File f = new File(sourcefile);
String base = f.getAbsolutePath();
base = base.substring(0, base.lastIndexOf(File.separatorChar));
BufferedReader r = null;
try
{
r = new BufferedReader(new FileReader(f));
String line = null;
line = r.readLine();
while (line != null)
{
if (line.contains("//"))
{
if (line.contains("Uses:"))
{
processUsesTag(line, base, filesToCompile, filesToCopy, testsToRun);
}
else if (line.contains("Files:"))
{
processFilesTag(line, base, filesToCopy);
}
else if (line.contains("CompileOptions:"))
{
processCompileOptions(line);
}
else if (line.contains("not-a-test"))
{
// Don't run this one but parse it's tags.
testsToRun.remove(sourcefile);
}
}
else if (line.contains("implements Testlet"))
{
// Don't read through the entire test once we've hit
// real code. Note that this doesn't work for all
// files, only ones that implement Testlet, but that
// is most files.
break;
}
line = r.readLine();
}
}
catch (IOException ioe)
{
// This shouldn't happen.
ioe.printStackTrace();
return false;
}
finally
{
try
{
r.close();
}
catch (IOException ioe)
{
// This shouldn't happen.
ioe.printStackTrace();
return false;
}
}
return true;
}
/**
* Parse and process compile options tag which can be specified
* in the test source file.
*
* @param sourcefile path of the source file
* @return compile options for given source file or null
* if none option is specified
*/
private static String parseCompileOptions(String sourcefile)
{
String compileOptions = null;
File f = new File(sourcefile);
String base = f.getAbsolutePath();
base = base.substring(0, base.lastIndexOf(File.separatorChar));
BufferedReader r = null;
try
{
r = new BufferedReader(new FileReader(f));
String line = null;
while ( (line = r.readLine()) != null)
{
if (line.contains("//"))
{
if (line.contains("CompileOptions:"))
{
compileOptions = line.substring(line.indexOf("CompileOptions:") + "CompileOptions:".length()) + " ";
}
}
}
}
catch (IOException ioe)
{
// This shouldn't happen.
ioe.printStackTrace();
return null;
}
finally
{
try
{
r.close();
}
catch (IOException ioe)
{
// This shouldn't happen.
ioe.printStackTrace();
return null;
}
}
return compileOptions;
}
/**
* Processes the // Uses: tag in a testlet's source.
*
* @param line string of the current source line
* @param base base directory of the current test
* @param filesToCompile LinkedHashSet of the current files to be compiled
*/
private static void processUsesTag(String line, String base,
LinkedHashSet<String> filesToCompile,
LinkedHashSet<String> filesToCopy,
LinkedHashSet<String> testsToRun)
{
StringTokenizer st =
new StringTokenizer(line.substring(line.indexOf("Uses:") + 5));
while (st.hasMoreTokens())
{
String depend = base;
String t = st.nextToken();
while (t.startsWith(".." + File.separator))
{
t = t.substring(3);
depend =
depend.substring(0, depend.lastIndexOf(File.separatorChar));
}
depend += File.separator + t;
if (depend.endsWith(".class"))
depend = depend.substring(0, depend.length() - 6);
if (!depend.endsWith(".java"))
depend += ".java";
// Check if the current dependency needs to be compiled (NOTE:
// This check does not include inner classes).
if (testNeedsToBeCompiled(depend))
{
// Add the current dependency.
filesToCompile.add(depend);
}
// Now parse the tags of the dependency.
parseTags(depend, filesToCompile, filesToCopy, testsToRun);
}
}
/**
* Processes the // CompileOptions: tag in a testlet's source.
*
* @param line string of the current source line
*/
private static void processCompileOptions(String line)
{
compileOptions = line.substring(line.indexOf("CompileOptions:") + "CompileOptions:".length());
compileOptions += " "; // add separator to a command line
}
/**
* Processes the // Files: tag in a testlet's source.
*
* @param base base directory of the current test
* @param line string of the current source line
*/
private static void processFilesTag(String line, String base,
LinkedHashSet<String> filesToCopy)
{
StringTokenizer st =
new StringTokenizer(line.substring(line.indexOf("Files:") + 6));
while (st.hasMoreTokens())
{
String src = base;
String t = st.nextToken();
while (t.startsWith(".." + File.separator))
{
t = t.substring(3);
src =
src.substring(0, src.lastIndexOf(File.separatorChar));
}
src += File.separator + t;
filesToCopy.add(src);
}
}
/**
* Copy the given files from the source directory to the build
* directory.
*
* @param filesToCopy files to copy
*
* @return true on success, false on error
*/
private static boolean copyFiles(LinkedHashSet<String> filesToCopy)
{
if (filesToCopy.size() == 0)
return true;
for (Iterator<String> it = filesToCopy.iterator(); it.hasNext(); )
{
String src = it.next();
String dest =
config.builddir + File.separatorChar + stripSourcePath(src);
try
{
File inputFile = new File(src);
File outputFile = new File(dest);
// Only copy newer files.
if (inputFile.lastModified() <= outputFile.lastModified())
continue;
// Create directories up to the new file.
outputFile.getParentFile().mkdirs();
FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile);
byte[] buf = new byte[1024];
int i = 0;
while((i = fis.read(buf)) != -1)
{
fos.write(buf, 0, i);
}
fis.close();
fos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
return false;
}
}
return true;
}
/**
* This method is used to potentially run a single test. If runAnyway is
* false we've reached here as a result of processing a directory and we
* should only run tests if they end in ".java" to avoid running tests
* multiple times.
*
* @param cname the name of the test to run
* @return -1 if the test was explicitly excluded via the -exclude option,
* 0 if cname represents a single test, 1 if cname does not represent a
* single test
*/
private static int processSingleTest(String cname)
{
LinkedHashSet<String> filesToCompile = new LinkedHashSet<String>();
LinkedHashSet<String> filesToCopy = new LinkedHashSet<String>();
LinkedHashSet<String> testsToRun = new LinkedHashSet<String>();
// If the test should be excluded return -1, this is a signal
// to processTest that it should quit.
if (excludeTests.contains(cname))
return -1;
// If it's not a single test, return 1, processTest will then try
// to process it as a directory.
String sourcefile = config.srcdir + File.separatorChar + cname + ".java";
File jf = new File(sourcefile);
if (!jf.exists())
return 1;
if (!compileTests)
{
if (testNeedsToBeCompiled(cname))
{
// There is an uncompiled test, but the -nocompile option was given
// so we just skip it
return -1;
}
}
else
{
if (testNeedsToBeCompiled(cname))
filesToCompile.add(sourcefile);
testsToRun.add(sourcefile);
// Process all tags in the source file.
if (!parseTags(sourcefile, filesToCompile, filesToCopy, testsToRun))
return -1;
if (!copyFiles(filesToCopy))
return -1;
// If compilation of the test fails, don't try to run it.
if (!compileFiles(filesToCompile, null))
return -1;
}
runTest(cname);
return 0;
}
/**
* This method processes all the tests in a folder. It does so in
* 3 steps: 1) compile a list of all the .java files in the folder,
* 2) compile those files unless compileTests is false,
* 3) run those tests.
* @param folderName
*/
private static void processFolder(String folderName)
{
File dir = new File(config.srcdir + File.separatorChar + folderName);
String dirPath = dir.getPath();
File[] files = dir.listFiles();
LinkedHashSet<String> filesToCompile = new LinkedHashSet<String>();
LinkedHashSet<String> filesToCopy = new LinkedHashSet<String>();
LinkedHashSet<String> testsToRun = new LinkedHashSet<String>();
// map containing compile options for given test, but only if
// explicit compile options are defined in the test source
Map<String, String> compileOptions = new HashMap<String, String>();
String fullPath = null;
boolean compilepassed = true;
// If files is null, it is likely that the user input an incorrect
// Harness command (like -test-vm TESTVM instead of -vm TESTVM).
if (files == null)
return;
// First, compile the list of .java files.
for (int i = 0; i < files.length; i++)
{
// Ignore the CVS folders.
String name = files[i].getName();
fullPath = dirPath + File.separatorChar + name;
String testName = stripSourcePath(fullPath);
if (name.equals("CVS") || excludeTests.contains(testName))
continue;
if (name.endsWith(".java") &&
!excludeTests.contains(testName.
substring(0, testName.length() - 5)))
{
if (testNeedsToBeCompiled(testName)) {
// test needs to be compiled
filesToCompile.add(fullPath);
// we have to know its compile options (if there are any)
String options = parseCompileOptions(fullPath);
if (options != null)
{
compileOptions.put(fullPath, options);
}
}
testsToRun.add(fullPath);
// Process all tags in the source file.
if (!parseTags(fullPath, filesToCompile, filesToCopy, testsToRun))
continue;
}
else
{
// Check if it's a folder, if so, call this method on it.
if (files[i].isDirectory() && recursion
&& ! excludeTests.contains(testName))
processFolder(testName);
}
}
if (!copyFiles(filesToCopy))
return;
// Exit if there were no .java files in this folder.
if (testsToRun.size() == 0)
return;
// Ignore the .java files in top level gnu/testlet folder.
if (dirPath.equals(config.srcdir + File.separatorChar +
"gnu" + File.separatorChar + "testlet"))
return;
// Now compile all those tests in a batch compilation, unless the
// -nocompile option was used.
if (compileTests)
compilepassed = compileFiles(filesToCompile, compileOptions);
// And now run those tests.
runFolder(testsToRun, compilepassed);
}
/**
* Runs all the tests in a folder. If the tests were compiled by
* compileFolder, and the compilation failed, then we must check to
* see if each individual test compiled before running it.
*
* @param testsToRun a list of all the tests to run
* @param compilepassed true if the compilation step happened and all
* tests passed or if compilation didn't happen (because of -nocompile).
*/
private static void runFolder(LinkedHashSet<String> testsToRun, boolean compilepassed)
{
String nextTest = null;
for (Iterator<String> it = testsToRun.iterator(); it.hasNext(); )
{
nextTest = it.next();
nextTest = stripSourcePath(nextTest);
if (!testNeedsToBeCompiled(nextTest)
&& (compilepassed || !excludeTests.contains(nextTest)))
{
nextTest = nextTest.substring(0, nextTest.length() - 5);
runTest(nextTest);
}
}
}
/**
* This method invokes the embedded ECJ compiler to compile a single
* test, which is stored in compileArgs[2].
* @return the return value from the compiler
* @throws Exception
*/
public static int compile() throws Exception
{
/*
* This code depends on the patch in Comment #10 in this bug
* report:
*
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=88364
*/
Object ecjInstance = ecjConstructor.newInstance (new Object[] {
new PrintWriter (System.out),
new PrintWriter (System.err),
Boolean.FALSE});
return ((Boolean) ecjMethod.invoke (ecjInstance, new Object[] {
compileString, ecjWriterOut, ecjWriterErr})).booleanValue() ? 0 : -1;
}
/**
* Compile the given files.
*
* @param filesToCompile LinkedHashSet of the files to compile
* @param testCompileOptions map containing compile options from given test(s), may be null
* @return true if compilation was successful
*/
private static boolean compileFiles(LinkedHashSet<String> filesToCompile, Map<String, String> testCompileOptions)
{
if (filesToCompile.size() == 0)
return true;
int result = - 1;
// tests without specific compile options can be compiled as a whole group
boolean doGroupCompile = false;
compileString = compileStringBase + compileOptions;
for (Iterator<String> it = filesToCompile.iterator(); it.hasNext(); )
{
String testName = it.next();
// only test without specific compile options can be added to a "compile group"
if (testCompileOptions == null || !testCompileOptions.containsKey(testName))
{
compileString += " " + testName;
doGroupCompile = true;
}
}
try
{
if (doGroupCompile)
{
result = compile();
}
// try to compile other tests - in this case test by test
if (testCompileOptions != null)
{
for (Map.Entry<String, String> test : testCompileOptions.entrySet())
{
String oldCompileString = compileStringBase + compileOptions;
compileString = compileStringBase + test.getValue() + " " + test.getKey();
result = result == -1 ? -1 : compile();
compileString = oldCompileString;
}
}
}
catch (Exception e)
{
System.err.println("compilation exception");
e.printStackTrace();
result = - 1;
}
return result == 0;
}
/**
* Returns true if the String argument passed is in the format of a
* compiler summary of errors in a folder.
* @param x the String to inspect
* @return true if the String is in the correct format
*/
private static boolean isCompileSummary(String x)
{
if (numCompileFailsInFolder == 1)
return x.startsWith("1 problem (1 error)");
else
{
String s = "" + numCompileFailsInFolder + " problems (";
s += "" + numCompileFailsInFolder + " errors)";
return x.startsWith(s);
}
}
/**
* A class that implements Runnable and simply reads from an InputStream
* and redirects it to System.err.
* @author Anthony Balkissoon abalkiss at redhat dot com
*
*/
private static class ErrorStreamPrinter
implements Runnable
{
private static BufferedReader in;
private Thread printerThread;
public ErrorStreamPrinter(InputStream input)
{
in = new BufferedReader
(new InputStreamReader(runnerProcess.getErrorStream()));
printerThread = new Thread(this);
}
/**
* Starts the thread that reads and redirects input.
*
*/
public void start()
{
printerThread.start();
}
/**
* Reads from the error stream of the runnerProcess and redirects to
* System.err.
*/
public void run()
{
try
{
while (true)
{
String temp = in.readLine();
if (temp == null)
{
// This means the RunnerProcess was restarted (because of a
// timeout) and we need to restart the error stream writer.
restartESP = true;
break;
}
System.err.println(temp);
}
}
catch (IOException ioe)
{
// Restart the runner error stream printer upon running
// the next test
restartESP = true;
}
}
}
/**
* This class is used for our timer to cancel tests that have hung.
* @author Anthony Balkissoon abalkiss at redhat dot com
*
*/
private static class TimeoutWatcher implements Runnable
{
private long millisToWait;
private Thread watcherThread;
private boolean started;
private boolean loop = true;
private boolean shouldContinue = true;
private final Process runnerProcess;
/**
* Creates a new TimeoutWatcher that will wait for <code>millis</code>
* milliseconds once started.
* @param millis the number of milliseconds to wait before declaring the
* test as hung
*/
public TimeoutWatcher(long millis, Process runnerProcess)
{
millisToWait = millis;
watcherThread = new Thread(this);
started = false;
this.runnerProcess = runnerProcess;
}
/**
* Stops the run() method.
*
*/
public synchronized void stop()
{
shouldContinue = false;
notify();
}
/**
* Reset the counter and wait another <code>millisToWait</code>
* milliseconds before declaring the test as hung.
*/
public synchronized void reset()
{
if (!started)
{
watcherThread.start();
started = true;
}
else
{
loop = true;
notify();
}
}
public synchronized void run()
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
while (loop && shouldContinue)
{
// We set loop to false here, it will get reset to true if
// reset() is called from the main Harness thread.
loop = false;
long start = System.currentTimeMillis();
long waited = 0;
while (waited < millisToWait)
{
try
{
wait(millisToWait - waited);
}
catch (InterruptedException ie)
{
// ignored.
}
waited = System.currentTimeMillis() - start;
}
}
if (shouldContinue)
{
// The test is hung, destroy and restart the RunnerProcess.
try
{
this.runnerProcess.destroy();
this.runnerProcess.getInputStream().close();
this.runnerProcess.getErrorStream().close();
this.runnerProcess.getOutputStream().close();
}
catch (IOException e)
{
System.err.println("Could not close the interprocess pipes.");
System.exit(- 1);
}
}
}
}
/**
* This tiny class is used for finding the bootclasspath of the VM used
* to run the tests.
* @author Anthony Balkissoon abalkiss at redhat dot com
*
*/
public static class DetectBootclasspath
{
/**
* Look in the system properties for the bootclasspath of the VM and return
* it to the process that forked this process via the System.out stream.
*
* Tries first to get the property "sun.boot.class.path", if there is none,
* then it tries "java.boot.class.path", if there is still no match, looks
* to see if there is a unique property key that contains "boot.class.path".
* If this fails too, prints an error message.
*/
public static void main (String[] args)
{
String result = "BCP_FINDER:";
// Sun's VM stores the bootclasspath as "sun.boot.class.path".
String temp = System.getProperty("sun.boot.class.path");
if (temp == null)
// JamVM stores it as "boot.class.path"
temp = System.getProperty("java.boot.class.path");
if (temp == null)
{
String[] s = (String[])(System.getProperties().keySet().toArray());
int count = 0;
String key = null;
for (int i = 0; i < s.length; i++)
{
if (s[i].indexOf("boot.class.path") != -1)
{
count ++;
key = s[i];
}
}
if (count == 1)
temp = System.getProperty(key);
else
{
System.err.println("WARNING: Cannot auto-detect the " +
"bootclasspath for your VM, please file a bug report" +
" specifying which VM you are testing.");
temp = "can't_find_bcp_";
}
}
System.out.println(result + temp);
}
}
/**
* A class used as a PrintWriter for the compiler to send error output to.
* This class formats the output and also affects the test run by parsing
* the output.
* @author Anthony Balkissoon abalkiss at redhat dot com
*
*/
private class CompilerErrorWriter extends PrintWriter
{
public CompilerErrorWriter(OutputStream out)
{
super(out);
}
/**
* This method is overridden for several reasons. It formats
* text to fit into the test report, adds tests that fail to compile
* to the list of tests to exclude from the run, prints header
* information for the failing tests, and properly increments
* the total test number and total failing test number.
*
* Basically, this method now parses the text its passed and causes
* side effects. It (sometimes) prints that text as well, after
* formatting and indenting.
*/
public void println(String x)
{
// Ignore incorrect classpath errors, since we detect this
// automatically, a proper classpath should be found in
// addition to any incorrect ones.
if (x.startsWith("incorrect classpath:") ||
x.startsWith("----------"))
return;
// Look for "gnu/testlet" to indicate we might be talking about a
// new file.
int loc = x.indexOf("gnu/testlet");
if (loc != -1)
{
String temp = x.substring(loc);
String shortName =
stripPrefix(temp).replace(File.separatorChar, '.');
if (shortName.endsWith(".java"))
shortName =
shortName.substring(0, shortName.length() - 5);
// Check if the name is different than the last file with
// compilation errors, so we're not dealing with multiple errors
// in one file.
if (!lastFailingCompile.equals(shortName))
{
// Print out a message saying the test failed.
if (verbose)
super.println("TEST: " + shortName
+ "\n FAIL: compilation errors:");
else
super.println("FAIL: " + shortName
+ "\n compilation errors:");
// Increment and set the relevant variables.
numCompileFailsInFolder = 1;
excludeTests.add(temp);
total_test_fails++;
total_tests++;
lastFailingCompile = shortName;
}
else
numCompileFailsInFolder++;
return;
}
// Get the line number from the compiler output and print
// it out to look like our other line numbers for failures.
loc = x.indexOf("(at line ");
if (loc != -1)
{
int endBracket = x.indexOf(')', loc);
String line = x.substring(loc + 4, endBracket) + ":";
// Print the line numbers with appropriate indentation.
if (verbose)
super.println(" "+line);
else
super.println(" "+line);
// Print the line from the test that caused the problem.
super.println(x.substring(endBracket + 2));
return;
}
// Print the lines with appropriate indentation.
if (verbose)
super.println(" " + x);
else
super.println(" " + x);
}
/**
* This method is overridden so that the compiler summary isn't
* printed out and also so that if the output is verbose we print
* our own summary.
*/
public void print(String x)
{
if (isCompileSummary(x))
{
if (verbose)
super.println("TEST FAILED: compile failed for "
+ lastFailingCompile);
}
else
super.print(x);
}
}
/**
* Reads from one stream and writes this to another. This is used to pipe
* the input (System.in) from the outside process to the test process.
*/
private static class InputPiperThread
extends Thread
{
InputStream in;
OutputStream out;
InputPiperThread(InputStream i, OutputStream o)
{
in = i;
out = o;
}
public void run()
{
int ch = 0;
do
{
try
{
if (in.available() > 0)
{
ch = in.read();
if (ch != '\n') // Skip the trailing newline.
out.write(ch);
out.flush();
}
else
Thread.sleep(200);
}
catch (IOException ex)
{
ex.printStackTrace();
}
catch (InterruptedException ex)
{
ch = -1; // Jump outside.
}
} while (ch != -1);
}
}
}
|