KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.tonbeller.jpivot.table.span;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import junit.framework.TestCase;
7
8 import com.tonbeller.jpivot.olap.model.Axis;
9 import com.tonbeller.jpivot.olap.model.Displayable;
10 import com.tonbeller.jpivot.olap.model.Hierarchy;
11 import com.tonbeller.jpivot.olap.model.Level;
12 import com.tonbeller.jpivot.olap.model.Member;
13 import com.tonbeller.jpivot.olap.model.Visitor;
14 import com.tonbeller.jpivot.table.LevelAxisDecorator;
15 import com.tonbeller.jpivot.test.olap.DimensionBuilder;
16 import com.tonbeller.jpivot.test.olap.TestDimension;
17 import com.tonbeller.jpivot.test.olap.TestHierarchy;
18 import com.tonbeller.jpivot.test.olap.TestMember;
19 import com.tonbeller.jpivot.test.olap.TestMemberTree;
20 import com.tonbeller.jpivot.test.olap.TestOlapModelUtils;
21
22 /**
23  * Created on 29.10.2002
24  *
25  * @author av
26  */

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

33   public SpanCalcTest(String JavaDoc arg0) {
34     super(arg0);
35   }
36
37   public static void main(String JavaDoc[] args) {
38     junit.textui.TestRunner.run(SpanCalcTest.class);
39   }
40
41   protected void setUp() throws Exception JavaDoc {
42     
43   }
44
45
46   /**
47    * Creates an axis for testing. 2 Hierachies/Dimensions A and X, A has 2 Levels B+C,
48    * the first member B1 is expanded.
49    * <pre>
50    * B0 Y0
51    * B0 Y1
52    * C0 Y0
53    * C0 Y1
54    * C1 Y0
55    * C1 Y1
56    * C2 Y0
57    * C2 Y1
58    * B1 Y0
59    * B1 Y1
60    * </pre>
61    * @author av
62    */

63
64   public static Axis createAxis1() {
65     DimensionBuilder db = new DimensionBuilder();
66
67     TestDimension dim1 = db.build("A", new String JavaDoc[]{"B", "C"}, new int[]{2, 3});
68     TestHierarchy hier1 = (TestHierarchy)dim1.getHierarchies()[0];
69     TestMember[] members = hier1.getRootMembers();
70
71     // expand first member
72
for (Iterator JavaDoc it = members[0].getChildMember().iterator(); it.hasNext(); )
73       ((TestMember)it.next()).setVisible(true);
74
75     TestDimension dim2 = db.build("X", new String JavaDoc[]{"Y"}, new int[]{2});
76     
77     TestDimension dims[] = new TestDimension[]{ dim1, dim2 };
78
79     return TestOlapModelUtils.createAxis(dims);
80   }
81
82   /**
83    * Creates an axis for testing. 2 Hierachies/Dimensions A and X, A has 2 Levels B+C,
84    * the first member B1 is expanded. Axis is decorated with parent members.
85    * <pre>
86    * B0 B0 Y0
87    * B0 B0 Y1
88    * B0 C0 Y0
89    * B0 C0 Y1
90    * B0 C1 Y0
91    * B0 C1 Y1
92    * B0 C2 Y0
93    * B0 C2 Y1
94    * B1 B1 Y0
95    * B1 B1 Y1
96    * </pre>
97    * @author av
98    */

99   public static Axis createAxis2() {
100     Axis axis = createAxis1();
101     return new LevelAxisDecorator(axis, new TestMemberTree());
102   }
103
104   static class SpanElem implements Displayable {
105     String JavaDoc s;
106     SpanElem(String JavaDoc s) {
107       this.s = s;
108     }
109     public String JavaDoc toString() {
110       return s;
111     }
112     public String JavaDoc getLabel() {
113       return s;
114     }
115     public void accept(Visitor visitor) {
116       throw new UnsupportedOperationException JavaDoc();
117     }
118   }
119
120   static class A extends SpanElem { public A() { super("a"); } }
121   static class B extends SpanElem { public B() { super("b"); } }
122   static class C extends SpanElem { public C() { super("c"); } }
123   static class D extends SpanElem { public D() { super("d"); } }
124   static class E extends SpanElem { public E() { super("e"); } }
125   static class F extends SpanElem { public F() { super("f"); } }
126   static class G extends SpanElem { public G() { super("g"); } }
127
128  
129   static final A a = new A();
130   static final B b = new B();
131   static final C c = new C();
132   static final D d = new D();
133   static final E e = new E();
134   static final F f = new F();
135   static final G g = new G();
136
137   /**
138    * <pre>
139    * baaa
140    * bbaa
141    * bbba
142    * bbbb
143    * </pre>
144    */

