KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > filesystem > TestPOIFSDocumentPath


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.poifs.filesystem;
20
21 import junit.framework.*;
22
23 /**
24  * Class to test POIFSDocumentPath functionality
25  *
26  * @author Marc Johnson
27  */

28
29 public class TestPOIFSDocumentPath
30     extends TestCase
31 {
32
33     /**
34      * Constructor TestPOIFSDocumentPath
35      *
36      * @param name
37      */

38
39     public TestPOIFSDocumentPath(String JavaDoc name)
40     {
41         super(name);
42     }
43
44     /**
45      * Test default constructor
46      */

47
48     public void testDefaultConstructor()
49     {
50         POIFSDocumentPath path = new POIFSDocumentPath();
51
52         assertEquals(0, path.length());
53     }
54
55     /**
56      * Test full path constructor
57      */

58
59     public void testFullPathConstructor()
60     {
61         String JavaDoc[] components =
62         {
63             "foo", "bar", "foobar", "fubar"
64         };
65
66         for (int j = 0; j < components.length; j++)
67         {
68             String JavaDoc[] params = new String JavaDoc[ 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         // test weird variants
99
assertEquals(0, new POIFSDocumentPath(null).length());
100         try
101         {
102             new POIFSDocumentPath(new String JavaDoc[]
103             {
104                 "fu", ""
105             });
106             fail("should have caught IllegalArgumentException");
107         }
108         catch (IllegalArgumentException JavaDoc ignored)
109         {
110         }
111         try
112         {
113             new POIFSDocumentPath(new String JavaDoc[]
114             {
115                 "fu", null
116             });
117             fail("should have caught IllegalArgumentException");
118         }
119         catch (IllegalArgumentException JavaDoc ignored)
120         {
121         }
122     }
123
124     /**
125      * Test relative path constructor
126      */

127
128     public void testRelativePathConstructor()
129     {
130         String JavaDoc[] initialComponents =
131         {
132             "a", "b", "c"
133         };
134
135         for (int n = 0; n < initialComponents.length; n++)
136         {
137             String JavaDoc[] initialParams = new String JavaDoc[ 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 JavaDoc[] components =
146             {
147                 "foo", "bar", "foobar", "fubar"
148             };
149
150             for (int j = 0; j < components.length; j++)
151             {
152                 String JavaDoc[] params = new String JavaDoc[ 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             // test weird variants
189
assertEquals(n, new POIFSDocumentPath(base, null).length());
190             try
191             {
192                 new POIFSDocumentPath(base, new String JavaDoc[]
193                 {
194                     "fu", ""
195                 });
196                 fail("should have caught IllegalArgumentException");
197             }
198             catch (IllegalArgumentException JavaDoc ignored)
199             {
200             }
201             try
202             {
203                 new POIFSDocumentPath(base, new String JavaDoc[]
204                 {
205                     "fu", null
206                 });
207                 fail("should have caught IllegalArgumentException");
208             }
209             catch (IllegalArgumentException JavaDoc ignored)
210             {
211             }
212         }
213     }
214
215     /**
216      * test equality
217      */

218
219     public void testEquality()
220     {
221         POIFSDocumentPath a1 = new POIFSDocumentPath();
222         POIFSDocumentPath a2 = new POIFSDocumentPath(null);
223         POIFSDocumentPath a3 = new POIFSDocumentPath(new String JavaDoc[ 0 ]);
224         POIFSDocumentPath a4 = new POIFSDocumentPath(a1, null);
225         POIFSDocumentPath a5 = new POIFSDocumentPath(a1,
226                                         new String JavaDoc[ 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 JavaDoc[]
241         {
242             "foo"
243         });
244         a3 = new POIFSDocumentPath(a2, new String JavaDoc[]
245         {
246             "bar"
247         });
248         a4 = new POIFSDocumentPath(a3, new String JavaDoc[]
249         {
250             "fubar"
251         });
252         a5 = new POIFSDocumentPath(a4, new String JavaDoc[]
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 JavaDoc[]
263             {
264                 "foo"
265             }), new POIFSDocumentPath(new String JavaDoc[]
266             {
267                 "foo", "bar"
268             }), new POIFSDocumentPath(new String JavaDoc[]
269             {
270                 "foo", "bar", "fubar"
271             }), new POIFSDocumentPath(new String JavaDoc[]
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 JavaDoc[]
297             {
298                 "_foo"
299             }), new POIFSDocumentPath(new String JavaDoc[]
300             {
301                 "foo", "_bar"
302             }), new POIFSDocumentPath(new String JavaDoc[]
303             {
304                 "foo", "bar", "_fubar"
305             }), new POIFSDocumentPath(new String JavaDoc[]
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     /**
322      * main method to run the unit tests
323      *
324      * @param ignored_args
325      */

326
327     public static void main(String JavaDoc [] 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