KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > mondrian > OrderingTest


1 package com.tonbeller.jpivot.mondrian;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
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 /**
19  * @author hh
20  *
21  */

22 public class OrderingTest extends TestCase {
23
24   /**
25    * Constructor
26    * @param arg0
27    */

28   public OrderingTest(String JavaDoc arg0) {
29     super(arg0);
30   }
31
32   public void testOrderDrillPos() throws Exception JavaDoc {
33
34     /*
35     select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns,
36     {([Gender].[All Gender], [Customers].[All Customers],
37       [Product].[All Products] ) } on rows
38       from Sales where ([Time].[1997])
39     */

40     String JavaDoc 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 JavaDoc renderFile = null; // "c:\\x\\testOrderDrillPos";
47
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     // first step
60
result = model.getResult();
61     if (renderFile != null)
62        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
63
64     // Expand All Customers
65
Axis[] axes = result.getAxes();
66     List JavaDoc 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]); // drilldown All Customers
71

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     // Expand All Gender, USA, All Products
81
pos = (Position)positions.get(3);
82     assertTrue(mdep.canExpand(pos, pos.getMembers()[2]));
83     mdep.expand(pos, pos.getMembers()[2]); // drilldown All Products
84
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     // Expand All Gender
93
pos = (Position)positions.get(0);
94     assertTrue(mdep.canExpand(pos, pos.getMembers()[0]));
95     mdep.expand(pos, pos.getMembers()[0]); // Expand All Gender
96
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     // Collapse All Gender
105
pos = (Position)positions.get(0);
106     assertTrue(mdep.canCollapse(pos, pos.getMembers()[0]));
107     mdep.collapse(pos, pos.getMembers()[0]); // collapse All Gender
108
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     // Order Unit Sales DESC
117
msr.setSortMode(SortRank.DESC);
118     positions = axes[0].getPositions();
119     Position sortPos = (Position)positions.get(0); // Unit Sales
120
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     // Order Store Cost ASC
131
msr.setSortMode(SortRank.ASC);
132     positions = axes[0].getPositions();
133     sortPos = (Position)positions.get(1); // Store Cost
134
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     // remove Sort
144
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 JavaDoc {
156
157     /*
158     select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns,
159     {([Product].[All Products], [Customers].[All Customers]) } on rows
160       from Sales where ([Time].[1997])
161     */

162     String JavaDoc 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 JavaDoc renderFile = null; // "c:\\x\\testOrderSwitchMode";
168
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     // first step
181
result = model.getResult();
182     if (renderFile != null)
183        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
184
185
186     // Expand All Products
187
Axis[] axes = result.getAxes();
188     List JavaDoc 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]); // expand All Products
193

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     // Expand Food
203
pos = (Position)positions.get(2);
204     assertTrue(mdep.canExpand(pos, pos.getMembers()[0]));
205     mdep.expand(pos, pos.getMembers()[0]); // expand Food
206
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     // Order Unit Sales DESC
215
msr.setSortMode(SortRank.DESC);
216     positions = axes[0].getPositions();
217     Position sortPos = (Position)positions.get(0); // Unit Sales
218
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     // Order TopCount
229
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     // Order ASC
239
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     // Order BottomCount
249
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     // remove unit sales from axis 0 (measures)
259
MondrianPlaceMembers mplace = (MondrianPlaceMembers)model.getExtension(PlaceMembersOnAxes.ID);
260     List JavaDoc ml = new ArrayList JavaDoc();
261     positions = axes[0].getPositions();
262     // omit first measure (Unit Sales)
263
for (int i = 1; i < 3; i++) {
264       Position p = (Position)positions.get(i);
265       ml.add(p.getMembers()[0]);
266     }
267     Object JavaDoc o = mplace.createMemberExpression(ml);
268     mplace.setQueryAxis(axes[0], new Object JavaDoc[] { 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     // nothing happens, except sorting is removed
276
assertPosition(result, 1, "Seafood", "All Customers", null);
277     // sorting must still be on
278
assertTrue((msr.isSorting()));
279
280     // expand Deli, BottomCount with Unit Sales is still active
281
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]); // expand Deli
285
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     // Order DESC
294
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     // remove Side Dishes from axis 1
304
ml = new ArrayList JavaDoc();
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 JavaDoc o1 = mplace.createMemberExpression(ml);
313     ml = new ArrayList JavaDoc();
314     Position p = (Position)positions.get(0);
315     ml.add(p.getMembers()[1]); // All Customers
316
Object JavaDoc o2 = mplace.createMemberExpression(ml);
317     mplace.setQueryAxis(axes[1], new Object JavaDoc[] { 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     // sorting must be off
325
assertTrue(!(msr.isSorting()));
326
327     // Order Store Sales DESC
328
msr.setSortMode(SortRank.DESC);
329     positions = axes[0].getPositions();
330     sortPos = (Position)positions.get(1); // Store Sales
331
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   /**
343    * assert position
344    */

345   private void assertPosition(Result result, int iPos, String JavaDoc first, String JavaDoc second, String JavaDoc third) {
346     Position pos = (Position)result.getAxes()[1].getPositions().get(iPos);
347     String JavaDoc 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 } // End OrderingTest
360
Popular Tags