/*
 * Copyright 2014-2020 Real Logic Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.aeron.test;

import org.agrona.IoUtil;
import org.agrona.collections.Object2ObjectHashMap;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;

import java.io.File;
import java.util.Map;

public class MediaDriverTestWatcher implements TestWatcher, DriverOutputConsumer
{
    private final Map<String, StdOutputFiles> outputFilesByAeronDirectoryName = new Object2ObjectHashMap<>();

    public void testFailed(final ExtensionContext context, final Throwable cause)
    {
        System.out.println("C Media Driver tests failed");
        outputFilesByAeronDirectoryName.forEach((aeronDirectoryName, files) ->
        {
            System.out.println("Media Driver: " + aeronDirectoryName);
            System.out.println("  stdout: " + files.stdout + ", stderr: " + files.stderr);
        });
    }

    public void testSuccessful(final ExtensionContext context)
    {
        outputFilesByAeronDirectoryName.forEach(
            (aeronDirectoryName, files) ->
            {
                IoUtil.delete(files.stdout, false);
                IoUtil.delete(files.stderr, false);
            });
    }

    public void outputFiles(final String aeronDirectoryName, final File stdoutFile, final File stderrFile)
    {
        outputFilesByAeronDirectoryName.put(aeronDirectoryName, new StdOutputFiles(stdoutFile, stderrFile));
    }

    private static final class StdOutputFiles
    {
        private final File stderr;
        private final File stdout;

        private StdOutputFiles(final File stderr, final File stdout)
        {
            this.stderr = stderr;
            this.stdout = stdout;
        }
    }
}

@@ -622,16 +622,50 @@
 .test;%0A%0A
+import io.aeron.archive.TestUtil;%0A
 import o
@@ -1162,32 +1162,99 @@
 le cause)%0A    %7B%0A
+        if (TestMediaDriver.shouldRunCMediaDriver())%0A        %7B%0A    
         System.o
@@ -1296,16 +1296,20 @@
 iled%22);%0A
+    
         
@@ -1379,34 +1379,42 @@
 les) -%3E%0A        
+  
-%7B%0A
+  %7B%0A    
             Syst
@@ -1464,24 +1464,28 @@
 ctoryName);%0A
+    
             
@@ -1563,36 +1563,50 @@
 tderr);%0A        
+  
+  
 %7D);%0A
+        %7D%0A
     %7D%0A%0A    publi
@@ -1657,32 +1657,99 @@
  context)%0A    %7B%0A
+        if (TestMediaDriver.shouldRunCMediaDriver())%0A        %7B%0A    
         outputFi
@@ -1793,16 +1793,20 @@
         
+    
 (aeronDi
@@ -1832,34 +1832,42 @@
  -%3E%0A            
+  
-%7B%0A
+  %7B%0A    
                 
@@ -1910,32 +1910,36 @@
                 
+    
 IoUtil.delete(fi
@@ -1966,28 +1966,42 @@
             
+  
+  
 %7D);%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -2392,19 +2392,19 @@
 File std
-err
+out
 , final 
@@ -2411,19 +2411,19 @@
 File std
-out
+err
 )%0A      

Remove confusing message on system test failure.