1 5 package com.tonbeller.jpivot.mondrian; 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.util.List ; 13 14 import junit.framework.TestCase; 15 16 import org.xml.sax.SAXException ; 17 18 import com.tonbeller.jpivot.olap.model.Axis; 19 import com.tonbeller.jpivot.olap.model.Hierarchy; 20 import com.tonbeller.jpivot.olap.model.Member; 21 import com.tonbeller.jpivot.olap.model.OlapException; 22 import com.tonbeller.jpivot.olap.model.Position; 23 import com.tonbeller.jpivot.olap.model.Result; 24 import com.tonbeller.jpivot.olap.navi.ChangeSlicer; 25 import com.tonbeller.jpivot.olap.navi.DrillExpandPosition; 26 import com.tonbeller.jpivot.olap.navi.DrillReplace; 27 import com.tonbeller.jpivot.olap.navi.ExpressionParser; 28 import com.tonbeller.jpivot.olap.navi.SortRank; 29 import com.tonbeller.jpivot.olap.query.DrillExpandPositionExt; 30 import com.tonbeller.jpivot.olap.query.DrillReplaceExt; 31 import com.tonbeller.jpivot.olap.query.ExpBean; 32 import com.tonbeller.jpivot.olap.query.PositionNodeBean; 33 import com.tonbeller.jpivot.olap.query.ResultBase; 34 import com.tonbeller.jpivot.tags.MondrianModelFactory; 35 import com.tonbeller.wcf.bookmarks.Bookmarkable; 36 37 40 public class MementoTest extends TestCase { 41 42 44 public MementoTest() { 45 super(); 46 } 47 48 51 public MementoTest(String arg0) { 52 super(arg0); 53 } 54 55 public void testMementoExpandExtensional() throws Exception { 56 MondrianMemento memento = createMementoExpand(Bookmarkable.EXTENSIONAL); 57 assertEquals(2, memento.getQuaxes().length); 58 PositionNodeBean nb = memento.getQuaxes()[1].getPosTreeRoot(); 59 assertEquals(3, nb.getChildren().length); 60 assertEquals( "[Gender].[All Gender].[F]", nb.getChildren()[2].getReference().getName()); 61 String xml = serializeMemento(memento); 62 setMementoExpand(xml); 63 } 64 65 public void testMementoExpandIntensional() throws Exception { 66 MondrianMemento memento = createMementoExpand(Bookmarkable.INTENSIONAL); 67 assertEquals(2, memento.getQuaxes().length); 68 PositionNodeBean nb = memento.getQuaxes()[0].getPosTreeRoot(); 70 assertEquals(1, nb.getChildren().length); 71 assertNull(nb.getReference()); 72 ExpBean eb = nb.getChildren()[0].getReference(); 73 assertEquals(3, eb.getArgs().length); 74 nb = memento.getQuaxes()[1].getPosTreeRoot(); 76 assertEquals(1, nb.getChildren().length); 77 assertNull(nb.getReference()); 78 String [] hiers = {"[Gender]", "[Marital Status]", "[Customers]", "[Product]"}; 79 for (int i = 0; i < hiers.length; i++) { 80 assertEquals(1, nb.getChildren().length); 81 nb = nb.getChildren()[0]; 82 eb = nb.getReference(); 83 assertEquals(ExpBean.TYPE_TOPLEVEL_MEMBERS, eb.getType()); 84 assertEquals(1, eb.getArgs().length); 85 ExpBean arg = eb.getArgs()[0]; 86 assertEquals(ExpBean.TYPE_HIER, arg.getType()); 87 assertEquals(hiers[i], arg.getName()); 88 } 89 90 MondrianModel model = createSimpleModel(); 92 model.setBookmarkState(memento); 93 Result result = model.getResult(); 94 Axis axis = result.getAxes()[1]; 95 assertEquals(1, axis.getPositions().size()); 97 MondrianHierarchy[] mh = (MondrianHierarchy[]) axis.getHierarchies(); 98 for (int i = 0; i < hiers.length; i++) { 99 assertEquals(hiers[i], mh[i].getUniqueName()); 100 } 101 } 102 103 public void testMementoDrillExtensional() throws Exception { 104 MondrianMemento memento = createMementoDrill(Bookmarkable.EXTENSIONAL); 105 assertEquals(2, memento.getQuaxes().length); 106 PositionNodeBean nb = memento.getQuaxes()[1].getPosTreeRoot(); 107 assertEquals(1, nb.getChildren().length); 108 assertEquals("[Store].[All Stores]", nb.getChildren()[0].getReference().getArgs()[0].getName()); 109 String xml = serializeMemento(memento); 110 setMementoDrill(xml); 111 } 112 113 public void testMementoDrillIntensional() throws Exception { 114 MondrianMemento memento = createMementoDrill(Bookmarkable.INTENSIONAL); 115 assertEquals(2, memento.getQuaxes().length); 116 PositionNodeBean nb = memento.getQuaxes()[1].getPosTreeRoot(); 117 assertNull(nb.getReference()); 118 assertEquals(1, nb.getChildren().length); 119 nb = nb.getChildren()[0]; 120 ExpBean eb = nb.getReference(); 121 assertEquals(ExpBean.TYPE_TOPLEVEL_MEMBERS, eb.getType()); 122 assertEquals(1, eb.getArgs().length); 123 eb = eb.getArgs()[0]; 124 assertEquals(ExpBean.TYPE_HIER, eb.getType()); 125 assertEquals("[Store]", eb.getName()); 126 127 MondrianModel model = createSimpleModel(); 128 model.setBookmarkState(memento); 129 Result result = model.getResult(); 130 131 Axis axis = result.getAxes()[1]; 133 assertEquals(1, axis.getPositions().size()); 134 } 135 136 139 public void testSlicerExtensional() throws Exception { 140 MondrianMemento memento = createMementoSlicer(Bookmarkable.EXTENSIONAL); 141 MondrianModel model = createSimpleModel(); 142 model.setBookmarkState(memento); 143 Result result = model.getResult(); 144 Axis axis = result.getSlicer(); 145 assertNotNull(axis); 146 Position pos = (Position) axis.getPositions().get(0); 147 MondrianMember[] m = (MondrianMember[]) pos.getMembers(); 148 assertEquals(1, m.length); 149 assertEquals("[Time].[1997]", m[0].getUniqueName()); 150 } 151 152 156 public void testSlicerIntensional() throws Exception { 157 MondrianMemento memento = createMementoSlicer(Bookmarkable.INTENSIONAL); 158 MondrianModel model = createSimpleModel(); 159 model.setBookmarkState(memento); 160 Result result = model.getResult(); 161 Axis axis = result.getSlicer(); 162 assertNotNull(axis); 163 Position pos = (Position) axis.getPositions().get(0); 164 MondrianMember[] m = (MondrianMember[]) pos.getMembers(); 165 assertEquals(0, m.length); 166 } 167 168 169 public void testSortExtensional() throws Exception { 170 MondrianMemento memento = createMementoSort(createSimpleModel(), Bookmarkable.EXTENSIONAL); 171 assertEquals("[Measures].[Unit Sales]", memento.getSortPosMembers()[0]); 172 Result result = restoreMementoSort(createCalcMeasureModel(), memento); 173 assertEquals(3, result.getAxes()[1].getPositions().size()); 174 } 175 176 public void testSortIntensional() throws Exception { 177 MondrianMemento memento = createMementoSort(createSimpleModel(), Bookmarkable.INTENSIONAL); 178 Result result = restoreMementoSort(createCalcMeasureModel(), memento); 179 assertEquals(1, result.getAxes()[1].getPositions().size()); 180 assertEquals("[Measures].[Unit Sales]", memento.getSortPosMembers()[0]); 181 MondrianModel model = createSimpleModel(); 183 SortRank sr = (SortRank) model.getExtension(SortRank.ID); 184 memento.setSortPosMembers(new String []{"[Measures].[Invalid Measure]"}); 185 model.setBookmarkState(memento); 186 assertFalse(sr.isSorting()); 187 result = model.getResult(); 188 assertEquals(1, result.getAxes()[1].getPositions().size()); 189 } 190 191 public void testSortCalcMeasureExtensional() throws Exception { 192 MondrianMemento memento = createMementoSort(createCalcMeasureModel(), Bookmarkable.EXTENSIONAL); 193 assertEquals("[Measures].[Calculated]", memento.getSortPosMembers()[0]); 194 Result result = restoreMementoSort(createSimpleModel(), memento); 195 assertEquals(3, result.getAxes()[1].getPositions().size()); 196 } 197 198 public void testSortCalcMeasureIntensional() throws Exception { 199 MondrianMemento memento = createMementoSort(createCalcMeasureModel(), Bookmarkable.INTENSIONAL); 200 assertEquals("[Measures].[Calculated]", memento.getSortPosMembers()[0]); 201 Result result = restoreMementoSort(createSimpleModel(), memento); 202 assertEquals(1, result.getAxes()[1].getPositions().size()); 203 } 204 205 public MondrianMemento createMementoSlicer(int levelOfDetail) throws Exception { 206 MondrianModel model = createSimpleModel(); 207 ChangeSlicer cs = (ChangeSlicer) model.getExtension(ChangeSlicer.ID); 208 ExpressionParser ep = (ExpressionParser)model.getExtension(ExpressionParser.ID); 209 Member m = ep.lookupMember("[Time].[1997]"); 210 cs.setSlicer(new Member[]{m}); 211 Result result = model.getResult(); 212 MondrianMemento memento = (MondrianMemento) model.getBookmarkState(levelOfDetail); 214 return memento; 215 } 216 217 public MondrianMemento createMementoSort(MondrianModel model, int levelOfDetail) throws Exception { 218 SortRank sr = (SortRank) model.getExtension(SortRank.ID); 219 Result result = model.getResult(); 221 sr.setSortMode(SortRank.ASC); 222 sr.setSorting(true); 223 Axis axis = result.getAxes()[0]; 224 Position pos = (Position) axis.getPositions().get(0); 225 sr.sort(axis, pos); 226 result = model.getResult(); 228 MondrianMemento memento = (MondrianMemento) model.getBookmarkState(levelOfDetail); 230 assertTrue(memento.isSorting()); 231 assertEquals(1, memento.getSortPosMembers().length); 232 return memento; 233 } 234 235 public Result restoreMementoSort(MondrianModel model, MondrianMemento memento) throws Exception { 236 SortRank sr = (SortRank) model.getExtension(SortRank.ID); 237 model.setBookmarkState(memento); 238 assertTrue(sr.isSorting()); 239 Result result = model.getResult(); 240 assertEquals(2, result.getAxes().length); 241 return result; 242 } 243 244 private MondrianModel createSimpleModel() throws SAXException , IOException , OlapException { 245 String mdxQuery = 246 "select {[Measures].[Unit Sales]} on columns," 247 + " [Product].[Product Family].Members ON rows\n" 248 + "from [Sales]"; 249 MondrianModel model = MondrianModelFactory.instance(); 250 model.setMdxQuery(mdxQuery); 251 TestConnection.initModel(model); 252 return model; 253 } 254 255 private MondrianModel createCalcMeasureModel() throws SAXException , IOException , OlapException { 256 String mdxQuery = 257 "with member [Measures].[Calculated] as '([Measures].[Store Sales] - [Measures].[Store Cost])', format_string = \"#,##0.00\"\n" 258 + "select {[Measures].[Calculated]} ON columns,\n" 259 + " [Product].[Product Family].Members ON rows\n" 260 + "from [Sales]\n"; 261 MondrianModel model = MondrianModelFactory.instance(); 262 model.setMdxQuery(mdxQuery); 263 TestConnection.initModel(model); 264 return model; 265 } 266 267 268 public MondrianMemento createMementoExpand(int levelOfDetail) throws Exception { 269 String renderFile = null; int renderNum = 0; 271 272 String mdxQuery = 273 "select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns," 274 + "{([Gender].[All Gender], [Marital Status].[All Marital Status]," 275 + "[Customers].[All Customers]," 276 + "[Product].[All Products] ) } on rows " 277 + "from Sales where ([Time].[1997])"; 278 279 Result result; 280 List positions; 281 282 MondrianModel model = MondrianModelFactory.instance(); 283 model.setMdxQuery(mdxQuery); 284 TestConnection.initModel(model); 285 286 DrillExpandPositionExt mdep = 287 (DrillExpandPositionExt) model.getExtension(DrillExpandPosition.ID); 288 289 result = model.getResult(); 291 if (renderFile != null) 292 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 293 positions = result.getAxes()[1].getPositions(); 294 Position pos = (Position) positions.get(0); 296 assertTrue(mdep.canExpand(pos, pos.getMembers()[0])); 297 mdep.expand(pos, pos.getMembers()[0]); 299 result = model.getResult(); 300 if (renderFile != null) 301 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 302 positions = result.getAxes()[1].getPositions(); 303 assertPosition( 304 result, 305 1, 306 new String [] { "F", "All Marital Status", "All Customers", "All Products" }); 307 308 pos = (Position) positions.get(1); 310 assertTrue(mdep.canExpand(pos, pos.getMembers()[2])); 311 mdep.expand(pos, pos.getMembers()[2]); 313 result = model.getResult(); 314 if (renderFile != null) 315 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 316 positions = result.getAxes()[1].getPositions(); 317 assertPosition(result, 4, new String [] { "F", "All Marital Status", "USA", "All Products" }); 318 319 pos = (Position) positions.get(4); 321 assertTrue(mdep.canExpand(pos, pos.getMembers()[3])); 322 mdep.expand(pos, pos.getMembers()[3]); 324 result = model.getResult(); 325 if (renderFile != null) 326 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 327 positions = result.getAxes()[1].getPositions(); 328 assertPosition(result, 5, new String [] { "F", "All Marital Status", "USA", "Drink" }); 329 330 pos = (Position) positions.get(5); 332 assertTrue(mdep.canExpand(pos, pos.getMembers()[2])); 333 mdep.expand(pos, pos.getMembers()[2]); 335 result = model.getResult(); 336 if (renderFile != null) 337 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 338 positions = result.getAxes()[1].getPositions(); 339 340 assertPosition(result, 9, new String [] { "F", "All Marital Status", "CA", "Drink" }); 341 assertPosition(result, 14, new String [] { "F", "All Marital Status", "OR", "Food" }); 342 343 pos = (Position) positions.get(1); 345 assertTrue(mdep.canExpand(pos, pos.getMembers()[1])); 346 mdep.expand(pos, pos.getMembers()[1]); 348 result = model.getResult(); 349 if (renderFile != null) 350 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 351 positions = result.getAxes()[1].getPositions(); 352 353 assertPosition(result, positions.size() - 2, new String [] { "F", "S", "WA", "Non-Consumable" }); 354 assertPosition(result, positions.size() - 5, new String [] { "F", "S", "WA", "All Products" }); 355 356 return (MondrianMemento) model.getBookmarkState(levelOfDetail); 357 358 } 359 360 361 public void setMementoExpand(String xml) throws Exception { 362 363 Result result; 364 List positions; 365 366 String renderFile = null; int renderNum = 0; 368 369 MondrianModel model = MondrianModelFactory.instance(); 370 372 DrillExpandPositionExt mdep = 373 (DrillExpandPositionExt) model.getExtension(DrillExpandPosition.ID); 374 375 XMLDecoder d = new XMLDecoder (new ByteArrayInputStream (xml.getBytes())); 377 Object mo = d.readObject(); 378 d.close(); 379 model.setBookmarkState(mo); 380 381 result = model.getResult(); 382 if (renderFile != null) 383 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 384 positions = result.getAxes()[1].getPositions(); 385 386 Position pos = (Position) positions.get(positions.size() - 5); 388 assertTrue(mdep.canCollapse(pos, pos.getMembers()[3])); 389 mdep.collapse(pos, pos.getMembers()[3]); 391 result = model.getResult(); 392 if (renderFile != null) 393 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 394 positions = result.getAxes()[1].getPositions(); 395 396 assertPosition(result, positions.size() - 2, new String [] { "F", "S", "WA", "All Products" }); 397 398 pos = (Position) positions.get(1); 400 assertTrue(mdep.canCollapse(pos, pos.getMembers()[1])); 401 mdep.collapse(pos, pos.getMembers()[1]); 403 result = model.getResult(); 404 if (renderFile != null) 405 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 406 positions = result.getAxes()[1].getPositions(); 407 408 assertPosition(result, 8, new String [] { "F", "All Marital Status", "CA", "All Products" }); 409 410 pos = (Position) positions.get(0); 412 assertTrue(mdep.canCollapse(pos, pos.getMembers()[0])); 413 mdep.collapse(pos, pos.getMembers()[0]); 415 result = model.getResult(); 416 if (renderFile != null) 417 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 418 positions = result.getAxes()[1].getPositions(); 419 assertEquals(positions.size(), 1); 420 } 421 422 public MondrianMemento createMementoDrill(int levelOfDetail) throws Exception { 423 424 String renderFile = null; int renderNum = 0; 426 427 432 String mdxQuery = 433 "select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns, " 434 + "CrossJoin( [Store].[USA].children, [Product].[All Products].[Drink].children) on rows " 435 + "from Sales where ([Time].[1997])"; 436 437 Result result; 438 439 MondrianModel model = MondrianModelFactory.instance(); 440 model.setMdxQuery(mdxQuery); 441 TestConnection.initModel(model); 442 443 result = model.getResult(); 445 if (renderFile != null) 446 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 447 448 assertPosition(result, 1, new String [] { "CA", "Beverages" }); 449 assertPosition(result, 6, new String [] { "WA", "Alcoholic Beverages" }); 450 451 DrillReplaceExt mdrep = (DrillReplaceExt) model.getExtension(DrillReplace.ID); 452 453 Axis[] axes = result.getAxes(); 455 List positions = axes[1].getPositions(); 456 Position pos1 = (Position) positions.get(1); 458 assertTrue(mdrep.canDrillDown(pos1.getMembers()[0])); 459 mdrep.drillDown(pos1.getMembers()[0]); 461 result = model.getResult(); 462 if (renderFile != null) 463 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 464 assertPosition(result, 6, new String [] { "Los Angeles", "Alcoholic Beverages" }); 465 assertPosition(result, 10, new String [] { "San Diego", "Beverages" }); 466 assertPosition(result, 14, new String [] { "San Francisco", "Dairy" }); 467 468 axes = result.getAxes(); 470 positions = axes[1].getPositions(); 471 Position pos0 = (Position) positions.get(0); Hierarchy hier = pos0.getMembers()[0].getLevel().getHierarchy(); 473 474 assertTrue(mdrep.canDrillUp(hier)); 475 mdrep.drillUp(hier); 477 result = model.getResult(); 478 if (renderFile != null) 479 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 480 481 assertPosition(result, 2, new String [] { "CA", "Dairy" }); 482 assertPosition(result, 7, new String [] { "WA", "Beverages" }); 483 484 axes = result.getAxes(); 486 positions = axes[1].getPositions(); 487 pos0 = (Position) positions.get(0); hier = pos0.getMembers()[0].getLevel().getHierarchy(); 489 490 assertTrue(mdrep.canDrillUp(hier)); 491 mdrep.drillUp(hier); 493 result = model.getResult(); 494 if (renderFile != null) 495 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 496 497 assertPosition(result, 7, new String [] { "USA", "Beverages" }); 498 499 return (MondrianMemento) model.getBookmarkState(levelOfDetail); 501 } 502 503 public void setMementoDrill(String xml) throws Exception { 504 505 String renderFile = null; int renderNum = 0; 507 509 Result result; 510 511 MondrianModel model = MondrianModelFactory.instance(); 512 DrillReplaceExt mdrep = (DrillReplaceExt) model.getExtension(DrillReplace.ID); 513 514 XMLDecoder d = new XMLDecoder (new ByteArrayInputStream (xml.getBytes())); 516 Object mo = d.readObject(); 517 d.close(); 518 model.setBookmarkState(mo); 519 520 result = model.getResult(); 521 if (renderFile != null) 522 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 523 524 Axis[] axes = result.getAxes(); 526 List positions = axes[1].getPositions(); 527 Position pos0 = (Position) positions.get(0); Hierarchy hier = pos0.getMembers()[0].getLevel().getHierarchy(); 529 530 assertTrue(mdrep.canDrillUp(hier)); 531 mdrep.drillUp(hier); 533 result = model.getResult(); 534 if (renderFile != null) 535 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 536 537 assertPosition(result, 2, new String [] { "All Stores", "Dairy" }); 538 539 assertTrue(!mdrep.canDrillUp(hier)); 541 542 } 543 544 547 private void assertPosition(Result result, int iPos, String [] posMembers) { 548 Position pos = (Position) result.getAxes()[1].getPositions().get(iPos); 549 for (int i = 0; i < posMembers.length; i++) { 550 String str = pos.getMembers()[i].getLabel(); 551 assertEquals(str, posMembers[i]); 552 } 553 } 554 555 private String serializeMemento(MondrianMemento memento) { 556 ByteArrayOutputStream outs = new ByteArrayOutputStream (); 557 XMLEncoder e = new XMLEncoder (outs); 558 e.writeObject(memento); 559 e.close(); 560 return outs.toString(); 561 } 562 563 } | Popular Tags |