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.assertFalse;
026: import static org.junit.Assert.assertNotNull;
027: import static org.junit.Assert.assertNull;
028: import static org.junit.Assert.assertTrue;
029:
030: import java.io.File;
031:
032: import junit.framework.JUnit4TestAdapter;
033: import net.sourceforge.pmd.Report;
034: import net.sourceforge.pmd.RuleContext;
035:
036: import org.junit.Test;
037:
038: public class RuleContextTest {
039:
040: @Test
041: public void testReport() {
042: RuleContext ctx = new RuleContext();
043: assertEquals(0, ctx.getReport().size());
044: Report r = new Report();
045: ctx.setReport(r);
046: Report r2 = ctx.getReport();
047: assertEquals("report object mismatch", r, r2);
048: }
049:
050: @Test
051: public void testSourceCodeFilename() {
052: RuleContext ctx = new RuleContext();
053: assertNull("filename should be null", ctx
054: .getSourceCodeFilename());
055: ctx.setSourceCodeFilename("foo");
056: assertEquals("filename mismatch", "foo", ctx
057: .getSourceCodeFilename());
058: }
059:
060: @Test
061: public void testSourceCodeFile() {
062: RuleContext ctx = new RuleContext();
063: assertNull("file should be null", ctx.getSourceCodeFile());
064: ctx.setSourceCodeFile(new File("somefile.java"));
065: assertEquals("filename mismatch", new File("somefile.java"),
066: ctx.getSourceCodeFile());
067: }
068:
069: @Test
070: public void testAttributes() {
071: RuleContext ctx1 = new RuleContext();
072: Object obj1 = new Object();
073: Object obj2 = new Object();
074: assertNull("attribute should be null", ctx1
075: .getAttribute("attribute"));
076: boolean set = ctx1.setAttribute("attribute", obj1);
077: assertTrue("attribute should have been set", set);
078: assertNotNull("attribute should not be null", ctx1
079: .getAttribute("attribute"));
080: assertTrue("attribute should be expected instance", ctx1
081: .getAttribute("attribute") == obj1);
082: set = ctx1.setAttribute("attribute", obj2);
083: assertFalse("attribute should not have been set", set);
084: assertTrue("attribute should be expected instance", ctx1
085: .getAttribute("attribute") == obj1);
086: Object value = ctx1.removeAttribute("attribute");
087: assertTrue("attribute value should be expected instance",
088: value == obj1);
089: assertNull("attribute should be null", ctx1
090: .getAttribute("attribute"));
091: }
092:
093: @Test
094: public void testSharedAttributes() {
095: RuleContext ctx1 = new RuleContext();
096: RuleContext ctx2 = new RuleContext(ctx1);
097: StringBuilder obj1 = new StringBuilder();
098: StringBuilder obj2 = new StringBuilder();
099:
100: ctx1.setAttribute("attribute1", obj1);
101: ctx2.setAttribute("attribute2", obj2);
102: assertNotNull("attribute should not be null", ctx1
103: .getAttribute("attribute1"));
104: assertNotNull("attribute should not be null", ctx1
105: .getAttribute("attribute2"));
106: assertNotNull("attribute should not be null", ctx2
107: .getAttribute("attribute1"));
108: assertNotNull("attribute should not be null", ctx2
109: .getAttribute("attribute2"));
110: assertTrue("attribute should be expected instance", ctx1
111: .getAttribute("attribute1") == obj1);
112: assertTrue("attribute should be expected instance", ctx1
113: .getAttribute("attribute2") == obj2);
114: assertTrue("attribute should be expected instance", ctx2
115: .getAttribute("attribute1") == obj1);
116: assertTrue("attribute should be expected instance", ctx2
117: .getAttribute("attribute2") == obj2);
118:
119: ctx1.removeAttribute("attribute1");
120: assertNull("attribute should be null", ctx1
121: .getAttribute("attribute1"));
122: assertNull("attribute should be null", ctx2
123: .getAttribute("attribute1"));
124: assertNotNull("attribute should not be null", ctx1
125: .getAttribute("attribute2"));
126: assertNotNull("attribute should not be null", ctx2
127: .getAttribute("attribute2"));
128:
129: StringBuilder value = (StringBuilder) ctx1
130: .getAttribute("attribute2");
131: assertEquals("attribute value should be empty", "", value
132: .toString());
133: value.append("x");
134: StringBuilder value1 = (StringBuilder) ctx1
135: .getAttribute("attribute2");
136: assertEquals("attribute value should be 'x'", "x", value1
137: .toString());
138: StringBuilder value2 = (StringBuilder) ctx2
139: .getAttribute("attribute2");
140: assertEquals("attribute value should be 'x'", "x", value2
141: .toString());
142: }
143:
144: public static junit.framework.Test suite() {
145: return new JUnit4TestAdapter(RuleContextTest.class);
146: }
147: }
|