1 /***
2 * <copyright>
3 * Copyright 1997-2002 InfoEther, LLC
4 * under sponsorship of the Defense Advanced Research Projects Agency
5 (DARPA).
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the Cougaar Open Source License as published
9 by
10 * DARPA on the Cougaar Open Source Website (www.cougaar.org).
11 *
12 * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
13 * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
14 * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
16 * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
17 * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
18 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
19 * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THE COUGAAR SOFTWARE.
21 * </copyright>
22 */
23 package test.net.sourceforge.pmd;
24
25 import junit.framework.TestCase;
26 import net.sourceforge.pmd.Rule;
27 import net.sourceforge.pmd.RuleContext;
28 import net.sourceforge.pmd.RuleViolation;
29 import test.net.sourceforge.pmd.testframework.MockRule;
30
31 public class RuleViolationTest extends TestCase {
32
33 public void testConstructor1() {
34 Rule rule = new MockRule("name", "desc", "msg");
35 RuleContext ctx = new RuleContext();
36 ctx.setSourceCodeFilename("filename");
37 RuleViolation r = new RuleViolation(rule, 2, ctx);
38 assertEquals("object mismatch", rule, r.getRule());
39 assertEquals("line number is wrong", 2, r.getLine());
40 assertEquals("filename is wrong", "filename", r.getFilename());
41 }
42
43 public void testConstructor2() {
44 Rule rule = new MockRule("name", "desc", "msg");
45 RuleContext ctx = new RuleContext();
46 ctx.setSourceCodeFilename("filename");
47 RuleViolation r = new RuleViolation(rule, 2, "description", ctx);
48 assertEquals("object mismatch", rule, r.getRule());
49 assertEquals("line number is wrong", 2, r.getLine());
50 assertEquals("filename is wrong", "filename", r.getFilename());
51 assertEquals("description is wrong", "description", r.getDescription());
52 }
53
54 public void testComparatorWithDifferentFilenames() {
55 Rule rule = new MockRule("name", "desc", "msg");
56 RuleViolation.RuleViolationComparator comp = new RuleViolation.RuleViolationComparator();
57 RuleContext ctx = new RuleContext();
58 ctx.setSourceCodeFilename("filename1");
59 RuleViolation r1 = new RuleViolation(rule, 10, "description", ctx);
60 ctx.setSourceCodeFilename("filename2");
61 RuleViolation r2 = new RuleViolation(rule, 20, "description", ctx);
62 assertEquals(-1, comp.compare(r1, r2));
63 assertEquals(1, comp.compare(r2, r1));
64 }
65
66 public void testComparatorWithSameFileDifferentLines() {
67 Rule rule = new MockRule("name", "desc", "msg");
68 RuleViolation.RuleViolationComparator comp = new RuleViolation.RuleViolationComparator();
69 RuleContext ctx = new RuleContext();
70 ctx.setSourceCodeFilename("filename");
71 RuleViolation r1 = new RuleViolation(rule, 10, "description", ctx);
72 RuleViolation r2 = new RuleViolation(rule, 20, "description", ctx);
73 assertTrue(comp.compare(r1, r2) < 0);
74 assertTrue(comp.compare(r2, r1) > 0);
75 }
76
77 public void testComparatorWithSameFileSameLines() {
78 Rule rule = new MockRule("name", "desc", "msg");
79 RuleViolation.RuleViolationComparator comp = new RuleViolation.RuleViolationComparator();
80 RuleContext ctx = new RuleContext();
81 ctx.setSourceCodeFilename("filename");
82 RuleViolation r1 = new RuleViolation(rule, 10, "description", ctx);
83 RuleViolation r2 = new RuleViolation(rule, 10, "description", ctx);
84 assertEquals(0, comp.compare(r1, r2));
85 assertEquals(0, comp.compare(r2, r1));
86 }
87 }
This page was automatically generated by Maven