/**
 * Copyright (c) 2010-2012 EBM WebSourcing, 2012-2015 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library 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 Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see <http://www.gnu.org/licenses/>
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.log;

import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.logging.LogManager;

import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;

public abstract class AbstractConfLog {

    @Rule
    public TemporaryFolder tempFolder = new TemporaryFolder();

    /**
     * Default value of the property "basedir" used by unit test, relative to
     * the root dir.
     */
    protected final static String BASEDIR_DEFAULT_TEST_VALUE = "my/test/basedir";

    /**
     * Default value of the property "flows-subdir" used by unit test, relative
     * to the root basedir.
     */
    protected final static String FLOWSUBDIR_DEFAULT_TEST_VALUE = "my/test/subdir";

    /**
     * Default value of the property "logFilename" used by unit test.
     */
    protected final static String LOGFILENAME_DEFAULT_TEST_VALUE = "myTestLogFilename";

    protected final static String UNRESOLVABLE_SUBDIR = "subDirTest/${unresolved_property}";

    protected final static String SUBDIR = "subDirTest/testDir";

    @Before
    public void before() {
        LogManager.getLogManager().reset();
    }

    protected interface ConfPropertiesSetter {

        public void customizeProperties(final Properties confProperties) throws Exception;

    }

    protected File generateLogConfFile(final ConfPropertiesSetter confPropertiesSetter)
            throws Exception {

        final InputStream is1 = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("logConf.properties");
        assertNotNull("Log file configuration not found", is1);
        try {
            final Properties confProperties = new Properties();
            confProperties.load(is1);
            confPropertiesSetter.customizeProperties(confProperties);
            final File logConfFile = this.tempFolder.newFile("logConfFile.properties");
            final OutputStream os = new FileOutputStream(logConfFile);
            try {
                confProperties.store(os, null);
            } finally {
                os.close();
            }
            return logConfFile;
        } finally {
            is1.close();
        }
    }

}
