1 /**
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package test.net.sourceforge.pmd;
5
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.assertFalse;
8 import static org.junit.Assert.assertNotNull;
9 import static org.junit.Assert.assertNull;
10 import static org.junit.Assert.assertTrue;
11
12 import java.io.File;
13
14 import junit.framework.JUnit4TestAdapter;
15 import net.sourceforge.pmd.Report;
16 import net.sourceforge.pmd.RuleContext;
17
18 import org.junit.Test;
19
20 public class RuleContextTest {
21
22 @Test
23 public void testReport() {
24 RuleContext ctx = new RuleContext();
25 assertEquals(0, ctx.getReport().size());
26 Report r = new Report();
27 ctx.setReport(r);
28 Report r2 = ctx.getReport();
29 assertEquals("report object mismatch", r, r2);
30 }
31
32 @Test
33 public void testSourceCodeFilename() {
34 RuleContext ctx = new RuleContext();
35 assertNull("filename should be null", ctx.getSourceCodeFilename());
36 ctx.setSourceCodeFilename("foo");
37 assertEquals("filename mismatch", "foo", ctx.getSourceCodeFilename());
38 }
39
40 @Test
41 public void testSourceCodeFile() {
42 RuleContext ctx = new RuleContext();
43 assertNull("file should be null", ctx.getSourceCodeFile());
44 ctx.setSourceCodeFile(new File("somefile.java"));
45 assertEquals("filename mismatch", new File("somefile.java"), ctx.getSourceCodeFile());
46 }
47
48 @Test
49 public void testAttributes() {
50 RuleContext ctx1 = new RuleContext();
51 Object obj1 = new Object();
52 Object obj2 = new Object();
53 assertNull("attribute should be null", ctx1.getAttribute("attribute"));
54 boolean set = ctx1.setAttribute("attribute", obj1);
55 assertTrue("attribute should have been set", set);
56 assertNotNull("attribute should not be null", ctx1.getAttribute("attribute"));
57 assertTrue("attribute should be expected instance", ctx1.getAttribute("attribute") == obj1);
58 set = ctx1.setAttribute("attribute", obj2);
59 assertFalse("attribute should not have been set", set);
60 assertTrue("attribute should be expected instance", ctx1.getAttribute("attribute") == obj1);
61 Object value = ctx1.removeAttribute("attribute");
62 assertTrue("attribute value should be expected instance", value == obj1);
63 assertNull("attribute should be null", ctx1.getAttribute("attribute"));
64 }
65
66 @Test
67 public void testSharedAttributes() {
68 RuleContext ctx1 = new RuleContext();
69 RuleContext ctx2 = new RuleContext(ctx1);
70 StringBuilder obj1 = new StringBuilder();
71 StringBuilder obj2 = new StringBuilder();
72
73 ctx1.setAttribute("attribute1", obj1);
74 ctx2.setAttribute("attribute2", obj2);
75 assertNotNull("attribute should not be null", ctx1.getAttribute("attribute1"));
76 assertNotNull("attribute should not be null", ctx1.getAttribute("attribute2"));
77 assertNotNull("attribute should not be null", ctx2.getAttribute("attribute1"));
78 assertNotNull("attribute should not be null", ctx2.getAttribute("attribute2"));
79 assertTrue("attribute should be expected instance", ctx1.getAttribute("attribute1") == obj1);
80 assertTrue("attribute should be expected instance", ctx1.getAttribute("attribute2") == obj2);
81 assertTrue("attribute should be expected instance", ctx2.getAttribute("attribute1") == obj1);
82 assertTrue("attribute should be expected instance", ctx2.getAttribute("attribute2") == obj2);
83
84 ctx1.removeAttribute("attribute1");
85 assertNull("attribute should be null", ctx1.getAttribute("attribute1"));
86 assertNull("attribute should be null", ctx2.getAttribute("attribute1"));
87 assertNotNull("attribute should not be null", ctx1.getAttribute("attribute2"));
88 assertNotNull("attribute should not be null", ctx2.getAttribute("attribute2"));
89
90 StringBuilder value = (StringBuilder)ctx1.getAttribute("attribute2");
91 assertEquals("attribute value should be empty", "", value.toString());
92 value.append("x");
93 StringBuilder value1 = (StringBuilder)ctx1.getAttribute("attribute2");
94 assertEquals("attribute value should be 'x'", "x", value1.toString());
95 StringBuilder value2 = (StringBuilder)ctx2.getAttribute("attribute2");
96 assertEquals("attribute value should be 'x'", "x", value2.toString());
97 }
98
99 public static junit.framework.Test suite() {
100 return new JUnit4TestAdapter(RuleContextTest.class);
101 }
102 }