KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > junit > XDocletTestBase


1 package xdoclet.junit;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import junit.framework.TestCase;
22
23 /**
24  * Base class for xdoclet test cases.
25  *
26  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
27  */

28 public abstract class XDocletTestBase extends TestCase
29 {
30     private HashMap JavaDoc _srcClasses = new HashMap JavaDoc();
31     private String JavaDoc _destFile = null;
32     private String JavaDoc _taskName = null;
33     private String JavaDoc _subTaskName = null;
34     private HashMap JavaDoc _taskProperties = new HashMap JavaDoc();
35     private HashMap JavaDoc _subTaskProperties = new HashMap JavaDoc();
36
37     public XDocletTestBase(String JavaDoc name)
38     {
39         super(name);
40     }
41
42     protected void addClass(String JavaDoc name, String JavaDoc content)
43     {
44         _srcClasses.put(name, content);
45     }
46
47     protected void setDestFile(String JavaDoc path)
48     {
49         _destFile = path;
50     }
51
52     protected void setTaskName(String JavaDoc className)
53     {
54         _taskName = className;
55     }
56
57     protected void setSubTaskName(String JavaDoc className)
58     {
59         _subTaskName = className;
60     }
61
62     protected void setTaskProperty(String JavaDoc name, Object JavaDoc value)
63     {
64         _taskProperties.put(name, value);
65     }
66
67     protected Object JavaDoc getTaskProperty(String JavaDoc propertyName)
68     {
69         return _taskProperties.get(propertyName);
70     }
71
72     protected void clearTaskProperties()
73     {
74         _taskProperties.clear();
75     }
76
77     protected void setSubTaskProperty(String JavaDoc name, Object JavaDoc value)
78     {
79         _subTaskProperties.put(name, value);
80     }
81
82     protected void clearSubTaskProperties()
83     {
84         _subTaskProperties.clear();
85     }
86
87     /**
88      * Runs the XDoclet task/subtask with the current settings. Note that XDoclet does not return an exception
89      * upon failure as one would expect (e.g. XDocletException), but simply returns <code>null</code>, so use
90      * <code>assertNull</code> if expecting an error.
91      *
92      * @return The content of the destination file trimmed at both ends (i.e. no whitespaces at the beginning or end)
93      */

94     protected String JavaDoc runXDoclet()
95     {
96         String JavaDoc classPath = System.getProperty("java.class.path");
97         //ClassLoader loader = new TestCaseClassLoader(classPath);
98
ClassLoader JavaDoc loader = new XDocletClassLoader(classPath);
99         Class JavaDoc runnerClass = null;
100         Object JavaDoc runner = null;
101
102         try
103         {
104             runnerClass = loader.loadClass(XDocletRunner.class.getName());
105             runner = runnerClass.newInstance();
106         }
107         catch (Exception JavaDoc ex)
108         {
109             throw new RuntimeException JavaDoc(ex);
110         }
111
112         invoke(runnerClass,
113                runner,
114                "setContextClassLoader",
115                new Class JavaDoc[]{ ClassLoader JavaDoc.class },
116                new Object JavaDoc[]{ loader });
117         copySettings(runnerClass, runner);
118         invoke(runnerClass,
119                runner,
120                "start",
121                null,
122                null);
123         while (((Boolean JavaDoc)invoke(runnerClass,
124                                 runner,
125                                 "isAlive",
126                                 null,
127                                 null)).booleanValue())
128         {
129             try
130             {
131                 Thread.sleep(10);
132             }
133             catch (InterruptedException JavaDoc ex)
134             {}
135         }
136
137         String JavaDoc result = (String JavaDoc)invoke(runnerClass,
138                                        runner,
139                                        "getResult",
140                                        null,
141                                        null);
142
143         invoke(runnerClass,
144                runner,
145                "destroy",
146                null,
147                null);
148         runnerClass = null;
149         runner = null;
150         loader = null;
151         System.gc();
152
153         return result;
154     }
155
156     private void copySettings(Class JavaDoc runnerClass, Object JavaDoc runner)
157     {
158         String JavaDoc name;
159
160         for (Iterator JavaDoc it = _srcClasses.keySet().iterator(); it.hasNext();)
161         {
162             name = (String JavaDoc)it.next();
163             invoke(runnerClass,
164                    runner,
165                    "addClass",
166                    new Class JavaDoc[]{ String JavaDoc.class, String JavaDoc.class },
167                    new Object JavaDoc[]{ name, (String JavaDoc)_srcClasses.get(name)});
168         }
169         invoke(runnerClass,
170                runner,
171                "setDestFile",
172                new Class JavaDoc[]{ String JavaDoc.class },
173                new Object JavaDoc[]{ _destFile });
174         invoke(runnerClass,
175                runner,
176                "setTaskName",
177                new Class JavaDoc[]{ String JavaDoc.class },
178                new Object JavaDoc[]{ _taskName });
179         for (Iterator JavaDoc it = _taskProperties.keySet().iterator(); it.hasNext();)
180         {
181             name = (String JavaDoc)it.next();
182             invoke(runnerClass,
183                    runner,
184                    "setTaskProperty",
185                    new Class JavaDoc[]{ String JavaDoc.class, Object JavaDoc.class },
186                    new Object JavaDoc[]{ name, _taskProperties.get(name)});
187         }
188         if (_subTaskName != null)
189         {
190             invoke(runnerClass,
191                    runner,
192                    "setSubTaskName",
193                    new Class JavaDoc[]{ String JavaDoc.class },
194                    new Object JavaDoc[]{ _subTaskName });
195             for (Iterator JavaDoc it = _subTaskProperties.keySet().iterator(); it.hasNext();)
196             {
197                 name = (String JavaDoc)it.next();
198                 invoke(runnerClass,
199                        runner,
200                        "setSubTaskProperty",
201                        new Class JavaDoc[]{ String JavaDoc.class, Object JavaDoc.class },
202                        new Object JavaDoc[]{ name, _subTaskProperties.get(name)});
203             }
204         }
205     }
206
207     private Object JavaDoc invoke(Class JavaDoc runnerClass, Object JavaDoc runner, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args)
208     {
209         try
210         {
211             return runnerClass.getMethod(methodName, argTypes).invoke(runner, args);
212         }
213         catch (Exception JavaDoc ex)
214         {
215             throw new RuntimeException JavaDoc(ex);
216         }
217     }
218 }
219
Popular Tags