You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestPOIFSDocumentPath.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.filesystem;
  16. import junit.framework.TestCase;
  17. /**
  18. * Class to test POIFSDocumentPath functionality
  19. *
  20. * @author Marc Johnson
  21. */
  22. public final class TestPOIFSDocumentPath extends TestCase {
  23. /**
  24. * Test default constructor
  25. */
  26. public void testDefaultConstructor() {
  27. POIFSDocumentPath path = new POIFSDocumentPath();
  28. assertEquals(0, path.length());
  29. }
  30. /**
  31. * Test full path constructor
  32. */
  33. public void testFullPathConstructor() {
  34. String[] components =
  35. {
  36. "foo", "bar", "foobar", "fubar"
  37. };
  38. for (int j = 0; j < components.length; j++)
  39. {
  40. String[] params = new String[ j ];
  41. for (int k = 0; k < j; k++)
  42. {
  43. params[ k ] = components[ k ];
  44. }
  45. POIFSDocumentPath path = new POIFSDocumentPath(params);
  46. assertEquals(j, path.length());
  47. for (int k = 0; k < j; k++)
  48. {
  49. assertEquals(components[ k ], path.getComponent(k));
  50. }
  51. if (j == 0)
  52. {
  53. assertNull(path.getParent());
  54. }
  55. else
  56. {
  57. POIFSDocumentPath parent = path.getParent();
  58. assertNotNull(parent);
  59. assertEquals(j - 1, parent.length());
  60. for (int k = 0; k < j - 1; k++)
  61. {
  62. assertEquals(components[ k ], parent.getComponent(k));
  63. }
  64. }
  65. }
  66. // test weird variants
  67. assertEquals(0, new POIFSDocumentPath(null).length());
  68. try
  69. {
  70. new POIFSDocumentPath(new String[]
  71. {
  72. "fu", ""
  73. });
  74. fail("should have caught IllegalArgumentException");
  75. }
  76. catch (IllegalArgumentException ignored)
  77. {
  78. }
  79. try
  80. {
  81. new POIFSDocumentPath(new String[]
  82. {
  83. "fu", null
  84. });
  85. fail("should have caught IllegalArgumentException");
  86. }
  87. catch (IllegalArgumentException ignored)
  88. {
  89. }
  90. }
  91. /**
  92. * Test relative path constructor
  93. */
  94. public void testRelativePathConstructor() {
  95. String[] initialComponents =
  96. {
  97. "a", "b", "c"
  98. };
  99. for (int n = 0; n < initialComponents.length; n++)
  100. {
  101. String[] initialParams = new String[ n ];
  102. for (int k = 0; k < n; k++)
  103. {
  104. initialParams[ k ] = initialComponents[ k ];
  105. }
  106. POIFSDocumentPath base =
  107. new POIFSDocumentPath(initialParams);
  108. String[] components =
  109. {
  110. "foo", "bar", "foobar", "fubar"
  111. };
  112. for (int j = 0; j < components.length; j++)
  113. {
  114. String[] params = new String[ j ];
  115. for (int k = 0; k < j; k++)
  116. {
  117. params[ k ] = components[ k ];
  118. }
  119. POIFSDocumentPath path = new POIFSDocumentPath(base, params);
  120. assertEquals(j + n, path.length());
  121. for (int k = 0; k < n; k++)
  122. {
  123. assertEquals(initialComponents[ k ],
  124. path.getComponent(k));
  125. }
  126. for (int k = 0; k < j; k++)
  127. {
  128. assertEquals(components[ k ], path.getComponent(k + n));
  129. }
  130. if ((j + n) == 0)
  131. {
  132. assertNull(path.getParent());
  133. }
  134. else
  135. {
  136. POIFSDocumentPath parent = path.getParent();
  137. assertNotNull(parent);
  138. assertEquals(j + n - 1, parent.length());
  139. for (int k = 0; k < (j + n - 1); k++)
  140. {
  141. assertEquals(path.getComponent(k),
  142. parent.getComponent(k));
  143. }
  144. }
  145. }
  146. // Test weird variants
  147. // This one is allowed, even if it's really odd
  148. assertEquals(n, new POIFSDocumentPath(base, null).length());
  149. new POIFSDocumentPath(base, new String[]
  150. {
  151. "fu", ""
  152. });
  153. // This one is allowed too
  154. new POIFSDocumentPath(base, new String[]
  155. {
  156. "", "fu"
  157. });
  158. // This one shouldn't be allowed
  159. try
  160. {
  161. new POIFSDocumentPath(base, new String[]
  162. {
  163. "fu", null
  164. });
  165. fail("should have caught IllegalArgumentException");
  166. }
  167. catch (IllegalArgumentException ignored)
  168. {
  169. }
  170. // Ditto
  171. try
  172. {
  173. new POIFSDocumentPath(base, new String[]
  174. {
  175. null, "fu"
  176. });
  177. fail("should have caught IllegalArgumentException");
  178. }
  179. catch (IllegalArgumentException ignored)
  180. {
  181. }
  182. }
  183. }
  184. /**
  185. * test equality
  186. */
  187. public void testEquality() {
  188. POIFSDocumentPath a1 = new POIFSDocumentPath();
  189. POIFSDocumentPath a2 = new POIFSDocumentPath(null);
  190. POIFSDocumentPath a3 = new POIFSDocumentPath(new String[ 0 ]);
  191. POIFSDocumentPath a4 = new POIFSDocumentPath(a1, null);
  192. POIFSDocumentPath a5 = new POIFSDocumentPath(a1,
  193. new String[ 0 ]);
  194. POIFSDocumentPath[] paths =
  195. {
  196. a1, a2, a3, a4, a5
  197. };
  198. for (int j = 0; j < paths.length; j++)
  199. {
  200. for (int k = 0; k < paths.length; k++)
  201. {
  202. assertEquals(String.valueOf(j) + "<>" + String.valueOf(k),
  203. paths[ j ], paths[ k ]);
  204. }
  205. }
  206. a2 = new POIFSDocumentPath(a1, new String[]
  207. {
  208. "foo"
  209. });
  210. a3 = new POIFSDocumentPath(a2, new String[]
  211. {
  212. "bar"
  213. });
  214. a4 = new POIFSDocumentPath(a3, new String[]
  215. {
  216. "fubar"
  217. });
  218. a5 = new POIFSDocumentPath(a4, new String[]
  219. {
  220. "foobar"
  221. });
  222. POIFSDocumentPath[] builtUpPaths =
  223. {
  224. a1, a2, a3, a4, a5
  225. };
  226. POIFSDocumentPath[] fullPaths =
  227. {
  228. new POIFSDocumentPath(), new POIFSDocumentPath(new String[]
  229. {
  230. "foo"
  231. }), new POIFSDocumentPath(new String[]
  232. {
  233. "foo", "bar"
  234. }), new POIFSDocumentPath(new String[]
  235. {
  236. "foo", "bar", "fubar"
  237. }), new POIFSDocumentPath(new String[]
  238. {
  239. "foo", "bar", "fubar", "foobar"
  240. })
  241. };
  242. for (int k = 0; k < builtUpPaths.length; k++)
  243. {
  244. for (int j = 0; j < fullPaths.length; j++)
  245. {
  246. if (k == j)
  247. {
  248. assertEquals(String.valueOf(j) + "<>"
  249. + String.valueOf(k), fullPaths[ j ],
  250. builtUpPaths[ k ]);
  251. }
  252. else
  253. {
  254. assertTrue(String.valueOf(j) + "<>" + String.valueOf(k),
  255. !(fullPaths[ j ].equals(builtUpPaths[ k ])));
  256. }
  257. }
  258. }
  259. POIFSDocumentPath[] badPaths =
  260. {
  261. new POIFSDocumentPath(new String[]
  262. {
  263. "_foo"
  264. }), new POIFSDocumentPath(new String[]
  265. {
  266. "foo", "_bar"
  267. }), new POIFSDocumentPath(new String[]
  268. {
  269. "foo", "bar", "_fubar"
  270. }), new POIFSDocumentPath(new String[]
  271. {
  272. "foo", "bar", "fubar", "_foobar"
  273. })
  274. };
  275. for (int k = 0; k < builtUpPaths.length; k++)
  276. {
  277. for (int j = 0; j < badPaths.length; j++)
  278. {
  279. assertTrue(String.valueOf(j) + "<>" + String.valueOf(k),
  280. !(fullPaths[ k ].equals(badPaths[ j ])));
  281. }
  282. }
  283. }
  284. }