<commit_before>/*
 * Copyright (c) 2005-2009 Grameen Foundation USA
 * All rights reserved.
 *
 * 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.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.framework.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.mifos.config.AccountingRules;
import org.mifos.config.ConfigLocale;
import org.mifos.config.ConfigurationManager;
import org.mifos.config.FiscalCalendarRules;
import org.mifos.config.Localization;
import org.mifos.framework.security.authorization.AuthorizationManager;
import org.mifos.framework.security.authorization.HierarchyManager;
import org.mifos.framework.util.helpers.FilePaths;
import org.mifos.service.test.TestMode;
import org.mifos.service.test.TestingService;
import org.mifos.core.MifosRuntimeException;
import org.mifos.core.MifosException;

/**
 * Encapsulates all logic necessary to have the application behave differently
 * during acceptance and integration tests.
 */
public class StandardTestingService implements TestingService {
    private final ConfigurationLocator configurationLocator;

    public StandardTestingService() {
        configurationLocator = new ConfigurationLocator();
    }

    public TestMode getTestMode() {
        String testMode = System.getProperty(TEST_MODE_SYSTEM_PROPERTY);
        if ("acceptance".equals(testMode)) {
            return TestMode.ACCEPTANCE;
        } else if ("integration".equals(testMode)) {
            return TestMode.INTEGRATION;
        } else {
            // "main" means we are *not* running in a test mode
            return TestMode.MAIN;
        }
    }

    public void setTestMode(TestMode newMode) {
        String modeString = newMode.toString().toLowerCase();
        System.setProperty(TEST_MODE_SYSTEM_PROPERTY, modeString);
    }

    public Properties getDatabaseConnectionSettings() throws IOException {
        String defaultSettingsFilename = getDefaultSettingsFilename(getTestMode());

        File defaultsFile = configurationLocator.getFile(defaultSettingsFilename);
        Properties mifosSpecific = new Properties();
        mifosSpecific.load(new FileInputStream(defaultsFile));

        Properties overrides = new Properties();

        try {
            File overridesFile = configurationLocator.getFile(FilePaths.LOCAL_CONFIGURATION_OVERRIDES);
            overrides.load(new FileInputStream(overridesFile));
        } catch (FileNotFoundException e) {
            // basically ignore; no matter if they don't have local overrides
            System.err.println("StandardTestingService: local overrides not found.");
        }

        mifosSpecific.putAll(overrides);

        return translateToHibernate(mifosSpecific, getTestMode());
    }

    public String[] getAllSettingsFilenames() throws IOException {
        ArrayList<String> settingsFilenames = new ArrayList<String>();
        settingsFilenames.add(configurationLocator.getFilePath(getDefaultSettingsFilename(getTestMode())));
        try {
            String optionalOverrides = configurationLocator.getFilePath(FilePaths.LOCAL_CONFIGURATION_OVERRIDES);
            settingsFilenames.add(optionalOverrides);
        } catch (FileNotFoundException e) {
            // basically ignore; no matter if they don't have local overrides
            System.err.println("StandardTestingService: no local overrides in use.");
        }
        return settingsFilenames.toArray(new String[] {});
    }

