/*  PAT: Persistent Applications Toolkit (patsystem.sf.net)
 *  Copyright (C) 2004, 2005  Tomasz Nazar, nthx at irc dot pl
 *
 *  This 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 of the License, or (at your option) any later version.
 *
 *  This 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 library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Full version of the license is /docs/LICENSE.txt
 */
package org.nthx.pat.tests;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Arrays;

/** 
 *  
 *  
 *  @version $Id: PatTestCase.java 3725 2005-06-09 23:57:03Z nthx $
 *  @author nthx@users.sourceforge.net
 */
public class PatTestCase
        extends TestCase
{

    public PatTestCase(String arg)
    {
        super(arg);
        getRepoDataAndPrepareAndCleanFilesystem();
    }

    // ----------------------------------------
    // ---- Helper functions ------------------
    // ----------------------------------------
    protected void getRepoDataAndPrepareAndCleanFilesystem()
    {
        logTest.debug("Preparing repo...");
        String repoPath = System.getProperty("repository.dir", "PrevalenceBase");
        
        repository = new File(repoPath);
        if (!repository.exists() && !repository.mkdirs()) 
            throw new AssertionFailedError("Directory doesn't exist "
                    + "and could not be created: " + repoPath);
        if (!repository.isDirectory())
            throw new AssertionFailedError(repository.getAbsolutePath()
                    + " must be directory");
        
        logTest.debug("repositor.dir=" + repoPath);
        logTest.debug("Real file: " + repository.getAbsolutePath());
        
        //System.setProperty("jboss.aop.path", "pat-aop.xml");
    }
    protected String getLastFileFromRepo()
    {
        String[] filesPaths = repository.list();
        Arrays.sort(filesPaths);
        logTest.debug("Got files: " + Arrays.asList(filesPaths));
        return filesPaths[filesPaths.length-1];
    }

    protected void cleanPrevaylersRepo()
    {
        System.gc();

        File[] files = repository.listFiles();
        logTest.warn("Removing all files from: " + repository.getAbsolutePath());
        for (int i = 0; i < files.length; i++)
        {
            File file = files[i];
            file.delete();
        }
        assertEquals("Failed to remove all files from: " + repository.getAbsolutePath()
                     + "\nProbably Windows only problem. Look into docs/problems.html",
                     0, getFilesCountInRepo());
    }

    protected int getFilesCountInRepo()
    {
         return repository.listFiles().length;
    }

    protected Logger logTest = Logger.getLogger("pat");
    private File repository;

}