Util.java :  » Report » openi » org » openi » test » Java Open Source

Java Open Source » Report » openi 
openi » org » openi » test » Util.java
/*********************************************************************************
 * The contents of this file are subject to the OpenI Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://www.openi.org/docs/LICENSE.txt
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is: OpenI Open Source
 *
 * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
 * Portions created by Loyalty Matrix, Inc. are
 * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
 *
 * Contributor(s): ______________________________________.
 *
 ********************************************************************************/
package org.openi.test;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.*;
import org.openi.analysis.Analysis;
import org.openi.application.Application;
import org.openi.project.Project;
import org.openi.project.ProjectContext;
import org.openi.project.ProjectUser;
import org.openi.xml.BeanStorage;


/**
 * Util for tests - Isn't this an anti pattern?
 */
public class Util {
  private static Logger logger = Logger.getLogger(Util.class);
    /**
     *
     * @return location of test directory - when tests are run from ant, this property is already set in
     * CVS/product/src/build.xml, see the runtests task (${basedir}/test. If running anywhere else,
     * you can set the system property TESTDIR, which takes precedence.
     */
    public static String findTestDirectory() {
        return System.getProperty("TESTDIR", "./test");
    }

    public static void setupLog4j() {
        PropertyConfigurator.configure(Util.findTestDirectory()
            + "/log4jtest.properties");
    }

  /**
   * 
   */
  public static void initApplicationConfig() {
        String appConfigPath = Util.findTestDirectory() +
        "/../conf/application.xml";
        try {
          /*
          File appTest = new File(appConfigPath);
          logger.debug("config exists? " + appTest.exists());
          logger.debug(appTest.getCanonicalPath());
          */
          new BeanStorage().restoreBeanFromFile(appConfigPath, Application.getInstance());

          // logger.debug(Application.getInstance().toString());
    } catch (IOException e) {
      // TODO Auto-generated catch block
      logger.error(e);
    }
    
  }

  /**
   * @param string
   * @return
   * @throws IOException
   */
  public static ProjectContext createTestProjectContext(String username) {
        String baseDir = Util.findTestDirectory() + "/projects/";
        return createTestProjectContext(username, "standard");
  }
  
  public static ProjectContext createTestProjectContext(String username, String projectId) {
        String baseDir = Util.findTestDirectory() + "/projects/";
        String projectFile = baseDir + "/" + projectId + "/project.xml";
        ProjectContext context = null;
    try {
      Project project = (Project) new BeanStorage().restoreBeanFromFile(projectFile);
      context= new ProjectContext(project, baseDir, getTestProjectUser(username));
    } catch (IOException e) {
      // TODO Auto-generated catch block
      logger.error(e);
    }
    return context;
  }
  
  public static ProjectUser getTestProjectUser(String userid) {
    ProjectUser user = new ProjectUser(Application.getInstance().getDefaultLocale());
    if(userid == null || "".equals(userid))
      user.setId("mouser");
    else
      user.setId(userid);
    return user;
  }

  public static Analysis restoreAnalysis(String analysisName) 
        throws IOException {
        return (Analysis) new BeanStorage().restoreBeanFromFile(Util.findTestDirectory() +
            "/projects/standard/public/" + analysisName);

  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.