/**
 *Created:         2005-3-31 9:30:42
 * Last Modified:   2005-3-31/2005-3-31
 * Description:
 *      class AbstractTest.java
 */
package com.trs;

import java.io.File;
import java.util.ArrayList;
import java.util.Properties;

import junit.framework.TestCase;

import org.apache.log4j.Logger;

import com.trs.cms.ContextHelper;
import com.trs.cms.auth.persistent.Group;
import com.trs.cms.auth.persistent.Groups;
import com.trs.cms.auth.persistent.User;
import com.trs.components.wcm.content.persistent.Channel;
import com.trs.components.wcm.content.persistent.WebSite;
import com.trs.infra.ServerControl;
import com.trs.infra.common.WCMException;
import com.trs.infra.config.domain.ConfigHelper;
import com.trs.infra.util.CMyFile;
import com.trs.infra.util.WCMDiscoveryFactory;
import com.trs.infra.util.store.FileService;
import com.trs.infra.util.store.FileStore;

/**
 * Title: TRS 内容协作平台（TRS WCM） <BR>
 * Description: <BR>
 * 测试用例的父类 <BR>
 * Copyright: Copyright (c) 2004-2005 TRS信息技术有限公司 <BR>
 * Company: TRS信息技术有限公司(www.trs.com.cn) <BR>
 * 
 * @author TRS信息技术有限公司
 * @version 1.0
 */
public class TRSWCMBaseTest extends TestCase {
    private Logger s_logger = Logger.getLogger(TRSWCMBaseTest.class);

    protected User m_oLoginUser = null;

    protected String m_sCurrUserName = "admin";

    public TRSWCMBaseTest(String _sName) {
        super(_sName);
    }

    public TRSWCMBaseTest() {
        super();
    }

    private static String getWCMLibs() {
        Properties sysProperties = System.getProperties();

        String[] pClassPaths = sysProperties.getProperty("java.class.path")
                .split(";");
        for (int i = 0; i < pClassPaths.length; i++) {
            String sClassPath = pClassPaths[i];
            File file = new File(sClassPath);
            if (file.isFile() && (sClassPath.indexOf("trswcmv6.jar") >= 0)) {
                return CMyFile.extractFilePath(file.getAbsolutePath());
            }
        }

        return null;

    }

    /*
     * (non-Javadoc)
     * 
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();

        // 设置当前在Eclipse中运行
        ConfigHelper.RUN_IN_ECLISPE = true;

        ArrayList arClassPaths = ConfigHelper.getClassPaths();
        // File[] pClassesDirs = TestHelper.getClassFilePaths();
        FileStore[] pClassesDirs = new FileService[arClassPaths.size()];
		for (int i = 0; i < pClassesDirs.length; i++) {
			pClassesDirs[i] = new FileService((String) arClassPaths.get(i));
		}

		FileService fileWCMLib = new FileService("./libs/WCM/");
        String sExtLibPath = getWCMLibs();
        System.out.println("sExtLibPath:" + sExtLibPath);
        if (sExtLibPath != null) {
            fileWCMLib = new FileService(sExtLibPath);
        }
        // System.out.println("the lib paht exists:" + fileWCMLib.exists());
        FileService[] pLibDirs = { fileWCMLib };
        WCMDiscoveryFactory.initDiscovery(pLibDirs, pClassesDirs);

        ServerControl control = ServerControl.createInstance();
        control.startup();
        // wenyh@2005-7-7 10:41:41 add comment:modify:for ant test...
        m_oLoginUser = User.findByName(m_sCurrUserName);
        ContextHelper.initContext(m_oLoginUser);
    }

    /*
     * (non-Javadoc)
     * 
     * @see junit.framework.TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        super.tearDown();

        ServerControl control = ServerControl.createInstance();
        control.shutdown();
    }

    protected void descTest(String _sTestDesc) {
        this.setName(_sTestDesc);
        s_logger.info(_sTestDesc);
    }

    protected Channel getChannelById(int _nChannelId) {
        Channel aChannel = null;
        try {
            aChannel = Channel.findById(_nChannelId);
        } catch (Exception ex) {
            s_logger.error("测试[getChannelById]出现异常！", ex);
            fail("测试[getChannelById]出现异常！");
        }
        if (aChannel == null) {
            s_logger.error("[getChannelById]所需的测试数据不正确！ChannelId:["
                    + _nChannelId + "]");
            fail("[getChannelById]所需的测试数据不正确！ChannelId:[" + _nChannelId + "]");
        }
        return aChannel;
    }

    /**
     * @param _nSiteId
     * @return
     * @throws WCMException
     */
    protected WebSite findSiteById(int _nSiteId) throws WCMException {
        WebSite currSite = WebSite.findById(_nSiteId);
        if (currSite == null)
            fail("指定的Site[" + currSite + "]不存在！");
        return currSite;
    }

    /**
     * @param _nUserId
     * @return
     * @throws WCMException
     */
    protected User findUserById(int _nUserId) throws WCMException {
        User currUser = User.findById(_nUserId);
        if (currUser == null)
            fail("指定的User[" + _nUserId + "]不存在！");
        return currUser;
    }

    protected Group findGroup(User _user) throws WCMException {
        Groups groups = _user.getGroups();
        Group group = null;
        for (int i = 0, nSize = groups.size(); i < nSize; i++) {
            group = (Group) groups.getAt(i);
            if (group == null)
                continue;

            break;
        }

        if (group == null) {
            fail("用户[" + _user.getId() + "]没有所属组织！");
        }

        return group;
    }
}