1 2 17 18 19 package org.apache.poi.poifs.filesystem; 20 21 import junit.framework.*; 22 23 28 29 public class TestPOIFSDocumentPath 30 extends TestCase 31 { 32 33 38 39 public TestPOIFSDocumentPath(String name) 40 { 41 super(name); 42 } 43 44 47 48 public void testDefaultConstructor() 49 { 50 POIFSDocumentPath path = new POIFSDocumentPath(); 51 52 assertEquals(0, path.length()); 53 } 54 55 58 59 public void testFullPathConstructor() 60 { 61 String [] components = 62 { 63 "foo", "bar", "foobar", "fubar" 64 }; 65 66 for (int j = 0; j < components.length; j++) 67 { 68 String [] params = new String [ j ]; 69 70 for (int k = 0; k < j; k++) 71 { 72 params[ k ] = components[ k ]; 73 } 74 POIFSDocumentPath path = new POIFSDocumentPath(params); 75 76 assertEquals(j, path.length()); 77 for (int k = 0; k < j; k++) 78 { 79 assertEquals(components[ k ], path.getComponent(k)); 80 } 81 if (j == 0) 82 { 83 assertNull(path.getParent()); 84 } 85 else 86 { 87 POIFSDocumentPath parent = path.getParent(); 88 89 assertNotNull(parent); 90 assertEquals(j - 1, parent.length()); 91 for (int k = 0; k < j - 1; k++) 92 { 93 assertEquals(components[ k ], parent.getComponent(k)); 94 } 95 } 96 } 97 98 assertEquals(0, new POIFSDocumentPath(null).length()); 100 try 101 { 102 new POIFSDocumentPath(new String [] 103 { 104 "fu", "" 105 }); 106 fail("should have caught IllegalArgumentException"); 107 } 108 catch (IllegalArgumentException ignored) 109 { 110 } 111 try 112 { 113 new POIFSDocumentPath(new String [] 114 { 115 "fu", null 116 }); 117 fail("should have caught IllegalArgumentException"); 118 } 119 catch (IllegalArgumentException ignored) 120 { 121 } 122 } 123 124 127 128 public void testRelativePathConstructor() 129 { 130 String [] initialComponents = 131 { 132 "a", "b", "c" 133 }; 134 135 for (int n = 0; n < initialComponents.length; n++) 136 { 137 String [] initialParams = new String [ n ]; 138 139 for (int k = 0; k < n; k++) 140 { 141 initialParams[ k ] = initialComponents[ k ]; 142 } 143 POIFSDocumentPath base = 144 new POIFSDocumentPath(initialParams); 145 String [] components = 146 { 147 "foo", "bar", "foobar", "fubar" 148 }; 149 150 for (int j = 0; j < components.length; j++) 151 { 152 String [] params = new String [ j ]; 153 154 for (int k = 0; k < j; k++) 155 { 156 params[ k ] = components[ k ]; 157 } 158 POIFSDocumentPath path = new POIFSDocumentPath(base, params); 159 160 assertEquals(j + n, path.length()); 161 for (int k = 0; k < n; k++) 162 { 163 assertEquals(initialComponents[ k ], 164 path.getComponent(k)); 165 } 166 for (int k = 0; k < j; k++) 167 { 168 assertEquals(components[ k ], path.getComponent(k + n)); 169 } 170 if ((j + n) == 0) 171 { 172 assertNull(path.getParent()); 173 } 174 else 175 { 176 POIFSDocumentPath parent = path.getParent(); 177 178 assertNotNull(parent); 179 assertEquals(j + n - 1, parent.length()); 180 for (int k = 0; k < (j + n - 1); k++) 181 { 182 assertEquals(path.getComponent(k), 183 parent.getComponent(k)); 184 } 185 } 186 } 187 188 assertEquals(n, new POIFSDocumentPath(base, null).length()); 190 try 191 { 192 new POIFSDocumentPath(base, new String [] 193 { 194 "fu", "" 195 }); 196 fail("should have caught IllegalArgumentException"); 197 } 198 catch (IllegalArgumentException ignored) 199 { 200 } 201 try 202 { 203 new POIFSDocumentPath(base, new String [] 204 { 205 "fu", null 206 }); 207 fail("should have caught IllegalArgumentException"); 208 } 209 catch (IllegalArgumentException ignored) 210 { 211 } 212 } 213 } 214 215 218 219 public void testEquality() 220 { 221 POIFSDocumentPath a1 = new POIFSDocumentPath(); 222 POIFSDocumentPath a2 = new POIFSDocumentPath(null); 223 POIFSDocumentPath a3 = new POIFSDocumentPath(new String [ 0 ]); 224 POIFSDocumentPath a4 = new POIFSDocumentPath(a1, null); 225 POIFSDocumentPath a5 = new POIFSDocumentPath(a1, 226 new String [ 0 ]); 227 POIFSDocumentPath[] paths = 228 { 229 a1, a2, a3, a4, a5 230 }; 231 232 for (int j = 0; j < paths.length; j++) 233 { 234 for (int k = 0; k < paths.length; k++) 235 { 236 assertEquals(String.valueOf(j) + "<>" + String.valueOf(k), 237 paths[ j ], paths[ k ]); 238 } 239 } 240 a2 = new POIFSDocumentPath(a1, new String [] 241 { 242 "foo" 243 }); 244 a3 = new POIFSDocumentPath(a2, new String [] 245 { 246 "bar" 247 }); 248 a4 = new POIFSDocumentPath(a3, new String [] 249 { 250 "fubar" 251 }); 252 a5 = new POIFSDocumentPath(a4, new String [] 253 { 254 "foobar" 255 }); 256 POIFSDocumentPath[] builtUpPaths = 257 { 258 a1, a2, a3, a4, a5 259 }; 260 POIFSDocumentPath[] fullPaths = 261 { 262 new POIFSDocumentPath(), new POIFSDocumentPath(new String [] 263 { 264 "foo" 265 }), new POIFSDocumentPath(new String [] 266 { 267 "foo", "bar" 268 }), new POIFSDocumentPath(new String [] 269 { 270 "foo", "bar", "fubar" 271 }), new POIFSDocumentPath(new String [] 272 { 273 "foo", "bar", "fubar", "foobar" 274 }) 275 }; 276 277 for (int k = 0; k < builtUpPaths.length; k++) 278 { 279 for (int j = 0; j < fullPaths.length; j++) 280 { 281 if (k == j) 282 { 283 assertEquals(String.valueOf(j) + "<>" 284 + String.valueOf(k), fullPaths[ j ], 285 builtUpPaths[ k ]); 286 } 287 else 288 { 289 assertTrue(String.valueOf(j) + "<>" + String.valueOf(k), 290 !(fullPaths[ j ].equals(builtUpPaths[ k ]))); 291 } 292 } 293 } 294 POIFSDocumentPath[] badPaths = 295 { 296 new POIFSDocumentPath(new String [] 297 { 298 "_foo" 299 }), new POIFSDocumentPath(new String [] 300 { 301 "foo", "_bar" 302 }), new POIFSDocumentPath(new String [] 303 { 304 "foo", "bar", "_fubar" 305 }), new POIFSDocumentPath(new String [] 306 { 307 "foo", "bar", "fubar", "_foobar" 308 }) 309 }; 310 311 for (int k = 0; k < builtUpPaths.length; k++) 312 { 313 for (int j = 0; j < badPaths.length; j++) 314 { 315 assertTrue(String.valueOf(j) + "<>" + String.valueOf(k), 316 !(fullPaths[ k ].equals(badPaths[ j ]))); 317 } 318 } 319 } 320 321 326 327 public static void main(String [] ignored_args) 328 { 329 System.out.println( 330 "Testing org.apache.poi.poifs.eventfilesystem.POIFSDocumentPath"); 331 junit.textui.TestRunner.run(TestPOIFSDocumentPath.class); 332 } 333 } 334 | Popular Tags |