Source Code Cross Referenced for JUnitUseExpected.java in  » Code-Analyzer » pmd-4.2rc1 » net » sourceforge » pmd » rules » migration » 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 » Code Analyzer » pmd 4.2rc1 » net.sourceforge.pmd.rules.migration 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003:         */package net.sourceforge.pmd.rules.migration;
004:
005:        import net.sourceforge.pmd.ast.ASTAnnotation;
006:        import net.sourceforge.pmd.ast.ASTBlock;
007:        import net.sourceforge.pmd.ast.ASTBlockStatement;
008:        import net.sourceforge.pmd.ast.ASTCatchStatement;
009:        import net.sourceforge.pmd.ast.ASTClassOrInterfaceBodyDeclaration;
010:        import net.sourceforge.pmd.ast.ASTMethodDeclaration;
011:        import net.sourceforge.pmd.ast.ASTName;
012:        import net.sourceforge.pmd.ast.ASTThrowStatement;
013:        import net.sourceforge.pmd.ast.ASTTryStatement;
014:        import net.sourceforge.pmd.ast.Node;
015:        import net.sourceforge.pmd.ast.SimpleNode;
016:        import net.sourceforge.pmd.rules.junit.AbstractJUnitRule;
017:
018:        import java.util.ArrayList;
019:        import java.util.List;
020:
021:        /**
022:         * This rule finds code like this:
023:         * 
024:         * <pre>
025:         * public void testFoo() {
026:         *     try {
027:         *         doSomething();
028:         *         fail(&quot;should have thrown an exception&quot;);
029:         *     } catch (Exception e) {
030:         *     }
031:         * }
032:         * </pre>
033:         * 
034:         * In JUnit 4, use
035:         * 
036:         * <pre>
037:         *  &#064;Test(expected = Exception.class)
038:         * </pre>
039:         * 
040:         * @author acaplan
041:         * 
042:         */
043:        public class JUnitUseExpected extends AbstractJUnitRule {
044:
045:            public Object visit(ASTClassOrInterfaceBodyDeclaration node,
046:                    Object data) {
047:                boolean inAnnotation = false;
048:                for (int i = 0; i < node.jjtGetNumChildren(); i++) {
049:                    Node child = node.jjtGetChild(i);
050:                    if (ASTAnnotation.class.equals(child.getClass())) {
051:                        ASTName annotationName = ((SimpleNode) child)
052:                                .getFirstChildOfType(ASTName.class);
053:                        if ("Test".equals(annotationName.getImage())) {
054:                            inAnnotation = true;
055:                            continue;
056:                        }
057:                    }
058:                    if (ASTMethodDeclaration.class.equals(child.getClass())) {
059:                        boolean isJUnitMethod = isJUnitMethod(
060:                                (ASTMethodDeclaration) child, data);
061:                        if (inAnnotation || isJUnitMethod) {
062:                            List<SimpleNode> found = new ArrayList<SimpleNode>();
063:                            found.addAll((List<SimpleNode>) visit(
064:                                    (ASTMethodDeclaration) child, data));
065:                            for (SimpleNode name : found) {
066:                                addViolation(data, name);
067:                            }
068:                        }
069:                    }
070:                    inAnnotation = false;
071:                }
072:
073:                return super .visit(node, data);
074:            }
075:
076:            public Object visit(ASTMethodDeclaration node, Object data) {
077:                List<ASTTryStatement> catches = node
078:                        .findChildrenOfType(ASTTryStatement.class);
079:                List<SimpleNode> found = new ArrayList<SimpleNode>();
080:                if (catches.isEmpty()) {
081:                    return found;
082:                }
083:                for (ASTTryStatement trySt : catches) {
084:                    ASTCatchStatement cStatement = getCatch(trySt);
085:                    if (cStatement != null) {
086:                        ASTBlock block = (ASTBlock) cStatement.jjtGetChild(1);
087:                        if (block.jjtGetNumChildren() != 0) {
088:                            continue;
089:                        }
090:                        List<ASTBlockStatement> blocks = ((SimpleNode) trySt
091:                                .jjtGetChild(0))
092:                                .findChildrenOfType(ASTBlockStatement.class);
093:                        if (blocks.isEmpty()) {
094:                            continue;
095:                        }
096:                        ASTBlockStatement st = blocks.get(blocks.size() - 1);
097:                        ASTName name = st.getFirstChildOfType(ASTName.class);
098:                        if (name != null && st.equals(name.getNthParent(5))
099:                                && "fail".equals(name.getImage())) {
100:                            found.add(name);
101:                            continue;
102:                        }
103:                        ASTThrowStatement th = st
104:                                .getFirstChildOfType(ASTThrowStatement.class);
105:                        if (th != null && st.equals(th.getNthParent(2))) {
106:                            found.add(th);
107:                            continue;
108:                        }
109:                    }
110:                }
111:                return found;
112:            }
113:
114:            private ASTCatchStatement getCatch(Node n) {
115:                for (int i = 0; i < n.jjtGetNumChildren(); i++) {
116:                    if (n.jjtGetChild(i) instanceof  ASTCatchStatement) {
117:                        return (ASTCatchStatement) n.jjtGetChild(i);
118:                    }
119:                }
120:                return null;
121:            }
122:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.