1 package xdoclet.junit; 2 3 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 21 import junit.framework.TestCase; 22 23 28 public abstract class XDocletTestBase extends TestCase 29 { 30 private HashMap _srcClasses = new HashMap (); 31 private String _destFile = null; 32 private String _taskName = null; 33 private String _subTaskName = null; 34 private HashMap _taskProperties = new HashMap (); 35 private HashMap _subTaskProperties = new HashMap (); 36 37 public XDocletTestBase(String name) 38 { 39 super(name); 40 } 41 42 protected void addClass(String name, String content) 43 { 44 _srcClasses.put(name, content); 45 } 46 47 protected void setDestFile(String path) 48 { 49 _destFile = path; 50 } 51 52 protected void setTaskName(String className) 53 { 54 _taskName = className; 55 } 56 57 protected void setSubTaskName(String className) 58 { 59 _subTaskName = className; 60 } 61 62 protected void setTaskProperty(String name, Object value) 63 { 64 _taskProperties.put(name, value); 65 } 66 67 protected Object getTaskProperty(String propertyName) 68 { 69 return _taskProperties.get(propertyName); 70 } 71 72 protected void clearTaskProperties() 73 { 74 _taskProperties.clear(); 75 } 76 77 protected void setSubTaskProperty(String name, Object value) 78 { 79 _subTaskProperties.put(name, value); 80 } 81 82 protected void clearSubTaskProperties() 83 { 84 _subTaskProperties.clear(); 85 } 86 87 94 protected String runXDoclet() 95 { 96 String classPath = System.getProperty("java.class.path"); 97 ClassLoader loader = new XDocletClassLoader(classPath); 99 Class runnerClass = null; 100 Object runner = null; 101 102 try 103 { 104 runnerClass = loader.loadClass(XDocletRunner.class.getName()); 105 runner = runnerClass.newInstance(); 106 } 107 catch (Exception ex) 108 { 109 throw new RuntimeException (ex); 110 } 111 112 invoke(runnerClass, 113 runner, 114 "setContextClassLoader", 115 new Class []{ ClassLoader .class }, 116 new Object []{ loader }); 117 copySettings(runnerClass, runner); 118 invoke(runnerClass, 119 runner, 120 "start", 121 null, 122 null); 123 while (((Boolean )invoke(runnerClass, 124 runner, 125 "isAlive", 126 null, 127 null)).booleanValue()) 128 { 129 try 130 { 131 Thread.sleep(10); 132 } 133 catch (InterruptedException ex) 134 {} 135 } 136 137 String result = (String )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 runnerClass, Object runner) 157 { 158 String name; 159 160 for (Iterator it = _srcClasses.keySet().iterator(); it.hasNext();) 161 { 162 name = (String )it.next(); 163 invoke(runnerClass, 164 runner, 165 "addClass", 166 new Class []{ String .class, String .class }, 167 new Object []{ name, (String )_srcClasses.get(name)}); 168 } 169 invoke(runnerClass, 170 runner, 171 "setDestFile", 172 new Class []{ String .class }, 173 new Object []{ _destFile }); 174 invoke(runnerClass, 175 runner, 176 "setTaskName", 177 new Class []{ String .class }, 178 new Object []{ _taskName }); 179 for (Iterator it = _taskProperties.keySet().iterator(); it.hasNext();) 180 { 181 name = (String )it.next(); 182 invoke(runnerClass, 183 runner, 184 "setTaskProperty", 185 new Class []{ String .class, Object .class }, 186 new Object []{ name, _taskProperties.get(name)}); 187 } 188 if (_subTaskName != null) 189 { 190 invoke(runnerClass, 191 runner, 192 "setSubTaskName", 193 new Class []{ String .class }, 194 new Object []{ _subTaskName }); 195 for (Iterator it = _subTaskProperties.keySet().iterator(); it.hasNext();) 196 { 197 name = (String )it.next(); 198 invoke(runnerClass, 199 runner, 200 "setSubTaskProperty", 201 new Class []{ String .class, Object .class }, 202 new Object []{ name, _subTaskProperties.get(name)}); 203 } 204 } 205 } 206 207 private Object invoke(Class runnerClass, Object runner, String methodName, Class [] argTypes, Object [] args) 208 { 209 try 210 { 211 return runnerClass.getMethod(methodName, argTypes).invoke(runner, args); 212 } 213 catch (Exception ex) 214 { 215 throw new RuntimeException (ex); 216 } 217 } 218 } 219 | Popular Tags |