1 package com.tonbeller.jpivot.table.span; 2 3 import java.util.List ; 4 5 import junit.framework.TestCase; 6 7 import org.jaxen.JaxenException; 8 import org.jaxen.XPath; 9 import org.jaxen.dom.DOMXPath; 10 import org.w3c.dom.Document ; 11 import org.w3c.dom.Element ; 12 import org.w3c.dom.Node ; 13 14 import com.tonbeller.jpivot.olap.model.Property; 15 import com.tonbeller.jpivot.olap.model.impl.PropertyHolderImpl; 16 import com.tonbeller.jpivot.olap.model.impl.PropertyImpl; 17 import com.tonbeller.wcf.utils.XmlUtils; 18 19 24 public class PropertyUtilsTest extends TestCase { 25 Document doc; 26 Element elm; 27 28 32 public PropertyUtilsTest(String arg0) { 33 super(arg0); 34 } 35 36 public static void main(String [] args) { 37 junit.textui.TestRunner.run(PropertyUtilsTest.class); 38 } 39 40 public void testIsStyleProperty() { 41 assertTrue(PropertyUtils.isStyleProperty(PropertyUtils.STYLE_PROPERTY)); 42 assertFalse(PropertyUtils.isStyleProperty(PropertyUtils.STYLE_PROPERTY + "x")); 43 assertFalse(PropertyUtils.isStyleProperty("")); 44 } 45 46 50 public void testAddProperties1() throws JaxenException { 51 PropertyHolderImpl phi = new PropertyHolderImpl(); 52 PropertyImpl pi = new PropertyImpl(); 53 pi.setName("color"); 54 pi.setValue("red"); 55 phi.setProperties(new Property[] { pi }); 56 PropertyUtils.addProperties(elm, phi.getProperties()); 57 assertNodeCount(elm, "property[@name='color' and @value='red']", 1); 58 } 59 60 70 public void testAddProperties2() throws JaxenException { 71 PropertyHolderImpl phi = new PropertyHolderImpl(); 72 73 PropertyImpl p1 = new PropertyImpl(); 74 p1.setName("image.href"); 75 p1.setValue("a"); 76 77 PropertyImpl p2 = new PropertyImpl(); 78 p2.setName("image.src"); 79 p2.setValue("b"); 80 81 phi.setProperties(new Property[] { p1, p2 }); 82 PropertyUtils.addProperties(elm, phi.getProperties()); 83 84 86 assertNodeCount(elm, "property[@name='image']", 1); 87 assertNodeCount(elm, "property", 1); 88 assertNodeCount(elm, "property/property[@name='href' and @value='a']", 1); 89 assertNodeCount(elm, "property/property[@name='src' and @value='b']", 1); 90 } 91 92 96 public void testAddInlineProperties() throws JaxenException { 97 PropertyHolderImpl phi = new PropertyHolderImpl(); 98 PropertyImpl pi1 = new PropertyImpl(); 100 pi1.setName("LiNk"); 101 pi1.setValue("VaLuE"); 102 103 PropertyImpl pi2 = new PropertyImpl(); 105 pi2.setName("NoLiNk"); 106 pi2.setValue("NoVaLuE"); 107 108 phi.setProperties(new Property[] { pi1, pi2 }); 109 PropertyUtils.addInlineProperties(elm, phi.getProperties()); 110 assertNodeCount(elm, "property[@name='link' and @value='VaLuE']", 1); 111 assertNodeCount(elm, "property[@name='LiNk']", 0); 112 assertNodeCount(elm, "property[@name='NoLiNk']", 0); 113 114 PropertyUtils.addProperties(elm, phi.getProperties()); 115 assertNodeCount(elm, "property[@name='LiNk']", 1); 116 assertNodeCount(elm, "property[@name='NoLiNk']", 1); 117 118 } 119 120 public void testAddInlineProperties2() throws JaxenException { 121 PropertyHolderImpl phi = new PropertyHolderImpl(); 122 PropertyImpl pi1 = new PropertyImpl(); 123 pi1.setName("BiLd"); 124 pi1.setValue("/path"); 125 phi.setProperties(new Property[] { pi1 }); 126 PropertyUtils.addInlineProperties(elm, phi.getProperties()); 127 assertNodeCount(elm, "property[@name='image' and @value='/path']", 1); 128 } 129 130 public void testNormalize1() { 131 Property p[] = new PropertyImpl[3]; 132 p[0] = new PropertyImpl("img", "my img"); 133 p[1] = new PropertyImpl("img.src", "src value"); 134 p[2] = new PropertyImpl("img.href", "href value"); 135 136 p = PropertyUtils.normalize(p); 137 assertEquals(1, p.length); 138 Property[] np = p[0].getProperties(); 139 assertEquals(2, np.length); 140 assertEquals("img", p[0].getName()); 141 assertEquals("my img", p[0].getValue()); 142 143 assertEquals("src", np[0].getName()); 144 assertEquals("src value", np[0].getValue()); 145 146 assertEquals("href", np[1].getName()); 147 assertEquals("href value", np[1].getValue()); 148 } 149 150 public void testNormalize2() { 151 Property p[] = new PropertyImpl[1]; 152 p[0] = new PropertyImpl("img", "my img"); 153 p = PropertyUtils.normalize(p); 154 assertEquals(1, p.length); 155 assertEquals("img", p[0].getName()); 156 assertEquals("my img", p[0].getValue()); 157 } 158 159 public void testNormalize3() { 160 Property p[] = new PropertyImpl[6]; 161 p[0] = new PropertyImpl("a", "a value"); 162 p[1] = new PropertyImpl("a.src", "a.src value"); 163 p[2] = new PropertyImpl("a.href", "a.href value"); 164 p[3] = new PropertyImpl("b", "b value"); 165 p[4] = new PropertyImpl("b.src", "b.src value"); 166 p[5] = new PropertyImpl("b.href", "b.href value"); 167 168 p = PropertyUtils.normalize(p); 169 assertEquals(2, p.length); 170 assert3("a", p[0]); 171 assert3("b", p[1]); 172 } 173 174 void assert3(String name, Property p) { 175 assertEquals(name, p.getName()); 176 assertEquals(name + " value", p.getValue()); 177 178 Property[] np = p.getProperties(); 179 assertEquals(2, np.length); 180 181 assertEquals("src", np[0].getName()); 182 assertEquals(name + ".src value", np[0].getValue()); 183 184 assertEquals("href", np[1].getName()); 185 assertEquals(name + ".href value", np[1].getValue()); 186 } 187 188 public void testFlags() { 189 assertTrue(PropertyUtils.isNested("a.b")); 190 assertFalse(PropertyUtils.isNested("a/b")); 191 192 assertTrue(PropertyUtils.isInline("link")); 193 assertTrue(PropertyUtils.isInline("style")); 194 assertTrue(PropertyUtils.isInline("arrow")); 195 assertTrue(PropertyUtils.isInline("image")); 196 assertTrue(PropertyUtils.isInline("stil")); 197 assertTrue(PropertyUtils.isInline("pfeil")); 198 assertTrue(PropertyUtils.isInline("bild")); 199 200 assertTrue(PropertyUtils.isInline("LiNk")); 201 assertTrue(PropertyUtils.isInline("pFeIl")); 202 203 assertFalse(PropertyUtils.isInline("xref")); 204 } 205 206 207 210 protected void setUp() throws Exception { 211 doc = XmlUtils.createDocument(); 212 elm = doc.createElement("elm"); 213 doc.appendChild(elm); 214 } 215 216 220 public void assertNodeCount(Node node, String xpathExpr, int nodeCount) throws JaxenException { 221 XPath xpath = new DOMXPath(xpathExpr); 222 List list = xpath.selectNodes(node); 223 assertEquals("Node count" + xpathExpr, nodeCount, list.size()); 224 } 225 226 } 227 | Popular Tags |