145   public static SpanCalc createTriangle() {
146     Span[][] spans = new Span[4][4];
147     A a = new A();
148     B b = new B();
149     for (int posIndex = 0; posIndex < spans.length; posIndex++) {
150       for (int hierIndex = 0; hierIndex < spans[posIndex].length; hierIndex++) {
151         if (posIndex < hierIndex)
152           spans[posIndex][hierIndex] = new Span(a);
153         else
154           spans[posIndex][hierIndex] = new Span(b);
155       }
156     }
157     return new SpanCalc(spans);
158   }
159
160   
161   
162
163
164   void check(SpanCalc sc, int posIndex, int hierIndex, boolean significant, int posSpan, int hierSpan) {
165     sc.initialize();
166     Span s = sc.getSpan(posIndex, hierIndex);
167     assertEquals("significance: ", significant, s.isSignificant());
168     assertEquals("positionSpan:" , posSpan, s.getPositionSpan());
169     assertEquals("hierarchySpan: ", hierSpan, s.getHierarchySpan());
170   }
171
172   void assertMember(SpanCalc sc, int posIndex, int hierIndex) {
173     sc.initialize();
174     Object JavaDoc o = sc.getSpan(posIndex, hierIndex).getObject();
175     assertTrue("Member expected", o instanceof Member);
176   }
177   
178   void assertLevel(SpanCalc sc, int posIndex, int hierIndex) {
179     sc.initialize();
180     Object JavaDoc o = sc.getSpan(posIndex, hierIndex).getObject();
181     assertTrue("Level expected", o instanceof Level);
182   }
183   
184   void assertHierarchy(SpanCalc sc, int posIndex, int hierIndex) {
185     sc.initialize();
186     Object JavaDoc o = sc.getSpan(posIndex, hierIndex).getObject();
187     assertTrue("Hierarchy expected", o instanceof Hierarchy);
188   }
189   
190   void checkPlausibility(SpanCalc sc) {
191     sc.initialize();
192     // equal size
193
Span[][] spans = sc.spans;
194     int length = sc.spans[0].length;
195     for (int i = 1; i < sc.spans.length; i++)
196       assertEquals("spans[" + i + "].length: ", length, sc.spans[i].length);
197       
198     int[] sumPos = new int[sc.getHierarchyCount()];
199     int[] sumHier = new int[sc.getPositionCount()];
200     
201     for (int posIndex = 0; posIndex < sc.getPositionCount(); posIndex ++) {
202       for (int hierIndex = 0; hierIndex < sc.getHierarchyCount(); hierIndex ++) {
203         Span s = sc.getSpan(posIndex, hierIndex);
204         assertEquals("span[posIndex=" + posIndex + ", hierIndex=" + hierIndex + "] Position Index: ", posIndex, s.getPositionIndex());
205         assertEquals("span[posIndex=" + posIndex + ", hierIndex=" + hierIndex + "] Hierarchy Index: ", hierIndex, s.getHierarchyIndex());
206         assertTrue("span[posIndex=" + posIndex + ", hierIndex=" + hierIndex + "] Significance: ", s.isSignificant() || (s.getHierarchySpan() == 0 && s.getPositionSpan() == 0));
207         if (s.isSignificant()) {
208           for (int pi = 0; pi < s.getPositionSpan(); pi++) {
209             for (int hi = 0; hi < s.getHierarchySpan(); hi++) {
210               sumPos[hierIndex + hi] += 1;
211               sumHier[posIndex + pi] += 1;
212             }
213           }
214         }
215       }
216     }
217
218     // check the sum of hierarchy/position Spans
219
int sum = sumPos[0];
220     for (int i = 0; i < sumPos.length; i++)
221       assertEquals("span sumPositions[hierarchy=" + i + "]: ", sum, sumPos[i]);
222       
223     sum = sumHier[0];
224     for (int i = 0; i < sumHier.length; i++)
225       assertEquals("span sumHier[position=" + i + "]: ", sum, sumHier[i]);
226   }
227   
228   /* -------------------------------- tests start here ----------------------------------- */
229   
230   public void testPlausibility() {
231     checkPlausibility(new SpanCalc(createAxis1()));
232     checkPlausibility(new SpanCalc(createAxis2()));
233     checkPlausibility(createTriangle());
234   }
235   
236   /**
237    * NO_SPAN for a and NO_SPAN for b
238    */

