1 package com.tonbeller.jpivot.mondrian; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import junit.framework.TestCase; 7 8 import com.tonbeller.jpivot.olap.model.Axis; 9 import com.tonbeller.jpivot.olap.model.Position; 10 import com.tonbeller.jpivot.olap.model.Result; 11 import com.tonbeller.jpivot.olap.navi.DrillExpandPosition; 12 import com.tonbeller.jpivot.olap.navi.PlaceMembersOnAxes; 13 import com.tonbeller.jpivot.olap.navi.SortRank; 14 import com.tonbeller.jpivot.olap.query.DrillExpandPositionExt; 15 import com.tonbeller.jpivot.olap.query.ResultBase; 16 import com.tonbeller.jpivot.tags.MondrianModelFactory; 17 18 22 public class OrderingTest extends TestCase { 23 24 28 public OrderingTest(String arg0) { 29 super(arg0); 30 } 31 32 public void testOrderDrillPos() throws Exception { 33 34 40 String mdxQuery = 41 "select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns," 42 + "{([Gender].[All Gender], [Customers].[All Customers]," 43 + "[Product].[All Products] ) } on rows" 44 + " from Sales where ([Time].[1997])"; 45 46 String renderFile = null; int renderNum = 0; 48 49 Result result; 50 51 MondrianModel model = MondrianModelFactory.instance(); 52 model.setMdxQuery(mdxQuery); 53 TestConnection.initModel(model); 54 55 DrillExpandPositionExt mdep = 56 (DrillExpandPositionExt)model.getExtension(DrillExpandPosition.ID); 57 MondrianSortRank msr = (MondrianSortRank)model.getExtension(SortRank.ID); 58 59 result = model.getResult(); 61 if (renderFile != null) 62 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 63 64 Axis[] axes = result.getAxes(); 66 List positions = axes[1].getPositions(); 67 Position pos = (Position)positions.get(0); 68 69 assertTrue(mdep.canExpand(pos, pos.getMembers()[1])); 70 mdep.expand(pos, pos.getMembers()[1]); 72 result = model.getResult(); 73 if (renderFile != null) 74 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 75 76 axes = result.getAxes(); 77 positions = axes[1].getPositions(); 78 assertPosition(result, 3, "All Gender", "USA", "All Products"); 79 80 pos = (Position)positions.get(3); 82 assertTrue(mdep.canExpand(pos, pos.getMembers()[2])); 83 mdep.expand(pos, pos.getMembers()[2]); result = model.getResult(); 85 if (renderFile != null) 86 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 87 88 axes = result.getAxes(); 89 positions = axes[1].getPositions(); 90 assertPosition(result, 5, "All Gender", "USA", "Food"); 91 92 pos = (Position)positions.get(0); 94 assertTrue(mdep.canExpand(pos, pos.getMembers()[0])); 95 mdep.expand(pos, pos.getMembers()[0]); result = model.getResult(); 97 if (renderFile != null) 98 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 99 100 axes = result.getAxes(); 101 positions = axes[1].getPositions(); 102 assertPosition(result, 11, "F", "USA", "Drink"); 103 104 pos = (Position)positions.get(0); 106 assertTrue(mdep.canCollapse(pos, pos.getMembers()[0])); 107 mdep.collapse(pos, pos.getMembers()[0]); result = model.getResult(); 109 if (renderFile != null) 110 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 111 112 axes = result.getAxes(); 113 positions = axes[1].getPositions(); 114 assertPosition(result, 4, "All Gender", "USA", "Drink"); 115 116 msr.setSortMode(SortRank.DESC); 118 positions = axes[0].getPositions(); 119 Position sortPos = (Position)positions.get(0); msr.setSorting(true); 121 msr.sort(axes[1], sortPos); 122 result = model.getResult(); 123 if (renderFile != null) 124 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 125 126 axes = result.getAxes(); 127 positions = axes[1].getPositions(); 128 assertPosition(result, 2, "All Gender", "USA", "Food"); 129 130 msr.setSortMode(SortRank.ASC); 132 positions = axes[0].getPositions(); 133 sortPos = (Position)positions.get(1); msr.sort(axes[1], sortPos); 135 result = model.getResult(); 136 if (renderFile != null) 137 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 138 139 axes = result.getAxes(); 140 positions = axes[1].getPositions(); 141 assertPosition(result, 6, "All Gender", "USA", "Food"); 142 143 msr.setSorting(false); 145 result = model.getResult(); 146 if (renderFile != null) 147 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 148 149 axes = result.getAxes(); 150 positions = axes[1].getPositions(); 151 assertPosition(result, 5, "All Gender", "USA", "Food"); 152 153 } 154 155 public void testOrderSwitchMode() throws Exception { 156 157 162 String mdxQuery = 163 "select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns," 164 + "{([Product].[All Products], [Customers].[All Customers]) } on rows" 165 + " from Sales where ([Time].[1997])"; 166 167 String renderFile = null; int renderNum = 0; 169 170 Result result; 171 172 MondrianModel model = MondrianModelFactory.instance(); 173 model.setMdxQuery(mdxQuery); 174 TestConnection.initModel(model); 175 176 DrillExpandPositionExt mdep = 177 (DrillExpandPositionExt)model.getExtension(DrillExpandPosition.ID); 178 MondrianSortRank msr = (MondrianSortRank)model.getExtension(SortRank.ID); 179 180 result = model.getResult(); 182 if (renderFile != null) 183 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 184 185 186 Axis[] axes = result.getAxes(); 188 List positions = axes[1].getPositions(); 189 Position pos = (Position)positions.get(0); 190 191 assertTrue(mdep.canExpand(pos, pos.getMembers()[0])); 192 mdep.expand(pos, pos.getMembers()[0]); 194 result = model.getResult(); 195 if (renderFile != null) 196 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 197 198 axes = result.getAxes(); 199 positions = axes[1].getPositions(); 200 assertPosition(result, 2, "Food", "All Customers", null); 201 202 pos = (Position)positions.get(2); 204 assertTrue(mdep.canExpand(pos, pos.getMembers()[0])); 205 mdep.expand(pos, pos.getMembers()[0]); result = model.getResult(); 207 if (renderFile != null) 208 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 209 210 axes = result.getAxes(); 211 positions = axes[1].getPositions(); 212 assertPosition(result, 5, "Breakfast Foods", "All Customers", null); 213 214 msr.setSortMode(SortRank.DESC); 216 positions = axes[0].getPositions(); 217 Position sortPos = (Position)positions.get(0); msr.setSorting(true); 219 msr.sort(axes[1], sortPos); 220 result = model.getResult(); 221 if (renderFile != null) 222 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 223 224 axes = result.getAxes(); 225 positions = axes[1].getPositions(); 226 assertPosition(result, 3, "Snack Foods", "All Customers", null); 227 228 msr.setSortMode(SortRank.TOPCOUNT); 230 result = model.getResult(); 231 if (renderFile != null) 232 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 233 234 axes = result.getAxes(); 235 positions = axes[1].getPositions(); 236 assertPosition(result, 3, "Produce", "All Customers", null); 237 238 msr.setSortMode(SortRank.ASC); 240 result = model.getResult(); 241 if (renderFile != null) 242 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 243 244 axes = result.getAxes(); 245 positions = axes[1].getPositions(); 246 assertPosition(result, 16, "Frozen Foods", "All Customers", null); 247 248 msr.setSortMode(SortRank.BOTTOMCOUNT); 250 result = model.getResult(); 251 if (renderFile != null) 252 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 253 254 axes = result.getAxes(); 255 positions = axes[1].getPositions(); 256 assertPosition(result, 1, "Seafood", "All Customers", null); 257 258 MondrianPlaceMembers mplace = (MondrianPlaceMembers)model.getExtension(PlaceMembersOnAxes.ID); 260 List ml = new ArrayList (); 261 positions = axes[0].getPositions(); 262 for (int i = 1; i < 3; i++) { 264 Position p = (Position)positions.get(i); 265 ml.add(p.getMembers()[0]); 266 } 267 Object o = mplace.createMemberExpression(ml); 268 mplace.setQueryAxis(axes[0], new Object [] { o }); 269 result = model.getResult(); 270 if (renderFile != null) 271 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 272 273 axes = result.getAxes(); 274 positions = axes[1].getPositions(); 275 assertPosition(result, 1, "Seafood", "All Customers", null); 277 assertTrue((msr.isSorting())); 279 280 pos = (Position)positions.get(8); 282 assertTrue(mdep.canExpand(pos, pos.getMembers()[0])); 283 assertPosition(result, 8, "Deli", "All Customers", null); 284 mdep.expand(pos, pos.getMembers()[0]); result = model.getResult(); 286 if (renderFile != null) 287 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 288 289 axes = result.getAxes(); 290 positions = axes[1].getPositions(); 291 assertPosition(result, 3, "Side Dishes", "All Customers", null); 292 293 msr.setSortMode(SortRank.DESC); 295 result = model.getResult(); 296 if (renderFile != null) 297 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 298 299 axes = result.getAxes(); 300 positions = axes[1].getPositions(); 301 assertPosition(result, 17, "Seafood", "All Customers", null); 302 303 ml = new ArrayList (); 305 assertPosition(result, 10, "Side Dishes", "All Customers", null); 306 for (int i = 0; i < positions.size(); i++) { 307 if (i == 10) 308 continue; 309 Position p = (Position)positions.get(i); 310 ml.add(p.getMembers()[0]); 311 } 312 Object o1 = mplace.createMemberExpression(ml); 313 ml = new ArrayList (); 314 Position p = (Position)positions.get(0); 315 ml.add(p.getMembers()[1]); Object o2 = mplace.createMemberExpression(ml); 317 mplace.setQueryAxis(axes[1], new Object [] { o1, o2 }); 318 result = model.getResult(); 319 if (renderFile != null) 320 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 321 322 axes = result.getAxes(); 323 positions = axes[1].getPositions(); 324 assertTrue(!(msr.isSorting())); 326 327 msr.setSortMode(SortRank.DESC); 329 positions = axes[0].getPositions(); 330 sortPos = (Position)positions.get(1); msr.setSorting(true); 332 msr.sort(axes[1], sortPos); 333 result = model.getResult(); 334 if (renderFile != null) 335 ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html"); 336 337 axes = result.getAxes(); 338 positions = axes[1].getPositions(); 339 assertPosition(result, 13, "Eggs", "All Customers", null); 340 } 341 342 345 private void assertPosition(Result result, int iPos, String first, String second, String third) { 346 Position pos = (Position)result.getAxes()[1].getPositions().get(iPos); 347 String s = pos.getMembers()[0].getLabel(); 348 assertEquals(s, first); 349 if (second != null) { 350 s = pos.getMembers()[1].getLabel(); 351 assertEquals(s, second); 352 } 353 if (third != null) { 354 s = pos.getMembers()[2].getLabel(); 355 assertEquals(s, third); 356 } 357 } 358 359 } | Popular Tags |