001: package xdoclet.junit;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.HashMap;
019: import java.util.Iterator;
020:
021: import junit.framework.TestCase;
022:
023: /**
024: * Base class for xdoclet test cases.
025: *
026: * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
027: */
028: public abstract class XDocletTestBase extends TestCase {
029: private HashMap _srcClasses = new HashMap();
030: private String _destFile = null;
031: private String _taskName = null;
032: private String _subTaskName = null;
033: private HashMap _taskProperties = new HashMap();
034: private HashMap _subTaskProperties = new HashMap();
035:
036: public XDocletTestBase(String name) {
037: super (name);
038: }
039:
040: protected void addClass(String name, String content) {
041: _srcClasses.put(name, content);
042: }
043:
044: protected void setDestFile(String path) {
045: _destFile = path;
046: }
047:
048: protected void setTaskName(String className) {
049: _taskName = className;
050: }
051:
052: protected void setSubTaskName(String className) {
053: _subTaskName = className;
054: }
055:
056: protected void setTaskProperty(String name, Object value) {
057: _taskProperties.put(name, value);
058: }
059:
060: protected Object getTaskProperty(String propertyName) {
061: return _taskProperties.get(propertyName);
062: }
063:
064: protected void clearTaskProperties() {
065: _taskProperties.clear();
066: }
067:
068: protected void setSubTaskProperty(String name, Object value) {
069: _subTaskProperties.put(name, value);
070: }
071:
072: protected void clearSubTaskProperties() {
073: _subTaskProperties.clear();
074: }
075:
076: /**
077: * Runs the XDoclet task/subtask with the current settings. Note that XDoclet does not return an exception
078: * upon failure as one would expect (e.g. XDocletException), but simply returns <code>null</code>, so use
079: * <code>assertNull</code> if expecting an error.
080: *
081: * @return The content of the destination file trimmed at both ends (i.e. no whitespaces at the beginning or end)
082: */
083: protected String runXDoclet() {
084: String classPath = System.getProperty("java.class.path");
085: //ClassLoader loader = new TestCaseClassLoader(classPath);
086: ClassLoader loader = new XDocletClassLoader(classPath);
087: Class runnerClass = null;
088: Object runner = null;
089:
090: try {
091: runnerClass = loader.loadClass(XDocletRunner.class
092: .getName());
093: runner = runnerClass.newInstance();
094: } catch (Exception ex) {
095: throw new RuntimeException(ex);
096: }
097:
098: invoke(runnerClass, runner, "setContextClassLoader",
099: new Class[] { ClassLoader.class },
100: new Object[] { loader });
101: copySettings(runnerClass, runner);
102: invoke(runnerClass, runner, "start", null, null);
103: while (((Boolean) invoke(runnerClass, runner, "isAlive", null,
104: null)).booleanValue()) {
105: try {
106: Thread.sleep(10);
107: } catch (InterruptedException ex) {
108: }
109: }
110:
111: String result = (String) invoke(runnerClass, runner,
112: "getResult", null, null);
113:
114: invoke(runnerClass, runner, "destroy", null, null);
115: runnerClass = null;
116: runner = null;
117: loader = null;
118: System.gc();
119:
120: return result;
121: }
122:
123: private void copySettings(Class runnerClass, Object runner) {
124: String name;
125:
126: for (Iterator it = _srcClasses.keySet().iterator(); it
127: .hasNext();) {
128: name = (String) it.next();
129: invoke(runnerClass, runner, "addClass", new Class[] {
130: String.class, String.class }, new Object[] { name,
131: (String) _srcClasses.get(name) });
132: }
133: invoke(runnerClass, runner, "setDestFile",
134: new Class[] { String.class },
135: new Object[] { _destFile });
136: invoke(runnerClass, runner, "setTaskName",
137: new Class[] { String.class },
138: new Object[] { _taskName });
139: for (Iterator it = _taskProperties.keySet().iterator(); it
140: .hasNext();) {
141: name = (String) it.next();
142: invoke(runnerClass, runner, "setTaskProperty", new Class[] {
143: String.class, Object.class }, new Object[] { name,
144: _taskProperties.get(name) });
145: }
146: if (_subTaskName != null) {
147: invoke(runnerClass, runner, "setSubTaskName",
148: new Class[] { String.class },
149: new Object[] { _subTaskName });
150: for (Iterator it = _subTaskProperties.keySet().iterator(); it
151: .hasNext();) {
152: name = (String) it.next();
153: invoke(runnerClass, runner, "setSubTaskProperty",
154: new Class[] { String.class, Object.class },
155: new Object[] { name,
156: _subTaskProperties.get(name) });
157: }
158: }
159: }
160:
161: private Object invoke(Class runnerClass, Object runner,
162: String methodName, Class[] argTypes, Object[] args) {
163: try {
164: return runnerClass.getMethod(methodName, argTypes).invoke(
165: runner, args);
166: } catch (Exception ex) {
167: throw new RuntimeException(ex);
168: }
169: }
170: }
|