KickJava   Java API By Example, From Geeks To Geeks.

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


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.Position;
11 import com.tonbeller.jpivot.olap.model.Result;
12 import com.tonbeller.jpivot.olap.navi.DrillExpandMember;
13 import com.tonbeller.jpivot.olap.navi.DrillExpandPosition;
14 import com.tonbeller.jpivot.olap.query.DrillExpandMemberExt;
15 import com.tonbeller.jpivot.olap.query.DrillExpandPositionExt;
16 import com.tonbeller.jpivot.olap.query.ResultBase;
17
18 /**
19  * @author hh
20  *
21  * Test expand/collapse with position and member
22  */

23 public class ExpandCollapseTest extends TestCase {
24
25   /**
26    * Constructor for ExpandCollapse.
27    * @param arg0
28    */

29   public ExpandCollapseTest(String JavaDoc arg0) {
30     super(arg0);
31   }
32
33   public void testExpandPosMem() throws Exception JavaDoc {
34
35     /*
36      select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns,
37      CrossJoin( [Store].[USA].children, [Product].[All Products].[Drink].children) 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         + "CrossJoin( [Store].[USA].children, [Product].[All Products].[Drink].children) on rows "
43         + "from Sales where ([Time].[1997])";
44
45     String JavaDoc renderFile = null; // "c:\\x\\testExpandPosMem";
46
int renderNum = 0;
47
48     Result result;
49
50     URL JavaDoc confUrl = XMLA_Model.class.getResource("config.xml");
51     XMLA_Model model = (XMLA_Model) ModelFactory.instance(confUrl);
52
53     model.setMdxQuery(mdxQuery);
54     TestConnection.initModel(model);
55
56     // first step
57
result = model.getResult();
58     if (renderFile != null)
59        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
60
61     assertPosition(result, 1, "CA", "Beverages");
62     assertPosition(result, 6, "WA", "Alcoholic Beverages");
63
64     DrillExpandPositionExt mdep =
65       (DrillExpandPositionExt) model.getExtension(DrillExpandPosition.ID);
66
67     // expand (CA,Beverages) for CA
68

69     Axis[] axes = result.getAxes();
70     List JavaDoc positions = axes[1].getPositions();
71     Position pos = (Position) positions.get(1); // (CA,Beverages)
72

73     assertTrue(mdep.canExpand(pos, pos.getMembers()[0]));
74     mdep.expand(pos, pos.getMembers()[0]); // expand (CA,Beverages) for CA
75

76     result = model.getResult();
77     if (renderFile != null)
78        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
79
80     positions = result.getAxes()[1].getPositions();
81     assertPosition(result, 6, "Beverly Hills", "Alcoholic Beverages");
82     assertPosition(result, 10, "Los Angeles", "Beverages");
83     assertPosition(result, 17, "San Francisco", "Dairy");
84     assertPosition(result, positions.size() - 1, "WA", "Dairy");
85
86     // expand (CA,Beverages)) for Beverages
87
pos = (Position) positions.get(1); // (CA,Beverages)
88
assertTrue(mdep.canExpand(pos, pos.getMembers()[1]));
89     mdep.expand(pos, pos.getMembers()[1]); // expand (CA,Beverages) for Beverages
90

91     result = model.getResult();
92     if (renderFile != null)
93        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
94
95     positions = result.getAxes()[1].getPositions();
96     assertPosition(result, 2, "CA", "Carbonated Beverages");
97     assertPosition(result, 5, "CA", "Pure Juice Beverages");
98     assertPosition(result, 10, "Beverly Hills", "Alcoholic Beverages");
99     assertPosition(result, positions.size() - 4, "OR", "Dairy");
100
101     // expand (Los Angeles,Beverages)) for Beverages
102

103     pos = (Position) positions.get(14); // (Los Angeles,Beverages)
104
assertTrue(mdep.canExpand(pos, pos.getMembers()[1]));
105     mdep.expand(pos, pos.getMembers()[1]); // expand (LA,Beverages) for Beverages
106

107     result = model.getResult();
108     if (renderFile != null)
109        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
110
111     positions = result.getAxes()[1].getPositions();
112     assertPosition(result, 15, "Los Angeles", "Carbonated Beverages");
113     assertPosition(result, 17, "Los Angeles", "Hot Beverages");
114     assertPosition(result, 21, "San Diego", "Beverages");
115
116     // collapse (CA,Beverages) for CA
117
pos = (Position) positions.get(1); // (CA,Beverages)
118
assertTrue(mdep.canCollapse(pos, pos.getMembers()[0]));
119     mdep.collapse(pos, pos.getMembers()[0]); // collapse (CA,Beverages) for CA
120

121     result = model.getResult();
122     if (renderFile != null)
123        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
124
125     positions = result.getAxes()[1].getPositions();
126     assertPosition(result, 0, "CA", "Alcoholic Beverages");
127     assertPosition(result, 5, "CA", "Pure Juice Beverages");
128     assertPosition(result, 10, "WA", "Alcoholic Beverages");
129
130     // collapse (CA,Beverages) for Beverages
131
pos = (Position) positions.get(1); // (CA,Beverages)
132
assertTrue(mdep.canCollapse(pos, pos.getMembers()[1]));
133     mdep.collapse(pos, pos.getMembers()[1]); // collapse (CA,Beverages) for Beverages
134

135     result = model.getResult();
136     if (renderFile != null)
137        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
138
139     positions = result.getAxes()[1].getPositions();
140     assertPosition(result, 8, "WA", "Dairy");
141     assertEquals(positions.size(), 9);
142
143   }
144
145   public void testExpandMem() throws Exception JavaDoc {
146     /*
147     select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns,
148     CrossJoin( [Store].[USA].children, [Product].[All Products].[Drink].children) on rows
149     from Sales where ([Time].[1997])
150      */

151
152     String JavaDoc mdxQuery =
153       "select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} on columns, "
154         + "CrossJoin( [Store].[USA].children, [Product].[All Products].[Drink].children) on rows "
155         + "from Sales where ([Time].[1997])";
156
157     String JavaDoc renderFile = null; // "c:\\x\\testExpandMem";
158
int renderNum = 0;
159
160     Result result;
161
162     URL JavaDoc confUrl = XMLA_Model.class.getResource("config.xml");
163     XMLA_Model model = (XMLA_Model) ModelFactory.instance(confUrl);
164
165     model.setMdxQuery(mdxQuery);
166     TestConnection.initModel(model);
167
168     // first step
169
result = model.getResult();
170     if (renderFile != null)
171        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
172
173     assertPosition(result, 1, "CA", "Beverages");
174     assertPosition(result, 6, "WA", "Alcoholic Beverages");
175
176     DrillExpandMemberExt mdem =
177       (DrillExpandMemberExt) model.getExtension(DrillExpandMember.ID);
178  
179     // expand for CA
180

181     Axis[] axes = result.getAxes();
182     List JavaDoc positions = axes[1].getPositions();
183     Position pos = (Position) positions.get(1); // (CA,Beverages)
184

185     assertTrue(mdem.canExpand(pos.getMembers()[0])); //CA
186
mdem.expand(pos.getMembers()[0]); // CA
187

188     result = model.getResult();
189     if (renderFile != null)
190        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
191
192     assertPosition(result, 8, "Beverly Hills", "Dairy");
193     assertPosition(result, 12, "San Diego", "Alcoholic Beverages");
194     assertPosition(result, 16, "San Francisco", "Beverages");
195
196     // expand for Beverages
197
positions = result.getAxes()[1].getPositions();
198     pos = (Position) positions.get(1); // (CA,Beverages)
199
assertTrue(mdem.canExpand(pos.getMembers()[1]));
200     mdem.expand(pos.getMembers()[1]); // expand for Beverages
201

202     result = model.getResult();
203     if (renderFile != null)
204        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
205
206     assertPosition(result, 2, "CA", "Carbonated Beverages");
207     assertPosition(result, 5, "CA", "Pure Juice Beverages");
208     assertPosition(result, 24, "Los Angeles", "Drinks");
209     assertPosition(result, 32, "San Diego", "Hot Beverages");
210     assertPosition(result, 44, "OR", "Carbonated Beverages");
211
212     // expand for Los Angeles
213

214     positions = result.getAxes()[1].getPositions();
215     pos = (Position) positions.get(21); // (LA,Alcoholic Beverages)
216
assertTrue(mdem.canExpand(pos.getMembers()[0])); // Los Angeles
217
mdem.expand(pos.getMembers()[0]); // expand for Los Angeles
218

219     result = model.getResult();
220     if (renderFile != null)
221        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
222
223     assertPosition(result, 30, "Store 7", "Carbonated Beverages");
224
225     // collapse for CA
226
positions = result.getAxes()[1].getPositions();
227     pos = (Position) positions.get(21); // (Los Angeles, Alcoholic Beverages)
228
assertTrue(mdem.canCollapse(pos.getMembers()[0])); // Los angeles
229
pos = (Position) positions.get(2); // (CA,Beverages)
230
assertTrue(mdem.canCollapse(pos.getMembers()[0])); // CA
231
mdem.collapse(pos.getMembers()[0]); // collapse CA
232

233     result = model.getResult();
234     if (renderFile != null)
235        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
236
237     assertPosition(result, 4, "CA", "Hot Beverages");
238     assertPosition(result, 12, "OR", "Pure Juice Beverages");
239     assertPosition(result, 14, "WA", "Alcoholic Beverages");
240
241     // collapse for Beverages
242
positions = result.getAxes()[1].getPositions();
243     pos = (Position) positions.get(1); // (CA,Beverages)
244
assertTrue(mdem.canCollapse(pos.getMembers()[1])); // Beverages
245
mdem.collapse(pos.getMembers()[1]); // collapse Beverages
246

247     result = model.getResult();
248     if (renderFile != null)
249        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
250
251     positions = result.getAxes()[1].getPositions();
252     assertPosition(result, 8, "WA", "Dairy");
253     assertEquals(positions.size(), 9);
254
255   }
256
257   public void testMixedMode() throws Exception JavaDoc {
258
259     String JavaDoc mdxQuery =
260       " select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} ON columns, "
261         + "CrossJoin( {[Promotion Media].[All Media]},"
262         + " {[Product].[All Products].[Drink].children} ) ON rows "
263         + "from [Sales] where ([Time].[1997]) ";
264
265     String JavaDoc renderFile = null; // "c:\\x\\testMixedMode";
266
int renderNum = 0;
267
268     Result result;
269
270     URL JavaDoc confUrl = XMLA_Model.class.getResource("config.xml");
271     XMLA_Model model = (XMLA_Model) ModelFactory.instance(confUrl);
272
273     model.setMdxQuery(mdxQuery);
274     TestConnection.initModel(model);
275
276     // first step
277
result = model.getResult();
278     if (renderFile != null)
279        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
280
281     assertPosition(result, 2, "All Media", "Dairy");
282
283     DrillExpandMemberExt mdem =
284       (DrillExpandMemberExt) model.getExtension(DrillExpandMember.ID);
285     DrillExpandPositionExt mdep =
286       (DrillExpandPositionExt) model.getExtension(DrillExpandPosition.ID);
287
288     // expand for All Media
289
Axis[] axes = result.getAxes();
290     List JavaDoc positions = axes[1].getPositions();
291     Position pos = (Position) positions.get(1); // (All Media,Beverages)
292

293     assertTrue(mdem.canExpand(pos.getMembers()[0])); // All Media
294
mdem.expand(pos.getMembers()[0]); // All Media
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, 3, "Bulk Mail", "Alcoholic Beverages");
303     assertPosition(result, 10, "Daily Paper", "Beverages");
304     assertPosition(result, 28, "Radio", "Beverages");
305     assertPosition(result, 44, "TV", "Dairy");
306
307     // expand (Radio, Beverages) for Beverages
308
pos = (Position) positions.get(27); // (Radio, Alcoholic Beverages)
309
assertTrue(!mdep.canExpand(pos, pos.getMembers()[0])); // Radio
310
assertTrue(mdep.canExpand(pos, pos.getMembers()[1])); // Alcoholic Beverages
311
mdep.expand(pos, pos.getMembers()[1]); // Alcoholic Beverages
312

313     result = model.getResult();
314     if (renderFile != null)
315        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
316
317     assertPosition(result, 28, "Radio", "Beer and Wine");
318     axes = result.getAxes();
319     positions = axes[1].getPositions();
320
321     // expand for Dairy
322
pos = (Position) positions.get(2); // (All Media, Dairy)
323
assertTrue(mdem.canExpand(pos.getMembers()[1])); // Dairy
324
mdem.expand(pos.getMembers()[1]); // Dairy
325

326     result = model.getResult();
327     if (renderFile != null)
328        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
329
330     axes = result.getAxes();
331     positions = axes[1].getPositions();
332     assertEquals(positions.size(), 61);
333
334     // Collapse All Media
335
pos = (Position) positions.get(0); // (All Media, )
336
assertTrue(mdem.canCollapse(pos.getMembers()[0])); // All Media
337
mdem.collapse(pos.getMembers()[0]); // All Media
338

339     result = model.getResult();
340     if (renderFile != null)
341        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
342     axes = result.getAxes();
343     positions = axes[1].getPositions();
344     assertEquals(positions.size(), 4);
345
346     // Collapse dairy
347
pos = (Position) positions.get(2); // (All Media, Dairy )
348
assertTrue(mdep.canCollapse(pos, pos.getMembers()[1])); // All Media, Dairy
349
mdep.collapse(pos, pos.getMembers()[1]);
350
351     result = model.getResult();
352     if (renderFile != null)
353        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
354     axes = result.getAxes();
355     positions = axes[1].getPositions();
356     assertEquals(positions.size(), 3);
357   }
358
359   /**
360    * assert position
361    */

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