KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > common > launch > JacORBLauncher


1 package org.jacorb.test.common.launch;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 2005 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21  * MA 02110-1301, USA.
22  */

23
24 import java.io.*;
25 import java.util.*;
26 import java.util.regex.*;
27 import java.lang.reflect.*;
28
29 import org.jacorb.test.common.*;
30
31 /**
32  * A JacORBLauncher runs a given main class against a specified
33  * JacORB version. The class JacORBLauncher itself is an abstract
34  * superclass for specific launchers that work with a given JacORB
35  * installation. To use, call JacORBLauncher.getLauncher(), then
36  * invoke the launch() method on the resulting object.
37  *
38  * @author Andre Spiegel spiegel@gnu.org
39  * @version $Id: JacORBLauncher.java,v 1.1 2005/05/10 13:57:56 andre.spiegel Exp $
40  */

41 public abstract class JacORBLauncher
42 {
43     private static Map launchers;
44     private static Properties testProperties;
45     private static List versions;
46     
47     protected String JavaDoc jacorbHome;
48     protected boolean coverage;
49     
50     protected JacORBLauncher (String JavaDoc jacorbHome, boolean coverage)
51     {
52         this.jacorbHome = jacorbHome;
53         this.coverage = coverage;
54     }
55     
56     public abstract Process JavaDoc launch (String JavaDoc classpath,
57                                     Properties props,
58                                     String JavaDoc mainClass,
59                                     String JavaDoc[] args);
60     
61     public String JavaDoc getJacorbHome()
62     {
63         return jacorbHome;
64     }
65     
66     public List propsToArgList (Properties props)
67     {
68         List result = new ArrayList();
69         
70         if (props == null) return result;
71
72         for (Iterator i = props.keySet().iterator(); i.hasNext();)
73         {
74             String JavaDoc key = (String JavaDoc)i.next();
75             String JavaDoc value = props.getProperty(key);
76             result.add ("-D" + key + "=" + value);
77         }
78         
79         return result;
80     }
81     
82     public String JavaDoc[] toStringArray (List l)
83     {
84         return ((String JavaDoc[])l.toArray (new String JavaDoc[0]));
85     }
86
87     /**
88      * Loads and returns the properties defined in the file test.properties
89      * in the regression suite.
90      */

91     public static Properties getTestProperties()
92     {
93         if (testProperties == null)
94         {
95             try
96             {
97                 InputStream in = new FileInputStream
98                 (
99                     TestUtils.testHome() + "/test.properties"
100                 );
101                 testProperties = new Properties();
102                 testProperties.load (in);
103             }
104             catch (IOException ex)
105             {
106                 testProperties = null;
107                 throw new RuntimeException JavaDoc (ex);
108             }
109         }
110         return testProperties;
111     }
112     
113     /**
114      * Returns a list of all the available JacORB versions.
115      */

116     public static List getVersions()
117     {
118         if (versions == null)
119         {
120             versions = new ArrayList();
121             for (int i=0; ; i++)
122             {
123                 String JavaDoc key = "jacorb.test.jacorb_version." + i + ".id";
124                 String JavaDoc value = getTestProperties().getProperty(key);
125                 if (value == null) break;
126                 versions.add (value);
127             }
128         }
129         return versions;
130     }
131
132     /**
133      * Returns a launcher for the specified JacORB version.
134      * If coverage is true, sets up the launcher to that
135      * coverage information will be gathered.
136      */

137     public static JacORBLauncher getLauncher (String JavaDoc version,
138                                               boolean coverage)
139     {
140         int index = getVersions().indexOf (version);
141         if (index == -1) throw new RuntimeException JavaDoc
142         (
143             "JacORB version " + version + " not available"
144         );
145         String JavaDoc key = "jacorb.test.jacorb_version." + index + ".home";
146         String JavaDoc home = getTestProperties().getProperty(key);
147         if (home == null)
148         {
149             if (version.equals("cvs"))
150                 home = getCVSHome();
151             else
152                 throw new RuntimeException JavaDoc
153                 (
154                     "No home directory for JacORB version " + version
155                 );
156         }
157         key = "jacorb.test.jacorb_version." + index + ".launcher";
158         String JavaDoc launcherClassName = getTestProperties().getProperty(key);
159         if (launcherClassName == null) throw new RuntimeException JavaDoc
160         (
161             "No launcher class defined for JacORB version " + version
162         );
163         try
164         {
165             Class JavaDoc launcherClass = Class.forName (launcherClassName);
166             Constructor c = launcherClass.getConstructor
167             (
168                 new Class JavaDoc[] { java.lang.String JavaDoc.class,
169                               boolean.class }
170             );
171             return (JacORBLauncher)c.newInstance
172             (
173                 new Object JavaDoc[] { home, new Boolean JavaDoc(coverage) }
174             );
175         }
176         catch (Exception JavaDoc ex)
177         {
178             throw new RuntimeException JavaDoc (ex);
179         }
180     }
181     
182     private static String JavaDoc getCVSHome()
183     {
184         String JavaDoc testHome = TestUtils.testHome();
185         Pattern homePattern = Pattern.compile
186         (
187             "^(.*?)/test/regression"
188         );
189         Matcher m = homePattern.matcher (testHome);
190         if (m.matches())
191             return m.group(1);
192         else
193             throw new RuntimeException JavaDoc ("couldn't find CVS home: "
194                                         + testHome);
195     }
196     
197 }
198
Popular Tags