/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   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.

====================================================================== */

package org.bodington.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import org.bodington.installation.Installer;
import org.bodington.server.realm.Acl;
import org.bodington.server.realm.User;
import org.bodington.server.realm.Zone;
import org.bodington.server.resources.Resource;
import org.bodington.server.resources.ResourceTreeManager;
import org.bodington.servlet.SetupServlet;

import junit.framework.TestCase;

/**
 * @author buckett
 */
abstract public class BuildingServerTest extends TestCase
{

    /**
     * This Database connection is used for setting up the database
     * and then pulling it down.
     */
    private Connection c;
    
    protected Acl acl;
    protected Resource resource;
    protected Zone zone;
    protected User user;

    protected void setUp() throws Exception
    {
        super.setUp();
        Class.forName("org.hsqldb.jdbcDriver");
        c = DriverManager.getConnection("jdbc:hsqldb:mem:aname", "sa", "");
    
        Installer.createTables(c, Installer.class, null, Installer
            .getSQLSubstitutions("org.hsqldb.jdbcDriver"), false, "", "");
        Properties props = new Properties();
        props.put("sqldatabase.url", "jdbc:hsqldb:mem:aname");
        props.put("sqldatabase.user_name", "sa");
        props.put("sqldatabase.password", "");
        props.put("sqldatabase.driver", "org.hsqldb.jdbcDriver");
        props.put("buildingserver.job_scheduler.enabled", "no");

    
        BuildingServer.createInstance(false, props);
    
        BuildingServer.getInstance().startContext();
        
        acl = new Acl();
        acl.setName("test");
        acl.save();
        
        // As we don't put this in the ResourceTree it is considered the root resource (no parent).
        resource = new Resource();
        resource.setName("test");
        resource.setTitle("My Test");
        resource.setDescription("My Test");
        resource.setIntroduction("My Test");
        resource.setAclId(acl.getPrimaryKey());
        resource.save();
        
        zone = new Zone();
        zone.setName("Test");
        zone.setPrefix("test");
        zone.save();
        
        user = new User();
        user.setZone(zone);
        user.setName("Test");
        user.setSurname("User");
        user.setInitials("A");
        user.save();
        
        // Flush through the root_id as resourceTree is static...
        ResourceTreeManager.getInstance().loadResources();

    }

    protected void tearDown() throws Exception
    {
        super.tearDown();
        BuildingServer.getInstance().endContext();
        BuildingServer.getInstance().shutdown();
        c.prepareStatement("SHUTDOWN").execute();
        c.close();
    }

}