239   public void testTriangle_AN_BN() {
240     SpanCalc sc = createTriangle();
241     checkPlausibility(sc);
242     for (int pi = 0; pi < sc.getPositionCount(); pi++)
243       for (int hi = 0; hi < sc.getHierarchyCount(); hi++)
244         check(sc, pi, hi, true, 1, 1);
245   }
246
247
248   /**
249    * A spans on hierarchies, B spans on hieraries
250    * <pre>
251    * baaa
252    * bbaa
253    * bbba
254    * bbbb
255    * </pre>
256    */

257   public void testTriangle_AH_BH() {
258     
259     SpanCalc sc = createTriangle();
260     SpanConfigSupport scs = new SpanConfigSupport();
261     scs.setDirection(A.class, SpanConfig.HIERARCHY_SPAN);
262     scs.setDirection(B.class, SpanConfig.HIERARCHY_SPAN);
263     sc.setConfig(scs);
264     checkPlausibility(sc);
265     // toString("testTriangle_AH_BH", sc);
266

267     check(sc, 0, 0, true, 1, 1);
268     check(sc, 0, 1, true, 1, 3);
269     check(sc, 0, 2, false, 0, 0);
270     check(sc, 0, 3, false, 0, 0);
271
272     check(sc, 1, 0, true, 1, 2);
273     check(sc, 1, 1, false, 0, 0);
274     check(sc, 1, 2, true, 1, 2);
275     check(sc, 1, 3, false, 0, 0);
276
277     check(sc, 2, 0, true, 1, 3);
278     check(sc, 2, 1, false, 0, 0);
279     check(sc, 2, 2, false, 0, 0);
280     check(sc, 2, 3, true, 1, 1);
281
282     check(sc, 3, 0, true, 1, 4);
283     check(sc, 3, 1, false, 0, 0);
284     check(sc, 3, 2, false, 0, 0);
285     check(sc, 3, 3, false, 0, 0);
286   }
287
288   /**
289    * A spans on positions, B spans on positions
290    * <pre>
291    * baaa
292    * bbaa
293    * bbba
294    * bbbb
295    * </pre>
296    */

297   public void testTriangle_AP_BP() {
298     
299     SpanCalc sc = createTriangle();
300     SpanConfigSupport scs = new SpanConfigSupport();
301     scs.setDirection(A.class, SpanConfig.POSITION_SPAN);
302     scs.setDirection(B.class, SpanConfig.POSITION_SPAN);
303     sc.setConfig(scs);
304     checkPlausibility(sc);
305     //toString("testTriangle_AP_BP", sc);
306
check_AP_BP(sc);
307
308     scs.setDirection(B.class, SpanConfig.HIERARCHY_THEN_POSITION_SPAN);
309     sc.setConfig(scs);
310     checkPlausibility(sc);
311     check_AP_BP(sc);
312
313   }
314
315   void check_AP_BP(SpanCalc sc) {
316     check(sc, 0, 0, true, 4, 1);
317     check(sc, 1, 0, false, 0, 0);
318     check(sc, 2, 0, false, 0, 0);
319     check(sc, 3, 0, false, 0, 0);
320     
321     check(sc, 0, 1, true, 1, 1);
322     check(sc, 1, 1, true, 3, 1);
323     check(sc, 2, 0, false, 0, 0);
324     check(sc, 3, 0, false, 0, 0);
325     
326     check(sc, 0, 2, true, 1, 1);
327     check(sc, 1, 2, true, 1, 1);
328     check(sc, 2, 2, true, 2, 1);
329     check(sc, 3, 2, false, 0, 0);
330
331     check(sc, 0, 3, true, 1, 1);
332     check(sc, 1, 3, true, 1, 1);
333     check(sc, 2, 3, true, 1, 1);
334     check(sc, 3, 3, true, 1, 1);
335   }
336
337   /**
338    * A spans on hierarchies, B spans on positions
339    * <pre>
340    * baaa
341    * bbaa
342    * bbba
343    * bbbb
344    * </pre>
345    */

