001: package salomeTMF_plug.simpleJunit;
002:
003: import java.io.BufferedOutputStream;
004: import java.io.File;
005: import java.io.FileOutputStream;
006: import java.io.PrintStream;
007: import java.lang.reflect.Constructor;
008: import java.lang.reflect.Method;
009: import java.util.Hashtable;
010: import java.util.Properties;
011:
012: import junit.framework.Test;
013: import junit.framework.TestCase;
014: import junit.framework.TestResult;
015: import junit.textui.ResultPrinter;
016: import junit.textui.TestRunner;
017:
018: import org.objectweb.salome_tmf.api.Api;
019: import org.objectweb.salome_tmf.api.ApiConstants;
020: import org.objectweb.salome_tmf.api.Util;
021: import org.objectweb.salome_tmf.data.Attachment;
022: import org.objectweb.salome_tmf.data.AutomaticTest;
023: import org.objectweb.salome_tmf.data.ExecutionResult;
024: import org.objectweb.salome_tmf.data.FileAttachment;
025: import org.objectweb.salome_tmf.data.Script;
026: import org.objectweb.salome_tmf.data.TestList;
027: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
028: import org.objectweb.salome_tmf.ihm.tools.Tools;
029:
030: public class Junit3ClassAnalyser extends JunitAnalyser implements
031: IJunitAnalyser {
032:
033: TestResult pTestResult = null;
034: SimpleJunitPlugin pSimpleJunitPlugin;
035: JarUtil pJarUtil;
036: ResultPrinter pResultPrinter = null;
037: StringPrintStream pResultStrem = null;
038:
039: //JUnitCore pJUnitCore;
040: Junit3ClassAnalyser(SimpleJunitPlugin pSimpleJunitPlugin) {
041: super ();
042: this .pSimpleJunitPlugin = pSimpleJunitPlugin;
043: pJarUtil = new JarUtil(pSimpleJunitPlugin);
044:
045: //pJUnitCore = new JUnitCore();
046: }
047:
048: public void createJunitTest(Attachment pSelectedattach,
049: String strClass, TestList pSuite, String toWrite) {
050: try {
051: ClassLoader pClassLoader = pJarUtil
052: .loadJar(pSelectedattach);
053: Util
054: .log("[SimpleJunitPlugin:createJunitTest] try to load class : "
055: + strClass);
056:
057: Class pTestClass = pClassLoader.loadClass(strClass);
058: Method[] pMethods = pTestClass.getDeclaredMethods();
059: for (int i = 0; i < pMethods.length; i++) {
060: Method pMethod = pMethods[i];
061:
062: String methName = pMethod.getName();
063: Util
064: .log("[SimpleJunitPlugin:createJunitTest] evalue la methode : "
065: + methName);
066:
067: if (methName.startsWith("test")) {
068: if (pMethod.getReturnType().equals(void.class)) {
069: Util
070: .log("[SimpleJunitPlugin:createJunitTest] Taille des arguments "
071: + pMethod.getParameterTypes().length);
072: Util
073: .log("[SimpleJunitPlugin:createJunitTest] add test for "
074: + methName);
075: TestInfo testInfo = pMethod
076: .getAnnotation(TestInfo.class);
077: if (testInfo == null) {
078: pSimpleJunitPlugin.addTest(methName,
079: pSuite, toWrite + "test_meth = "
080: + methName + "\n");
081: } else {
082: String description = testInfo.description();
083: String[] idReqs = testInfo.requirementsId();
084: pSimpleJunitPlugin.addTest(methName,
085: description, idReqs, pSuite,
086: toWrite + "test_meth = " + methName
087: + "\n");
088: }
089: }
090: }
091: }
092: } catch (Exception e) {
093: Tools.ihmExceptionView(e);
094: e.printStackTrace();
095: }
096: try {
097: if (pSuite.getFamilyFromModel() != null) {
098: DataModel.reloadData(pSuite);
099: } else {
100: DataModel.reloadFromBase(true);
101: }
102: } catch (Exception e) {
103: Tools.ihmExceptionView(e);
104: e.printStackTrace();
105: }
106: }
107:
108: public int runTest(String strID, String strAttachment,
109: String strClass, String meth, String testScript,
110: org.objectweb.salome_tmf.data.AutomaticTest pTest,
111: Hashtable argTest, String plugArg) throws Exception {
112:
113: setExceutionResult(((ExecutionResult) argTest
114: .get("salome_ExecResultObject")));
115:
116: // Add strAttachment in classloader if not
117: Util.log("[SimpleJunitPlugin:runTest] chargement du jar "
118: + strAttachment);
119: //ClassLoader pClassLoader = loadJar(strID, strAttachment, pTest.getTestListFromModel(), testScript, pTest);
120: ClassLoader pClassLoader = pJarUtil.loadJar(strID,
121: strAttachment, testScript, pTest);
122:
123: Util.log("[SimpleJunitPlugin:runTest] chargement de la classe "
124: + strClass);
125: //Create instance of strClass
126: Class pTestClass;
127: try {
128: pTestClass = (Class) cacheClass.get(strClass);
129: if (pTestClass == null) {
130: pTestClass = pClassLoader.loadClass(strClass);
131: cacheClass.put(strClass, pTestClass);
132: }
133: } catch (Exception e) {
134: e.printStackTrace();
135: throw e;
136: }
137:
138: Util.log("[SimpleJunitPlugin:runTest] classe chargé "
139: + pTestClass);
140:
141: Constructor pConstructor = null;
142: boolean isStringConstructor = false;
143: try {
144: //pConstructor = pTestClass.getConstructor(parameterTypes);
145: pConstructor = pTestClass.getConstructor(Hashtable.class);
146: Util
147: .log("[SimpleJunitPlugin:runTest] Use constructor with Hashtable");
148: } catch (Exception ex) {
149: // ex.printStackTrace();
150: Util
151: .log("[SimpleJunitPlugin:runTest] No constructor with Hashtable");
152: try {
153: pConstructor = pTestClass.getConstructor(String.class);
154: Util
155: .log("[SimpleJunitPlugin:runTest] Use constructor with String");
156: isStringConstructor = true;
157: } catch (Exception ex2) {
158: pConstructor = null;
159: }
160: }
161:
162: Util
163: .log("[SimpleJunitPlugin:runTest] création de l'instance de l'objet tester");
164:
165: Object pTester = null;
166: try {
167: if (pConstructor != null) {
168: if (isStringConstructor) {
169: pTester = pConstructor.newInstance(meth);
170: } else {
171: pTester = pConstructor.newInstance(argTest);
172: }
173: Util
174: .log("[SimpleJunitPlugin:runTest] (constructor with arg) pTester = "
175: + pTester);
176:
177: } else {
178: pTester = pTestClass.newInstance();
179: Util
180: .log("[SimpleJunitPlugin:runTest] (constructor without arg) pTester = "
181: + pTester);
182:
183: }
184: } catch (Exception ex2) {
185: ex2.printStackTrace();
186: }
187: if (pTester == null)
188: Util.log("[SimpleJunitPlugin:runTest] !! ptester == null");
189:
190: Util.log("[SimpleJunitPlugin:runTest] !! ptester before cast"
191: + pTester.getClass().toString()
192: + " in junit.framework.TestCase");
193: ((TestCase) pTester).setName(meth);
194: Util.log("[SimpleJunitPlugin:runTest] Création du test result");
195:
196: //Ajout du printer//
197: pResultStrem = new StringPrintStream();
198: pResultPrinter = new ResultPrinter(
199: new PrintStream(pResultStrem));
200:
201: Util.log("[SimpleJunitPlugin:runTest] exécution du test");
202: junit.textui.TestRunner pTestRunner = new TestRunner();
203: pTestRunner.setPrinter(pResultPrinter);
204:
205: strLog = (String) argTest.get("testLog");
206: if (strLog == null) {
207: strLog = "";
208: }
209: pTestResult = pTestRunner.doRun((Test) pTester);
210: strLog = (String) argTest.get("testLog");
211: Util.log("[SimpleJunitPlugin:runTest] log de test testLog: "
212: + argTest.get("testLog"));
213: Util.log("[SimpleJunitPlugin:runTest] log de test"
214: + pResultStrem.getLog());
215: if (pTestResult.wasSuccessful())
216: return 0;
217: else
218: return 1;
219: }
220:
221: public String getTestLog() {
222: String res = "";
223: if (pResultStrem != null) {
224: res = pResultStrem.getLog();
225: if (strLog != null && !strLog.equals("")) {
226: res += "\nLOG FROM SALOME :\n" + strLog;
227: }
228: } else {
229: if (strLog != null && !strLog.equals("")) {
230: res += "\nLOG FROM SALOME :\n" + strLog;
231: }
232: }
233: return res;
234: }
235:
236: public void stopTest() throws Exception {
237: if (pTestResult != null) {
238: Util.log("[SimpleJunitPlugin:stopTest] Stop the test");
239: pTestResult.stop();
240: }
241: }
242:
243: /******************************** Beanshell *****************************************/
244:
245: public void createBeanShellJunitTest(Attachment pSelectedattach,
246: String strClass, TestList pSuite) {
247: try {
248: ClassLoader pClassLoader = pJarUtil
249: .loadJar(pSelectedattach);
250: Util
251: .log("[SimpleJunitPlugin:createBeanShellJunitTest] try to load class : "
252: + strClass);
253:
254: Class pTestClass = pClassLoader.loadClass(strClass);
255:
256: Method[] pMethods = pTestClass.getDeclaredMethods();
257: for (int i = 0; i < pMethods.length; i++) {
258: Method pMethod = pMethods[i];
259: String methName = pMethod.getName();
260: Util
261: .log("[SimpleJunitPlugin:createBeanShellJunitTest] evalue la methode : "
262: + methName);
263:
264: if (methName.startsWith("test")) {
265: if (pMethod.getReturnType().equals(void.class)) {
266: Util
267: .log("[SimpleJunitPlugin:createBeanShellJunitTest] Taille des arguments "
268: + pMethod.getParameterTypes().length);
269: Util
270: .log("[SimpleJunitPlugin:createBeanShellJunitTest] add test for "
271: + methName);
272: AddBeanshellTest(methName, pSuite,
273: pSelectedattach, strClass);
274: }
275: }
276: }
277: if (pSuite.getFamilyFromModel() != null) {
278: DataModel.reloadData(pSuite);
279: } else {
280: DataModel.reloadFromBase(true);
281: }
282: } catch (Exception e) {
283: Tools.ihmExceptionView(e);
284: e.printStackTrace();
285: }
286: }
287:
288: void AddBeanshellTest(String methName, TestList pSuite,
289: Attachment pSelectedattach, String strClass) {
290: String name = pSimpleJunitPlugin.getTestName(methName, pSuite,
291: 0);
292: AutomaticTest pTest = new AutomaticTest(name.trim(),
293: "Junit TestCase", "beanshell.TestDriver");
294: //pTest.setName(name.trim());
295: pTest.setConceptorLoginInModel(DataModel.getCurrentUser()
296: .getLoginFromModel());
297: //pTest.setDescription("Junit TestCase");
298: if (Api.isConnected()) {
299: try {
300: pSuite.addTestInDBAndModel(pTest);
301: Util
302: .log("[SimpleJunitPlugin:AddBeanshellTest] add test "
303: + name + "in DB & Model");
304:
305: } catch (Exception e1) {
306: Tools.ihmExceptionView(e1);
307: return;
308: }
309: }
310:
311: File file = null;
312: try {
313: Util
314: .log("[SimpleJunitPlugin:AddBeanshellTest] Create test Script file");
315: file = writeBenshellJunitFile(pTest, methName,
316: pSelectedattach, strClass);
317: } catch (Exception e2) {
318: Tools.ihmExceptionView(e2);
319: return;
320: }
321: Script pScript = new Script(file.getName(), "Junit test script");
322: //pScript.setName(file.getName());
323: pScript.setLocalisation(file.getAbsolutePath());
324: pScript.setTypeInModel(ApiConstants.TEST_SCRIPT);
325: pScript
326: .setScriptExtensionInModel(pTest
327: .getExtensionFromModel());
328: //pScript.setPlugArg("");
329:
330: if (Api.isConnected()) {
331: try {
332: Util
333: .log("[SimpleJunitPlugin:AddBeanshellTest] Add : "
334: + pScript.getNameFromModel()
335: + " to DB & Model");
336: pTest.addScriptInDBAndModel(pScript, file);
337: } catch (Exception e3) {
338: Tools.ihmExceptionView(e3);
339: }
340: }
341: }
342:
343: File writeBenshellJunitFile(AutomaticTest pTest, String methName,
344: Attachment pSelectedattach, String strClass)
345: throws Exception {
346: String fileName;
347: File pFile;
348: Properties sys = System.getProperties();
349: String tmpDir = sys.getProperty("java.io.tmpdir");
350: String fs = sys.getProperty("file.separator");
351: fileName = tmpDir + fs + pTest.getNameFromModel() + ".bsh";
352: Util
353: .log("[SimpleJunitPlugin:writeBenshellJunitFile] write file : "
354: + fileName);
355:
356: pFile = new File(fileName);
357: if (pFile.exists()) {
358: pFile.delete();
359: pFile = new File(fileName);
360: }
361: //Util.log("[SimpleJunitPlugin:writeJunitFile] write " + toWtrite + ", in " + fileName);
362: FileOutputStream fos = new FileOutputStream(pFile);
363: BufferedOutputStream bos = new BufferedOutputStream(fos);
364: String toWrite = "import java.lang.Class;\n";
365: toWrite += "import java.util.Hashtable;\n";
366: toWrite += "import java.lang.reflect.Method;\n";
367: toWrite += "import java.lang.Class;\n";
368: toWrite += "import junit.framework.*;\n\n";
369:
370: if (pSelectedattach instanceof FileAttachment) {
371: //toWrite += "fileJar = (salome_SuiteTestObject.getAttachmentFromModel(\""+ pSelectedattach.getNameFromModel()+"\")).getFileFromDB(); \n";
372: toWrite += "fileJar = (salome_ProjectObject.getAttachmentFromModel(\""
373: + pSelectedattach.getNameFromModel()
374: + "\")).getFileFromDB(); \n";
375: toWrite += "addJar(fileJar.getAbsolutePath());\n";
376:
377: } else {
378: toWrite += "addJar(\"" + pSelectedattach.getNameFromModel()
379: + "\");\n";
380: }
381:
382: toWrite += "testClass = Class.forName(\"" + strClass
383: + "\", false, salome_classloder);\n";
384: toWrite += "testCase = testClass.newInstance();\n";
385: toWrite += "testCase.setName(\"" + methName + "\");\n";
386:
387: toWrite += "try {\n";
388: toWrite += "\tClass[] parameterTypes = new Class[1];\n";
389: toWrite += "\tparameterTypes[0] = Hashtable.class;\n";
390: toWrite += "\tpMethod = testClass.getDeclaredMethod(\"setParam\", parameterTypes);\n";
391: toWrite += "\tObject[] args = new Object[1];\n";
392: toWrite += "\targs[1] =testargs;\n";
393: toWrite += "\tpMethod.invoke(testCase, args);\n";
394: toWrite += "} catch (Exception e){\n";
395: toWrite += "\t}\n";
396:
397: //if testCase have method setParam
398:
399: toWrite += "result = junit.textui.TestRunner.run(testCase);\n";
400: toWrite += "if (result.wasSuccessful()){\n";
401: toWrite += "\tVerdict = \"pass\";\n";
402: toWrite += "} else {\n";
403: toWrite += "\tVerdict = \"fail\";\n";
404: toWrite += "}";
405:
406: Util.log("[SimpleJunitPlugin:writeBenshellJunitFile] write : "
407: + toWrite);
408:
409: byte[] b = toWrite.getBytes();
410: bos.write(b, 0, b.length);
411: bos.flush();
412: bos.close();
413: return pFile;
414: }
415: }
|