KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.tonbeller.jpivot.mondrian;
2
3 import java.util.List JavaDoc;
4
5 import junit.framework.TestCase;
6 import mondrian.mdx.LevelExpr;
7 import mondrian.mdx.MemberExpr;
8 import mondrian.mdx.UnresolvedFunCall;
9 import mondrian.olap.Exp;
10 import mondrian.olap.FunCall;
11 import mondrian.olap.Literal;
12 import mondrian.olap.Syntax;
13
14 import com.tonbeller.jpivot.olap.model.Axis;
15 import com.tonbeller.jpivot.olap.model.Position;
16 import com.tonbeller.jpivot.olap.model.Result;
17 import com.tonbeller.jpivot.olap.navi.DrillExpandPosition;
18 import com.tonbeller.jpivot.olap.navi.PlaceHierarchiesOnAxes;
19 import com.tonbeller.jpivot.olap.query.DrillExpandPositionExt;
20 import com.tonbeller.jpivot.olap.query.ResultBase;
21 import com.tonbeller.jpivot.tags.MondrianModelFactory;
22 import com.tonbeller.wcf.bookmarks.Bookmarkable;
23
24 /**
25  * Test Bookmarks
26  */

27 public class BookmarkTest extends TestCase {
28
29   /**
30    * Constructor
31    * @param arg0
32    */

33   public BookmarkTest(String JavaDoc arg0) {
34     super(arg0);
35   }
36
37   public void testTopCount() throws Exception JavaDoc {
38
39     /*
40     select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]}
41     ON columns,
42     {([Time].[1997],[Product].[All Products].[Food].[Produce].[Vegetables].[Fresh Vegetables].[Hermanos])}
43     ON rows from [Sales]
44      */

45     String JavaDoc mdxQuery = "select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].[Store Sales]} "
46         + "ON columns, "
47         + "{([Time].[1997],[Product].[All Products].[Food].[Produce].[Vegetables].[Fresh Vegetables].[Hermanos])}"
48         + " ON rows from [Sales]";
49
50     String JavaDoc renderFile = null; // "c:\\x\\testBMTopCount";
51
int renderNum = 0;
52
53     Result result;
54
55     MondrianModel model = MondrianModelFactory.instance();
56     model.setMdxQuery(mdxQuery);
57     TestConnection.initModel(model);
58
59     DrillExpandPositionExt mdep = (DrillExpandPositionExt) model
60         .getExtension(DrillExpandPosition.ID);
61     PlaceHierarchiesOnAxes placeHier = (PlaceHierarchiesOnAxes) model
62         .getExtension(PlaceHierarchiesOnAxes.ID);
63     //PlaceMembersOnAxes placeMem = (PlaceMembersOnAxes) model.getExtension(PlaceMembersOnAxes.ID);
64

65     // first step
66
result = model.getResult();
67     if (renderFile != null)
68       ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ + ".html");
69
70     assertPosition(result, 1, 0, new String JavaDoc[] { "1997"});
71
72     // put Time dimension on rows axis
73
Axis[] axes = result.getAxes();
74     List JavaDoc positions = axes[1].getPositions();
75     Position pos0 = (Position) positions.get(0);
76     MondrianMember allTime = (MondrianMember) pos0.getMembers()[0];
77     Object JavaDoc exp1 = placeHier.createMemberExpression(allTime.getLevel().getHierarchy());
78     MondrianMember hermanos = (MondrianMember) pos0.getMembers()[1];
79     MondrianLevel brandLevel = (MondrianLevel) hermanos.getLevel();
80     positions = axes[0].getPositions();
81     pos0 = (Position) positions.get(0);
82     MondrianMember unitsales = (MondrianMember) pos0.getMembers()[0];
83     // TopCount([Product].[Brand Name].Members, 5, [Measures].[Unit Sales]))
84
UnresolvedFunCall brandNameMembers = new UnresolvedFunCall("members", Syntax.Property, new Exp[] {
85         new LevelExpr(brandLevel.getMonLevel())});
86
87     UnresolvedFunCall funOrder = new UnresolvedFunCall("topcount", new Exp[] { brandNameMembers,
88         Literal.create(new Integer JavaDoc(5)), new MemberExpr(unitsales.getMonMember())});
89     placeHier.setQueryAxis(axes[1], new Object JavaDoc[] { exp1, funOrder});
90     result = model.getResult();
91     if (renderFile != null)
92       ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ + ".html");
93     assertPosition(result, 1, 1, new String JavaDoc[] { "1997", "Tell Tale"});
94
95     // Drill Down
96
axes = result.getAxes();
97     positions = axes[1].getPositions();
98     pos0 = (Position) positions.get(0);
99     mdep.expand(pos0, pos0.getMembers()[0]); // drilldown 1997
100
result = model.getResult();
101     if (renderFile != null)
102       ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ + ".html");
103
104     assertPosition(result, 1, 5, new String JavaDoc[] { "Q1", "Hermanos"});
105
106     // create Bookmark
107
Object JavaDoc bm = model.getBookmarkState(Bookmarkable.EXTENSIONAL);
108
109     model.setBookmarkState(bm);
110     result = model.getResult();
111     if (renderFile != null)
112       ResultBase.renderHtml(result, model.getCurrentMdx(), renderFile + renderNum++ + ".html");
113     assertPosition(result, 1, 7, new String JavaDoc[] { "Q1", "Ebony"});
114     axes = result.getAxes();
115     positions = axes[1].getPositions();
116     Position pos = (Position) positions.get(1);
117     assertTrue(mdep.canExpand(pos, pos.getMembers()[1]));
118   }
119
120   /**
121    * assert position
122    */

123   private void assertPosition(Result result, int iAxis, int iPos, String JavaDoc[] posMembers) {
124     Position pos = (Position) result.getAxes()[iAxis].getPositions().get(iPos);
125     for (int i = 0; i < posMembers.length; i++) {
126       String JavaDoc str = pos.getMembers()[i].getLabel();
127       assertEquals(str, posMembers[i]);
128     }
129   }
130 } // VariousTest
131
Popular Tags