KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.tonbeller.jpivot.mondrian;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5
6 import junit.framework.TestCase;
7
8 import com.tonbeller.jpivot.olap.model.Axis;
9 import com.tonbeller.jpivot.olap.model.Dimension;
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.DrillExpandPosition;
14 import com.tonbeller.jpivot.olap.navi.PlaceHierarchiesOnAxes;
15 import com.tonbeller.jpivot.olap.navi.PlaceMembersOnAxes;
16 import com.tonbeller.jpivot.olap.query.DrillExpandPositionExt;
17 import com.tonbeller.jpivot.olap.query.ResultBase;
18 import com.tonbeller.jpivot.tags.MondrianModelFactory;
19
20 /**
21  * Tests various bug fixes
22  */

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

29   public VariousTest(String JavaDoc arg0) {
30     super(arg0);
31   }
32
33   public void testSetCollapse() throws Exception JavaDoc {
34
35     /*
36      select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} ON columns,
37      {[Product].[All Products]} ON rows from [Sales]
38      */

39     String JavaDoc mdxQuery = "select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} "
40         + "ON columns, " + "{[Product].[All Products]} ON rows from [Sales]";
41
42     String JavaDoc renderFile = null; // "c:\\x\\testSetCollapse";
43
int renderNum = 0;
44
45     Result result;
46
47     MondrianModel model = MondrianModelFactory.instance();
48     model.setMdxQuery(mdxQuery);
49     TestConnection.initModel(model);
50
51     DrillExpandPositionExt mdep = (DrillExpandPositionExt) model
52         .getExtension(DrillExpandPosition.ID);
53     PlaceHierarchiesOnAxes placeHier = (PlaceHierarchiesOnAxes) model
54         .getExtension(PlaceHierarchiesOnAxes.ID);
55     PlaceMembersOnAxes placeMem = (PlaceMembersOnAxes) model.getExtension(PlaceMembersOnAxes.ID);
56
57     // first step
58
result = model.getResult();
59     if (renderFile != null)
60       ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ + ".html");
61
62     assertPosition(result, 1, 0, new String JavaDoc[] { "All Products"});
63
64     // Drill Down
65
Axis[] axes = result.getAxes();
66     List JavaDoc positions = axes[1].getPositions();
67     Position pos0 = (Position) positions.get(0); // All Products
68
Hierarchy hierProduct = pos0.getMembers()[0].getLevel().getHierarchy();
69     mdep.expand(pos0, pos0.getMembers()[0]); // drilldown All Products
70
result = model.getResult();
71     if (renderFile != null)
72       ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ + ".html");
73
74     axes = result.getAxes();
75     positions = axes[1].getPositions();
76     Position pos3 = (Position) positions.get(3); // Non-Consumable
77
assertPosition(result, 1, 3, new String JavaDoc[] { "Non-Consumable"});
78     MondrianMember nonCons = (MondrianMember) pos3.getMembers()[0];
79
80     List JavaDoc mList = placeMem.findVisibleMembers(hierProduct);
81     // Remove Non-Consumable from List
82
for (Iterator JavaDoc iter = mList.iterator(); iter.hasNext();) {
83       MondrianMember m = (MondrianMember) iter.next();
84       if (m.equals(nonCons))
85         iter.remove();
86     }
87     // get time Hierarchy
88
Dimension[] dims = model.getDimensions();
89     Dimension timeDim = null;
90     for (int i = 0; i < dims.length; i++) {
91       if (((MondrianDimension) dims[i]).getLabel().equals("Time")) {
92         timeDim = dims[i];
93         break;
94       }
95     }
96     Object JavaDoc oTime = placeHier.createMemberExpression(timeDim.getHierarchies()[0]);
97     Object JavaDoc oSet = placeMem.createMemberExpression(mList);
98     placeHier.setQueryAxis(axes[1], new Object JavaDoc[] { oSet, oTime});
99
100     result = model.getResult();
101     if (renderFile != null)
102       ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ + ".html");
103     axes = result.getAxes();
104     positions = axes[1].getPositions();
105
106     // Expand [All Products] , [1997]
107
assertPosition(result, 1, 0, new String JavaDoc[] { "All Products", "1997"});
108     pos0 = (Position) positions.get(0);
109     mdep.expand(pos0, pos0.getMembers()[1]);
110
111     result = model.getResult();
112     if (renderFile != null)
113       ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ + ".html");
114
115     assertPosition(result, 1, 4, new String JavaDoc[] { "All Products", "Q4"});
116
117     // Collapse All Products
118
axes = result.getAxes();
119     positions = axes[1].getPositions();
120     Position pos4 = (Position) positions.get(4); // "All Products", "Q4"
121
mdep.collapse(pos4, pos4.getMembers()[0]);
122     result = model.getResult();
123     if (renderFile != null)
124       ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ + ".html");
125
126     axes = result.getAxes();
127     positions = axes[1].getPositions();
128     assertPosition(result, 1, 1, new String JavaDoc[] { "All Products", "Q1"});
129     assertEquals(positions.size(), 6);
130   }
131
132   /**
133    * assert position
134    */

135   private void assertPosition(Result result, int iAxis, int iPos, String JavaDoc[] posMembers) {
136     Position pos = (Position) result.getAxes()[iAxis].getPositions().get(iPos);
137     for (int i = 0; i < posMembers.length; i++) {
138       String JavaDoc str = pos.getMembers()[i].getLabel();
139       assertEquals(str, posMembers[i]);
140     }
141   }
142 } // VariousTest
143
Popular Tags