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


001:        /**
002:         * <copyright>
003:         *  Copyright 1997-2002 InfoEther, LLC
004:         *  under sponsorship of the Defense Advanced Research Projects Agency
005:         (DARPA).
006:         *
007:         *  This program is free software; you can redistribute it and/or modify
008:         *  it under the terms of the Cougaar Open Source License as published
009:         by
010:         *  DARPA on the Cougaar Open Source Website (www.cougaar.org).
011:         *
012:         *  THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
013:         *  PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
014:         *  IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
015:         *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
016:         *  ANY WARRANTIES AS TO NON-INFRINGEMENT.  IN NO EVENT SHALL COPYRIGHT
017:         *  HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
018:         *  DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
019:         *  TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
020:         *  PERFORMANCE OF THE COUGAAR SOFTWARE.
021:         * </copyright>
022:         */package test.net.sourceforge.pmd;
023:
024:        import static org.junit.Assert.assertEquals;
025:        import static org.junit.Assert.assertTrue;
026:
027:        import java.util.Iterator;
028:        import java.util.Map;
029:
030:        import junit.framework.JUnit4TestAdapter;
031:        import net.sourceforge.pmd.AbstractRule;
032:        import net.sourceforge.pmd.IRuleViolation;
033:        import net.sourceforge.pmd.MockRule;
034:        import net.sourceforge.pmd.PMD;
035:        import net.sourceforge.pmd.Report;
036:        import net.sourceforge.pmd.ReportListener;
037:        import net.sourceforge.pmd.Rule;
038:        import net.sourceforge.pmd.RuleContext;
039:        import net.sourceforge.pmd.RuleViolation;
040:        import net.sourceforge.pmd.SourceType;
041:        import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
042:        import net.sourceforge.pmd.ast.SimpleJavaNode;
043:        import net.sourceforge.pmd.ast.SimpleNode;
044:        import net.sourceforge.pmd.renderers.Renderer;
045:        import net.sourceforge.pmd.renderers.XMLRenderer;
046:        import net.sourceforge.pmd.stat.Metric;
047:        import net.sourceforge.pmd.symboltable.SourceFileScope;
048:
049:        import org.junit.Test;
050:
051:        import test.net.sourceforge.pmd.testframework.RuleTst;
052:
053:        public class ReportTest extends RuleTst implements  ReportListener {
054:
055:            private static class FooRule extends AbstractRule {
056:                public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
057:                    if ("Foo".equals(c.getImage()))
058:                        addViolation(ctx, c);
059:                    return ctx;
060:                }
061:
062:                public String getMessage() {
063:                    return "blah";
064:                }
065:
066:                public String getName() {
067:                    return "Foo";
068:                }
069:
070:                public String getRuleSetName() {
071:                    return "RuleSet";
072:                }
073:
074:                public String getDescription() {
075:                    return "desc";
076:                }
077:            }
078:
079:            private boolean violationSemaphore;
080:            private boolean metricSemaphore;
081:
082:            public void ruleViolationAdded(IRuleViolation ruleViolation) {
083:                violationSemaphore = true;
084:            }
085:
086:            public void metricAdded(Metric metric) {
087:                metricSemaphore = true;
088:            }
089:
090:            @Test
091:            public void testBasic() throws Throwable {
092:                Report r = new Report();
093:                runTestFromString(TEST1, new FooRule(), r);
094:                assertTrue(!r.isEmpty());
095:            }
096:
097:            @Test
098:            public void testMetric0() {
099:                Report r = new Report();
100:                assertTrue("Default report shouldn't contain metrics", !r
101:                        .hasMetrics());
102:            }
103:
104:            @Test
105:            public void testMetric1() {
106:                Report r = new Report();
107:                assertTrue("Default report shouldn't contain metrics", !r
108:                        .hasMetrics());
109:
110:                r.addMetric(new Metric("m1", 0, 0.0, 1.0, 2.0, 3.0, 4.0));
111:                assertTrue("Expected metrics weren't there", r.hasMetrics());
112:
113:                Iterator ms = r.metrics();
114:                assertTrue("Should have some metrics in there now", ms
115:                        .hasNext());
116:
117:                Object o = ms.next();
118:                assertTrue("Expected Metric, got " + o.getClass(),
119:                        o instanceof  Metric);
120:
121:                Metric m = (Metric) o;
122:                assertEquals("metric name mismatch", "m1", m.getMetricName());
123:                assertEquals("wrong low value", 1.0, m.getLowValue(), 0.05);
124:                assertEquals("wrong high value", 2.0, m.getHighValue(), 0.05);
125:                assertEquals("wrong avg value", 3.0, m.getAverage(), 0.05);
126:                assertEquals("wrong std dev value", 4.0, m
127:                        .getStandardDeviation(), 0.05);
128:            }
129:
130:            @Test
131:            public void testExclusionsInReportWithAnnotations()
132:                    throws Throwable {
133:                Report rpt = new Report();
134:                runTestFromString(TEST2, new FooRule(), rpt, SourceType.JAVA_15);
135:                assertTrue(rpt.isEmpty());
136:                assertEquals(1, rpt.getSuppressedRuleViolations().size());
137:            }
138:
139:            @Test
140:            public void testExclusionsInReportWithNOPMD() throws Throwable {
141:                Report rpt = new Report();
142:                runTestFromString(TEST3, new FooRule(), rpt);
143:                assertTrue(rpt.isEmpty());
144:                assertEquals(1, rpt.getSuppressedRuleViolations().size());
145:            }
146:
147:            private static final String TEST1 = "public class Foo {}" + PMD.EOL;
148:
149:            private static final String TEST2 = "@SuppressWarnings(\"PMD\")"
150:                    + PMD.EOL + "public class Foo {}";
151:
152:            private static final String TEST3 = "public class Foo {} // NOPMD";
153:
154:            // Files are grouped together now.
155:            @Test
156:            public void testSortedReport_File() {
157:                Report r = new Report();
158:                RuleContext ctx = new RuleContext();
159:                ctx.setSourceCodeFilename("foo");
160:                SimpleNode s = getNode(10, 5, ctx.getSourceCodeFilename());
161:                r.addRuleViolation(new RuleViolation(new MockRule("name",
162:                        "desc", "msg", "rulesetname"), ctx, s));
163:                ctx.setSourceCodeFilename("bar");
164:                SimpleNode s1 = getNode(10, 5, ctx.getSourceCodeFilename());
165:                r.addRuleViolation(new RuleViolation(new MockRule("name",
166:                        "desc", "msg", "rulesetname"), ctx, s1));
167:                Renderer rend = new XMLRenderer();
168:                String result = rend.render(r);
169:                assertTrue("sort order wrong", result.indexOf("bar") < result
170:                        .indexOf("foo"));
171:            }
172:
173:            @Test
174:            public void testSortedReport_Line() {
175:                Report r = new Report();
176:                RuleContext ctx = new RuleContext();
177:                ctx.setSourceCodeFilename("foo1");
178:                SimpleNode s = getNode(10, 5, ctx.getSourceCodeFilename());
179:                r.addRuleViolation(new RuleViolation(new MockRule("rule2",
180:                        "rule2", "msg", "rulesetname"), ctx, s));
181:                ctx.setSourceCodeFilename("foo2");
182:                SimpleNode s1 = getNode(20, 5, ctx.getSourceCodeFilename());
183:                r.addRuleViolation(new RuleViolation(new MockRule("rule1",
184:                        "rule1", "msg", "rulesetname"), ctx, s1));
185:                Renderer rend = new XMLRenderer();
186:                String result = rend.render(r);
187:                assertTrue("sort order wrong", result.indexOf("rule2") < result
188:                        .indexOf("rule1"));
189:            }
190:
191:            @Test
192:            public void testListener() {
193:                Report rpt = new Report();
194:                rpt.addListener(this );
195:                violationSemaphore = false;
196:                RuleContext ctx = new RuleContext();
197:                ctx.setSourceCodeFilename("file");
198:                SimpleNode s = getNode(5, 5, ctx.getSourceCodeFilename());
199:                rpt.addRuleViolation(new RuleViolation(new MockRule("name",
200:                        "desc", "msg", "rulesetname"), ctx, s));
201:                assertTrue(violationSemaphore);
202:
203:                metricSemaphore = false;
204:                rpt.addMetric(new Metric("test", 0, 0.0, 0.0, 0.0, 0.0, 0.0));
205:
206:                assertTrue("no metric", metricSemaphore);
207:            }
208:
209:            @Test
210:            public void testSummary() {
211:                Report r = new Report();
212:                RuleContext ctx = new RuleContext();
213:                ctx.setSourceCodeFilename("foo1");
214:                SimpleNode s = getNode(5, 5, ctx.getSourceCodeFilename());
215:                Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
216:                r.addRuleViolation(new RuleViolation(rule, ctx, s));
217:                ctx.setSourceCodeFilename("foo2");
218:                Rule mr = new MockRule("rule1", "rule1", "msg", "rulesetname");
219:                SimpleNode s1 = getNode(20, 5, ctx.getSourceCodeFilename());
220:                SimpleNode s2 = getNode(30, 5, ctx.getSourceCodeFilename());
221:                r.addRuleViolation(new RuleViolation(mr, ctx, s1));
222:                r.addRuleViolation(new RuleViolation(mr, ctx, s2));
223:                Map summary = r.getSummary();
224:                assertEquals(summary.keySet().size(), 2);
225:                assertTrue(summary.values().contains(new Integer(1)));
226:                assertTrue(summary.values().contains(new Integer(2)));
227:            }
228:
229:            private SimpleNode getNode(int line, int column, String scopeName) {
230:                SimpleNode s = new SimpleJavaNode(2);
231:                SimpleNode parent = new SimpleJavaNode(1);
232:                parent.testingOnly__setBeginLine(line);
233:                parent.testingOnly__setBeginColumn(column);
234:                s.jjtSetParent(parent);
235:                s.setScope(new SourceFileScope(scopeName));
236:                s.testingOnly__setBeginLine(10);
237:                s.testingOnly__setBeginColumn(5);
238:                return s;
239:            }
240:
241:            public static junit.framework.Test suite() {
242:                return new JUnit4TestAdapter(ReportTest.class);
243:            }
244:
245:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.