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