KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > eventfilesystem > TestPOIFSReaderRegistry


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.eventfilesystem;
20
21 import junit.framework.*;
22
23 import java.util.*;
24
25 import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
26
27 /**
28  * Class to test POIFSReaderRegistry functionality
29  *
30  * @author Marc Johnson
31  */

32
33 public class TestPOIFSReaderRegistry
34     extends TestCase
35 {
36     private POIFSReaderListener[] listeners =
37     {
38         new Listener(), new Listener(), new Listener(), new Listener()
39     };
40     private POIFSDocumentPath[] paths =
41     {
42         new POIFSDocumentPath(), new POIFSDocumentPath(new String JavaDoc[]
43         {
44             "a"
45         }), new POIFSDocumentPath(new String JavaDoc[]
46         {
47             "b"
48         }), new POIFSDocumentPath(new String JavaDoc[]
49         {
50             "c"
51         })
52     };
53     private String JavaDoc[] names =
54     {
55         "a0", "a1", "a2", "a3"
56     };
57
58     /**
59      * Constructor TestPOIFSReaderRegistry
60      *
61      * @param name
62      */

63
64     public TestPOIFSReaderRegistry(String JavaDoc name)
65     {
66         super(name);
67     }
68
69     /**
70      * Test empty registry
71      */

72
73     public void testEmptyRegistry()
74     {
75         POIFSReaderRegistry registry = new POIFSReaderRegistry();
76
77         for (int j = 0; j < paths.length; j++)
78         {
79             for (int k = 0; k < names.length; k++)
80             {
81                 Iterator listeners = registry.getListeners(paths[ j ],
82                                                            names[ k ]);
83
84                 assertTrue(!listeners.hasNext());
85             }
86         }
87     }
88
89     /**
90      * Test mixed registration operations
91      */

92
93     public void testMixedRegistrationOperations()
94     {
95         POIFSReaderRegistry registry = new POIFSReaderRegistry();
96
97         for (int j = 0; j < listeners.length; j++)
98         {
99             for (int k = 0; k < paths.length; k++)
100             {
101                 for (int n = 0; n < names.length; n++)
102                 {
103                     if ((j != k) && (k != n))
104                     {
105                         registry.registerListener(listeners[ j ], paths[ k ],
106                                                   names[ n ]);
107                     }
108                 }
109             }
110         }
111         for (int k = 0; k < paths.length; k++)
112         {
113             for (int n = 0; n < names.length; n++)
114             {
115                 Iterator listeners = registry.getListeners(paths[ k ],
116                                                            names[ n ]);
117
118                 if (k == n)
119                 {
120                     assertTrue(!listeners.hasNext());
121                 }
122                 else
123                 {
124                     Set registeredListeners = new HashSet();
125
126                     while (listeners.hasNext())
127                     {
128                         registeredListeners.add(listeners.next());
129                     }
130                     assertEquals(this.listeners.length - 1,
131                                  registeredListeners.size());
132                     for (int j = 0; j < this.listeners.length; j++)
133                     {
134                         if (j == k)
135                         {
136                             assertTrue(!registeredListeners
137                                 .contains(this.listeners[ j ]));
138                         }
139                         else
140                         {
141                             assertTrue(registeredListeners
142                                 .contains(this.listeners[ j ]));
143                         }
144                     }
145                 }
146             }
147         }
148         for (int j = 0; j < listeners.length; j++)
149         {
150             registry.registerListener(listeners[ j ]);
151         }
152         for (int k = 0; k < paths.length; k++)
153         {
154             for (int n = 0; n < names.length; n++)
155             {
156                 Iterator listeners =
157                     registry.getListeners(paths[ k ], names[ n ]);
158                 Set registeredListeners = new HashSet();
159
160                 while (listeners.hasNext())
161                 {
162                     registeredListeners.add(listeners.next());
163                 }
164                 assertEquals(this.listeners.length,
165                              registeredListeners.size());
166                 for (int j = 0; j < this.listeners.length; j++)
167                 {
168                     assertTrue(registeredListeners
169                         .contains(this.listeners[ j ]));
170                 }
171             }
172         }
173     }
174
175     /**
176      * main method to run the unit tests
177      *
178      * @param ignored_args
179      */

180
181     public static void main(String JavaDoc [] ignored_args)
182     {
183         System.out.println(
184             "Testing org.apache.poi.poifs.eventfilesystem.POIFSReaderRegistry");
185         junit.textui.TestRunner.run(TestPOIFSReaderRegistry.class);
186     }
187 }
188
Popular Tags