KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > resources > BaseDirContextTestCase


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

16
17
18 package org.apache.naming.resources;
19
20 import java.util.Date JavaDoc;
21
22 import javax.naming.Binding JavaDoc;
23 import javax.naming.NameClassPair JavaDoc;
24 import javax.naming.NamingEnumeration JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26
27 import javax.naming.directory.Attribute JavaDoc;
28 import javax.naming.directory.Attributes JavaDoc;
29 import javax.naming.directory.DirContext JavaDoc;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35
36 /**
37  * <p>Basic unit tests for the <code>javax.naming.directory.DirContext</code>
38  * implementations. This class must be subclassed for each individual
39  * <code>DirContext</code> implementation.</p>
40  *
41  * <p><strong>WARNING</strong>: These tests make certain assumptions that
42  * can generate "false negative" results if they are violated:</p>
43  * <ul>
44  * <li>The pathname of a directory (or WAR file) containing the static
45  * resources of the <code>/examples</code> web application shipped
46  * with Tomcat is passed to our test class as a system property
47  * named <code>doc.base</code>.</li>
48  * <li>The entry names that can be found in the top-level DirContext of
49  * the static resources are enumerated in the <code>topLevelNames</code>
50  * variable.</li>
51  * <li>The entry names that can be found in the WEB-INF DirContext of
52  * the static resources are enumerated in the <code>webInfNames</code>
53  * variable.</li>
54  * <li>The entry names in either the top-level or WEB-INF DirContext contexts
55  * that should themselves be <code>DirContext</code> implementations (i.e.
56  * "subdirectories" in the static resources for this web application)
57  * are enumerated in the <code>dirContextNames</code> variable.</li>
58  * </ul>
59  *
60  * @author Craig R. McClanahan
61  * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:55 $
62  */

