summaryrefslogtreecommitdiff
path: root/src/main/java/testing/QuietProtocolTestRunner.java
blob: d3b35a160635fe476259562501596f3cf8a0b0f0 (plain)
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
package testing;

import java.io.PrintStream;
import java.io.OutputStream;

/**
 * A test runner that suppresses GUI-related error messages while still showing test results.
 * This provides a cleaner output when running headless tests.
 */
public class QuietProtocolTestRunner {
    
    public static void main(String[] args) {
        // Create a custom PrintStream that filters out specific error messages
        PrintStream originalErr = System.err;
        PrintStream filteredErr = new PrintStream(new FilteredOutputStream(originalErr));
        
        try {
            // Redirect System.err to our filtered stream
            System.setErr(filteredErr);
            
            // Run the actual test runner
            System.out.println("=== DS-Sim Protocol Test Runner (Quiet Mode) ===\n");
            System.out.println("Note: GUI errors are suppressed for cleaner output.\n");
            
            // Pass through any arguments (like -v for verbose)
            ProtocolTestRunnerWithLogs.main(args);
            
        } finally {
            // Restore original error stream
            System.setErr(originalErr);
        }
    }
    
    /**
     * An OutputStream that filters out specific error messages.
     */
    private static class FilteredOutputStream extends OutputStream {
        private final PrintStream target;
        private final StringBuilder buffer = new StringBuilder();
        
        public FilteredOutputStream(PrintStream target) {
            this.target = target;
        }
        
        @Override
        public void write(int b) {
            buffer.append((char) b);
            
            // Check if we have a complete line
            if (b == '\n') {
                String line = buffer.toString();
                
                // Filter out specific GUI-related errors
                if (!line.contains("Component must have a valid peer") &&
                    !line.contains("java.lang.IllegalStateException") &&
                    !line.contains("at java.desktop/") &&
                    !line.contains("at simulator.VSSimulatorVisualization.paint") &&
                    !line.contains("createBufferStrategy") &&
                    !line.contains("FlipBufferStrategy")) {
                    
                    // Pass through other messages
                    target.print(line);
                }
                
                buffer.setLength(0);
            }
        }
        
        @Override
        public void flush() {
            target.flush();
        }
        
        @Override
        public void close() {
            target.close();
        }
    }
}