1 5 package com.tonbeller.jpivot.xmla; 6 7 import java.beans.XMLDecoder ; 8 import java.beans.XMLEncoder ; 9 import java.io.ByteArrayInputStream ; 10 import java.io.ByteArrayOutputStream ; 11 import java.io.IOException ; 12 import java.io.StringReader ; 13 import java.net.URL ; 14 import java.util.List ; 15 16 import javax.xml.parsers.DocumentBuilder ; 17 import javax.xml.parsers.DocumentBuilderFactory ; 18 import javax.xml.parsers.ParserConfigurationException ; 19 20 import junit.framework.TestCase; 21 22 import org.jaxen.XPath; 23 import org.jaxen.dom.DOMXPath; 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.Node ; 27 import org.xml.sax.InputSource ; 28 import org.xml.sax.SAXException ; 29 30 import com.tonbeller.jpivot.core.ModelFactory; 31 import com.tonbeller.jpivot.olap.model.Axis; 32 import com.tonbeller.jpivot.olap.model.Hierarchy; 33 import com.tonbeller.jpivot.olap.model.Position; 34 import com.tonbeller.jpivot.olap.model.Result; 35 import com.tonbeller.jpivot.olap.navi.DrillExpandPosition; 36 import com.tonbeller.jpivot.olap.navi.DrillReplace; 37 import com.tonbeller.jpivot.olap.query.DrillExpandPositionExt; 38 import com.tonbeller.jpivot.olap.query.DrillReplaceExt; 39 import com.tonbeller.jpivot.olap.query.ResultBase; 40 import com.tonbeller.wcf.bookmarks.Bookmarkable; 41 42 45 public class MementoTest extends TestCase { 46 47 private static String CLASS_EXPBEAN = "[@class='com.tonbeller.jpivot.olap.query.ExpBean']"; 48 private static String CLASS_POSITIONNODEBEAN = 49 "[@class='com.tonbeller.jpivot.olap.query.PositionNodeBean']"; 50 51 53 public MementoTest() { 54 super(); 55 } 56 57 60 public MementoTest(String arg0) { 61 super(arg0); 62 } 63 64 public void testMementoExpand() throws Exception { 65 String xml = createMementoExpand(); 66 setMementoExpand(xml); 67 } 68 69 public void testMementoDrill() throws Exception { 70 String xml = createMementoDrill(); 71 setMementoDrill(xml); 72 } 73 74 public String createMementoExpand() throws Exception { 75 82 String renderFile = null; int renderNum = 0; 84 85 String mdxQuery = 86 "select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns," 87 + "{([Gender].[All Gender], [Marital Status].[All Marital Status]," 88 + "[Customers].[All Customers]," 89 + "[Product].[All Products] ) } on rows " 90 + "from Sales where ([Time].[1997])"; 91 92 Result result; 93 List positions; 94 95 URL confUrl = XMLA_Model.class.getResource("config.xml"); 96 XMLA_Model model = (XMLA_Model) ModelFactory.instance(confUrl); 97 98 model.setMdxQuery(mdxQuery); 99 TestConnection.initModel(model); 100 101 DrillExpandPositionExt mdep = 102 (DrillExpandPositionExt) model.getExtension(DrillExpandPosition.ID); 103 104 result = model.getResult(); 106 if (renderFile != null) 107 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 108 positions = result.getAxes()[1].getPositions(); 109 Position pos = (Position) positions.get(0); 111 assertTrue(mdep.canExpand(pos, pos.getMembers()[0])); 112 mdep.expand(pos, pos.getMembers()[0]); 114 result = model.getResult(); 115 if (renderFile != null) 116 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 117 positions = result.getAxes()[1].getPositions(); 118 assertPosition( 119 result, 120 1, 121 new String [] { "F", "All Marital Status", "All Customers", "All Products" }); 122 123 pos = (Position) positions.get(1); 125 assertTrue(mdep.canExpand(pos, pos.getMembers()[2])); 126 mdep.expand(pos, pos.getMembers()[2]); 128 result = model.getResult(); 129 if (renderFile != null) 130 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 131 positions = result.getAxes()[1].getPositions(); 132 assertPosition(result, 4, new String [] { "F", "All Marital Status", "USA", "All Products" }); 133 134 pos = (Position) positions.get(4); 136 assertTrue(mdep.canExpand(pos, pos.getMembers()[3])); 137 mdep.expand(pos, pos.getMembers()[3]); 139 result = model.getResult(); 140 if (renderFile != null) 141 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 142 positions = result.getAxes()[1].getPositions(); 143 assertPosition(result, 5, new String [] { "F", "All Marital Status", "USA", "Drink" }); 144 145 pos = (Position) positions.get(5); 147 assertTrue(mdep.canExpand(pos, pos.getMembers()[2])); 148 mdep.expand(pos, pos.getMembers()[2]); 150 result = model.getResult(); 151 if (renderFile != null) 152 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 153 positions = result.getAxes()[1].getPositions(); 154 155 assertPosition(result, 9, new String [] { "F", "All Marital Status", "CA", "Drink" }); 156 assertPosition(result, 14, new String [] { "F", "All Marital Status", "OR", "Food" }); 157 158 pos = (Position) positions.get(1); 160 assertTrue(mdep.canExpand(pos, pos.getMembers()[1])); 161 mdep.expand(pos, pos.getMembers()[1]); 163 result = model.getResult(); 164 if (renderFile != null) 165 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 166 positions = result.getAxes()[1].getPositions(); 167 168 assertPosition(result, positions.size() - 2, new String [] { "F", "S", "WA", "Non-Consumable" }); 169 assertPosition(result, positions.size() - 5, new String [] { "F", "S", "WA", "All Products" }); 170 171 XMLA_Memento memento = (XMLA_Memento) model.getBookmarkState(Bookmarkable.EXTENSIONAL); 173 ByteArrayOutputStream outs = new ByteArrayOutputStream (); 174 XMLEncoder e = new XMLEncoder (outs); 175 e.writeObject(memento); 176 e.close(); 177 178 String xml = outs.toString(); 179 Document dom = buildDomTree(xml); 180 181 XPath xpath = 184 new DOMXPath("/java/object[@class='com.tonbeller.jpivot.xmla.XMLA_Memento']}"); 185 List results = xpath.selectNodes(dom); 186 assertEquals(results.size(), 1); Node node = (Node ) results.get(0); 188 xpath = new DOMXPath("//object[@class='com.tonbeller.jpivot.olap.query.QuaxBean']"); 191 results = xpath.selectNodes(node); 192 assertEquals(results.size(), 2); node = (Node ) results.get(1); 195 201 xpath = new DOMXPath("./void[@property='posTreeRoot']/object" + CLASS_POSITIONNODEBEAN); 203 results = xpath.selectNodes(node); 204 node = (Node ) results.get(0); xpath = new DOMXPath("./void[@property='children']/array" + CLASS_POSITIONNODEBEAN); 206 results = xpath.selectNodes(node); 207 node = (Node ) results.get(0); String length = ((Element ) node).getAttribute("length"); 209 assertEquals(length, "3"); 210 xpath = 213 new DOMXPath( 214 "./void[@index='2']/object" 215 + CLASS_POSITIONNODEBEAN 216 + "/void[@property='reference']/object" 217 + CLASS_EXPBEAN 218 + "/void[@property='name']/string"); 219 results = xpath.selectNodes(node); 220 node = (Node ) results.get(0); 221 String text = ((Element ) node).getFirstChild().getNodeValue(); 222 assertEquals(text, "[Gender].[All Gender].[F]"); 223 224 return xml; 225 } 226 227 public void setMementoExpand(String xml) throws Exception { 228 229 Result result; 230 List positions; 231 232 String renderFile = null; int renderNum = 0; 234 235 URL confUrl = XMLA_Model.class.getResource("config.xml"); 236 XMLA_Model model = (XMLA_Model) ModelFactory.instance(confUrl); 237 238 240 DrillExpandPositionExt mdep = 241 (DrillExpandPositionExt) model.getExtension(DrillExpandPosition.ID); 242 243 XMLDecoder d = new XMLDecoder (new ByteArrayInputStream (xml.getBytes())); 245 Object mo = d.readObject(); 246 d.close(); 247 model.setBookmarkState(mo); 248 249 result = model.getResult(); 250 if (renderFile != null) 251 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 252 positions = result.getAxes()[1].getPositions(); 253 254 Position pos = (Position) positions.get(positions.size() - 5); 256 assertTrue(mdep.canCollapse(pos, pos.getMembers()[3])); 257 mdep.collapse(pos, pos.getMembers()[3]); 259 result = model.getResult(); 260 if (renderFile != null) 261 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 262 positions = result.getAxes()[1].getPositions(); 263 264 assertPosition(result, positions.size() - 2, new String [] { "F", "S", "WA", "All Products" }); 265 266 pos = (Position) positions.get(1); 268 assertTrue(mdep.canCollapse(pos, pos.getMembers()[1])); 269 mdep.collapse(pos, pos.getMembers()[1]); 271 result = model.getResult(); 272 if (renderFile != null) 273 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 274 positions = result.getAxes()[1].getPositions(); 275 276 assertPosition(result, 8, new String [] { "F", "All Marital Status", "CA", "All Products" }); 277 278 pos = (Position) positions.get(0); 280 assertTrue(mdep.canCollapse(pos, pos.getMembers()[0])); 281 mdep.collapse(pos, pos.getMembers()[0]); 283 result = model.getResult(); 284 if (renderFile != null) 285 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 286 positions = result.getAxes()[1].getPositions(); 287 assertEquals(positions.size(), 1); 288 } 289 290 public String createMementoDrill() throws Exception { 291 292 String renderFile = null; int renderNum = 0; 294 295 300 String mdxQuery = 301 "select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns, " 302 + "CrossJoin( [Store].[USA].children, [Product].[All Products].[Drink].children) on rows " 303 + "from Sales where ([Time].[1997])"; 304 305 Result result; 306 307 URL confUrl = XMLA_Model.class.getResource("config.xml"); 308 XMLA_Model model = (XMLA_Model) ModelFactory.instance(confUrl); 309 310 model.setMdxQuery(mdxQuery); 311 TestConnection.initModel(model); 312 313 result = model.getResult(); 315 if (renderFile != null) 316 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 317 318 assertPosition(result, 1, new String [] { "CA", "Beverages" }); 319 assertPosition(result, 6, new String [] { "WA", "Alcoholic Beverages" }); 320 321 DrillReplaceExt mdrep = (DrillReplaceExt) model.getExtension(DrillReplace.ID); 322 323 Axis[] axes = result.getAxes(); 325 List positions = axes[1].getPositions(); 326 Position pos1 = (Position) positions.get(1); 328 assertTrue(mdrep.canDrillDown(pos1.getMembers()[0])); 329 mdrep.drillDown(pos1.getMembers()[0]); 331 result = model.getResult(); 332 if (renderFile != null) 333 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 334 assertPosition(result, 6, new String [] { "Los Angeles", "Alcoholic Beverages" }); 335 assertPosition(result, 10, new String [] { "San Diego", "Beverages" }); 336 assertPosition(result, 14, new String [] { "San Francisco", "Dairy" }); 337 338 axes = result.getAxes(); 340 positions = axes[1].getPositions(); 341 Position pos0 = (Position) positions.get(0); Hierarchy hier = pos0.getMembers()[0].getLevel().getHierarchy(); 343 344 assertTrue(mdrep.canDrillUp(hier)); 345 mdrep.drillUp(hier); 347 result = model.getResult(); 348 if (renderFile != null) 349 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 350 351 assertPosition(result, 2, new String [] { "CA", "Dairy" }); 352 assertPosition(result, 7, new String [] { "WA", "Beverages" }); 353 354 axes = result.getAxes(); 356 positions = axes[1].getPositions(); 357 pos0 = (Position) positions.get(0); hier = pos0.getMembers()[0].getLevel().getHierarchy(); 359 360 assertTrue(mdrep.canDrillUp(hier)); 361 mdrep.drillUp(hier); 363 result = model.getResult(); 364 if (renderFile != null) 365 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 366 367 assertPosition(result, 7, new String [] { "USA", "Beverages" }); 368 369 XMLA_Memento memento = (XMLA_Memento) model.getBookmarkState(Bookmarkable.EXTENSIONAL); 371 ByteArrayOutputStream outs = new ByteArrayOutputStream (); 372 XMLEncoder e = new XMLEncoder (outs); 373 e.writeObject(memento); 374 e.close(); 375 376 String xml = outs.toString(); 377 Document dom = buildDomTree(xml); 378 System.out.println(xml); 379 380 XPath xpath = 383 new DOMXPath("/java/object[@class='com.tonbeller.jpivot.xmla.XMLA_Memento'"); 384 List results = xpath.selectNodes(dom); 385 assertEquals(results.size(), 1); Node node = (Node ) results.get(0); 387 xpath = new DOMXPath("//object[@class='com.tonbeller.jpivot.olap.query.QuaxBean'"); 390 results = xpath.selectNodes(node); 391 assertEquals(results.size(), 2); node = (Node ) results.get(1); 394 400 xpath = 402 new DOMXPath( 403 "./void[@property='posTreeRoot']/object" 404 + CLASS_POSITIONNODEBEAN 405 + "/void[@property='children']/array" 406 + CLASS_POSITIONNODEBEAN 407 + "/void[@index='0']/object" 408 + CLASS_POSITIONNODEBEAN 409 + "/void[@property='reference']/object" 410 + CLASS_EXPBEAN); 411 results = xpath.selectNodes(node); 412 node = (Node ) results.get(0); 413 415 xpath = 416 new DOMXPath( 417 "./void[@property='args']/array" 418 + CLASS_EXPBEAN 419 + "/void[@index='0']/object" 420 + CLASS_EXPBEAN 421 + "/void[@property='name']/string"); 422 results = xpath.selectNodes(node); 423 node = (Node ) results.get(0); 424 String text = ((Element ) node).getFirstChild().getNodeValue(); 426 assertEquals(text, "[Store].[All Stores]"); 427 428 return xml; 429 430 } 431 432 public void setMementoDrill(String xml) throws Exception { 433 434 String renderFile = null; int renderNum = 0; 436 438 Result result; 439 440 URL confUrl = XMLA_Model.class.getResource("config.xml"); 441 XMLA_Model model = (XMLA_Model) ModelFactory.instance(confUrl); 442 443 DrillReplaceExt mdrep = (DrillReplaceExt) model.getExtension(DrillReplace.ID); 444 445 XMLDecoder d = new XMLDecoder (new ByteArrayInputStream (xml.getBytes())); 447 Object mo = d.readObject(); 448 d.close(); 449 model.setBookmarkState(mo); 450 451 result = model.getResult(); 452 if (renderFile != null) 453 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 454 455 Axis[] axes = result.getAxes(); 457 List positions = axes[1].getPositions(); 458 Position pos0 = (Position) positions.get(0); Hierarchy hier = pos0.getMembers()[0].getLevel().getHierarchy(); 460 461 assertTrue(mdrep.canDrillUp(hier)); 462 mdrep.drillUp(hier); 464 result = model.getResult(); 465 if (renderFile != null) 466 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 467 468 assertPosition(result, 2, new String [] { "All Stores", "Dairy" }); 469 470 assertTrue(!mdrep.canDrillUp(hier)); 472 473 } 474 475 478 private void assertPosition(Result result, int iPos, String [] posMembers) { 479 Position pos = (Position) result.getAxes()[1].getPositions().get(iPos); 480 for (int i = 0; i < posMembers.length; i++) { 481 String str = pos.getMembers()[i].getLabel(); 482 assertEquals(str, posMembers[i]); 483 } 484 } 485 486 489 private Document buildDomTree(String source) 490 throws ParserConfigurationException , IOException , SAXException { 491 Document xmlDoc = null; 492 493 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); 495 DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); 497 if (source != null && source.compareTo("") != 0) 499 xmlDoc = dBuilder.parse(new InputSource (new StringReader (source))); 501 else 502 xmlDoc = dBuilder.newDocument(); 503 504 return xmlDoc; 505 } 506 507 } | Popular Tags |