63
64 public abstract class BaseDirContextTestCase extends TestCase {
65
66
67     // ----------------------------------------------------- Instance Variables
68

69
70     /**
71      * A directory context implementation to be tested.
72      */

73     protected DirContext JavaDoc context = null;
74
75
76     /**
77      * The pathname of the document base directory for this directory context.
78      */

79     protected String JavaDoc docBase = System.getProperty("doc.base");
80
81
82     /**
83      * Entry names that must be DirContexts. Names not on this list are
84      * assumed to be Resources.
85      */

86     protected static final String JavaDoc dirContextNames[] =
87     { "classes", "images", "jsp", "lib", "META-INF", "WEB-INF" };
88
89
90     /**
91      * The set of names that should be present in the top-level
92      * directory context.
93      */

94     protected static final String JavaDoc topLevelNames[] =
95     { "index.jsp", "jakarta-banner.gif", "tomcat.gif", "tomcat-power.gif", "META-INF", "WEB-INF" };
96
97     /**
98      * The set of names that should be present in the WEB-INF
99      * directory context.
100      */

101     protected static final String JavaDoc webInfNames[] =
102     { "classes", "jsp", "lib", "web.xml" };
103
104
105     /**
106      * The set of names that should be attributes of WEB-INF.
107      */

108     protected static final String JavaDoc webInfAttrs[] =
109     { "creationdate", "displayname", "getcontentlength", "getlastmodified",
110       "resourcetype" };
111
112
113     /**
114      * The set of names that should be attributes of WEB-INF/web.xml.
115      */

116     protected static final String JavaDoc webXmlAttrs[] =
117     { "creationdate", "displayname", "getcontentlength", "getlastmodified",
118       "resourcetype" };
119
120
121     // ----------------------------------------------------------- Constructors
122

123
124     /**
125      * Construct a new instance of this test case.
126      *
127      * @param name Name of the test case
128      */

129     public BaseDirContextTestCase(String JavaDoc name) {
130
131         super(name);
132
133     }
134
135
136     // --------------------------------------------------- Overall Test Methods
137

138
139     /**
140      * Set up instance variables required by this test case. This method
141      * <strong>MUST</strong> be implemented by a subclass.
142      */

143     public abstract void setUp();
144
145
146     /**
147      * Return the tests included in this test suite. This method
148      * <strong>MUST</strong> be implemented by a subclass.
149      */

150     // public abstract static Test suite();
151

152
153     /**
154      * Tear down instance variables required by this test case. This method
155      * <strong>MUST</strong> be implemented by a subclass.
156      */

157     public abstract void tearDown();
158
159
160     // ------------------------------------------------ Individual Test Methods
161

162
163     /**
164      * Test the attributes returned for the <code>WEB-INF</code> entry.
165      */

166     public abstract void testGetAttributesWebInf();
167
168
169     /**
170      * Test the attributes returned for the <code>WEB-INF/web.xml</code>
171      * entry.
172      */

173     public abstract void testGetAttributesWebXml();
174
175
176     /**
177      * We should be able to list the contents of the top-level context itself,
178      * and locate some entries we know are there.
179      */

180     public void testListTopLevel() {
181
182         try {
183             checkList(context.list(""), topLevelNames);
184         } catch (NamingException JavaDoc e) {
185             fail("NamingException: " + e);
186         }
187
188     }
189
190
191     /**
192      * We should be able to list the contents of the WEB-INF entry,
193      * and locate some entries we know are there.
194      */

195     public void testListWebInfDirect() {
196
197         try {
198
199             // Look up the WEB-INF entry
200
Object JavaDoc webInfEntry = context.lookup("WEB-INF");
201             assertNotNull("Found WEB-INF entry", webInfEntry);
202             assertTrue("WEB-INF entry is a DirContext",
203                    webInfEntry instanceof DirContext JavaDoc);
204             DirContext JavaDoc webInfContext = (DirContext JavaDoc) webInfEntry;
205
206             // Check the contents of the WEB-INF context directly
207
checkList(webInfContext.list(""), webInfNames);
208
209         } catch (NamingException JavaDoc e) {
210             fail("NamingException: " + e);
211         }
212
213
214     }
215
216
217     /**
218      * We should be able to list the contents of the WEB-INF entry,
219      * and locate some entries we know are there.
220      */

221     public void testListWebInfIndirect() {
222
223         try {
224             checkList(context.list("WEB-INF"), webInfNames);
225         } catch (NamingException JavaDoc e) {
226             fail("NamingException: " + e);
227         }
228
229     }
230
231
232     /**
233      * We should be able to list the bindings of the top-level context itself,
234      * and locate some entries we know are there.
235      */

236     public void testListBindingsTopLevel() {
237
238         try {
239             checkListBindings(context.listBindings(""), topLevelNames);
240         } catch (NamingException JavaDoc e) {
241             fail("NamingException: " + e);
242         }
243
244     }
245
246
247     /**
248      * We should be able to list the bindings of the WEB-INF entry,
249      * and locate some entries we know are there.
250      */

251     public void testListBindingsWebInfDirect() {
252
253         try {
254
255             // Look up the WEB-INF entry
256
Object JavaDoc webInfEntry = context.lookup("WEB-INF");
257             assertNotNull("Found WEB-INF entry", webInfEntry);
258             assertTrue("WEB-INF entry is a DirContext",
259                    webInfEntry instanceof DirContext JavaDoc);
260             DirContext JavaDoc webInfContext = (DirContext JavaDoc) webInfEntry;
261
262             // Check the bindings of the WEB-INF context directly
263
checkListBindings(webInfContext.listBindings(""), webInfNames);
264
265         } catch (NamingException JavaDoc e) {
266             fail("NamingException: " + e);
267         }
268
269
270     }
271
272
273     /**
274      * We should be able to list the bindings of the WEB-INF entry,
275      * and locate some entries we know are there.
276      */

277     public void testListBindingsWebInfIndirect() {
278
279         try {
280             checkListBindings(context.listBindings("WEB-INF"), webInfNames);
281         } catch (NamingException JavaDoc e) {
282             fail("NamingException: " + e);
283         }
284
285     }
286
287
288     // -------------------------------------------------------- Support Methods
289

290
291     /**
292      * Check the results of a list() call against the specified entry list.
293      *
294      * @param enum The naming enumeration we are checking
295      * @param list The list of entry names we are expecting
296      *
297      * @exception NamingException if a naming exception occurs
298      */

299     protected void checkList(NamingEnumeration JavaDoc ne, String JavaDoc list[])
300         throws NamingException JavaDoc {
301
302         String JavaDoc contextClassName = context.getClass().getName();
303
304         assertNotNull("NamingEnumeration is not null", ne);
305         while (ne.hasMore()) {
306
307             Object JavaDoc next = ne.next();
308             assertTrue("list() returns NameClassPair instances",
309                    next instanceof NameClassPair JavaDoc);
310             NameClassPair JavaDoc ncp = (NameClassPair JavaDoc) next;
311
312             assertTrue("Name '" + ncp.getName() + "' is expected",
313                    isListed(ncp.getName(), list));
314
315             if (isDirContext(ncp.getName())) {
316                 assertTrue("Class '" + ncp.getClassName() + "' is '" +
317                        contextClassName + "'",
318                        contextClassName.equals(ncp.getClassName()));
319             }
320
321             assertTrue("Relative is 'true'", ncp.isRelative());
322
323         }
324
325     }
326
327
328     /**
329      * Check the results of a listBindings() call against the
330      * specified entry list.
331      *
332      * @param enum The naming enumeration we are checking
333      * @param list The list of entry names we are expecting
334      *
335      * @exception NamingException if a naming exception occurs
336      */

337     protected void checkListBindings(NamingEnumeration JavaDoc ne, String JavaDoc list[])
338         throws NamingException JavaDoc {
339
340         String JavaDoc contextClassName = context.getClass().getName();
341
342         assertNotNull("NamingEnumeration is not null", ne);
343         while (ne.hasMore()) {
344
345             Object JavaDoc next = ne.next();
346             assertTrue("listBindings() returns Binding instances",
347                    next instanceof Binding JavaDoc);
348             Binding JavaDoc b = (Binding JavaDoc) next;
349
350             assertTrue("Name '" + b.getName() + "' is expected",
351                    isListed(b.getName(), list));
352
353             if (isDirContext(b.getName())) {
354                 assertTrue("Class '" + b.getClassName() + "' is '" +
355                        contextClassName + "'",
356                        contextClassName.equals(b.getClassName()));
357             }
358
359             assertTrue("Relative is 'true'", b.isRelative());
360
361             Object JavaDoc object = b.getObject();
362             assertNotNull("Name '" + b.getName() + "' has a non-null object",
363                           object);
364             if(isDirContext(b.getName())) {
365                 assertTrue("Entry '" + b.getName() + "' is a DirContext",
366                         object instanceof DirContext JavaDoc);
367             } else {
368                 assertTrue("Entry '" + b.getName() + "' is a Resource",
369                        object instanceof Resource);
370             }
371
372         }
373
374     }
375
376
377     /**
378      * Check the attributes associated with a WEB-INF entry.
379      *
380      * @param attrs The attributes for this entry
381      * @param creationDate The creation date for this entry
382      * @param contentLength The content length for this entry
383      * @param displayName The display name for this entry
384      * @param lastModifiedDate The last modified date for this entry
385      */

386     protected void checkWebInfAttributes(Attributes JavaDoc attrs,
387                                          Date JavaDoc creationDate,
388                                          long contentLength,
389                                          String JavaDoc displayName,
390                                          Date JavaDoc lastModifiedDate)
391         throws NamingException JavaDoc {
392
393         assertNotNull("getAttributes() returned non-null", attrs);
394
395         NamingEnumeration JavaDoc ne = attrs.getAll();
396         assertNotNull("getAll() returned non-null", ne);
397         while (ne.hasMore()) {
398
399             Object JavaDoc next = ne.next();
400             assertTrue("getAll() returns Attribute instances",
401                    next instanceof Attribute JavaDoc);
402             Attribute JavaDoc attr = (Attribute JavaDoc) next;
403             String JavaDoc name = attr.getID();
404             int index = getIndex(name, webInfAttrs);
405             assertTrue("WEB-INF attribute '" + name + "' is expected",
406                    index >= 0);
407             Object JavaDoc value = attr.get();
408             assertNotNull("get() returned non-null", value);
409
410             if (name.equals("creationdate")) {
411                 assertTrue("Creation date is a date",
412                        value instanceof Date JavaDoc);
413                 assertTrue("Creation date equals " + creationDate,
414                        creationDate.equals((Date JavaDoc) value));
415             } else if (name.equals("displayname")) {
416                 assertTrue("Display name is a string",
417                        value instanceof String JavaDoc);
418                 assertTrue("Display name equals " + displayName,
419                        displayName.equals((String JavaDoc) value));
420             } else if (name.equals("getcontentlength")) {
421                 assertTrue("Content length is a long",
422                        value instanceof Long JavaDoc);
423                 assertTrue("Content length equals " + contentLength,
424                        contentLength == ((Long JavaDoc) value).longValue());
425             } else if (name.equals("getlastmodified")) {
426                 assertTrue("Last modified date is a date",
427                        value instanceof Date JavaDoc);
428                 assertTrue("Last modified date is " + lastModifiedDate,
429                        lastModifiedDate.equals((Date JavaDoc) value));
430             }
431
432         }
433
434     }
435
436
437     /**
438      * Check the attributes associated with a WEB-INF/web.xml entry.
439      *
440      * @param attrs The attributes for this entry
441      * @param creationDate The creation date for this entry
442      * @param contentLength The content length for this entry
443      * @param displayName The display name for this entry
444      * @param lastModifiedDate The last modified date for this entry
445      */

446     protected void checkWebXmlAttributes(Attributes JavaDoc attrs,
447                                          Date JavaDoc creationDate,
448                                          long contentLength,
449                                          String JavaDoc displayName,
450                                          Date JavaDoc lastModifiedDate)
451         throws NamingException JavaDoc {
452
453         assertNotNull("getAttributes() returned non-null", attrs);
454
455         NamingEnumeration JavaDoc ne = attrs.getAll();
456         assertNotNull("getAll() returned non-null", ne);
457         while (ne.hasMore()) {
458
459             Object JavaDoc next = ne.next();
460             assertTrue("getAll() returns Attribute instances",
461                    next instanceof Attribute JavaDoc);
462             Attribute JavaDoc attr = (Attribute JavaDoc) next;
463             String JavaDoc name = attr.getID();
464             int index = getIndex(name, webXmlAttrs);
465             assertTrue("WEB-INF/web.xml attribute '" + name + "' is expected",
466                    index >= 0);
467             Object JavaDoc value = attr.get();
468             assertNotNull("get() returned non-null", value);
469
470             if (name.equals("creationdate")) {
471                 assertTrue("Creation date is a date",
472                        value instanceof Date JavaDoc);
473                 assertTrue("Creation date equals " + creationDate,
474                        creationDate.equals((Date JavaDoc) value));
475             } else if (name.equals("displayname")) {
476                 assertTrue("Display name is a string",
477                        value instanceof String JavaDoc);
478                 assertTrue("Display name equals " + displayName,
479                        displayName.equals((String JavaDoc) value));
480             } else if (name.equals("getcontentlength")) {
481                 assertTrue("Content length is a long",
482                        value instanceof Long JavaDoc);
483                 assertTrue("Content length equals " + contentLength,
484                        contentLength == ((Long JavaDoc) value).longValue());
485             } else if (name.equals("getlastmodified")) {
486                 assertTrue("Last modified date is a date",
487                        value instanceof Date JavaDoc);
488                 assertTrue("Last modified date is " + lastModifiedDate,
489                        lastModifiedDate.equals((Date JavaDoc) value));
490             }
491
492         }
493
494     }
495
496
497     /**
498      * Return the index of the specified name in the specified list, or
499      * -1 if the name is not present.
500      *
501      * @param name Name to be looked up
502      * @param list List of names to be checked
503      */

504     protected int getIndex(String JavaDoc name, String JavaDoc list[]) {
505
506         for (int i = 0; i < list.length; i++) {
507             if (name.equals(list[i]))
508                 return (i);
509         }
510         return (-1);
511
512     }
513
514
515     /**
516      * Is an entry of the specified name supposed to be a DirContext?
517      *
518      * @param name Name to be checked
519      */

520     protected boolean isDirContext(String JavaDoc name) {
521
522         return (isListed(name, dirContextNames));
523
524     }
525
526
527     /**
528      * Returns <code>true</code> if the specified name is found in the
529      * specified list.
530      *
531      * @param name Name to be looked up
532      * @param list List to be looked up into
533      */

534     protected boolean isListed(String JavaDoc name, String JavaDoc list[]) {
535
536         return (getIndex(name, list) >= 0);
537
538     }
539
540
541 }
542
Popular Tags