KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

41     String JavaDoc 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 JavaDoc renderFile = null; // "c:\\x\\testOrderDrillPos";
48
int renderNum = 0;
49
50     Result result;
51
52     URL JavaDoc 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     // first step
63
result = model.getResult();
64     if (renderFile != null)
65        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
66
67     // Expand All Customers
68
Axis[] axes = result.getAxes();
69     List JavaDoc 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]); // drilldown All Customers
74

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     // Expand All Gender, USA, All Products
84
pos = (Position)positions.get(3);
85     assertTrue(mdep.canExpand(pos, pos.getMembers()[2]));
86     mdep.expand(pos, pos.getMembers()[2]); // drilldown All Products
87
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     // Expand All Gender
96
pos = (Position)positions.get(0);
97     assertTrue(mdep.canExpand(pos, pos.getMembers()[0]));
98     mdep.expand(pos, pos.getMembers()[0]); // Expand All Gender
99
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     // Collapse All Gender
108
pos = (Position)positions.get(0);
109     assertTrue(mdep.canCollapse(pos, pos.getMembers()[0]));
110     mdep.collapse(pos, pos.getMembers()[0]); // collapse All Gender
111
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     // Order Unit Sales DESC
120
msr.setSortMode(SortRank.DESC);
121     positions = axes[0].getPositions();
122     Position sortPos = (Position)positions.get(0); // Unit Sales
123
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     // Order Store Cost ASC
134
msr.setSortMode(SortRank.ASC);
135     positions = axes[0].getPositions();
136     sortPos = (Position)positions.get(1); // Store Cost
137
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     // remove Sort
147
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 JavaDoc {
159
160     /*
161     select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns,
162     {([Product].[All Products], [Customers].[All Customers]) } on rows
163       from Sales where ([Time].[1997])
164     */

165     String JavaDoc 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 JavaDoc renderFile = null; // "c:\\x\\testOrderSwitchMode";
171
int renderNum = 0;
172
173     Result result;
174
175     URL JavaDoc 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     // first step
186
result = model.getResult();
187     if (renderFile != null)
188        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
189
190
191     // Expand All Products
192
Axis[] axes = result.getAxes();
193     List JavaDoc 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]); // expand All Products
198

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     // Expand Food
208
pos = (Position)positions.get(2);
209     assertTrue(mdep.canExpand(pos, pos.getMembers()[0]));
210     mdep.expand(pos, pos.getMembers()[0]); // expand Food
211
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     // Order Unit Sales DESC
220
msr.setSortMode(SortRank.DESC);
221     positions = axes[0].getPositions();
222     Position sortPos = (Position)positions.get(0); // Unit Sales
223
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     // Order TopCount
234
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     // Order ASC
244
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     // Order BottomCount
254
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     // remove unit sales from axis 0 (measures)
264
XMLA_PlaceMembers mplace = (XMLA_PlaceMembers)model.getExtension(PlaceMembersOnAxes.ID);
265     List JavaDoc ml = new ArrayList JavaDoc();
266     positions = axes[0].getPositions();
267     // omit first measure (Unit Sales)
268
for (int i = 1; i < 3; i++) {
269       Position p = (Position)positions.get(i);
270       ml.add(p.getMembers()[0]);
271     }
272     Object JavaDoc o = mplace.createMemberExpression(ml);
273     mplace.setQueryAxis(axes[0], new Object JavaDoc[] { 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     // nothing happens, except sorting is removed
281
assertPosition(result, 1, "Seafood", "All Customers", null);
282     // sorting must still be on
283
assertTrue((msr.isSorting()));
284
285     // expand Deli, BottomCount with Unit Sales is still active
286
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]); // expand Deli
290
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     // Order DESC
299
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     // remove Side Dishes from axis 1
309
ml = new ArrayList JavaDoc();
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 JavaDoc o1 = mplace.createMemberExpression(ml);
318     ml = new ArrayList JavaDoc();
319     Position p = (Position)positions.get(0);
320     ml.add(p.getMembers()[1]); // All Customers
321
Object JavaDoc o2 = mplace.createMemberExpression(ml);
322     mplace.setQueryAxis(axes[1], new Object JavaDoc[] { 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     // sorting must be off
330
assertTrue(!(msr.isSorting()));
331
332     // Order Store Sales DESC
333
msr.setSortMode(SortRank.DESC);
334     positions = axes[0].getPositions();
335     sortPos = (Position)positions.get(1); // Store Sales
336
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   /**
348    * assert position
349    */

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