Source Code Cross Referenced for JTestCaseDigester.java in  » Testing » jtestcase » org » jtestcase » core » digester » 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 » Testing » jtestcase » org.jtestcase.core.digester 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * This program is licensed under Common Public License Version 0.5.
003:         *
004:         * For License Information and conditions of use, see "LICENSE" in packaged
005:         * 
006:         */
007:        package org.jtestcase.core.digester;
008:
009:        import java.io.FileInputStream;
010:        import java.io.FileNotFoundException;
011:        import java.io.IOException;
012:        import java.io.InputStream;
013:        import java.util.ArrayList;
014:        import java.util.Iterator;
015:        import java.util.List;
016:        import java.util.Vector;
017:
018:        import org.jdom.Document;
019:        import org.jdom.Element;
020:        import org.jdom.JDOMException;
021:        import org.jdom.input.SAXBuilder;
022:        import org.jtestcase.JTestCase;
023:        import org.jtestcase.TestCaseInstance;
024:        import org.jtestcase.core.model.AssertGroupInstance;
025:        import org.jtestcase.core.model.AssertInstance;
026:        import org.jtestcase.core.model.AssertParamGroupInstance;
027:        import org.jtestcase.core.model.AssertParamInstance;
028:        import org.jtestcase.core.model.ParamGroupInstance;
029:        import org.jtestcase.core.model.ParamInstance;
030:
031:        /**
032:         * Helper for handling the JTestCaseWizard XML. Reads data from the
033:         * JTestCaseWizard XML into internal data structures.
034:         * 
035:         * @author <a href="mailto:fausto_lelli@hotmail.com">Fausto Lelli </a>
036:         * 
037:         * $Id: JTestCaseDigester.java,v 1.7 2005/11/22 09:53:39 faustothegrey Exp $
038:         */
039:        public class JTestCaseDigester {
040:
041:            /**
042:             * Name of the JTestCaseWizard XML file
043:             */
044:            private String fileName = "";
045:
046:            /**
047:             * Instance of the XQueryParser used
048:             */
049:            private XQueryParser xqueryparser = new XQueryParserImpl_Jaxen();
050:
051:            /**
052:             * JTestCase to which this digester belongs
053:             */
054:            private JTestCase jtestcase;
055:
056:            /**
057:             * JDom representation of the data xml file
058:             */
059:            private Document jDomDocument;
060:
061:            /**
062:             * Standard constructor.
063:             * 
064:             * @param fileName
065:             *            name of the JTestCaseWizard XML file
066:             * @throws XQueryException
067:             * @throws FileNotFoundException
068:             */
069:            public JTestCaseDigester(String fileName, JTestCase jtestcase)
070:                    throws FileNotFoundException, XQueryException {
071:                this .fileName = fileName;
072:                this .jtestcase = jtestcase;
073:                this .jDomDocument = getDocument(fileName);
074:            }
075:
076:            /**
077:             * Returns all test cases for one method element from the XML.
078:             * 
079:             * @param className
080:             *            the name of the class element
081:             * @param methodName
082:             *            the name of the method element the test cases haven to be read
083:             * @return list of all JTestCaseIdentifiers for this method element
084:             * @throws DigesterException
085:             *             in case of parsing errors
086:             */
087:            public Vector getTestCasesPerMethod(String methodName)
088:                    throws DigesterException {
089:                Vector vector = new Vector();
090:                try {
091:                    String expression = "//tests/class[@name='"
092:                            + jtestcase.getMClassName() + "']/method[@name='"
093:                            + methodName + "']";
094:                    List testCasesList = xqueryparser.getElements(jDomDocument,
095:                            expression);
096:                    for (Iterator iter = testCasesList.iterator(); iter
097:                            .hasNext();) {
098:                        Element element = (Element) iter.next();
099:
100:                        if (element.getAttribute("test-case") != null) {
101:                            // get test case from attribute
102:                            vector.add(new TestCaseInstance(element
103:                                    .getAttribute("name").getValue(), element
104:                                    .getAttribute("test-case").getValue(),
105:                                    jtestcase));
106:                        } else {
107:                            // get test case from test-case tag
108:                            List children = element.getChildren("test-case");
109:                            for (Iterator childrenIter = children.iterator(); childrenIter
110:                                    .hasNext();) {
111:                                Element testcaseChild = (Element) childrenIter
112:                                        .next();
113:                                vector
114:                                        .add(new TestCaseInstance(element
115:                                                .getAttribute("name")
116:                                                .getValue(), testcaseChild
117:                                                .getAttributeValue("name"),
118:                                                jtestcase));
119:                            }
120:                        }
121:                    }
122:                    return vector;
123:                } catch (Exception e) {
124:                    throw new DigesterException(
125:                            "Error parsing xml file : " + e.getMessage()
126:                                    + "  (in file : " + fileName + ")", e);
127:                }
128:            }
129:
130:            /**
131:             * Returns all test cases in one XML file. A test case is identifies by the
132:             * name of the test method and the name of the test case.
133:             * 
134:             * @param className
135:             *            the name of the class element
136:             * @return list of all JTestCaseWizard identifiers in this file
137:             * @throws DigesterException
138:             *             in case of parsing errors
139:             */
140:            public Vector getTestCases(String className, JTestCase jtestcase)
141:                    throws DigesterException {
142:                Vector vector = new Vector();
143:                try {
144:                    String expression = "//tests/class[@name='" + className
145:                            + "']/method";
146:                    List testCasesList = xqueryparser.getElements(jDomDocument,
147:                            expression);
148:                    for (Iterator iter = testCasesList.iterator(); iter
149:                            .hasNext();) {
150:                        Element element = (Element) iter.next();
151:                        if (element.getAttribute("test-case") != null) {
152:                            // get test case from attribute
153:                            vector.add(new TestCaseInstance(element
154:                                    .getAttribute("name").getValue(), element
155:                                    .getAttribute("test-case").getValue(),
156:                                    jtestcase));
157:                        } else {
158:                            // get test case from test-case tag
159:                            vector.add(new TestCaseInstance(element
160:                                    .getAttribute("name").getValue(), element
161:                                    .getChild("test-case").getAttributeValue(
162:                                            "name"), jtestcase));
163:
164:                        }
165:                    }
166:                    return vector;
167:                } catch (Exception e) {
168:                    throw new DigesterException(
169:                            "Error parsing xml file : " + e.getMessage()
170:                                    + "  (in file : " + fileName + ")", e);
171:                }
172:            }
173:
174:            /**
175:             * Returns a list of all assert instances for the given method and test case
176:             * 
177:             * @param className
178:             *            the name of the class element
179:             * @param methodName
180:             *            name of the method element
181:             * @param testCase
182:             *            name of the test case
183:             * @return a list of assert instances
184:             * @throws DigesterException
185:             *             in case of arsing errors
186:             */
187:            public List getAssertsInstances(String className,
188:                    String methodName, String testCase)
189:                    throws DigesterException {
190:                List list = new ArrayList();
191:                try {
192:                    List expectedElList = xqueryparser.getElements(
193:                            jDomDocument, "/tests/class[@name='" + className
194:                                    + "']/method[./@name='" + methodName
195:                                    + "' and ./@test-case='" + testCase
196:                                    + "']/asserts/assert");
197:                    for (Iterator iter = expectedElList.iterator(); iter
198:                            .hasNext();) {
199:                        Element expectedEl = (Element) iter.next();
200:                        AssertInstance assertion = getAssertInstanceFromElement(expectedEl);
201:                        list.add(assertion);
202:                    }
203:
204:                    List expectedElList2 = xqueryparser.getElements(
205:                            jDomDocument, "/tests/class[@name='" + className
206:                                    + "']/method[./@name='" + methodName + "']"
207:                                    + "/test-case[./@name='" + testCase
208:                                    + "']/asserts/assert");
209:                    for (Iterator iter = expectedElList2.iterator(); iter
210:                            .hasNext();) {
211:                        Element expectedEl2 = (Element) iter.next();
212:                        AssertInstance assertion = getAssertInstanceFromElement(expectedEl2);
213:                        list.add(assertion);
214:                    }
215:
216:                    return list;
217:                } catch (Exception e) {
218:                    throw new DigesterException(e);
219:                }
220:            }
221:
222:            /**
223:             * Returns a list of all assertparam instances for the given method and test
224:             * case
225:             * 
226:             * @param className
227:             *            the name of the class element
228:             * @param methodName
229:             *            name of the method element
230:             * @param testCase
231:             *            name of the test case
232:             * @return a list of assert instances
233:             * @throws DigesterException
234:             *             in case of arsing errors
235:             */
236:            public List getAssertsParamInstances(String className,
237:                    String methodName, String testCase)
238:                    throws DigesterException {
239:                List list = new ArrayList();
240:                try {
241:                    List expectedElList = xqueryparser.getElements(
242:                            jDomDocument, "/tests/class[@name='" + className
243:                                    + "']/method[./@name='" + methodName
244:                                    + "' and ./@test-case='" + testCase
245:                                    + "']/asserts/assertparam");
246:                    for (Iterator iter = expectedElList.iterator(); iter
247:                            .hasNext();) {
248:                        Element expectedEl = (Element) iter.next();
249:                        AssertParamInstance assertion = getAssertParamInstanceFromElement(expectedEl);
250:                        list.add(assertion);
251:                    }
252:
253:                    List expectedElList2 = xqueryparser.getElements(
254:                            jDomDocument, "/tests/class[@name='" + className
255:                                    + "']/method[./@name='" + methodName + "']"
256:                                    + "/test-case[./@name='" + testCase
257:                                    + "']/asserts/assertparam");
258:                    for (Iterator iter = expectedElList2.iterator(); iter
259:                            .hasNext();) {
260:                        Element expectedEl2 = (Element) iter.next();
261:                        AssertParamInstance assertion = getAssertParamInstanceFromElement(expectedEl2);
262:                        list.add(assertion);
263:                    }
264:
265:                    return list;
266:                } catch (Exception e) {
267:                    throw new DigesterException(e);
268:                }
269:            }
270:
271:            /**
272:             * Analyses a assert element from the XML and creates an assert instance.
273:             * 
274:             * @param element
275:             *            the XML assert element
276:             * @return an assert instance
277:             * @throws DigesterException
278:             *             in case of any errors
279:             */
280:            private AssertParamInstance getAssertParamInstanceFromElement(
281:                    Element element) throws DigesterException {
282:                AssertParamInstance assertion = AssertParamInstance
283:                        .createAssertParamInstance(element);
284:                // now, if this parameter element has nested params,
285:                // add the nested elements to this instance (recursively)
286:
287:                // 1) get the children of this element
288:                List nestedElementList = element.getChildren();
289:
290:                // 2) add them to father, and for each, do it recursively
291:                for (Iterator iter = nestedElementList.iterator(); iter
292:                        .hasNext();) {
293:                    Element nestedElement = (Element) iter.next();
294:                    if (nestedElement.getName().equals("assert")) {
295:                        // recursively call this function
296:                        AssertParamInstance nestedInstance = getAssertParamInstanceFromElement(nestedElement);
297:                        // add the istance to the father param
298:                        assertion.addAssert(nestedInstance);
299:                    } else
300:                        throw new DigesterException(
301:                                "assertparam can contain only nested assertparams tags ",
302:                                null);
303:                }
304:                return assertion;
305:            }
306:
307:            /**
308:             * Analyses a assert element from the XML and creates an assert instance.
309:             * 
310:             * @param element
311:             *            the XML assert element
312:             * @return an assert instance
313:             * @throws DigesterException
314:             *             in case of any errors
315:             */
316:            private AssertInstance getAssertInstanceFromElement(Element element)
317:                    throws DigesterException {
318:                AssertInstance assertion = AssertInstance
319:                        .createAssertInstance(element);
320:                // now, if this parameter element has nested params,
321:                // add the nested elements to this instance (recursively)
322:
323:                // 1) get the children of this element
324:                List nestedElementList = element.getChildren();
325:
326:                // 2) add them to father, and for each, do it recursively
327:                for (Iterator iter = nestedElementList.iterator(); iter
328:                        .hasNext();) {
329:                    Element nestedElement = (Element) iter.next();
330:                    if (nestedElement.getName().equals("assert")) {
331:                        // recursively call this function
332:                        AssertInstance nestedInstance = getAssertInstanceFromElement(nestedElement);
333:                        // add the istance to the father param
334:                        assertion.addAssert(nestedInstance);
335:                    } else
336:                        throw new DigesterException(
337:                                "assert can contain only nested assert tags ",
338:                                null);
339:                }
340:                return assertion;
341:            }
342:
343:            /**
344:             * Returns a list of assert group instances for a given method name and test
345:             * case.
346:             * 
347:             * @param className
348:             *            the name of the class element
349:             * @param methodName
350:             *            a method name
351:             * @param testCase
352:             *            a test case
353:             * @return a list of assert group instances
354:             * @throws DigesterException
355:             *             in case of any errors
356:             * @throws FileNotFoundException
357:             */
358:            public List getAssertGroupInstances(String className,
359:                    String methodName, String testCase)
360:                    throws DigesterException, FileNotFoundException {
361:                List instances = new ArrayList();
362:                try {
363:                    List paramElList = new ArrayList();
364:
365:                    paramElList = xqueryparser.getElements(jDomDocument,
366:                            "/tests/class[@name='" + className
367:                                    + "']/method[./@name='" + methodName
368:                                    + "' and ./@test-case='" + testCase
369:                                    + "']/asserts/assertgroup");
370:
371:                    for (Iterator elements = paramElList.iterator(); elements
372:                            .hasNext();) {
373:                        // top level paramgroup instances
374:                        Element item = (Element) elements.next();
375:                        AssertGroupInstance pgInstance = parseAssertGroup(item);
376:                        instances.add(pgInstance);
377:                    }
378:
379:                    List paramElList2 = xqueryparser
380:                            .getElements(
381:                                    jDomDocument,
382:                                    "/tests/class[@name='"
383:                                            + className
384:                                            + "']/method[./@name='"
385:                                            + methodName
386:                                            + "']/test-case[./@name='testCase']/asserts/assertgroup");
387:
388:                    for (Iterator elements = paramElList2.iterator(); elements
389:                            .hasNext();) {
390:                        // top level paramgroup instances
391:                        Element item = (Element) elements.next();
392:                        AssertGroupInstance pgInstance = parseAssertGroup(item);
393:                        instances.add(pgInstance);
394:                    }
395:
396:                } catch (XQueryException e) {
397:                    throw new DigesterException(e);
398:                } catch (JDOMException jde) {
399:                    throw new DigesterException(jde);
400:                }
401:                return instances;
402:            }
403:
404:            /**
405:             * Returns a list of assert group instances for a given method name and test
406:             * case.
407:             * 
408:             * @param className
409:             *            the name of the class element
410:             * @param methodName
411:             *            a method name
412:             * @param testCase
413:             *            a test case
414:             * @return a list of assert group instances
415:             * @throws DigesterException
416:             *             in case of any errors
417:             * @throws FileNotFoundException
418:             */
419:            public List getAssertParamGroupInstances(String className,
420:                    String methodName, String testCase)
421:                    throws DigesterException, FileNotFoundException {
422:                List instances = new ArrayList();
423:                try {
424:                    List paramElList = new ArrayList();
425:
426:                    paramElList = xqueryparser.getElements(jDomDocument,
427:                            "/tests/class[@name='" + className
428:                                    + "']/method[./@name='" + methodName
429:                                    + "' and ./@test-case='" + testCase
430:                                    + "']/asserts/assertparamgroup");
431:
432:                    for (Iterator elements = paramElList.iterator(); elements
433:                            .hasNext();) {
434:                        // top level paramgroup instances
435:                        Element item = (Element) elements.next();
436:                        AssertParamGroupInstance pgInstance = parseAssertParamGroup(item);
437:                        instances.add(pgInstance);
438:                    }
439:
440:                    List paramElList2 = xqueryparser
441:                            .getElements(
442:                                    jDomDocument,
443:                                    "/tests/class[@name='"
444:                                            + className
445:                                            + "']/method[./@name='"
446:                                            + methodName
447:                                            + "']/test-case[./@name='testCase']/asserts/assertgroup");
448:
449:                    for (Iterator elements = paramElList2.iterator(); elements
450:                            .hasNext();) {
451:                        // top level paramgroup instances
452:                        Element item = (Element) elements.next();
453:                        AssertParamGroupInstance pgInstance = parseAssertParamGroup(item);
454:                        instances.add(pgInstance);
455:                    }
456:
457:                } catch (XQueryException e) {
458:                    throw new DigesterException(e);
459:                } catch (JDOMException jde) {
460:                    throw new DigesterException(jde);
461:                }
462:                return instances;
463:            }
464:
465:            /**
466:             * Parses an assert group element and creates an assert group instance.
467:             * 
468:             * @param item
469:             *            the assert group element
470:             * @return an assert group instance
471:             * @throws DigesterException
472:             *             in case of any errors
473:             */
474:            private AssertGroupInstance parseAssertGroup(Element item)
475:                    throws DigesterException {
476:                AssertGroupInstance pgInstance = createAssertGroupInstance(item);
477:
478:                // checking for inner contents
479:                List childrenList = item.getChildren();
480:                for (Iterator children = childrenList.iterator(); children
481:                        .hasNext();) {
482:                    Element child = (Element) children.next();
483:                    // adding parameter instance
484:                    if (child.getName().equals("assert")) {
485:                        pgInstance.addAssertInstance(AssertInstance
486:                                .createAssertInstance(child));
487:                    }
488:                    // adding paramgroup instance
489:                    else if (child.getName().equals("assertgroup")) {
490:                        AssertGroupInstance nestedPGInstance = parseAssertGroup(child);
491:                        pgInstance
492:                                .addAssertGroupNestedInstance(nestedPGInstance);
493:                    } else {
494:                        // I should never get here anyway.
495:                        // just throw an exception by now ...
496:                        throw new DigesterException(
497:                                "paramgroup can contain only param or nested param group tags ",
498:                                null);
499:                    }
500:                }
501:                return pgInstance;
502:            }
503:
504:            /**
505:             * Parses an assert group element and creates an assert group instance.
506:             * 
507:             * @param item
508:             *            the assert group element
509:             * @return an assert group instance
510:             * @throws DigesterException
511:             *             in case of any errors
512:             */
513:            private AssertParamGroupInstance parseAssertParamGroup(Element item)
514:                    throws DigesterException {
515:                AssertParamGroupInstance pgInstance = createAssertParamGroupInstance(item);
516:
517:                // checking for inner contents
518:                List childrenList = item.getChildren();
519:                for (Iterator children = childrenList.iterator(); children
520:                        .hasNext();) {
521:                    Element child = (Element) children.next();
522:                    // adding parameter instance
523:                    if (child.getName().equals("assertparam")) {
524:                        pgInstance.addAssertParamInstance(AssertParamInstance
525:                                .createAssertParamInstance(child));
526:                    }
527:                    // adding paramgroup instance
528:                    else if (child.getName().equals("assertparamgroup")) {
529:                        AssertParamGroupInstance nestedPGInstance = parseAssertParamGroup(child);
530:                        pgInstance
531:                                .addAssertParamGroupNestedInstance(nestedPGInstance);
532:                    } else {
533:                        // I should never get here anyway.
534:                        // just throw an exception by now ...
535:                        throw new DigesterException(
536:                                "assertparamgroup can contain only assertparam or nested assertparamgroup tags ",
537:                                null);
538:                    }
539:                }
540:                return pgInstance;
541:            }
542:
543:            /**
544:             * Initialize an assertparam group instance without nested elements
545:             * 
546:             * @param item
547:             *            the assert group element
548:             * @return an assert group instance
549:             */
550:            private AssertGroupInstance createAssertGroupInstance(Element item) {
551:                AssertGroupInstance pgInstance = new AssertGroupInstance(item
552:                        .getAttributeValue("name"));
553:                return pgInstance;
554:            }
555:
556:            /**
557:             * Initialize an assertparam group instance without nested elements
558:             * 
559:             * @param item
560:             *            the assert group element
561:             * @return an assert group instance
562:             */
563:            private AssertParamGroupInstance createAssertParamGroupInstance(
564:                    Element item) {
565:                AssertParamGroupInstance pgInstance = new AssertParamGroupInstance(
566:                        item.getAttributeValue("name"));
567:                return pgInstance;
568:            }
569:
570:            /**
571:             * Returns a list of all param instances for the given method and test case
572:             * 
573:             * @param className
574:             *            the name of the class element
575:             * @param methodName
576:             *            name of the method element
577:             * @param testCase
578:             *            name of the test case
579:             * @return a list of param instances
580:             * @throws DigesterException
581:             *             in case of parsing errors
582:             */
583:            public List getParamsInstances(String className, String methodName,
584:                    String testCase) throws DigesterException {
585:                List list = new ArrayList();
586:                try {
587:                    List paramElList = xqueryparser.getElements(jDomDocument,
588:                            "/tests/class[@name='" + className
589:                                    + "']/method[./@name='" + methodName
590:                                    + "' and ./@test-case='" + testCase
591:                                    + "']/params/param");
592:                    for (Iterator iter = paramElList.iterator(); iter.hasNext();) {
593:                        Element paramEl = (Element) iter.next();
594:                        ParamInstance parameter = getParamInstanceFromElement(paramEl);
595:                        list.add(parameter);
596:                    }
597:                    List paramElList2 = xqueryparser.getElements(jDomDocument,
598:                            "/tests/class[@name='" + className
599:                                    + "']/method[./@name='" + methodName + "']"
600:                                    + "/test-case[./@name='" + testCase
601:                                    + "']/params/param");
602:                    for (Iterator iter = paramElList2.iterator(); iter
603:                            .hasNext();) {
604:                        Element paramEl2 = (Element) iter.next();
605:                        ParamInstance assertion = getParamInstanceFromElement(paramEl2);
606:                        list.add(assertion);
607:                    }
608:
609:                    return list;
610:                } catch (Exception e) {
611:                    throw new DigesterException(e);
612:                }
613:            }
614:
615:            /**
616:             * Analyses a param element from the XML and creates an param instance.
617:             * 
618:             * @param element
619:             *            the XML param element
620:             * @return an param instance
621:             * @throws DigesterException
622:             *             in case of any errors
623:             */
624:            private ParamInstance getParamInstanceFromElement(Element element)
625:                    throws DigesterException {
626:                ParamInstance parameter = createParamInstance(element);
627:                // now, if this parameter element has nested params,
628:                // add the nested elements to this instance (recursively)
629:
630:                // 1) get the children of this element
631:                List nestedElementList = element.getChildren();
632:
633:                // 2) add them to father, and for each, do it recursively
634:                for (Iterator iter = nestedElementList.iterator(); iter
635:                        .hasNext();) {
636:                    Element nestedElement = (Element) iter.next();
637:                    if (nestedElement.getName().equals("param")) {
638:                        // recursively call this function
639:                        ParamInstance nestedInstance = getParamInstanceFromElement(nestedElement);
640:                        // add the istance to the father param
641:                        parameter.addParam(nestedInstance);
642:                    } else if (nestedElement.getName().equals("jice")) {
643:                        // ignore it !!!!!
644:                    }
645:
646:                    else
647:                        throw new DigesterException(
648:                                "param can contain only nested param tags ",
649:                                null);
650:
651:                }
652:
653:                return parameter;
654:            }
655:
656:            /**
657:             * Creates a param instance from an param element. Creates the instance
658:             * without the nested instances.
659:             * 
660:             * @param param
661:             *            the param element
662:             * @return an param instance
663:             */
664:            private ParamInstance createParamInstance(Element param) {
665:                String param_name = param.getAttributeValue("name");
666:                String param_type = param.getAttributeValue("type");
667:                String param_jice = param.getAttributeValue("use-jice");
668:                String param_key_type = param.getAttributeValue("key-type");
669:                String param_value_type = param.getAttributeValue("value-type");
670:
671:                String param_content = param.getTextTrim();
672:
673:                ParamInstance parameter = null;
674:
675:                if (!"yes".equalsIgnoreCase(param_jice)) {
676:                    parameter = new ParamInstance(param_content, param_name,
677:                            param_type, param_key_type, param_value_type);
678:                } else {
679:                    parameter = new ParamInstance(param_content, param_name,
680:                            param_type, param_key_type, param_value_type, param
681:                                    .getChild("jice"));
682:                }
683:                return parameter;
684:            }
685:
686:            /**
687:             * Returns a list of param group instances for a given method name and test
688:             * case.
689:             * 
690:             * @param className
691:             *            the name of the class element
692:             * @param methodName
693:             *            a method name
694:             * @param testCase
695:             *            a test case
696:             * @return a list of param group instances
697:             * @throws DigesterException
698:             *             in case of any errors
699:             * @throws FileNotFoundException
700:             */
701:            public List getParamGroupInstances(String className,
702:                    String methodName, String testCase)
703:                    throws DigesterException, FileNotFoundException {
704:
705:                List instances = new ArrayList();
706:                try {
707:                    List paramElList = new ArrayList();
708:
709:                    paramElList = xqueryparser.getElements(jDomDocument,
710:                            "/tests/class[@name='" + className
711:                                    + "']/method[./@name='" + methodName
712:                                    + "' and ./@test-case='" + testCase
713:                                    + "']/params/paramgroup");
714:
715:                    for (Iterator elements = paramElList.iterator(); elements
716:                            .hasNext();) {
717:                        // top level paramgroup instances
718:                        Element item = (Element) elements.next();
719:                        ParamGroupInstance pgInstance = parseParamGroup(item);
720:                        instances.add(pgInstance);
721:                    }
722:
723:                    List paramElList2 = xqueryparser.getElements(jDomDocument,
724:                            "/tests/class[@name='" + className
725:                                    + "']/method[./@name='" + methodName + "']"
726:                                    + "/test-case[./@name='" + testCase
727:                                    + "']/params/paramgroup");
728:                    for (Iterator iter = paramElList2.iterator(); iter
729:                            .hasNext();) {
730:                        Element item = (Element) iter.next();
731:                        ParamGroupInstance pgInstance = parseParamGroup(item);
732:                        instances.add(pgInstance);
733:                    }
734:
735:                } catch (XQueryException e) {
736:                    throw new DigesterException(e);
737:                } catch (JDOMException jde) {
738:                    throw new DigesterException(jde);
739:                }
740:
741:                return instances;
742:
743:            }
744:
745:            /**
746:             * Parses a param group element and creates a param group instance.
747:             * 
748:             * @param item
749:             *            the param group element
750:             * @return an param group instance
751:             * @throws DigesterException
752:             *             in case of any errors
753:             */
754:            private ParamGroupInstance parseParamGroup(Element item)
755:                    throws DigesterException {
756:                ParamGroupInstance pgInstance = createParamGroupInstance(item);
757:
758:                // checking for inner contents
759:                List childrenList = item.getChildren();
760:                for (Iterator children = childrenList.iterator(); children
761:                        .hasNext();) {
762:                    Element child = (Element) children.next();
763:                    // adding parameter instance
764:                    if (child.getName().equals("param")) {
765:                        pgInstance.addParamInstance(createParamInstance(child));
766:                    }
767:                    // adding paramgroup instance
768:                    else if (child.getName().equals("paramgroup")) {
769:                        ParamGroupInstance nestedPGInstance = parseParamGroup(child);
770:                        pgInstance
771:                                .addParamGroupNestedInstance(nestedPGInstance);
772:                    } else {
773:                        // I should never get here anyway.
774:                        // just throw an exception by now ...
775:                        throw new DigesterException(
776:                                "paramgroup can contain only param or nested param group tags ",
777:                                null);
778:                    }
779:                }
780:                return pgInstance;
781:
782:            }
783:
784:            /**
785:             * Initialize a param group instance without nested elements
786:             * 
787:             * @param item
788:             *            the param group element
789:             * @return an param group instance
790:             */
791:            private ParamGroupInstance createParamGroupInstance(Element item) {
792:                ParamGroupInstance pgInstance = new ParamGroupInstance(item
793:                        .getAttributeValue("name"));
794:                return pgInstance;
795:            }
796:
797:            /**
798:             * Returns all control params .
799:             * 
800:             * @return list of ParamInstance
801:             * @throws DigesterException
802:             *             in case of parsing errors
803:             */
804:            public List getTestCaseControlParams() throws DigesterException {
805:                List list = new ArrayList();
806:                try {
807:                    List expectedElList = xqueryparser.getElements(
808:                            jDomDocument, "/tests/params/param");
809:                    for (Iterator iter = expectedElList.iterator(); iter
810:                            .hasNext();) {
811:                        Element expectedEl = (Element) iter.next();
812:                        list.add(createParamInstance(expectedEl));
813:                    }
814:                    return list;
815:                } catch (Exception e) {
816:                    throw new DigesterException(e);
817:                }
818:            }
819:
820:            /**
821:             * Returns all control paramgroup .
822:             * 
823:             * @return list of ParamInstance
824:             * @throws DigesterException
825:             *             in case of parsing errors
826:             * @throws FileNotFoundException
827:             */
828:            public List getTestCaseControlParamGroups()
829:                    throws DigesterException, FileNotFoundException {
830:                List instances = new ArrayList();
831:                try {
832:                    List paramElList = new ArrayList();
833:
834:                    paramElList = xqueryparser.getElements(jDomDocument,
835:                            "/tests/params/paramgroup");
836:
837:                    for (Iterator elements = paramElList.iterator(); elements
838:                            .hasNext();) {
839:                        // top level paramgroup instances
840:                        Element item = (Element) elements.next();
841:                        ParamGroupInstance pgInstance = parseParamGroup(item);
842:                        instances.add(pgInstance);
843:                    }
844:
845:                } catch (XQueryException e) {
846:                    throw new DigesterException(e);
847:                } catch (JDOMException jde) {
848:                    throw new DigesterException(jde);
849:                }
850:
851:                return instances;
852:            }
853:
854:            /**
855:             * Returns all global params for given class.
856:             * 
857:             * @param className
858:             *            the name of the enclosing tag class
859:             * @return list of ParamInstance
860:             * @throws DigesterException
861:             *             in case of parsing errors
862:             */
863:            public List getTestCaseGlobalParams(String className)
864:                    throws DigesterException, FileNotFoundException {
865:                List list = new ArrayList();
866:                List expectedElList;
867:                try {
868:                    expectedElList = xqueryparser.getElements(jDomDocument,
869:                            "//tests/class[@name='" + className
870:                                    + "']/params/param");
871:
872:                    for (Iterator iter = expectedElList.iterator(); iter
873:                            .hasNext();) {
874:                        Element expectedEl = (Element) iter.next();
875:                        list.add(createParamInstance(expectedEl));
876:                    }
877:                } catch (XQueryException e) {
878:                    throw new DigesterException(e);
879:                } catch (JDOMException jde) {
880:                    throw new DigesterException(jde);
881:                }
882:
883:                return list;
884:
885:            }
886:
887:            /**
888:             * Returns all global paramgroup's for given class.
889:             * 
890:             * @param className
891:             *            the name of the enclosing tag class
892:             * @return list of ParamInstance
893:             * @throws DigesterException
894:             *             in case of parsing errors
895:             * @throws FileNotFoundException
896:             */
897:            public List getTestCaseGlobalParamGroups(String className)
898:                    throws DigesterException, FileNotFoundException {
899:                List instances = new ArrayList();
900:                try {
901:                    List paramElList = new ArrayList();
902:
903:                    paramElList = xqueryparser.getElements(jDomDocument,
904:                            "//tests/class[@name='" + className
905:                                    + "']/params/paramgroup");
906:
907:                    for (Iterator elements = paramElList.iterator(); elements
908:                            .hasNext();) {
909:                        // top level paramgroup instances
910:                        Element item = (Element) elements.next();
911:                        ParamGroupInstance pgInstance = parseParamGroup(item);
912:                        instances.add(pgInstance);
913:                    }
914:
915:                } catch (XQueryException e) {
916:                    throw new DigesterException(e);
917:                } catch (JDOMException jde) {
918:                    throw new DigesterException(jde);
919:                }
920:
921:                return instances;
922:            }
923:
924:            public Document getDocument(String fileName)
925:                    throws FileNotFoundException, XQueryException {
926:
927:                Document doc;
928:
929:                InputStream is = null;
930:                try {
931:                    is = new FileInputStream(fileName);
932:                } catch (FileNotFoundException e) {
933:                    // fails absolute path, will try relative path based on classpath
934:                }
935:
936:                if (is == null) {
937:                    is = getClass().getClassLoader().getResourceAsStream(
938:                            fileName);
939:                }
940:
941:                if (is == null)
942:                    throw new FileNotFoundException();
943:
944:                try {
945:
946:                    SAXBuilder builder = new SAXBuilder();
947:                    doc = builder.build(is);
948:
949:                } catch (JDOMException e) {
950:                    String msg = "***Error: " + e.getMessage();
951:                    throw new XQueryException(msg);
952:                } catch (IOException e) {
953:                    e.printStackTrace();
954:                    throw new FileNotFoundException();
955:                }
956:
957:                return doc;
958:            }
959:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.