001: /*********************************************************************************
002: * The contents of this file are subject to the OpenI Public License Version 1.0
003: * ("License"); You may not use this file except in compliance with the
004: * License. You may obtain a copy of the License at
005: * http://www.openi.org/docs/LICENSE.txt
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is: OpenI Open Source
012: *
013: * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
014: * Portions created by Loyalty Matrix, Inc. are
015: * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
016: *
017: * Contributor(s): ______________________________________.
018: *
019: ********************************************************************************/package org.openi.test;
020:
021: import java.io.File;
022: import java.io.IOException;
023:
024: import org.apache.log4j.*;
025: import org.openi.analysis.Analysis;
026: import org.openi.application.Application;
027: import org.openi.project.Project;
028: import org.openi.project.ProjectContext;
029: import org.openi.project.ProjectUser;
030: import org.openi.xml.BeanStorage;
031:
032: /**
033: * Util for tests - Isn't this an anti pattern?
034: */
035: public class Util {
036: private static Logger logger = Logger.getLogger(Util.class);
037:
038: /**
039: *
040: * @return location of test directory - when tests are run from ant, this property is already set in
041: * CVS/product/src/build.xml, see the runtests task (${basedir}/test. If running anywhere else,
042: * you can set the system property TESTDIR, which takes precedence.
043: */
044: public static String findTestDirectory() {
045: return System.getProperty("TESTDIR", "./test");
046: }
047:
048: public static void setupLog4j() {
049: PropertyConfigurator.configure(Util.findTestDirectory()
050: + "/log4jtest.properties");
051: }
052:
053: /**
054: *
055: */
056: public static void initApplicationConfig() {
057: String appConfigPath = Util.findTestDirectory()
058: + "/../conf/application.xml";
059: try {
060: /*
061: File appTest = new File(appConfigPath);
062: logger.debug("config exists? " + appTest.exists());
063: logger.debug(appTest.getCanonicalPath());
064: */
065: new BeanStorage().restoreBeanFromFile(appConfigPath,
066: Application.getInstance());
067:
068: // logger.debug(Application.getInstance().toString());
069: } catch (IOException e) {
070: // TODO Auto-generated catch block
071: logger.error(e);
072: }
073:
074: }
075:
076: /**
077: * @param string
078: * @return
079: * @throws IOException
080: */
081: public static ProjectContext createTestProjectContext(
082: String username) {
083: String baseDir = Util.findTestDirectory() + "/projects/";
084: return createTestProjectContext(username, "standard");
085: }
086:
087: public static ProjectContext createTestProjectContext(
088: String username, String projectId) {
089: String baseDir = Util.findTestDirectory() + "/projects/";
090: String projectFile = baseDir + "/" + projectId + "/project.xml";
091: ProjectContext context = null;
092: try {
093: Project project = (Project) new BeanStorage()
094: .restoreBeanFromFile(projectFile);
095: context = new ProjectContext(project, baseDir,
096: getTestProjectUser(username));
097: } catch (IOException e) {
098: // TODO Auto-generated catch block
099: logger.error(e);
100: }
101: return context;
102: }
103:
104: public static ProjectUser getTestProjectUser(String userid) {
105: ProjectUser user = new ProjectUser(Application.getInstance()
106: .getDefaultLocale());
107: if (userid == null || "".equals(userid))
108: user.setId("mouser");
109: else
110: user.setId(userid);
111: return user;
112: }
113:
114: public static Analysis restoreAnalysis(String analysisName)
115: throws IOException {
116: return (Analysis) new BeanStorage().restoreBeanFromFile(Util
117: .findTestDirectory()
118: + "/projects/standard/public/" + analysisName);
119:
120: }
121: }
|