KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.tonbeller.jpivot.xmla;
2
3 import java.net.URL JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import junit.framework.TestCase;
8
9 import com.tonbeller.jpivot.core.ModelFactory;
10 import com.tonbeller.jpivot.olap.model.Axis;
11 import com.tonbeller.jpivot.olap.model.Dimension;
12 import com.tonbeller.jpivot.olap.model.Hierarchy;
13 import com.tonbeller.jpivot.olap.model.Position;
14 import com.tonbeller.jpivot.olap.model.Result;
15 import com.tonbeller.jpivot.olap.navi.DrillExpandPosition;
16 import com.tonbeller.jpivot.olap.navi.PlaceHierarchiesOnAxes;
17 import com.tonbeller.jpivot.olap.navi.PlaceMembersOnAxes;
18 import com.tonbeller.jpivot.olap.query.DrillExpandPositionExt;
19 import com.tonbeller.jpivot.olap.query.ResultBase;
20
21 /**
22  * Tests various bug fixes
23  */

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

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

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

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