    public String getDefaultSettingsFilename(TestMode testMode) {
        String defaultSettingsFilename = null;
        if (testMode == TestMode.MAIN) {
            defaultSettingsFilename = FilePaths.DATABASE_CONFIGURATION;
        } else if (testMode == TestMode.ACCEPTANCE) {
            defaultSettingsFilename = FilePaths.ACCEPTANCE_DATABASE_CONFIGURATION;
        } else if (testMode == TestMode.INTEGRATION) {
            defaultSettingsFilename = FilePaths.INTEGRATION_DATABASE_CONFIGURATION;<commit_msg>Use primordial logger instead of System.*.println following pattern in ConfigurationLocator.java and Upgrade.java<commit_after>/*
 * Copyright (c) 2005-2009 Grameen Foundation USA
 * All rights reserved.
 *
 * 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.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.framework.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

import org.mifos.config.AccountingRules;
import org.mifos.config.ConfigLocale;
import org.mifos.config.ConfigurationManager;
import org.mifos.config.FiscalCalendarRules;
import org.mifos.config.Localization;
import org.mifos.framework.components.logger.LoggerConstants;
import org.mifos.framework.security.authorization.AuthorizationManager;
import org.mifos.framework.security.authorization.HierarchyManager;
import org.mifos.framework.util.helpers.FilePaths;
import org.mifos.service.test.TestMode;
import org.mifos.service.test.TestingService;
import org.mifos.core.MifosRuntimeException;
import org.mifos.core.MifosException;

/**
 * Encapsulates all logic necessary to have the application behave differently
 * during acceptance and integration tests.
 */
public class StandardTestingService implements TestingService {
    private static final Logger LOG = Logger.getLogger(LoggerConstants.FRAMEWORKLOGGER);
    private final ConfigurationLocator configurationLocator;

    public StandardTestingService() {
        configurationLocator = new ConfigurationLocator();
    }

    public TestMode getTestMode() {
        String testMode = System.getProperty(TEST_MODE_SYSTEM_PROPERTY);
        if ("acceptance".equals(testMode)) {
            return TestMode.ACCEPTANCE;
        } else if ("integration".equals(testMode)) {
            return TestMode.INTEGRATION;
        } else {
            // "main" means we are *not* running in a test mode
            return TestMode.MAIN;
        }
    }

    public void setTestMode(TestMode newMode) {
        String modeString = newMode.toString().toLowerCase();
        System.setProperty(TEST_MODE_SYSTEM_PROPERTY, modeString);
    }

    public Properties getDatabaseConnectionSettings() throws IOException {
        String defaultSettingsFilename = getDefaultSettingsFilename(getTestMode());

        File defaultsFile = configurationLocator.getFile(defaultSettingsFilename);
        Properties mifosSpecific = new Properties();
        mifosSpecific.load(new FileInputStream(defaultsFile));

        Properties overrides = new Properties();

        try {
            File overridesFile = configurationLocator.getFile(FilePaths.LOCAL_CONFIGURATION_OVERRIDES);
            overrides.load(new FileInputStream(overridesFile));
        } catch (FileNotFoundException e) {
            // basically ignore; no matter if they don't have local overrides
            LOG.info("local overrides not found.");
        }

        mifosSpecific.putAll(overrides);

        return translateToHibernate(mifosSpecific, getTestMode());
    }

    public String[] getAllSettingsFilenames() throws IOException {
        ArrayList<String> settingsFilenames = new ArrayList<String>();
        settingsFilenames.add(configurationLocator.getFilePath(getDefaultSettingsFilename(getTestMode())));
        try {
            String optionalOverrides = configurationLocator.getFilePath(FilePaths.LOCAL_CONFIGURATION_OVERRIDES);
            settingsFilenames.add(optionalOverrides);
        } catch (FileNotFoundException e) {
            // basically ignore; no matter if they don't have local overrides
            LOG.info("no local overrides in use.");
        }
        return settingsFilenames.toArray(new String[] {});
    }

    public String getDefaultSettingsFilename(TestMode testMode) {
        String defaultSettingsFilename = null;
        if (testMode == TestMode.MAIN) {
            defaultSettingsFilename = FilePaths.DATABASE_CONFIGURATION;
        } else if (testMode == TestMode.ACCEPTANCE) {
            defaultSettingsFilename = FilePaths.ACCEPTANCE_DATABASE_CONFIGURATION;
        } else if (testMode == TestMode.INTEGRATION) {
            defaultSettingsFilename = FilePaths.INTEGRATION_DATABASE_CONFIGURATION;
{'fraction_non_alphanumeric': 0.08226407284768211, 'fraction_numerical': 0.0028973509933774835, 'mean_word_length': 3.671338811019816, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3112614', 'n_tokens_mistral': 2526, 'n_tokens_neox': 2497, 'n_words': 658}