/*
 * File   : $Source: /alkacon/cvs/opencms/test/org/opencms/test/OpenCmsTestProperties.java,v $
 * Date   : $Date: 2005/04/13 12:46:01 $
 * Version: $Revision: 1.5 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (C) 2002 - 2005 Alkacon Software (http://www.alkacon.com)
 *
 * 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.1 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.
 *
 * For further information about Alkacon Software, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * 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
 */

package org.opencms.test;

import org.opencms.util.CmsFileUtil;
import org.opencms.util.CmsPropertyUtils;

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

import org.apache.commons.collections.ExtendedProperties;

/**
 * Reads and manages the test.properties file.<p>
 * 
 * @author Michael Moossen (m.moossen@alkacon.com)
 * @version $Revision: 1.5 $
 * 
 * @since 6.0.0
 */
public final class OpenCmsTestProperties {

    /**
     * the singleton instance.
     */
    private static OpenCmsTestProperties m_testSingleton;

    /**
     * the path to the test.properties file.
     */
    private String m_basePath;

    /**
     * the path to the data test folder.
     */
    private String m_testDataPath;

    /**
     * the path to the webapp test folder.
     */
    private String m_testWebappPath;
    
    /**
     * the database to use. 
     */
    private String m_dbProduct;

    /**
     * private default constructor.
     */
    private OpenCmsTestProperties() {

        // noop
    }

    /**
     * @return the singleton instance
     */
    public static OpenCmsTestProperties getInstance() {

        if (m_testSingleton == null) {
            throw new RuntimeException("You have to initialize the test properties.");
        }
        return m_testSingleton;
    }

    /**
     * Reads property file test.properties and fills singleton members.<p>
     * 
     * @param basePath the path where to find the test.properties file
     */
    public static void initialize(String basePath) {

        if (m_testSingleton != null) {
            return;
        }

        ExtendedProperties props = null;
        String testPropPath;
        m_testSingleton = new OpenCmsTestProperties();

        m_testSingleton.m_basePath = basePath;
        if (!m_testSingleton.m_basePath.endsWith("/")) {
            m_testSingleton.m_basePath += "/";
        }

        try {
            testPropPath = CmsFileUtil.getResourcePathFromClassloader("test.properties");
            if(testPropPath == null){
              throw new RuntimeException("Test property file ('test.properties') could not be found by context Classloader."); 
            }
            File f = new File(testPropPath);
            if(!f.exists()){
                throw new RuntimeException("Test property file ('test.properties') could not be found. Context Classloader suggested location: "+testPropPath); 
            }
            props = CmsPropertyUtils.loadProperties(testPropPath);
        } catch (IOException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException(e);
        }

        m_testSingleton.m_testDataPath = props.getString("test.data.path");
        m_testSingleton.m_testWebappPath = props.getString("test.webapp.path");
        m_testSingleton.m_dbProduct = props.getString("db.product");
        
    }

    /**
     * @return Returns the path to the test.properties file
     */
    public String getBasePath() {

        return m_basePath;
    }

    /**
     * @return the path to the data test directory
     */
    public String getTestDataPath() {

        return m_testDataPath;
    }

    /**
     * @return the path to the webapp test directory
     */
    public String getTestWebappPath() {

        return m_testWebappPath;
    }

    /**
     * 
     * @return a String identifying the db.product property value of the 'test.properties' value.
     */
    public String getDbProduct() {
    
        return m_dbProduct;
    }
    
}
@@ -126,15 +126,15 @@
 13 1
-2:46:01
+3:25:43
  $%0A 
@@ -153,25 +153,25 @@
 Revision: 1.
-5
+6
  $%0A *%0A * Thi
@@ -1680,17 +1680,17 @@
 sion: 1.
-5
+6
  $%0A * %0A 
@@ -2223,16 +2223,76 @@
 oduct;%0A%0A
+    %0A    private static ExtendedProperties m_configuration;%0A
     /**%0A
@@ -2389,32 +2389,37 @@
  // noop%0A    %7D%0A%0A
+    %0A
     /**%0A     * @
@@ -2986,49 +2986,8 @@
  %7D%0A%0A
-        ExtendedProperties props = null;%0A
     
@@ -3774,21 +3774,31 @@
         
-props
+m_configuration
  = CmsPr
@@ -4007,29 +4007,39 @@
 tDataPath = 
-props
+m_configuration
 .getString(%22
@@ -4099,21 +4099,31 @@
 pPath = 
-props
+m_configuration
 .getStri
@@ -4188,13 +4188,23 @@
 t = 
-props
+m_configuration
 .get
@@ -4870,20 +4870,213 @@
 roduct;%0A
-
     %7D%0A
+%0A%0A    /**%0A     * @return the parsed configuration file ('test.properties')%0A     */%0A    %0A    public ExtendedProperties getConfiguration() %7B%0A    %0A        return m_configuration;%0A    %7D%0A    %0A%0A    %0A
     %0A%7D

Add member m_configuration to avoid double-loading 'test.properties' in OpenCmsTestCase again.