RuleContextTest xref

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

AltStyle によって変換されたページ (->オリジナル) /