346   public void testTriangle_AH_BP() {
347     
348     SpanCalc sc = createTriangle();
349     SpanConfigSupport scs = new SpanConfigSupport();
350     scs.setDirection(A.class, SpanConfig.HIERARCHY_SPAN);
351     scs.setDirection(B.class, SpanConfig.POSITION_SPAN);
352     sc.setConfig(scs);
353     checkPlausibility(sc);
354     //toString("testTriangle_AH_BP", sc);
355

356     check(sc, 0, 0, true, 4, 1);
357     check(sc, 1, 0, false, 0, 0);
358     check(sc, 2, 0, false, 0, 0);
359     check(sc, 3, 0, false, 0, 0);
360     
361     check(sc, 0, 1, true, 1, 3);
362     check(sc, 1, 1, true, 3, 1);
363     check(sc, 2, 0, false, 0, 0);
364     check(sc, 3, 0, false, 0, 0);
365     
366     check(sc, 0, 2, false, 0, 0);
367     check(sc, 1, 2, true, 1, 2);
368     check(sc, 2, 2, true, 2, 1);
369     check(sc, 3, 2, false, 0, 0);
370
371     check(sc, 0, 3, false, 0, 0);
372     check(sc, 1, 3, false, 0, 0);
373     check(sc, 2, 3, true, 1, 1);
374     check(sc, 3, 3, true, 1, 1);
375   }
376
377
378   public void testTriangle_AHP_BHP() {
379     SpanCalc sc = createTriangle();
380     SpanConfigSupport scs = new SpanConfigSupport();
381     scs.setDirection(A.class, SpanConfig.HIERARCHY_THEN_POSITION_SPAN);
382     scs.setDirection(B.class, SpanConfig.HIERARCHY_THEN_POSITION_SPAN);
383     sc.setConfig(scs);
384     checkPlausibility(sc);
385     //toString("testTriangle_AHP_BHP", sc);
386

387     check(sc, 0, 0, true, 4, 1);
388     check(sc, 0, 1, true, 1, 3);
389     check(sc, 0, 2, false, 0, 0);
390     check(sc, 0, 3, false, 0, 0);
391     check(sc, 1, 0, false, 0, 0);
392     check(sc, 1, 1, true, 3, 1);
393     check(sc, 1, 2, true, 1, 2);
394     check(sc, 1, 3, false, 0, 0);
395     check(sc, 2, 0, false, 0, 0);
396     check(sc, 2, 1, false, 0, 0);
397     check(sc, 2, 2, true, 2, 1);
398     check(sc, 2, 3, true, 1, 1);
399     check(sc, 3, 0, false, 0, 0);
400     check(sc, 3, 1, false, 0, 0);
401     check(sc, 3, 2, false, 0, 0);
402     check(sc, 3, 3, true, 1, 1);
403
404   }
405
406   /**
407    * <pre>
408    * ab
409    * ac
410    * bc
411    * </pre>
412    * because of hierarchical position break algorithm, the 2 c's are not combined into a single span.
413    */

414   public void testHierarchicalPositionSpans1() {
415     Span[][] spans = new Span[3][2];
416     spans[0][0] = new Span(a);
417     spans[0][1] = new Span(b);
418     spans[1][0] = new Span(a);
419     spans[1][1] = new Span(c);
420     spans[2][0] = new Span(b);
421     spans[2][1] = new Span(c);
422
423
424     SpanCalc sc = new SpanCalc(spans);
425     SpanConfigSupport scs = new SpanConfigSupport();
426     scs.setDefaultDirection(SpanConfig.POSITION_SPAN);
427     sc.setConfig(scs);
428     checkPlausibility(sc);
429     
430     check(sc, 0, 0, true, 2, 1);
431     check(sc, 1, 0, false, 0, 0);
432     check(sc, 2, 0, true, 1, 1);
433     
434     // there are two C spans!!
435
check(sc, 0, 1, true, 1, 1);
436     check(sc, 1, 1, true, 1, 1);
437     check(sc, 2, 1, true, 1, 1);
438    
439   }
440   
441   /**
442    * <pre>
443    * ac
444    * bc
445    * </pre>
446    */

