/*
 * Copyright (c) 2014. Vlad Ilyushchenko
 *
 * 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 com.nfsdb.test.tools;

import com.nfsdb.*;
import com.nfsdb.exceptions.JournalConfigurationException;
import com.nfsdb.exceptions.JournalException;
import com.nfsdb.factory.JournalClosingListener;
import com.nfsdb.factory.JournalFactory;
import com.nfsdb.factory.configuration.JournalConfiguration;
import com.nfsdb.factory.configuration.MetadataBuilder;
import com.nfsdb.utils.Files;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import java.util.ArrayList;
import java.util.List;

public class JournalTestFactory extends JournalFactory implements TestRule, JournalClosingListener {

    private final List<Journal> journals = new ArrayList<>();

    public JournalTestFactory(JournalConfiguration configuration) throws JournalConfigurationException {
        super(configuration);
    }

    @Override
    public Statement apply(final Statement base, final Description desc) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                Throwable throwable = null;
                try {
                    Files.deleteOrException(getConfiguration().getJournalBase());
                    Files.mkDirsOrException(getConfiguration().getJournalBase());
                    base.evaluate();
                } catch (Throwable e) {
                    throwable = e;
                } finally {
                    for (Journal journal : journals) {
                        if (journal != null && journal.isOpen()) {
                            journal.setCloseListener(null);
                            journal.close();
                        }
                    }
                    journals.clear();
                    Files.deleteOrException(getConfiguration().getJournalBase());
                }

                if (throwable != null) {
                    throw throwable;
                }
            }
        };
    }

    @Override
    public <T> JournalBulkReader<T> bulkReader(JournalKey<T> key) throws JournalException {
        JournalBulkReader<T> reader = super.bulkReader(key);
        journals.add(reader);
        reader.setCloseListener(this);
        return reader;
    }

    @Override
    public <T> Journal<T> reader(JournalKey<T> key) throws JournalException {
        Journal<T> result = super.reader(key);
        journals.add(result);
        result.setCloseListener(this);
        return result;
    }

    @Override
    public <T> JournalBulkWriter<T> bulkWriter(JournalKey<T> key) throws JournalException {
        JournalBulkWriter<T> writer = super.bulkWriter(key);
        journals.add(writer);
        writer.setCloseListener(this);
        return writer;
    }

    @Override
    public <T> JournalWriter<T> writer(JournalKey<T> key) throws JournalException {
        JournalWriter<T> writer = super.writer(key);
        journals.add(writer);
        writer.setCloseListener(this);
        return writer;
    }

    public <T> JournalWriter<T> writer(MetadataBuilder<T> b) throws JournalException {
        JournalWriter<T> writer = super.writer(b);
        journals.add(writer);
        writer.setCloseListener(this);
        return writer;
    }

    @Override
    public boolean closing(Journal journal) {
        journals.remove(journal);
        return true;
    }
}
@@ -17,16 +17,21 @@
 (c) 2014
+-2015
 . Vlad I
@@ -42,16 +42,16 @@
 hchenko%0A
-
  *%0A * Li
@@ -904,24 +904,80 @@
 figuration;%0A
+import com.nfsdb.factory.configuration.JournalMetadata;%0A
 import com.n
@@ -2902,246 +2902,8 @@
  %7D%0A%0A
-    @Override%0A    public %3CT%3E Journal%3CT%3E reader(JournalKey%3CT%3E key) throws JournalException %7B%0A        Journal%3CT%3E result = super.reader(key);%0A        journals.add(result);%0A        result.setCloseListener(this);%0A        return result;%0A    %7D%0A%0A
     
@@ -3156,32 +3156,685 @@
  writer;%0A    %7D%0A%0A
+    @Override%0A    public boolean closing(Journal journal) %7B%0A        journals.remove(journal);%0A        return true;%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    @Override%0A    public Journal reader(JournalMetadata metadata) throws JournalException %7B%0A        Journal reader = new Journal(metadata, metadata.getKey());%0A        journals.add(reader);%0A        reader.setCloseListener(this);%0A        return reader;%0A    %7D%0A%0A    @Override%0A    public %3CT%3E Journal%3CT%3E reader(JournalKey%3CT%3E key) throws JournalException %7B%0A        Journal%3CT%3E result = super.reader(key);%0A        journals.add(result);%0A        result.setCloseListener(this);%0A        return result;%0A    %7D%0A%0A
     @Override%0A  
@@ -4266,32 +4266,32 @@
 Listener(this);%0A
+
         return w
@@ -4306,128 +4306,6 @@
    %7D
-%0A%0A    @Override%0A    public boolean closing(Journal journal) %7B%0A        journals.remove(journal);%0A        return true;%0A    %7D
 %0A%7D

test factory fix