1 2 17 18 19 package org.apache.poi.poifs.storage; 20 21 import java.io.*; 22 23 import java.util.*; 24 25 import junit.framework.*; 26 27 import org.apache.poi.poifs.property.Property; 28 29 34 35 public class TestPropertyBlock 36 extends TestCase 37 { 38 39 44 45 public TestPropertyBlock(String name) 46 { 47 super(name); 48 } 49 50 55 56 public void testCreatePropertyBlocks() 57 throws IOException 58 { 59 60 List properties = new ArrayList(); 62 BlockWritable[] blocks = 63 PropertyBlock.createPropertyBlockArray(properties); 64 65 assertEquals(0, blocks.length); 66 67 properties.add(new LocalProperty("Root Entry")); 69 blocks = PropertyBlock.createPropertyBlockArray(properties); 70 assertEquals(1, blocks.length); 71 byte[] testblock = new byte[ 512 ]; 72 73 for (int j = 0; j < 4; j++) 74 { 75 setDefaultBlock(testblock, j); 76 } 77 testblock[ 0x0000 ] = ( byte ) 'R'; 78 testblock[ 0x0002 ] = ( byte ) 'o'; 79 testblock[ 0x0004 ] = ( byte ) 'o'; 80 testblock[ 0x0006 ] = ( byte ) 't'; 81 testblock[ 0x0008 ] = ( byte ) ' '; 82 testblock[ 0x000A ] = ( byte ) 'E'; 83 testblock[ 0x000C ] = ( byte ) 'n'; 84 testblock[ 0x000E ] = ( byte ) 't'; 85 testblock[ 0x0010 ] = ( byte ) 'r'; 86 testblock[ 0x0012 ] = ( byte ) 'y'; 87 testblock[ 0x0040 ] = ( byte ) 22; 88 verifyCorrect(blocks, testblock); 89 90 properties.add(new LocalProperty("workbook")); 92 properties.add(new LocalProperty("summary")); 93 blocks = PropertyBlock.createPropertyBlockArray(properties); 94 assertEquals(1, blocks.length); 95 testblock[ 0x0080 ] = ( byte ) 'w'; 96 testblock[ 0x0082 ] = ( byte ) 'o'; 97 testblock[ 0x0084 ] = ( byte ) 'r'; 98 testblock[ 0x0086 ] = ( byte ) 'k'; 99 testblock[ 0x0088 ] = ( byte ) 'b'; 100 testblock[ 0x008A ] = ( byte ) 'o'; 101 testblock[ 0x008C ] = ( byte ) 'o'; 102 testblock[ 0x008E ] = ( byte ) 'k'; 103 testblock[ 0x00C0 ] = ( byte ) 18; 104 testblock[ 0x0100 ] = ( byte ) 's'; 105 testblock[ 0x0102 ] = ( byte ) 'u'; 106 testblock[ 0x0104 ] = ( byte ) 'm'; 107 testblock[ 0x0106 ] = ( byte ) 'm'; 108 testblock[ 0x0108 ] = ( byte ) 'a'; 109 testblock[ 0x010A ] = ( byte ) 'r'; 110 testblock[ 0x010C ] = ( byte ) 'y'; 111 testblock[ 0x0140 ] = ( byte ) 16; 112 verifyCorrect(blocks, testblock); 113 114 properties.add(new LocalProperty("wintery")); 116 blocks = PropertyBlock.createPropertyBlockArray(properties); 117 assertEquals(1, blocks.length); 118 testblock[ 0x0180 ] = ( byte ) 'w'; 119 testblock[ 0x0182 ] = ( byte ) 'i'; 120 testblock[ 0x0184 ] = ( byte ) 'n'; 121 testblock[ 0x0186 ] = ( byte ) 't'; 122 testblock[ 0x0188 ] = ( byte ) 'e'; 123 testblock[ 0x018A ] = ( byte ) 'r'; 124 testblock[ 0x018C ] = ( byte ) 'y'; 125 testblock[ 0x01C0 ] = ( byte ) 16; 126 verifyCorrect(blocks, testblock); 127 128 properties.add(new LocalProperty("foo")); 130 blocks = PropertyBlock.createPropertyBlockArray(properties); 131 assertEquals(2, blocks.length); 132 testblock = new byte[ 1024 ]; 133 for (int j = 0; j < 8; j++) 134 { 135 setDefaultBlock(testblock, j); 136 } 137 testblock[ 0x0000 ] = ( byte ) 'R'; 138 testblock[ 0x0002 ] = ( byte ) 'o'; 139 testblock[ 0x0004 ] = ( byte ) 'o'; 140 testblock[ 0x0006 ] = ( byte ) 't'; 141 testblock[ 0x0008 ] = ( byte ) ' '; 142 testblock[ 0x000A ] = ( byte ) 'E'; 143 testblock[ 0x000C ] = ( byte ) 'n'; 144 testblock[ 0x000E ] = ( byte ) 't'; 145 testblock[ 0x0010 ] = ( byte ) 'r'; 146 testblock[ 0x0012 ] = ( byte ) 'y'; 147 testblock[ 0x0040 ] = ( byte ) 22; 148 testblock[ 0x0080 ] = ( byte ) 'w'; 149 testblock[ 0x0082 ] = ( byte ) 'o'; 150 testblock[ 0x0084 ] = ( byte ) 'r'; 151 testblock[ 0x0086 ] = ( byte ) 'k'; 152 testblock[ 0x0088 ] = ( byte ) 'b'; 153 testblock[ 0x008A ] = ( byte ) 'o'; 154 testblock[ 0x008C ] = ( byte ) 'o'; 155 testblock[ 0x008E ] = ( byte ) 'k'; 156 testblock[ 0x00C0 ] = ( byte ) 18; 157 testblock[ 0x0100 ] = ( byte ) 's'; 158 testblock[ 0x0102 ] = ( byte ) 'u'; 159 testblock[ 0x0104 ] = ( byte ) 'm'; 160 testblock[ 0x0106 ] = ( byte ) 'm'; 161 testblock[ 0x0108 ] = ( byte ) 'a'; 162 testblock[ 0x010A ] = ( byte ) 'r'; 163 testblock[ 0x010C ] = ( byte ) 'y'; 164 testblock[ 0x0140 ] = ( byte ) 16; 165 testblock[ 0x0180 ] = ( byte ) 'w'; 166 testblock[ 0x0182 ] = ( byte ) 'i'; 167 testblock[ 0x0184 ] = ( byte ) 'n'; 168 testblock[ 0x0186 ] = ( byte ) 't'; 169 testblock[ 0x0188 ] = ( byte ) 'e'; 170 testblock[ 0x018A ] = ( byte ) 'r'; 171 testblock[ 0x018C ] = ( byte ) 'y'; 172 testblock[ 0x01C0 ] = ( byte ) 16; 173 testblock[ 0x0200 ] = ( byte ) 'f'; 174 testblock[ 0x0202 ] = ( byte ) 'o'; 175 testblock[ 0x0204 ] = ( byte ) 'o'; 176 testblock[ 0x0240 ] = ( byte ) 8; 177 verifyCorrect(blocks, testblock); 178 } 179 180 private void setDefaultBlock(byte [] testblock, int j) 181 { 182 int base = j * 128; 183 int index = 0; 184 185 for (; index < 0x40; index++) 186 { 187 testblock[ base++ ] = ( byte ) 0; 188 } 189 testblock[ base++ ] = ( byte ) 2; 190 testblock[ base++ ] = ( byte ) 0; 191 index += 2; 192 for (; index < 0x44; index++) 193 { 194 testblock[ base++ ] = ( byte ) 0; 195 } 196 for (; index < 0x50; index++) 197 { 198 testblock[ base++ ] = ( byte ) 0xff; 199 } 200 for (; index < 0x80; index++) 201 { 202 testblock[ base++ ] = ( byte ) 0; 203 } 204 } 205 206 private void verifyCorrect(BlockWritable [] blocks, byte [] testblock) 207 throws IOException 208 { 209 ByteArrayOutputStream stream = new ByteArrayOutputStream(512 210 * blocks.length); 211 212 for (int j = 0; j < blocks.length; j++) 213 { 214 blocks[ j ].writeBlocks(stream); 215 } 216 byte[] output = stream.toByteArray(); 217 218 assertEquals(testblock.length, output.length); 219 for (int j = 0; j < testblock.length; j++) 220 { 221 assertEquals("mismatch at offset " + j, testblock[ j ], 222 output[ j ]); 223 } 224 } 225 226 231 232 public static void main(String [] ignored_args) 233 { 234 System.out 235 .println("Testing org.apache.poi.poifs.storage.PropertyBlock"); 236 junit.textui.TestRunner.run(TestPropertyBlock.class); 237 } 238 } 239 | Popular Tags |