447   public void testHierarchicalPositionSpans2() {
448     Span[][] spans = new Span[2][2];
449     spans[0][0] = new Span(a);
450     spans[1][0] = new Span(b);
451     spans[0][1] = new Span(c);
452     spans[1][1] = new Span(c);
453     SpanCalc sc = new SpanCalc(spans);
454
455     SpanConfigSupport scs = new SpanConfigSupport();
456     scs.setDefaultDirection(SpanConfig.POSITION_SPAN);
457     sc.setConfig(scs);
458     sc.initialize();
459     checkPlausibility(sc);
460
461     // there are two C spans!!
462
check(sc, 0, 0, true, 1, 1);
463     check(sc, 1, 0, true, 1, 1);
464     check(sc, 0, 1, true, 1, 1);
465     check(sc, 1, 1, true, 1, 1);
466    
467   }
468
469   /**
470    * <pre>
471    * aae
472    * abe
473    * ace
474    * ade
475    * </pre>
476    */

477   public void testBug1() {
478     Span[][] spans = new Span[4][3];
479     
480     spans[0][0] = new Span(a);
481     spans[1][0] = new Span(a);
482     spans[2][0] = new Span(a);
483     spans[3][0] = new Span(a);
484
485     spans[0][1] = new Span(a);
486     spans[1][1] = new Span(b);
487     spans[2][1] = new Span(c);
488     spans[3][1] = new Span(d);
489
490     spans[0][2] = new Span(e);
491     spans[1][2] = new Span(e);
492     spans[2][2] = new Span(e);
493     spans[3][2] = new Span(e);
494     
495     SpanCalc sc = new SpanCalc(spans);
496
497     SpanConfigSupport scs = new SpanConfigSupport();
498     scs.setDefaultDirection(SpanConfig.HIERARCHY_THEN_POSITION_SPAN);
499     sc.setConfig(scs);
500     sc.initialize();
501     checkPlausibility(sc);
502
503     // 4 differnt e's
504
check(sc, 0, 2, true, 1, 1);
505     check(sc, 1, 2, true, 1, 1);
506     check(sc, 2, 2, true, 1, 1);
507     check(sc, 3, 2, true, 1, 1);
508    
509   }
510
511
512
513   /**
514    * <pre>
515    * aa
516    * aa
517    * </pre>
518    */

519   public void testMultiSpan1() {
520     Span[][] spans = new Span[2][2];
521     spans[0][0] = new Span(a);
522     spans[1][0] = new Span(a);
523     spans[0][1] = new Span(a);
524     spans[1][1] = new Span(a);
525     SpanCalc sc = new SpanCalc(spans);
526
527     SpanConfigSupport scs = new SpanConfigSupport();
528     scs.setDefaultDirection(SpanConfig.HIERARCHY_THEN_POSITION_SPAN);
529     sc.setConfig(scs);
530     checkPlausibility(sc);
531     //toString("testMultiSpan1", sc);
532

533     // all a's are united
534
check(sc, 0, 0, true, 2, 2);
535     check(sc, 1, 0, false, 0, 0);
536     check(sc, 0, 1, false, 0, 0);
537     check(sc, 1, 1, false, 0, 0);
538    
539   }
540
541   /**
542    * <pre>
543    * aacd
544    * aace
545    * abcf
546    * abcg
547    * </pre>
548    */

549   public SpanCalc createMulti1() {
550     Span[][] spans = new Span[4][4];
551     spans[0][0] = new Span(a);
552     spans[1][0] = new Span(a);
553     spans[2][0] = new Span(a);
554     spans[3][0] = new Span(a);
555
556     spans[0][1] = new Span(a);
557     spans[1][1] = new Span(a);
558     spans[2][1] = new Span(b);
559     spans[3][1] = new Span(b);
560
561     spans[0][2] = new Span(c);
562     spans[1][2] = new Span(c);
563     spans[2][2] = new Span(c);
564     spans[3][2] = new Span(c);
565
566     spans[0][3] = new Span(d);
567     spans[1][3] = new Span(e);
568     spans[2][3] = new Span(f);
569     spans[3][3] = new Span(g);
570
571     return new SpanCalc(spans);
572   }
573   
574
575
576   /**
577    * <pre>
578    * aacd
579    * aace
580    * abcf
581    * abcg
582    * </pre>
583    */

