KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > test > Util


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.test;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import org.apache.log4j.*;
26 import org.openi.analysis.Analysis;
27 import org.openi.application.Application;
28 import org.openi.project.Project;
29 import org.openi.project.ProjectContext;
30 import org.openi.xml.BeanStorage;
31
32
33 /**
34  * Util for tests - Isn't this an anti pattern?
35  */

36 public class Util {
37     private static Logger logger = Logger.getLogger(Util.class);
38     /**
39      *
40      * @return location of test directory - when tests are run from ant, this property is already set in
41      * CVS/product/src/build.xml, see the runtests task (${basedir}/test. If running anywhere else,
42      * you can set the system property TESTDIR, which takes precedence.
43      */

44     public static String JavaDoc findTestDirectory() {
45         return System.getProperty("TESTDIR", "./test");
46     }
47
48     public static void setupLog4j() {
49         PropertyConfigurator.configure(Util.findTestDirectory()
50             + "/log4jtest.properties");
51     }
52
53     /**
54      *
55      */

56     public static void initApplicationConfig() {
57         String JavaDoc appConfigPath = Util.findTestDirectory() +
58         "/../conf/application.xml";
59         try {
60             /*
61             File appTest = new File(appConfigPath);
62             logger.debug("config exists? " + appTest.exists());
63             logger.debug(appTest.getCanonicalPath());
64             */

65             new BeanStorage().restoreBeanFromFile(appConfigPath, Application.getInstance());
66
67             // logger.debug(Application.getInstance().toString());
68
} catch (IOException JavaDoc e) {
69             // TODO Auto-generated catch block
70
logger.error(e);
71         }
72         
73     }
74
75     /**
76      * @param string
77      * @return
78      * @throws IOException
79      */

80     public static ProjectContext createTestProjectContext(String JavaDoc username) {
81         String JavaDoc baseDir = Util.findTestDirectory() + "/projects/";
82         return createTestProjectContext(username, "standard");
83     }
84     
85     public static ProjectContext createTestProjectContext(String JavaDoc username, String JavaDoc projectId) {
86         String JavaDoc baseDir = Util.findTestDirectory() + "/projects/";
87         String JavaDoc projectFile = baseDir + "/" + projectId + "/project.xml";
88         ProjectContext context = null;
89         try {
90             Project project = (Project) new BeanStorage().restoreBeanFromFile(projectFile);
91             context= new ProjectContext(project, baseDir, username);
92         } catch (IOException JavaDoc e) {
93             // TODO Auto-generated catch block
94
logger.error(e);
95         }
96         return context;
97     }
98
99     public static Analysis restoreAnalysis(String JavaDoc analysisName)
100         throws IOException JavaDoc {
101         return (Analysis) new BeanStorage().restoreBeanFromFile(Util.findTestDirectory() +
102             "/projects/standard/public/" + analysisName);
103
104     }
105 }
106
Popular Tags