Source Code Cross Referenced for Junit4ClassAnalyser.java in  » Test-Coverage » salome-tmf » salomeTMF_plug » simpleJunit » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Test Coverage » salome tmf » salomeTMF_plug.simpleJunit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package salomeTMF_plug.simpleJunit;
002:
003:        import java.io.BufferedOutputStream;
004:        import java.io.File;
005:        import java.io.FileOutputStream;
006:        import java.lang.annotation.Annotation;
007:        import java.lang.reflect.Method;
008:        import java.util.Hashtable;
009:        import java.util.List;
010:        import java.util.Properties;
011:
012:        import junit.textui.ResultPrinter;
013:
014:        import org.junit.runner.JUnitCore;
015:        import org.junit.runner.Request;
016:        import org.junit.runner.Result;
017:        import org.junit.runner.notification.Failure;
018:        import org.junit.runner.notification.RunNotifier;
019:        import org.objectweb.salome_tmf.api.Api;
020:        import org.objectweb.salome_tmf.api.ApiConstants;
021:        import org.objectweb.salome_tmf.api.Util;
022:        import org.objectweb.salome_tmf.data.Attachment;
023:        import org.objectweb.salome_tmf.data.AutomaticTest;
024:        import org.objectweb.salome_tmf.data.ExecutionResult;
025:        import org.objectweb.salome_tmf.data.FileAttachment;
026:        import org.objectweb.salome_tmf.data.Script;
027:        import org.objectweb.salome_tmf.data.TestList;
028:        import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
029:        import org.objectweb.salome_tmf.ihm.tools.Tools;
030:
031:        public class Junit4ClassAnalyser extends JunitAnalyser implements 
032:                IJunitAnalyser {
033:            //TestResult pTestResult = null;
034:            RunNotifier pRunNotifier = null;
035:            Result pResult;
036:            SimpleJunitPlugin pSimpleJunitPlugin;
037:            JarUtil pJarUtil;
038:            ResultPrinter pResultPrinter = null;
039:            StringPrintStream pResultStrem = null;
040:            JUnitCore pJUnitCore;
041:
042:            //org.junit.runner.JUnitCore 
043:
044:            Junit4ClassAnalyser(SimpleJunitPlugin pSimpleJunitPlugin) {
045:                super ();
046:                this .pSimpleJunitPlugin = pSimpleJunitPlugin;
047:                pJarUtil = new JarUtil(pSimpleJunitPlugin);
048:                pJUnitCore = new JUnitCore();
049:            }
050:
051:            public void createJunitTest(Attachment pSelectedattach,
052:                    String strClass, TestList pSuite, String toWrite) {
053:                try {
054:                    ClassLoader pClassLoader = pJarUtil
055:                            .loadJar(pSelectedattach);
056:                    Util
057:                            .log("[SimpleJunitPlugin:createJunitTest] try to load class : "
058:                                    + strClass);
059:
060:                    Class pTestClass = pClassLoader.loadClass(strClass);
061:                    Method[] pMethods = pTestClass.getDeclaredMethods();
062:                    for (int i = 0; i < pMethods.length; i++) {
063:                        Method pMethod = pMethods[i];
064:
065:                        Annotation[] pAnnotations = pMethod
066:                                .getDeclaredAnnotations();
067:                        for (int j = 0; j < pAnnotations.length; j++) {
068:                            Annotation pAnnotation = pAnnotations[j];
069:                            Util
070:                                    .log("[SimpleJunitPlugin:createJunitTest] Find Association : "
071:                                            + pAnnotation);
072:                            Util
073:                                    .log("[SimpleJunitPlugin:createJunitTest] Find Association Type: "
074:                                            + pAnnotation.annotationType());
075:                            if (pAnnotation.annotationType().equals(
076:                                    org.junit.Test.class)) {
077:                                String methName = pMethod.getName();
078:                                Util
079:                                        .log("[SimpleJunitPlugin:createJunitTest] Taille des arguments "
080:                                                + pMethod.getParameterTypes().length);
081:                                Util
082:                                        .log("[SimpleJunitPlugin:createJunitTest] add test for "
083:                                                + methName);
084:                                TestInfo testInfo = pMethod
085:                                        .getAnnotation(TestInfo.class);
086:                                if (testInfo == null) {
087:                                    pSimpleJunitPlugin.addTest(methName,
088:                                            pSuite, toWrite + "test_meth = "
089:                                                    + methName + "\n");
090:                                } else {
091:                                    String description = testInfo.description();
092:                                    String[] idReqs = testInfo.requirementsId();
093:                                    pSimpleJunitPlugin.addTest(methName,
094:                                            description, idReqs, pSuite,
095:                                            toWrite + "test_meth = " + methName
096:                                                    + "\n");
097:                                }
098:                                j = pAnnotations.length;
099:                            }
100:
101:                        }
102:
103:                    }
104:                } catch (Exception e) {
105:                    Tools.ihmExceptionView(e);
106:                    e.printStackTrace();
107:                }
108:                try {
109:                    if (pSuite.getFamilyFromModel() != null) {
110:                        DataModel.reloadData(pSuite);
111:                    } else {
112:                        DataModel.reloadFromBase(true);
113:                    }
114:                } catch (Exception e) {
115:                    Tools.ihmExceptionView(e);
116:                    e.printStackTrace();
117:                }
118:            }
119:
120:            public int runTest(String strID, String strAttachment,
121:                    String strClass, String meth, String testScript,
122:                    org.objectweb.salome_tmf.data.AutomaticTest pTest,
123:                    Hashtable argTest, String plugArg) throws Exception {
124:
125:                setExceutionResult(((ExecutionResult) argTest
126:                        .get("salome_ExecResultObject")));
127:
128:                // Add strAttachment in classloader if not
129:                Util.log("[SimpleJunitPlugin:runTest] chargement du jar "
130:                        + strAttachment);
131:                //ClassLoader pClassLoader = loadJar(strID, strAttachment, pTest.getTestListFromModel(), testScript, pTest);
132:                ClassLoader pClassLoader = pJarUtil.loadJar(strID,
133:                        strAttachment, testScript, pTest);
134:
135:                Util.log("[SimpleJunitPlugin:runTest] chargement de la classe "
136:                        + strClass);
137:                //Create instance of strClass
138:                Class pTestClass;
139:                try {
140:                    pTestClass = (Class) cacheClass.get(strClass);
141:                    if (pTestClass == null) {
142:                        pTestClass = pClassLoader.loadClass(strClass);
143:                        cacheClass.put(strClass, pTestClass);
144:                    }
145:                } catch (Exception e) {
146:                    e.printStackTrace();
147:                    throw e;
148:                }
149:
150:                try {
151:                    Method pParamMethod = findStrMedtode("setSalomeParameters",
152:                            pTestClass);
153:                    if (pParamMethod != null) {
154:                        Util
155:                                .log("[SimpleJunitPlugin:runTest] Find setSalomeParameters method");
156:                        pParamMethod.invoke(null, argTest);
157:                    }
158:                } catch (Exception e) {
159:                    e.printStackTrace();
160:                }
161:                strLog = (String) argTest.get("testLog");
162:                if (strLog == null) {
163:                    strLog = "";
164:                }
165:                pRunNotifier = new RunNotifier();
166:                Request pRequest = Request.method(pTestClass, meth);
167:                pResult = pJUnitCore.run(pRequest);
168:                strLog = (String) argTest.get("testLog");
169:
170:                Util.log("[SimpleJunitPlugin:runTest] Nb Result : "
171:                        + pResult.getRunCount());
172:
173:                if (pResult.getRunCount() < 1) {
174:                    return 1;
175:                }
176:                if (pResult.wasSuccessful())
177:                    return 0;
178:                else
179:                    return 1;
180:
181:            }
182:
183:            public String getTestLog() {
184:                String res = "";
185:                if (pResult != null) {
186:                    if (!pResult.wasSuccessful() || pResult.getRunCount() < 1) {
187:                        List<Failure> listFail = pResult.getFailures();
188:                        int size = listFail.size();
189:                        long time = pResult.getRunTime();
190:                        res += "Time: " + time;
191:                        res += "\nThere was 1 failure:";
192:                        if (pResult.getRunCount() < 1) {
193:                            res = "\nNO TEST EXECUTED";
194:                        }
195:                        for (int i = 0; i < size; i++) {
196:                            Failure f = listFail.get(i);
197:
198:                            res += "\nTest :" + f.getTestHeader();
199:                            res += "\n Description :"
200:                                    + f.getDescription().toString();
201:                            res += "\n Message :" + f.getMessage();
202:                            res += "\n Trace :" + f.getTrace();
203:                        }
204:                        if (strLog != null && !strLog.equals("")) {
205:                            res += "\nLOG FROM SALOME :\n" + strLog;
206:                        }
207:                    } else {
208:                        long time = pResult.getRunTime();
209:                        res += "Time: " + time;
210:                        res += "\nOK (1 test)";
211:                        if (strLog != null && !strLog.equals("")) {
212:                            res += "\nLOG FROM SALOME :\n" + strLog;
213:                        }
214:                    }
215:                }
216:                return res;
217:
218:            }
219:
220:            public void stopTest() throws Exception {
221:                if (pRunNotifier != null) {
222:                    Util.log("[SimpleJunitPlugin:stopTest] Stop the test");
223:                    pRunNotifier.pleaseStop();
224:                }
225:            }
226:
227:            Method findTestMedtode(String meth, Class pTestClass) {
228:                Method pMethod = null;
229:                Method[] pMethods = pTestClass.getDeclaredMethods();
230:                for (int i = 0; i < pMethods.length; i++) {
231:                    pMethod = pMethods[i];
232:                    Annotation[] pAnnotations = pMethod
233:                            .getDeclaredAnnotations();
234:                    for (int j = 0; j < pAnnotations.length; j++) {
235:                        Annotation pAnnotation = pAnnotations[j];
236:                        if (pAnnotation.annotationType().equals(
237:                                org.junit.Test.class)) {
238:                            String methName = pMethod.getName();
239:                            if (meth.equals(methName)) {
240:                                return pMethod;
241:                            }
242:                        }
243:                    }
244:                }
245:                return null;
246:            }
247:
248:            /********************************** Beanshell *********************************************/
249:            public void createBeanShellJunitTest(Attachment pSelectedattach,
250:                    String strClass, TestList pSuite) {
251:                try {
252:                    ClassLoader pClassLoader = pJarUtil
253:                            .loadJar(pSelectedattach);
254:                    Util
255:                            .log("[SimpleJunitPlugin:createJunitTest] try to load class : "
256:                                    + strClass);
257:
258:                    Class pTestClass = pClassLoader.loadClass(strClass);
259:                    Method[] pMethods = pTestClass.getDeclaredMethods();
260:                    for (int i = 0; i < pMethods.length; i++) {
261:                        Method pMethod = pMethods[i];
262:
263:                        Annotation[] pAnnotations = pMethod
264:                                .getDeclaredAnnotations();
265:                        for (int j = 0; j < pAnnotations.length; j++) {
266:                            Annotation pAnnotation = pAnnotations[j];
267:                            Util
268:                                    .log("[SimpleJunitPlugin:createJunitTest] Find Association : "
269:                                            + pAnnotation);
270:                            Util
271:                                    .log("[SimpleJunitPlugin:createJunitTest] Find Association Type: "
272:                                            + pAnnotation.annotationType());
273:                            if (pAnnotation.annotationType().equals(
274:                                    org.junit.Test.class)) {
275:                                String methName = pMethod.getName();
276:                                Util
277:                                        .log("[SimpleJunitPlugin:createJunitTest] Taille des arguments "
278:                                                + pMethod.getParameterTypes().length);
279:                                Util
280:                                        .log("[SimpleJunitPlugin:createJunitTest] add test for "
281:                                                + methName);
282:                                AddBeanshellTest(methName, pSuite,
283:                                        pSelectedattach, strClass);
284:                            }
285:
286:                        }
287:
288:                    }
289:                    if (pSuite.getFamilyFromModel() != null) {
290:                        DataModel.reloadData(pSuite);
291:                    } else {
292:                        DataModel.reloadFromBase(true);
293:                    }
294:                } catch (Exception e) {
295:                    Tools.ihmExceptionView(e);
296:                    e.printStackTrace();
297:                }
298:
299:            }
300:
301:            void AddBeanshellTest(String methName, TestList pSuite,
302:                    Attachment pSelectedattach, String strClass) {
303:                String name = pSimpleJunitPlugin.getTestName(methName, pSuite,
304:                        0);
305:                AutomaticTest pTest = new AutomaticTest(name.trim(),
306:                        "Junit TestCase", "beanshell.TestDriver");
307:                pTest.setConceptorLoginInModel(DataModel.getCurrentUser()
308:                        .getLoginFromModel());
309:                if (Api.isConnected()) {
310:                    try {
311:                        pSuite.addTestInDBAndModel(pTest);
312:                        Util
313:                                .log("[SimpleJunitPlugin:AddBeanshellTest] add test "
314:                                        + name + "in DB & Model");
315:
316:                    } catch (Exception e1) {
317:                        Tools.ihmExceptionView(e1);
318:                        return;
319:                    }
320:                }
321:
322:                File file = null;
323:                try {
324:                    Util
325:                            .log("[SimpleJunitPlugin:AddBeanshellTest] Create test Script file");
326:                    file = writeBenshellJunitFile(pTest, methName,
327:                            pSelectedattach, strClass);
328:                } catch (Exception e2) {
329:                    Tools.ihmExceptionView(e2);
330:                    return;
331:                }
332:                Script pScript = new Script(file.getName(), "Junit test script");
333:                //pScript.setName(file.getName());
334:                pScript.setLocalisation(file.getAbsolutePath());
335:                pScript.setTypeInModel(ApiConstants.TEST_SCRIPT);
336:                pScript
337:                        .setScriptExtensionInModel(pTest
338:                                .getExtensionFromModel());
339:                //pScript.setPlugArg("");
340:
341:                if (Api.isConnected()) {
342:                    try {
343:                        Util
344:                                .log("[SimpleJunitPlugin:AddBeanshellTest] Add  : "
345:                                        + pScript.getNameFromModel()
346:                                        + " to DB & Model");
347:                        pTest.addScriptInDBAndModel(pScript, file);
348:                    } catch (Exception e3) {
349:                        Tools.ihmExceptionView(e3);
350:                    }
351:                }
352:            }
353:
354:            File writeBenshellJunitFile(AutomaticTest pTest, String methName,
355:                    Attachment pSelectedattach, String strClass)
356:                    throws Exception {
357:                String fileName;
358:                File pFile;
359:                Properties sys = System.getProperties();
360:                String tmpDir = sys.getProperty("java.io.tmpdir");
361:                String fs = sys.getProperty("file.separator");
362:                fileName = tmpDir + fs + pTest.getNameFromModel() + ".bsh";
363:                Util
364:                        .log("[SimpleJunitPlugin:writeBenshellJunitFile] write file  : "
365:                                + fileName);
366:
367:                pFile = new File(fileName);
368:                if (pFile.exists()) {
369:                    pFile.delete();
370:                    pFile = new File(fileName);
371:                }
372:                FileOutputStream fos = new FileOutputStream(pFile);
373:                BufferedOutputStream bos = new BufferedOutputStream(fos);
374:                String toWrite = "import java.lang.Class;\n";
375:                toWrite += "import java.util.Hashtable;\n";
376:                toWrite += "import java.lang.reflect.Method;\n";
377:                toWrite += "import org.junit.runner.JUnitCore;\n";
378:                toWrite += "import org.junit.runner.Request;\n";
379:                toWrite += "import org.junit.runner.Result;\n";
380:                toWrite += "import java.lang.Class;\n";
381:                toWrite += "import java.lang.annotation.Annotation;\n";
382:                toWrite += "import java.lang.reflect.Method;\n\n";
383:
384:                toWrite += "Method findStrMedtode(String meth, Class pTestClass){\n";
385:                toWrite += "	Method pMethod = null;\n";
386:                toWrite += "	Method[] pMethods = pTestClass.getDeclaredMethods();\n";
387:                toWrite += "	for (int i = 0; i < pMethods.length; i++){\n";
388:                toWrite += "		pMethod = pMethods[i];\n";
389:                toWrite += "		String methName = pMethod.getName();\n";
390:                toWrite += "		if (meth.equals(methName)){\n";
391:                toWrite += "			return pMethod;\n";
392:                toWrite += "		}\n";
393:                toWrite += "	}\n";
394:                toWrite += "	return null;\n";
395:                toWrite += "}\n";
396:
397:                toWrite += "/********** LOAD THE JAR ************/\n";
398:                if (pSelectedattach instanceof  FileAttachment) {
399:                    toWrite += "fileJar = (salome_ProjectObject.getAttachmentFromModel(\""
400:                            + pSelectedattach.getNameFromModel()
401:                            + "\")).getFileFromDB(); \n";
402:                    toWrite += "addJar(fileJar.getAbsolutePath());\n";
403:
404:                } else {
405:                    toWrite += "addJar(\"" + pSelectedattach.getNameFromModel()
406:                            + "\");\n";
407:                }
408:
409:                toWrite += "testClass = Class.forName(\"" + strClass
410:                        + "\", false,  salome_classloder);\n";
411:
412:                toWrite += "/********** CALL setSalomeParameters if exist ************/\n";
413:                toWrite += "try {\n";
414:                toWrite += "	Method pParamMethod = findStrMedtode(\"setSalomeParameters\", testClass);\n";
415:                toWrite += "	if (pParamMethod != null){\n";
416:                toWrite += "		pParamMethod.invoke(null, new Object[] {testargs});\n";
417:                toWrite += "	}\n";
418:                toWrite += "} catch (Exception e){\n";
419:                toWrite += "	//e.printStackTrace();\n";
420:                toWrite += "}\n";
421:
422:                toWrite += "/********** CALL the test methode ************/\n";
423:
424:                toWrite += "Request pRequest =  Request.method(testClass, \""
425:                        + methName + "\");\n";
426:                toWrite += "pJUnitCore = new JUnitCore();\n";
427:                toWrite += "pResult = pJUnitCore.run(pRequest);\n";
428:
429:                toWrite += "if (pResult.getRunCount() < 1){\n";
430:                toWrite += "	Verdict = \"fail\";\n";
431:                toWrite += "}\n";
432:                toWrite += "if (pResult.wasSuccessful())\n";
433:                toWrite += "	Verdict = \"pass\";\n";
434:                toWrite += "else\n";
435:                toWrite += "	Verdict = \"fail\";\n";
436:
437:                Util.log("[SimpleJunitPlugin:writeBenshellJunitFile] write  : "
438:                        + toWrite);
439:
440:                byte[] b = toWrite.getBytes();
441:                bos.write(b, 0, b.length);
442:                bos.flush();
443:                bos.close();
444:                return pFile;
445:            }
446:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.