/*
 * Copyright 2016 higherfrequencytrading.com
 *
 * 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
 *
 *     http://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 net.openhft.chronicle.queue;

import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.queue.impl.single.Utils;
import net.openhft.chronicle.wire.WireKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Rule;
import org.junit.rules.*;
import org.junit.runner.Description;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class ChronicleQueueTestBase {
    protected static final Logger LOGGER = LoggerFactory.getLogger(ChronicleQueueTestBase.class);
    private static final boolean TRACE_TEST_EXECUTION = Boolean.getBoolean("queue.traceTestExecution");

    // *************************************************************************
    // JUNIT Rules
    // *************************************************************************

    @Rule
    public final TestName testName = new TestName();

    @Rule
    public final TemporaryFolder folder = new TemporaryFolder(new File(System.getProperty("java.io.tmpdir")));

    @Rule
    public final ErrorCollector errorCollector = new ErrorCollector();

    @Rule
    public TestRule watcher = new TestWatcher() {
        @Override
        protected void starting(Description description) {
            if (TRACE_TEST_EXECUTION) {
                LOGGER.info("Starting test: {}.{}",
                        description.getClassName(),
                        description.getMethodName()
                );
            }
        }
    };

    // *************************************************************************
    //
    // *************************************************************************

    static void deleteDir(@NotNull String... dirs) {
        for (String dir : dirs) {
            try {
                deleteDir(new File(dir));
            } catch (Exception e) {
                Jvm.warn().on(ChronicleQueueTestBase.class, e);
            }
        }

    }

    public static void deleteDir(@NotNull File dir) {
        if (dir.isDirectory()) {
            @Nullable File[] files = dir.listFiles();
            if (files != null) {
                for (@NotNull File file : files) {
                    if (file.isDirectory()) {
                        deleteDir(file);
                    } else
                        //noinspection ResultOfMethodCallIgnored
                        file.delete();
                }
            }
        }

        dir.delete();
    }

    protected File getTmpDir() {
        return Utils.tempDir(testName.getMethodName());
    }


    public enum TestKey implements WireKey {
        test, test2
    }

}

@@ -641,16 +641,62 @@
 queue;%0A%0A
+import net.openhft.chronicle.bytes.BytesUtil;%0A
 import n
@@ -904,24 +904,48 @@
 s.Nullable;%0A
+import org.junit.After;%0A
 import org.j
@@ -3233,16 +3233,116 @@
 %0A    %7D%0A%0A
+    @After%0A    public void checkRegisteredBytes() %7B%0A        BytesUtil.checkRegisteredBytes();%0A    %7D%0A
 %0A    pub
@@ -3405,11 +3405,10 @@
 2%0A    %7D%0A
-%0A
 %7D%0A

Clean up handling of Bytes