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.eventfilesystem;
019:
020: import junit.framework.*;
021:
022: import java.util.*;
023:
024: import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
025:
026: /**
027: * Class to test POIFSReaderRegistry functionality
028: *
029: * @author Marc Johnson
030: */
031:
032: public class TestPOIFSReaderRegistry extends TestCase {
033: private POIFSReaderListener[] listeners = { new Listener(),
034: new Listener(), new Listener(), new Listener() };
035: private POIFSDocumentPath[] paths = { new POIFSDocumentPath(),
036: new POIFSDocumentPath(new String[] { "a" }),
037: new POIFSDocumentPath(new String[] { "b" }),
038: new POIFSDocumentPath(new String[] { "c" }) };
039: private String[] names = { "a0", "a1", "a2", "a3" };
040:
041: /**
042: * Constructor TestPOIFSReaderRegistry
043: *
044: * @param name
045: */
046:
047: public TestPOIFSReaderRegistry(String name) {
048: super (name);
049: }
050:
051: /**
052: * Test empty registry
053: */
054:
055: public void testEmptyRegistry() {
056: POIFSReaderRegistry registry = new POIFSReaderRegistry();
057:
058: for (int j = 0; j < paths.length; j++) {
059: for (int k = 0; k < names.length; k++) {
060: Iterator listeners = registry.getListeners(paths[j],
061: names[k]);
062:
063: assertTrue(!listeners.hasNext());
064: }
065: }
066: }
067:
068: /**
069: * Test mixed registration operations
070: */
071:
072: public void testMixedRegistrationOperations() {
073: POIFSReaderRegistry registry = new POIFSReaderRegistry();
074:
075: for (int j = 0; j < listeners.length; j++) {
076: for (int k = 0; k < paths.length; k++) {
077: for (int n = 0; n < names.length; n++) {
078: if ((j != k) && (k != n)) {
079: registry.registerListener(listeners[j],
080: paths[k], names[n]);
081: }
082: }
083: }
084: }
085: for (int k = 0; k < paths.length; k++) {
086: for (int n = 0; n < names.length; n++) {
087: Iterator listeners = registry.getListeners(paths[k],
088: names[n]);
089:
090: if (k == n) {
091: assertTrue(!listeners.hasNext());
092: } else {
093: Set registeredListeners = new HashSet();
094:
095: while (listeners.hasNext()) {
096: registeredListeners.add(listeners.next());
097: }
098: assertEquals(this .listeners.length - 1,
099: registeredListeners.size());
100: for (int j = 0; j < this .listeners.length; j++) {
101: if (j == k) {
102: assertTrue(!registeredListeners
103: .contains(this .listeners[j]));
104: } else {
105: assertTrue(registeredListeners
106: .contains(this .listeners[j]));
107: }
108: }
109: }
110: }
111: }
112: for (int j = 0; j < listeners.length; j++) {
113: registry.registerListener(listeners[j]);
114: }
115: for (int k = 0; k < paths.length; k++) {
116: for (int n = 0; n < names.length; n++) {
117: Iterator listeners = registry.getListeners(paths[k],
118: names[n]);
119: Set registeredListeners = new HashSet();
120:
121: while (listeners.hasNext()) {
122: registeredListeners.add(listeners.next());
123: }
124: assertEquals(this .listeners.length, registeredListeners
125: .size());
126: for (int j = 0; j < this .listeners.length; j++) {
127: assertTrue(registeredListeners
128: .contains(this .listeners[j]));
129: }
130: }
131: }
132: }
133:
134: /**
135: * main method to run the unit tests
136: *
137: * @param ignored_args
138: */
139:
140: public static void main(String[] ignored_args) {
141: System.out
142: .println("Testing org.apache.poi.poifs.eventfilesystem.POIFSReaderRegistry");
143: junit.textui.TestRunner.run(TestPOIFSReaderRegistry.class);
144: }
145: }
|