KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

28   public ExpandCollapseTest(String JavaDoc arg0) {
29     super(arg0);
30   }
31
32   public void testExpandPosMem() 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\\testExpandPosMem";
45
int renderNum = 0;
46
47     Result result;
48
49     MondrianModel model = MondrianModelFactory.instance();
50     model.setMdxQuery(mdxQuery);
51     TestConnection.initModel(model);
52
53     // first step
54
result = model.getResult();
55     if (renderFile != null)
56        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
57
58     assertPosition(result, 1, "CA", "Beverages");
59     assertPosition(result, 6, "WA", "Alcoholic Beverages");
60
61     DrillExpandPositionExt mdep =
62       (DrillExpandPositionExt) model.getExtension(DrillExpandPosition.ID);
63
64     // expand (CA,Beverages) for CA
65

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

70     assertTrue(mdep.canExpand(pos, pos.getMembers()[0]));
71     mdep.expand(pos, pos.getMembers()[0]); // expand (CA,Beverages) for CA
72

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

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

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

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

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

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

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

176     Axis[] axes = result.getAxes();
177     List JavaDoc positions = axes[1].getPositions();
178     Position pos = (Position) positions.get(1); // (CA,Beverages)
179

180     assertTrue(mdem.canExpand(pos.getMembers()[0])); //CA
181
mdem.expand(pos.getMembers()[0]); // CA
182

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

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

209     positions = result.getAxes()[1].getPositions();
210     pos = (Position) positions.get(21); // (LA,Alcoholic Beverages)
211
assertTrue(mdem.canExpand(pos.getMembers()[0])); // Los Angeles
212
mdem.expand(pos.getMembers()[0]); // expand for Los Angeles
213

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

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

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

286     assertTrue(mdem.canExpand(pos.getMembers()[0])); // All Media
287
mdem.expand(pos.getMembers()[0]); // All Media
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, 3, "Bulk Mail", "Alcoholic Beverages");
296     assertPosition(result, 10, "Daily Paper", "Beverages");
297     assertPosition(result, 28, "Radio", "Beverages");
298     assertPosition(result, 44, "TV", "Dairy");
299
300     // expand (Radio, Beverages) for Beverages
301
pos = (Position) positions.get(27); // (Radio, Alcoholic Beverages)
302
assertTrue(!mdep.canExpand(pos, pos.getMembers()[0])); // Radio
303
assertTrue(mdep.canExpand(pos, pos.getMembers()[1])); // Alcoholic Beverages
304
mdep.expand(pos, pos.getMembers()[1]); // Alcoholic Beverages
305

306     result = model.getResult();
307     if (renderFile != null)
308        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
309
310     assertPosition(result, 28, "Radio", "Beer and Wine");
311     axes = result.getAxes();
312     positions = axes[1].getPositions();
313
314     // expand for Dairy
315
pos = (Position) positions.get(2); // (All Media, Dairy)
316
assertTrue(mdem.canExpand(pos.getMembers()[1])); // Dairy
317
mdem.expand(pos.getMembers()[1]); // Dairy
318

319     result = model.getResult();
320     if (renderFile != null)
321        ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ +".html");
322
323     axes = result.getAxes();
324     positions = axes[1].getPositions();
325     assertEquals(positions.size(), 61);
326
327     // Collapse All Media
328
pos = (Position) positions.get(0); // (All Media, )
329
assertTrue(mdem.canCollapse(pos.getMembers()[0])); // All Media
330
mdem.collapse(pos.getMembers()[0]); // All Media
331

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

355   private void assertPosition(Result result, int iPos, String JavaDoc firstMember, String JavaDoc secondMember) {
356     Position pos = (Position) result.getAxes()[1].getPositions().get(iPos);
357     String JavaDoc s = pos.getMembers()[0].getLabel();
358     assertEquals(s, firstMember);
359     s = pos.getMembers()[1].getLabel();
360     assertEquals(s, secondMember);
361   }
362
363 } // End ExpandCollapseTest
364
Popular Tags