-
Notifications
You must be signed in to change notification settings - Fork 2
/
Harness.java
1881 lines (1710 loc) · 61.9 KB
/
Harness.java
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
// Copyright (c) 2006, 2007, 2012 Red Hat, Inc.
// Written by Anthony Balkissoon <[email protected]>
// 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("//"))
{