584   public void testMultiSpan2() {
585     SpanCalc sc = createMulti1();
586
587     SpanConfigSupport scs = new SpanConfigSupport();
588     scs.setDefaultDirection(SpanConfig.HIERARCHY_THEN_POSITION_SPAN);
589     sc.setConfig(scs);
590     sc.initialize();
591     checkPlausibility(sc);
592
593     // there are two C spans!!
594
check(sc, 0, 2, true, 2, 1);
595     check(sc, 1, 2, false, 0, 0);
596     check(sc, 2, 2, true, 2, 1);
597     check(sc, 3, 1, false, 0, 0);
598   }
599
600   
601   /** --------------------------------------------------------------------- **/
602
603
604   static class MySpanCalc extends SpanCalc {
605     int counter;
606     public MySpanCalc(Axis axis) {
607       super(axis);
608     }
609     public void initialize() {
610       ++ counter;
611       super.initialize();
612     }
613   }
614   
615   public void testInitializer() {
616     Axis axis1 = createAxis1();
617     MySpanCalc sc = new MySpanCalc(axis1);
618     assertEquals(sc.counter, 0);
619     sc.getHierarchyCount();
620     sc.getHierarchyCount();
621     assertEquals(sc.counter, 1);
622
623     sc = new MySpanCalc(axis1);
624     assertEquals(sc.counter, 0);
625     sc.getPositionCount();
626     sc.getPositionCount();
627     assertEquals(sc.counter, 1);
628     
629     sc = new MySpanCalc(axis1);
630     assertEquals(sc.counter, 0);
631     sc.getSpan(0,0);
632     sc.getSpan(0,0);
633     assertEquals(sc.counter, 1);
634     
635   }
636   
637   /** --------------------------------------------------------------------- **/
638   class AllEqual implements Displayable {
639     public boolean equals(Object JavaDoc o) {
640       return o instanceof AllEqual;
641     }
642     public String JavaDoc getLabel() {
643       return this.toString();
644     }
645     public void accept(Visitor visitor) {
646       throw new UnsupportedOperationException JavaDoc();
647     }
648   }
649   
650   SpanHeaderFactory allEqualFactory = new SpanHeaderFactory() {
651     public Span create(Span span) {
652       AllEqual ae = new AllEqual();
653       return new Span(span.getAxis(), span.getPosition(), ae);
654     }
655   };
656
657   /**
658    * <pre>
659    * Adds an object O to the hierarchy, that is equal with all its instances.
660    * O B0 B0 Y0
661    * O B0 B0 Y1
662    * O B0 C0 Y0
663    * O B0 C0 Y1
664    * O B0 C1 Y0
665    * O B0 C1 Y1
666    * O B0 C2 Y0
667    * O B0 C2 Y1
668    * O B1 B1 Y0
669    * O B1 B1 Y1
670    * </pre>
671    */

672   public void testAddAllEqualToHierarchy() {
673     SpanCalc span2 = new SpanCalc(createAxis2());
674     span2.addHierarchyHeader(allEqualFactory, true);
675     checkPlausibility(span2);
676     assertEquals(4, span2.getHierarchyCount());
677   }
678
679   /** --------------------------------------------------------------------- **/
680
681   /**
682    * <pre>
683    * H1 B0 B0 H2 Y0
684    * H1 B0 B0 H2 Y1
685    * H1 B0 C0 H2 Y0
686    * H1 B0 C0 H2 Y1
687    * H1 B0 C1 H2 Y0
688    * H1 B0 C1 H2 Y1
689    * H1 B0 C2 H2 Y0
690    * H1 B0 C2 H2 Y1
691    * H1 B1 B1 H2 Y0
692    * H1 B1 B1 H2 Y1
693    * </pre>
694    */

695   public void testAddHierarchiesToHierarchy() {
696     SpanCalc sc = new SpanCalc(createAxis2());
697     sc.addHierarchyHeader(new HierarchyHeaderFactory(), true);
698     SpanConfigSupport scs = new SpanConfigSupport();
699     scs.setDefaultDirection(SpanConfig.POSITION_SPAN);
700     sc.setConfig(scs);
701     checkPlausibility(sc);
702
703     assertEquals(5, sc.getHierarchyCount());
704     assertEquals(10, sc.getPositionCount());
705
706     assertHierarchy(sc, 0, 0);
707     assertMember(sc, 0, 1);
708     assertMember(sc, 0, 2);
709     assertHierarchy(sc, 0, 3);
710     assertMember(sc, 0, 4);
711     
712     // hierarchical spans on positions:
713
// H2 is combined into spans of height 2 because of its parents!
714

715     check(sc, 0, 0, true, 10, 1);
716     check(sc, 0, 1, true, 8, 1);
717     check(sc, 0, 2, true, 2, 1);
718     check(sc, 0, 3, true, 2, 1);
719     check(sc, 0, 4, true, 1, 1);
720   }
721
722
723   /** --------------------------------------------------------------------- **/
724
725   /**
726    * <pre>
727    * L1 B0 B0 B0 L3 Y0
728    * L1 B0 B0 B0 L3 Y1
729    * L1 B0 L2 C0 L3 Y0
730    * L1 B0 L2 C0 L3 Y1
731    * L1 B0 L2 C1 L3 Y0
732    * L1 B0 L2 C1 L3 Y1
733    * L1 B0 L2 C2 L3 Y0
734    * L1 B0 L2 C2 L3 Y1
735    * L1 B1 B1 B1 L3 Y0
736    * L1 B1 B1 B1 L3 Y1
737    * </pre>
738    */

