package org.apache.torque;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.util.Map;

import org.apache.torque.adapter.Adapter;
import org.apache.torque.adapter.PostgresAdapter;
import org.apache.torque.dsfactory.DataSourceFactory;

/**
 * Tests the TorqueInstance Class.
 *
 * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a>
 * @version $Id: TorqueInstanceTest.java 1355228 2012-06-29 03:38:08Z tfischer $
 */
public class TorqueInstanceTest extends BaseTestCase
{
    /** The name of the "default" dataSourceFactory. */
    private static final String DEFAULT_NAME = "default";

    /** The name of the "postgresql" dataSourceFactory */
    private static final String POSTGRESQL_NAME = "postgresql";

    /** The name of the "mysql" dataSourceFactory */
    private static final String MYSQL_NAME = "mysql";

    /** The name of the "oracle" dataSourceFactory */
    private static final String ORACLE_NAME = "oracle";

    /**
     * Tests whether an external adapter is loaded correctly.
     * @throws Exception if an error occurs during the Test.
     */
    public void testExternalAdapter() throws Exception
    {
        Adapter adapter = Torque.getDatabase(POSTGRESQL_NAME).getAdapter();
        assertNotNull(adapter);
        assertTrue(adapter instanceof PostgresAdapter);
    }

    /**
     * Checks whether a DataSourceFactory with the name
     * <code>DEFAULT_NAME</code> is defined. (TRQS 322)
     * @throws Exception if an error occurs during the Test.
     */
    public void testDefaultDataSourceFactory() throws Exception
    {
        DataSourceFactory defaultDataSourceFactory
                = Torque.getInstance().getDataSourceFactory(DEFAULT_NAME);
        assertNotNull(
                "The DataSourceFactory for Key "
                + DEFAULT_NAME
                + " should not be null",
                defaultDataSourceFactory);
        DataSourceFactory postgresqlDataSourceFactory
                = Torque.getInstance().getDataSourceFactory(POSTGRESQL_NAME);
        assertSame("The default DataSourceFactory "
                + "and the postgresql DataSourceFactory "
                + "are not the same object",
                defaultDataSourceFactory,
                postgresqlDataSourceFactory);
    }

    /**
     * Tests whether the databaseInfo objects are filled correctly.
     * @throws Exception if an error occurs during the Test.
     */
    public void testDatabases() throws Exception
    {
        Map<String, Database> databases = Torque.getDatabases();
        // check whether all expected databases are contained in the Map
        assertEquals(
                "Databases should contain 4 Databases, not "
                    + databases.size(),
                4,
                databases.size());

        // check that the default database and the turbine database
        // refer to the same object
        Database defaultDatabase = Torque.getDatabase(DEFAULT_NAME);
        Database postgresqlDatabase = Torque.getDatabase(POSTGRESQL_NAME);

        assertNotSame("The default database and the turbine database "
                        + "are the same object",
                    defaultDatabase,
                    postgresqlDatabase);
    }

    /**
     * Tests whether the schema information in the databases is filled
     * correctly.
     */
    public void testSchemata() throws Exception
    {
        Torque.shutdown();
        Torque.init("src/test/resources/torque-schematest.properties");

        Map<String, Database> databases = Torque.getDatabases();

        assertEquals(
                "myschema",
                databases.get(MYSQL_NAME).getSchema());
        assertEquals(
                "defaultschema",
                databases.get(POSTGRESQL_NAME).getSchema());
        assertEquals(
                "orschema2",
                databases.get(ORACLE_NAME).getSchema());
    }

    public void testShutdown() throws Exception
    {
        // because we have not properly initialized the DataSourceFactory,
        // closing the DatasourceFactory down would result in an error.
        // So we have to remove the reference to the DatasourceFactory.
        Torque.getDatabase(POSTGRESQL_NAME).setDataSourceFactory(null);

        Torque.shutdown();
        assertFalse("Torque.isInit() should return false after shutdown",
                Torque.isInit());
        try
        {
            Torque.getDatabases();
            fail("Torque.getDatabases() should throw an Exception "
                    + "after shutdown");
        }
        catch (TorqueException e)
        {
            assertEquals("Torque is not initialized.", e.getMessage());
        }
    }
}
