KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.tonbeller.jpivot.mondrian;
2
3 import java.util.List JavaDoc;
4
5 import junit.framework.TestCase;
6
7 import com.tonbeller.jpivot.olap.model.Axis;
8 import com.tonbeller.jpivot.olap.model.Hierarchy;
9 import com.tonbeller.jpivot.olap.model.Position;
10 import com.tonbeller.jpivot.olap.model.Result;
11 import com.tonbeller.jpivot.olap.navi.DrillReplace;
12 import com.tonbeller.jpivot.olap.query.DrillReplaceExt;
13 import com.tonbeller.jpivot.olap.query.ResultBase;
14 import com.tonbeller.jpivot.tags.MondrianModelFactory;
15
16 /**
17  * @author hh
18  *
19  * Test expand/collapse with position and member
20  */

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

27   public DrillReplaceTest(String JavaDoc arg0) {
28     super(arg0);
29   }
30
31   public void testCrossJoin() throws Exception JavaDoc {
32
33     /*
34     select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns,
35     CrossJoin( [Store].[USA].children, [Product].[All Products].[Drink].children) on rows
36     from Sales where ([Time].[1997])
37     */

38     String JavaDoc mdxQuery =
39       "select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns, "
40         + "CrossJoin( [Store].[USA].children, [Product].[All Products].[Drink].children) on rows "
41         + "from Sales where ([Time].[1997])";
42
43     String JavaDoc renderFile = null; // "c:\\x\\testCrossJoin";
44
int renderNum = 0;
45
46     Result result;
47
48     MondrianModel model = MondrianModelFactory.instance();
49     model.setMdxQuery(mdxQuery);
50     TestConnection.initModel(model);
51
52     // first step
53
result = model.getResult();
54     if (renderFile != null)
55        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
56
57     assertPosition(result, 1, "CA", "Beverages");
58     assertPosition(result, 6, "WA", "Alcoholic Beverages");
59
60     DrillReplaceExt mdrep = (DrillReplaceExt) model.getExtension(DrillReplace.ID);
61
62     // Drill down CA
63
Axis[] axes = result.getAxes();
64     List JavaDoc positions = axes[1].getPositions();
65     Position pos1 = (Position) positions.get(1); // (CA,Beverages)
66

67     assertTrue(mdrep.canDrillDown(pos1.getMembers()[0]));
68     mdrep.drillDown(pos1.getMembers()[0]); // drilldown CA
69

70     result = model.getResult();
71     if (renderFile != null)
72        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
73
74     assertPosition(result, 6, "Los Angeles", "Alcoholic Beverages");
75     assertPosition(result, 10, "San Diego", "Beverages");
76     assertPosition(result, 14, "San Francisco", "Dairy");
77
78     // drill up the Store hierarchy
79
axes = result.getAxes();
80     positions = axes[1].getPositions();
81     Position pos0 = (Position) positions.get(0); // no matter what it is
82
Hierarchy hier = pos0.getMembers()[0].getLevel().getHierarchy();
83
84     assertTrue(mdrep.canDrillUp(hier));
85     mdrep.drillUp(hier); // drill up "Store"
86

87     result = model.getResult();
88     if (renderFile != null)
89        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
90
91     assertPosition(result, 2, "CA", "Dairy");
92     assertPosition(result, 7, "WA", "Beverages");
93
94     // another drill up of the Store hierarchy
95
axes = result.getAxes();
96     positions = axes[1].getPositions();
97     pos0 = (Position) positions.get(0); // no matter what it is
98
hier = pos0.getMembers()[0].getLevel().getHierarchy();
99
100     assertTrue(mdrep.canDrillUp(hier));
101     mdrep.drillUp(hier); // drill up "Store"
102

103     result = model.getResult();
104     if (renderFile != null)
105        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
106
107     assertPosition(result, 7, "USA", "Beverages");
108
109     // last drill up of the Store hierarchy reaching "All Stores"
110
axes = result.getAxes();
111     positions = axes[1].getPositions();
112     pos0 = (Position) positions.get(0); // no matter what it is
113
hier = pos0.getMembers()[0].getLevel().getHierarchy();
114
115     assertTrue(mdrep.canDrillUp(hier));
116     mdrep.drillUp(hier); // drill up "Store"
117

118     result = model.getResult();
119     if (renderFile != null)
120        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
121
122     assertPosition(result, 2, "All Stores", "Dairy");
123
124     // now drill up is not possible any more
125
assertTrue(!mdrep.canDrillUp(hier));
126
127   }
128
129   public void testSingle() throws Exception JavaDoc {
130
131     /*
132       select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns,
133       { [Store].[USA].children } on rows
134       from Sales where ([Time].[1997])
135     */

136     String JavaDoc mdxQuery =
137       "select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns, "
138         + "{ [Store].[USA].children } on rows "
139         + "from Sales where ([Time].[1997])";
140
141     String JavaDoc renderFile = null; // "c:\\x\\testSingle";
142
int renderNum = 0;
143
144     Result result;
145
146     MondrianModel model = MondrianModelFactory.instance();
147     model.setMdxQuery(mdxQuery);
148     TestConnection.initModel(model);
149
150     // first step
151
result = model.getResult();
152     if (renderFile != null)
153        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
154
155     assertPosition(result, 0, "CA", null);
156     assertPosition(result, 1, "OR", null);
157
158     DrillReplaceExt mdrep = (DrillReplaceExt) model.getExtension(DrillReplace.ID);
159
160     // Drill down CA
161
Axis[] axes = result.getAxes();
162     List JavaDoc positions = axes[1].getPositions();
163     Position pos = (Position) positions.get(0); // CA
164
assertTrue(mdrep.canDrillDown(pos.getMembers()[0]));
165     mdrep.drillDown(pos.getMembers()[0]); // drilldown CA
166

167     result = model.getResult();
168     if (renderFile != null)
169        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
170
171     assertPosition(result, 1, "Beverly Hills", null);
172     assertPosition(result, 3, "San Diego", null);
173
174     // Drill down San Diego
175
axes = result.getAxes();
176     positions = axes[1].getPositions();
177     pos = (Position) positions.get(3); // San Diego
178

179     assertTrue(mdrep.canDrillDown(pos.getMembers()[0]));
180     mdrep.drillDown(pos.getMembers()[0]); // drilldown San Diego
181

182     result = model.getResult();
183     if (renderFile != null)
184        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
185
186     assertPosition(result, 0, "Store 24", null);
187
188     // this is the lowest level
189
axes = result.getAxes();
190     positions = axes[1].getPositions();
191     pos = (Position) positions.get(0);
192     assertTrue(!mdrep.canDrillDown(pos.getMembers()[0]));
193
194     // drill up the Store hierarchy
195
axes = result.getAxes();
196     positions = axes[1].getPositions();
197     pos = (Position) positions.get(0); // no matter what it is
198
Hierarchy hier = pos.getMembers()[0].getLevel().getHierarchy();
199
200     assertTrue(mdrep.canDrillUp(hier));
201     mdrep.drillUp(hier); // drill up "Store"
202

203     result = model.getResult();
204     if (renderFile != null)
205        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
206
207     assertPosition(result, 2, "Los Angeles", null);
208     assertPosition(result, 4, "San Francisco", null);
209
210     // drill up for state level
211
assertTrue(mdrep.canDrillUp(hier));
212     mdrep.drillUp(hier); // drill up "Store"
213

214     result = model.getResult();
215     if (renderFile != null)
216        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
217
218     assertPosition(result, 2, "WA", null);
219
220     // drill up for country level
221
assertTrue(mdrep.canDrillUp(hier));
222     mdrep.drillUp(hier); // drill up "Store"
223

224     result = model.getResult();
225     if (renderFile != null)
226        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
227
228     assertPosition(result, 2, "USA", null);
229
230     // drill up for All Stores level
231
assertTrue(mdrep.canDrillUp(hier));
232     mdrep.drillUp(hier); // drill up "Store"
233

234     result = model.getResult();
235     if (renderFile != null)
236        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
237
238     assertPosition(result, 0, "All Stores", null);
239
240     // now drill up is not possible any more
241
assertTrue(!mdrep.canDrillUp(hier));
242
243   }
244
245   public void testTwoCrossjDown() throws Exception JavaDoc {
246
247     String JavaDoc mdxQuery =
248       "select {[Measures].[Sales Count]} on columns, "
249         + "{ CrossJoin( [Customers].[All Customers].[USA].[CA].children, [Product].[All Products].[Drink].children), "
250         + "CrossJoin( [Customers].[All Customers].[USA].[WA].children, [Product].[All Products].[Drink].children) } on rows "
251         + "from Sales where ([Time].[1997])";
252    
253     String JavaDoc renderFile = null; // "c:\\x\\testTwoCrossjDown";
254
int renderNum = 0;
255
256     Result result;
257
258     MondrianModel model = MondrianModelFactory.instance();
259     model.setMdxQuery(mdxQuery);
260     TestConnection.initModel(model);
261
262     // first step
263
result = model.getResult();
264     if (renderFile != null)
265        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
266
267     assertPosition(result, 2, "Altadena", "Dairy");
268     assertPosition(result, 9, "Berkeley", "Alcoholic Beverages");
269
270     DrillReplaceExt mdrep = (DrillReplaceExt) model.getExtension(DrillReplace.ID);
271
272     // Drill down Altadena
273
Axis[] axes = result.getAxes();
274     List JavaDoc positions = axes[1].getPositions();
275
276     assertPosition(result, positions.size() - 1, "Yakima", "Dairy"); // last of WA expected
277

278     Position pos = (Position) positions.get(0); // (Altadena,x)
279
Hierarchy hier = pos.getMembers()[0].getLevel().getHierarchy();
280     assertTrue(mdrep.canDrillDown(pos.getMembers()[0]));
281     mdrep.drillDown(pos.getMembers()[0]); // drilldown Altadena
282

283     result = model.getResult();
284     if (renderFile != null)
285        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
286
287     axes = result.getAxes();
288     positions = axes[1].getPositions();
289     assertPosition(result, 10, "Angela Evans", "Beverages");
290     assertPosition(result, positions.size() - 7, "Wendell Kersten", "Dairy");
291
292     // Drill up Customer hierarchy
293
assertTrue(mdrep.canDrillUp(hier));
294     mdrep.drillUp(hier); // drill up Customer
295

296     result = model.getResult();
297     if (renderFile != null)
298        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
299
300     axes = result.getAxes();
301     positions = axes[1].getPositions();
302     assertPosition(result, positions.size() - 1, "Woodland Hills", "Dairy"); // last of CA expected
303
}
304
305   public void testTwoCrossjUp() throws Exception JavaDoc {
306
307     /*
308      select {[Measures].[Sales Count]} on columns,
309      { CrossJoin( [Customers].[All Customers].[USA].[CA].[Berkeley].children, [Product].[All Products].[Drink].children),
310        CrossJoin( [Customers].[All Customers].[USA].[WA].[Seattle].children, [Product].[All Products].[Drink].children) } on rows
311        from Sales where ([Time].[1997])
312      */

313
314     String JavaDoc mdxQuery =
315       "select {[Measures].[Sales Count]} on columns, "
316         + "{ CrossJoin( [Customers].[All Customers].[USA].[CA].[Berkeley].children, [Product].[All Products].[Drink].children), "
317         + "CrossJoin( [Customers].[All Customers].[USA].[WA].[Seattle].children, [Product].[All Products].[Drink].children) } on rows "
318         + "from Sales where ([Time].[1997])";
319
320     String JavaDoc renderFile = null; // "c:\\x\\testTwoCrossjUp";
321
int renderNum = 0;
322
323     Result result;
324
325     MondrianModel model = MondrianModelFactory.instance();
326     model.setMdxQuery(mdxQuery);
327     TestConnection.initModel(model);
328
329     // first step
330
result = model.getResult();
331     if (renderFile != null)
332        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
333
334     Axis[] axes = result.getAxes();
335     List JavaDoc positions = axes[1].getPositions();
336     Position pos = (Position) positions.get(0);
337     Hierarchy hier = pos.getMembers()[0].getLevel().getHierarchy();
338
339     assertPosition(result, 4, "Alma Shelton", "Beverages");
340     assertPosition(result, positions.size() - 3, "Yvonne Rose", "Alcoholic Beverages");
341
342     DrillReplaceExt mdrep = (DrillReplaceExt) model.getExtension(DrillReplace.ID);
343
344     // Drill up Customer hierarchy
345
assertTrue(mdrep.canDrillUp(hier));
346     mdrep.drillUp(hier); // drill up Customer
347

348     result = model.getResult();
349     if (renderFile != null)
350        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
351
352     axes = result.getAxes();
353     positions = axes[1].getPositions();
354
355     // ((MondrianResult)result).renderHtml("c:\\x\\x.html");
356
assertPosition(result, 0, "Altadena", "Alcoholic Beverages"); // first of WA expected
357
assertPosition(result, positions.size() - 1, "Yakima", "Dairy"); // last of WA expected
358

359   }
360
361   /**
362    * assert position
363    */

364   private void assertPosition(Result result, int iPos, String JavaDoc firstMember, String JavaDoc secondMember) {
365     Position pos = (Position) result.getAxes()[1].getPositions().get(iPos);
366     String JavaDoc s = pos.getMembers()[0].getLabel();
367     assertEquals(s, firstMember);
368     if (secondMember != null) {
369       s = pos.getMembers()[1].getLabel();
370       assertEquals(s, secondMember);
371     }
372   }
373
374 } // End DrillReplaceTest
375
Popular Tags