739   public void testAddLevelsToHierarchy() {
740     SpanCalc sc = new SpanCalc(createAxis2());
741     sc.addHierarchyHeader(new LevelHeaderFactory(), true);
742     SpanConfigSupport scs = new SpanConfigSupport();
743     scs.setDefaultDirection(SpanConfig.POSITION_SPAN);
744     sc.setConfig(scs);
745     checkPlausibility(sc);
746     assertEquals(6, sc.getHierarchyCount());
747     assertEquals(10, sc.getPositionCount());
748
749     assertLevel(sc, 0, 0);
750     assertLevel(sc, 1, 0);
751     assertLevel(sc, 1, 4);
752
753     assertMember(sc, 0, 2);
754     assertMember(sc, 1, 2);
755     assertLevel (sc, 2, 2);
756     assertLevel (sc, 3, 2);
757     assertLevel (sc, 4, 2);
758     assertLevel (sc, 5, 2);
759     assertLevel (sc, 6, 2);
760     assertLevel (sc, 7, 2);
761     assertMember(sc, 8, 2);
762     assertMember(sc, 9, 2);
763
764     assertMember(sc, 0, 1);
765     assertMember(sc, 1, 1);
766
767     // hierarchical spans on positions:
768
// L3 is combined into spans of height 2 because of its parents!
769

770     check(sc, 0, 0, true, 10, 1);
771     check(sc, 0, 1, true, 8, 1);
772     check(sc, 0, 2, true, 2, 1);
773     check(sc, 0, 3, true, 2, 1);
774     check(sc, 0, 4, true, 2, 1);
775     check(sc, 0, 5, true, 1, 1);
776
777     check(sc, 1, 0, false, 0, 0);
778     check(sc, 1, 1, false, 0, 0);
779     check(sc, 1, 2, false, 0, 0);
780     check(sc, 1, 3, false, 0, 0);
781     check(sc, 1, 4, false, 0, 0);
782     check(sc, 1, 5, true, 1, 1);
783
784     check(sc, 2, 2, true, 6, 1);
785     
786   }
787
788   /** --------------------------------------------------------------------- **/
789
790   /**
791    * <pre>
792    * B C Y (Levels)
793    * --------
794    * B0 B0 Y0
795    * B0 B0 Y1
796    * B0 C0 Y0
797    * B0 C0 Y1
798    * B0 C1 Y0
799    * B0 C1 Y1
800    * B0 C2 Y0
801    * B0 C2 Y1
802    * B1 B1 Y0
803    * B1 B1 Y1
804    * </pre>
805    * @author av
806    */

807
808   public void testCreatePositionHeader1() {
809     SpanCalc sc = new SpanCalc(createAxis2());
810     
811     SpanCalc h = sc.createPositionHeader(new LevelHeaderFactory());
812     checkPlausibility(h);
813     assertEquals(1, h.getPositionCount());
814     assertEquals(3, h.getHierarchyCount());
815
816     check(h, 0, 0, true, 1, 1);
817     check(h, 0, 1, true, 1, 1);
818     check(h, 0, 2, true, 1, 1);
819     assertEquals("B", ((Level)h.getSpan(0,0).getObject()).getLabel());
820     assertEquals("C", ((Level)h.getSpan(0,1).getObject()).getLabel());
821     assertEquals("Y", ((Level)h.getSpan(0,2).getObject()).getLabel());
822   }
823
824   /**
825    * <pre>
826    * A A X (Hierarchies)
827    * --------
828    * B0 B0 Y0
829    * B0 B0 Y1
830    * B0 C0 Y0
831    * B0 C0 Y1
832    * B0 C1 Y0
833    * B0 C1 Y1
834    * B0 C2 Y0
835    * B0 C2 Y1
836    * B1 B1 Y0
837    * B1 B1 Y1
838    * </pre>
839    * @author av
840    */

