1 package com.tonbeller.jpivot.table.span; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 import java.util.StringTokenizer ; 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 arg0) { 24 super(arg0); 25 } 26 27 class MyMember extends MemberImpl implements Displayable { 28 public MyMember(String label, Level level, List props) { 29 super(props); 30 setLevel(level); 31 setLabel(label); 32 } 33 } 34 35 private Member member(String label, Level level, String propstr) { 36 List props = new ArrayList (); 37 StringTokenizer st = new StringTokenizer (propstr, ",", false); 38 while (st.hasMoreTokens()) { 39 String tok = st.nextToken(); 40 int i = tok.indexOf('='); 41 String name = tok.substring(0, i); 42 String 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 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 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 assertSC(sc, new String [] { "A A 1 2 *", "A I 3 * 4" }); 86 } 87 88 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 assertSC(sc, new String [] { "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 [] rows) { 116 assertEquals(rows.length, sc.getPositionCount()); 117 for (int pi = 0; pi < rows.length; pi++) { 118 StringTokenizer st = new StringTokenizer (rows[pi]); 119 int HC = st.countTokens(); 120 assertEquals(HC, sc.getHierarchyCount()); 121 for (int hi = 0; hi < HC; hi++) { 122 String 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 assertSC(sc, new String [] { "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 assertSC(sc, new String [] { "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 vpm = new ArrayList (); 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 assertSC(sc, new String [] { "A D 4 1", "B E 5 *", "C F 6 *" }); 195 } 196 197 public void testVisibleNames2() { 198 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 vpm = new ArrayList (); 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 [] { "A D 4 1", "B E 5 *", "C F 6 *" }); 218 } 219 220 public void testVisibleNames3() { 221 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 vpm = new ArrayList (); 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 [] { "A 1 D 4", "B * E 5", "C * F 6" }); 242 } 243 244 public void testVisibleNames4() { 245 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 vpm = new ArrayList (); 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 [] { "A * 1 D 7", "B 2 * E *", "C * * F *" }); 267 } 268 269 protected void setUp() throws Exception { 270 extension = new TestMemberProperties(); 271 } 272 273 } 274 | Popular Tags |