KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > table > span > PropertySpanBuilderTest


1 package com.tonbeller.jpivot.table.span;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.StringTokenizer JavaDoc;
6
7 import junit.framework.TestCase;
8
9 import com.tonbeller.jpivot.olap.model.Displayable;
10 import com.tonbeller.jpivot.olap.model.Level;
11 import com.tonbeller.jpivot.olap.model.Member;
12 import com.tonbeller.jpivot.olap.model.MemberPropertyMeta;
13 import com.tonbeller.jpivot.olap.model.Property;
14 import com.tonbeller.jpivot.olap.model.impl.HierarchyImpl;
15 import com.tonbeller.jpivot.olap.model.impl.LevelImpl;
16 import com.tonbeller.jpivot.olap.model.impl.MemberImpl;
17 import com.tonbeller.jpivot.olap.model.impl.PropertyImpl;
18
19 public class PropertySpanBuilderTest extends TestCase {
20
21   private TestMemberProperties extension;
22
23   public PropertySpanBuilderTest(String JavaDoc arg0) {
24     super(arg0);
25   }
26
27   class MyMember extends MemberImpl implements Displayable {
28     public MyMember(String JavaDoc label, Level level, List JavaDoc props) {
29       super(props);
30       setLevel(level);
31       setLabel(label);
32     }
33   }
34
35   private Member member(String JavaDoc label, Level level, String JavaDoc propstr) {
36     List JavaDoc props = new ArrayList JavaDoc();
37     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(propstr, ",", false);
38     while (st.hasMoreTokens()) {
39       String JavaDoc tok = st.nextToken();
40       int i = tok.indexOf('=');
41       String JavaDoc name = tok.substring(0, i);
42       String JavaDoc value = tok.substring(i + 1);
43       PropertyImpl p = new PropertyImpl(name, value);
44       p.setLabel(value);
45       props.add(p);
46     }
47     return new MyMember(label, level, props);
48   }
49
50   MemberPropertyMeta meta(String JavaDoc name, Level level) {
51     if (extension.isLevelScope())
52       return new MemberPropertyMeta(name, name, level.getLabel());
53     return new MemberPropertyMeta(name, name, level.getHierarchy().getLabel());
54   }
55
56   Level getLevel1() {
57     HierarchyImpl hier = new HierarchyImpl();
58     hier.setLabel("hier");
59     LevelImpl level = new LevelImpl("level", hier);
60     return level;
61   }
62
63   // Achse mit Parent Membern:
64
// Input:
65
// All(a,b) All(a,b)
66
// All(a,b) Item(a,c)
67
// Ergebnis (b wird in Item Spalte NICHT wiederholt):
68
// A a b A a _
69
// A a b I a c
70
public void testHierarchy() {
71     Level level = getLevel1();
72     Span[][] s = new Span[2][2];
73     Span all = new Span(member("A", level, "a=1,b=2"));
74     Span item = new Span(member("I", level, "a=3,c=4"));
75     s[0][0] = all;
76     s[0][1] = all;
77     s[1][0] = all;
78     s[1][1] = item;
79     SpanCalc sc = new SpanCalc(s);
80
81     PropertySpanBuilder psb = new PropertySpanBuilder(extension);
82     psb.setShowProperties(true);
83     psb.addPropertySpans(sc);
84     //dump(sc);
85
assertSC(sc, new String JavaDoc[] { "A A 1 2 *", "A I 3 * 4" });
86   }
87
88   // Input
89
// A B
90
// A C
91
// Output
92
// A B a b *
93
// A C a * c
94
// wobei die beiden a's identisch gleich sein müssen (wg. SpanConfig.equals())
95
public void testIdentity() {
96     Level level = getLevel1();
97     Span[][] s = new Span[2][2];
98     Span A = new Span(member("A", level, "a=1"));
99     Span B = new Span(member("B", level, "b=2"));
100     Span C = new Span(member("C", level, "c=3"));
101     s[0][0] = A;
102     s[0][1] = B;
103     s[1][0] = A;
104     s[1][1] = C;
105     SpanCalc sc = new SpanCalc(s);
106
107     PropertySpanBuilder psb = new PropertySpanBuilder(extension);
108     psb.setShowProperties(true);
109     psb.addPropertySpans(sc);
110     //dump(sc);
111
assertSC(sc, new String JavaDoc[] { "A B 1 2 *", "A C 1 * 3" });
112     assertTrue(sc.getSpan(0, 2).getObject() == sc.getSpan(1, 2).getObject());
113   }
114
115   void assertSC(SpanCalc sc, String JavaDoc[] rows) {
116     assertEquals(rows.length, sc.getPositionCount());
117     for (int pi = 0; pi < rows.length; pi++) {
118       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(rows[pi]);
119       int HC = st.countTokens();
120       assertEquals(HC, sc.getHierarchyCount());
121       for (int hi = 0; hi < HC; hi++) {
122         String JavaDoc label = st.nextToken();
123         if ("*".equals(label))
124           continue;
125         assertEquals(label, sc.getSpan(pi, hi).getObject().getLabel());
126       }
127     }
128   }
129
130   void dump(SpanCalc sc) {
131     for (int pi = 0; pi < sc.getPositionCount(); pi++) {
132       for (int hi = 0; hi < sc.getHierarchyCount(); hi++) {
133         System.out.print(" " + sc.getSpan(pi, hi).getObject().getLabel());
134       }
135       System.out.println();
136     }
137   }
138
139   SpanCalc makeSC32(Level level0, Level level1) {
140     Span[][] s = new Span[3][2];
141     s[0][0] = new Span(member("A", level0, "a=1"));
142     s[1][0] = new Span(member("B", level0, "b=2"));
143     s[2][0] = new Span(member("C", level0, "c=3"));
144     s[0][1] = new Span(member("D", level1, "b=4,c=7"));
145     s[1][1] = new Span(member("E", level1, "b=5,d=8"));
146     s[2][1] = new Span(member("F", level1, "b=6,e=9"));
147     return new SpanCalc(s);
148   }
149
150   public void testAddPropertySpansSameHier() {
151     Level level = getLevel1();
152     SpanCalc sc = makeSC32(level, level);
153     PropertySpanBuilder psb = new PropertySpanBuilder(extension);
154     psb.addPropertySpans(sc);
155     psb.setShowProperties(true);
156     psb.addPropertySpans(sc);
157     //dump(sc);
158
assertSC(sc, new String JavaDoc[] { "A D 1 4 7 * *", "B E * 5 * 8 *", "C F * 6 3 * 9" });
159   }
160
161   public void testAddPropertySpansSameDiffHier() {
162     LevelImpl level0 = new LevelImpl();
163     level0.setLabel("level0");
164     HierarchyImpl hier0 = new HierarchyImpl();
165     hier0.setLabel("hier0");
166     level0.setHierarchy(hier0);
167
168     LevelImpl level1 = new LevelImpl();
169     HierarchyImpl hier1 = new HierarchyImpl();
170     hier1.setLabel("hier1");
171     level1.setHierarchy(hier1);
172     level1.setLabel("level1");
173
174     SpanCalc sc = makeSC32(level0, level1);
175     PropertySpanBuilder psb = new PropertySpanBuilder(extension);
176     psb.addPropertySpans(sc);
177     psb.setShowProperties(true);
178     psb.addPropertySpans(sc);
179     //dump(sc);
180
assertSC(sc, new String JavaDoc[] { "A 1 * * D 4 7 * *", "B * 2 * E 5 * 8 *", "C * * 3 F 6 3 * 9" });
181   }
182
183   public void testVisibleNames1() {
184     Level level = getLevel1();
185     SpanCalc sc = makeSC32(level, level);
186     PropertySpanBuilder psb = new PropertySpanBuilder(extension);
187     List JavaDoc vpm = new ArrayList JavaDoc();
188     vpm.add(meta("b", level));
189     vpm.add(meta("a", level));
190     psb.setVisiblePropertyMetas(vpm);
191     psb.setShowProperties(true);
192     psb.addPropertySpans(sc);
193     //dump(sc);
194
assertSC(sc, new String JavaDoc[] { "A D 4 1", "B E 5 *", "C F 6 *" });
195   }
196
197   public void testVisibleNames2() {
198     // different levels, same hierarchy
199
HierarchyImpl hier = new HierarchyImpl();
200     hier.setLabel("hier");
201     LevelImpl level0 = new LevelImpl();
202     level0.setLabel("level0");
203     level0.setHierarchy(hier);
204     LevelImpl level1 = new LevelImpl();
205     level1.setHierarchy(hier);
206     level1.setLabel("level1");
207
208     SpanCalc sc = makeSC32(level0, level1);
209     PropertySpanBuilder psb = new PropertySpanBuilder(extension);
210     List JavaDoc vpm = new ArrayList JavaDoc();
211     vpm.add(meta("b", level1));
212     vpm.add(meta("a", level0));
213     psb.setVisiblePropertyMetas(vpm);
214     psb.setShowProperties(true);
215     psb.addPropertySpans(sc);
216
217     assertSC(sc, new String JavaDoc[] { "A D 4 1", "B E 5 *", "C F 6 *" });
218   }
219
220   public void testVisibleNames3() {
221     // different levels, different hierarchies (1)
222
HierarchyImpl hier0 = new HierarchyImpl();
223     hier0.setLabel("hier0");
224     LevelImpl level0 = new LevelImpl();
225     level0.setLabel("level0");
226     level0.setHierarchy(hier0);
227     HierarchyImpl hier1 = new HierarchyImpl();
228     hier1.setLabel("hier1");
229     LevelImpl level1 = new LevelImpl();
230     level1.setHierarchy(hier1);
231     level1.setLabel("level1");
232
233     SpanCalc sc = makeSC32(level0, level1);
234     PropertySpanBuilder psb = new PropertySpanBuilder(extension);
235     List JavaDoc vpm = new ArrayList JavaDoc();
236     vpm.add(meta("b", level1));
237     vpm.add(meta("a", level0));
238     psb.setVisiblePropertyMetas(vpm);
239     psb.setShowProperties(true);
240     psb.addPropertySpans(sc);
241     assertSC(sc, new String JavaDoc[] { "A 1 D 4", "B * E 5", "C * F 6" });
242   }
243
244   public void testVisibleNames4() {
245     // different levels, different hierarchies (2)
246
HierarchyImpl hier0 = new HierarchyImpl();
247     hier0.setLabel("hier0");
248     LevelImpl level0 = new LevelImpl();
249     level0.setLabel("level0");
250     level0.setHierarchy(hier0);
251     HierarchyImpl hier1 = new HierarchyImpl();
252     hier1.setLabel("hier1");
253     LevelImpl level1 = new LevelImpl();
254     level1.setHierarchy(hier1);
255     level1.setLabel("level1");
256
257     SpanCalc sc = makeSC32(level0, level1);
258     PropertySpanBuilder psb = new PropertySpanBuilder(extension);
259     List JavaDoc vpm = new ArrayList JavaDoc();
260     vpm.add(meta("b", level0));
261     vpm.add(meta("a", level0));
262     vpm.add(meta("c", level1));
263     psb.setVisiblePropertyMetas(vpm);
264     psb.setShowProperties(true);
265     psb.addPropertySpans(sc);
266     assertSC(sc, new String JavaDoc[] { "A * 1 D 7", "B 2 * E *", "C * * F *" });
267   }
268
269   protected void setUp() throws Exception JavaDoc {
270     extension = new TestMemberProperties();
271   }
272
273 }
274
Popular Tags