841
842   public void testCreatePositionHeader2() {
843     SpanCalc sc = new SpanCalc(createAxis2());
844     
845     SpanCalc h = sc.createPositionHeader(new HierarchyHeaderFactory());
846     checkPlausibility(h);
847     assertEquals(1, h.getPositionCount());
848     assertEquals(3, h.getHierarchyCount());
849
850     check(h, 0, 0, true, 1, 1);
851     check(h, 0, 1, true, 1, 1);
852     check(h, 0, 2, true, 1, 1);
853     assertEquals("A", ((Hierarchy)h.getSpan(0,0).getObject()).getLabel());
854     assertEquals("A", ((Hierarchy)h.getSpan(0,1).getObject()).getLabel());
855     assertEquals("X", ((Hierarchy)h.getSpan(0,2).getObject()).getLabel());
856   }
857   
858   public void toString(String JavaDoc title, SpanCalc sc) {
859     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(System.out);
860     pw.println("*** " + title + " ***");
861     pw.println("objects:");
862     printObjects(sc, pw);
863     pw.println("spans:");
864     printSpans(sc, pw);
865     pw.println("breaks");
866     printBreaks(sc, pw);
867     pw.println("code:");
868     printCode(sc, pw);
869     //pw.println("-----------------------------------------");
870
pw.flush();
871   }
872   
873   void printObjects(SpanCalc sc, PrintWriter JavaDoc out) {
874     for (int pi = 0; pi < sc.getPositionCount(); pi++) {
875       for (int hi = 0; hi < sc.getHierarchyCount(); hi++) {
876         Span s = sc.getSpan(pi, hi);
877         out.print(" ");
878         out.print(s.getObject().toString());
879       }
880       out.println();
881     }
882   }
883
884   void printBreaks(SpanCalc sc, PrintWriter JavaDoc out) {
885     for (int pi = 0; pi < sc.getPositionCount(); pi++) {
886       for (int hi = 0; hi < sc.getHierarchyCount(); hi++) {
887         Span s = sc.getSpan(pi, hi);
888         out.print(" ");
889         out.print(sc.forcePositionBreak[pi][hi] ? "x" : ".");
890       }
891       out.println();
892     }
893   }
894
895   void printCode(SpanCalc sc, PrintWriter JavaDoc out) {
896     for (int pi = 0; pi < sc.getPositionCount(); pi++) {
897       for (int hi = 0; hi < sc.getHierarchyCount(); hi++) {
898         Span s = sc.getSpan(pi, hi);
899         out.println(" check(sc, "
900           + s.getPositionIndex() + ", "
901           + s.getHierarchyIndex() + ", "
902           + s.isSignificant() + ", "
903           + s.getPositionSpan() + ", "
904           + s.getHierarchySpan() + ");");
905       }
906     }
907   }
908   
909   void printSpans(SpanCalc sc, PrintWriter JavaDoc out) {
910     
911     char[][] carr = new char[sc.getPositionCount()][sc.getHierarchyCount()];
912     char letter = 'a';
913     
914     for (int pi = 0; pi < sc.getPositionCount(); pi++) {
915       for (int hi = 0; hi < sc.getHierarchyCount(); hi++) {
916         Span s = sc.getSpan(pi, hi);
917         assertEquals(pi, s.getPositionIndex());
918         assertEquals(hi, s.getHierarchyIndex());
919         if (s.isSignificant()) {
920           for (int pspan = 0; pspan < s.getPositionSpan(); pspan++) {
921             for (int hspan = 0; hspan < s.getHierarchySpan(); hspan++) {
922               carr[pi + pspan][hi + hspan] = letter;
923             }
924           }
925           letter ++;
926         }
927       }
928     }
929     
930     for (int i = 0; i < carr.length; i++) {
931       for (int j = 0; j < carr[i].length; j++) {
932         out.print(" ");
933         out.print(carr[i][j]);
934       }
935       out.println();
936     }
937     
938   }
939   
940 }
941
Popular Tags