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