KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

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

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

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

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

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

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

183     assertTrue(mdrep.canDrillDown(pos.getMembers()[0]));
184     mdrep.drillDown(pos.getMembers()[0]); // drilldown San Diego
185

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

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

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

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

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

284     Position pos = (Position) positions.get(0); // (Altadena,x)
285
Hierarchy hier = pos.getMembers()[0].getLevel().getHierarchy();
286     assertTrue(mdrep.canDrillDown(pos.getMembers()[0]));
287     mdrep.drillDown(pos.getMembers()[0]); // drilldown Altadena
288

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

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

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

356     result = model.getResult();
357     if (renderFile != null)
358        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
359
360     axes = result.getAxes();
361     positions = axes[1].getPositions();
362
363     // ((XMLA_Result)result).renderHtml("c:\\x\\x.html");
364
assertPosition(result, 0, "Altadena", "Alcoholic Beverages"); // first of WA expected
365
assertPosition(result, positions.size() - 1, "Yakima", "Dairy"); // last of WA expected
366

367   }
368
369   /**
370    * assert position
371    */

372   private void assertPosition(Result result, int iPos, String JavaDoc firstMember, String JavaDoc secondMember) {
373     Position pos = (Position) result.getAxes()[1].getPositions().get(iPos);
374     String JavaDoc s = pos.getMembers()[0].getLabel();
375     assertEquals(s, firstMember);
376     if (secondMember != null) {
377       s = pos.getMembers()[1].getLabel();
378       assertEquals(s, secondMember);
379     }
380   }
381
382 } // End DrillReplaceTest
383
Popular Tags