001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.poifs.filesystem;
019:
020: import junit.framework.*;
021:
022: /**
023: * Class to test POIFSDocumentPath functionality
024: *
025: * @author Marc Johnson
026: */
027:
028: public class TestPOIFSDocumentPath extends TestCase {
029:
030: /**
031: * Constructor TestPOIFSDocumentPath
032: *
033: * @param name
034: */
035:
036: public TestPOIFSDocumentPath(String name) {
037: super (name);
038: }
039:
040: /**
041: * Test default constructor
042: */
043:
044: public void testDefaultConstructor() {
045: POIFSDocumentPath path = new POIFSDocumentPath();
046:
047: assertEquals(0, path.length());
048: }
049:
050: /**
051: * Test full path constructor
052: */
053:
054: public void testFullPathConstructor() {
055: String[] components = { "foo", "bar", "foobar", "fubar" };
056:
057: for (int j = 0; j < components.length; j++) {
058: String[] params = new String[j];
059:
060: for (int k = 0; k < j; k++) {
061: params[k] = components[k];
062: }
063: POIFSDocumentPath path = new POIFSDocumentPath(params);
064:
065: assertEquals(j, path.length());
066: for (int k = 0; k < j; k++) {
067: assertEquals(components[k], path.getComponent(k));
068: }
069: if (j == 0) {
070: assertNull(path.getParent());
071: } else {
072: POIFSDocumentPath parent = path.getParent();
073:
074: assertNotNull(parent);
075: assertEquals(j - 1, parent.length());
076: for (int k = 0; k < j - 1; k++) {
077: assertEquals(components[k], parent.getComponent(k));
078: }
079: }
080: }
081:
082: // test weird variants
083: assertEquals(0, new POIFSDocumentPath(null).length());
084: try {
085: new POIFSDocumentPath(new String[] { "fu", "" });
086: fail("should have caught IllegalArgumentException");
087: } catch (IllegalArgumentException ignored) {
088: }
089: try {
090: new POIFSDocumentPath(new String[] { "fu", null });
091: fail("should have caught IllegalArgumentException");
092: } catch (IllegalArgumentException ignored) {
093: }
094: }
095:
096: /**
097: * Test relative path constructor
098: */
099:
100: public void testRelativePathConstructor() {
101: String[] initialComponents = { "a", "b", "c" };
102:
103: for (int n = 0; n < initialComponents.length; n++) {
104: String[] initialParams = new String[n];
105:
106: for (int k = 0; k < n; k++) {
107: initialParams[k] = initialComponents[k];
108: }
109: POIFSDocumentPath base = new POIFSDocumentPath(
110: initialParams);
111: String[] components = { "foo", "bar", "foobar", "fubar" };
112:
113: for (int j = 0; j < components.length; j++) {
114: String[] params = new String[j];
115:
116: for (int k = 0; k < j; k++) {
117: params[k] = components[k];
118: }
119: POIFSDocumentPath path = new POIFSDocumentPath(base,
120: params);
121:
122: assertEquals(j + n, path.length());
123: for (int k = 0; k < n; k++) {
124: assertEquals(initialComponents[k], path
125: .getComponent(k));
126: }
127: for (int k = 0; k < j; k++) {
128: assertEquals(components[k], path
129: .getComponent(k + n));
130: }
131: if ((j + n) == 0) {
132: assertNull(path.getParent());
133: } else {
134: POIFSDocumentPath parent = path.getParent();
135:
136: assertNotNull(parent);
137: assertEquals(j + n - 1, parent.length());
138: for (int k = 0; k < (j + n - 1); k++) {
139: assertEquals(path.getComponent(k), parent
140: .getComponent(k));
141: }
142: }
143: }
144:
145: // test weird variants
146: assertEquals(n, new POIFSDocumentPath(base, null).length());
147: try {
148: new POIFSDocumentPath(base, new String[] { "fu", "" });
149: fail("should have caught IllegalArgumentException");
150: } catch (IllegalArgumentException ignored) {
151: }
152: try {
153: new POIFSDocumentPath(base, new String[] { "fu", null });
154: fail("should have caught IllegalArgumentException");
155: } catch (IllegalArgumentException ignored) {
156: }
157: }
158: }
159:
160: /**
161: * test equality
162: */
163:
164: public void testEquality() {
165: POIFSDocumentPath a1 = new POIFSDocumentPath();
166: POIFSDocumentPath a2 = new POIFSDocumentPath(null);
167: POIFSDocumentPath a3 = new POIFSDocumentPath(new String[0]);
168: POIFSDocumentPath a4 = new POIFSDocumentPath(a1, null);
169: POIFSDocumentPath a5 = new POIFSDocumentPath(a1, new String[0]);
170: POIFSDocumentPath[] paths = { a1, a2, a3, a4, a5 };
171:
172: for (int j = 0; j < paths.length; j++) {
173: for (int k = 0; k < paths.length; k++) {
174: assertEquals(String.valueOf(j) + "<>"
175: + String.valueOf(k), paths[j], paths[k]);
176: }
177: }
178: a2 = new POIFSDocumentPath(a1, new String[] { "foo" });
179: a3 = new POIFSDocumentPath(a2, new String[] { "bar" });
180: a4 = new POIFSDocumentPath(a3, new String[] { "fubar" });
181: a5 = new POIFSDocumentPath(a4, new String[] { "foobar" });
182: POIFSDocumentPath[] builtUpPaths = { a1, a2, a3, a4, a5 };
183: POIFSDocumentPath[] fullPaths = {
184: new POIFSDocumentPath(),
185: new POIFSDocumentPath(new String[] { "foo" }),
186: new POIFSDocumentPath(new String[] { "foo", "bar" }),
187: new POIFSDocumentPath(new String[] { "foo", "bar",
188: "fubar" }),
189: new POIFSDocumentPath(new String[] { "foo", "bar",
190: "fubar", "foobar" }) };
191:
192: for (int k = 0; k < builtUpPaths.length; k++) {
193: for (int j = 0; j < fullPaths.length; j++) {
194: if (k == j) {
195: assertEquals(String.valueOf(j) + "<>"
196: + String.valueOf(k), fullPaths[j],
197: builtUpPaths[k]);
198: } else {
199: assertTrue(String.valueOf(j) + "<>"
200: + String.valueOf(k), !(fullPaths[j]
201: .equals(builtUpPaths[k])));
202: }
203: }
204: }
205: POIFSDocumentPath[] badPaths = {
206: new POIFSDocumentPath(new String[] { "_foo" }),
207: new POIFSDocumentPath(new String[] { "foo", "_bar" }),
208: new POIFSDocumentPath(new String[] { "foo", "bar",
209: "_fubar" }),
210: new POIFSDocumentPath(new String[] { "foo", "bar",
211: "fubar", "_foobar" }) };
212:
213: for (int k = 0; k < builtUpPaths.length; k++) {
214: for (int j = 0; j < badPaths.length; j++) {
215: assertTrue(
216: String.valueOf(j) + "<>" + String.valueOf(k),
217: !(fullPaths[k].equals(badPaths[j])));
218: }
219: }
220: }
221:
222: /**
223: * main method to run the unit tests
224: *
225: * @param ignored_args
226: */
227:
228: public static void main(String[] ignored_args) {
229: System.out
230: .println("Testing org.apache.poi.poifs.eventfilesystem.POIFSDocumentPath");
231: junit.textui.TestRunner.run(TestPOIFSDocumentPath.class);
232: }
233: }
|