KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004,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 package org.apache.naming.resources;
17
18 import java.util.Date JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.naming.Context JavaDoc;
23 import javax.naming.Name JavaDoc;
24
25 import javax.naming.directory.DirContext JavaDoc;
26 import javax.naming.directory.Attributes JavaDoc;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30 import junit.textui.TestRunner;
31
32
33 /**
34  * Unit tests for basic ops on a {@link FileDirContext}.
35  *
36  * @version $Revision: 125387 $ $Date: 2003/11/30 05:36:07 $
37  */

38 public class FileDirContextTest extends AbstractDirContextTest {
39     
40     public FileDirContextTest(String JavaDoc name) {
41         super(name);
42     }
43
44     public static void main(String JavaDoc[] args) {
45         TestRunner.run(suite());
46     }
47
48     public static Test suite() {
49         TestSuite suite = new TestSuite(FileDirContextTest.class);
50         suite.setName("FileDirContext Tests");
51         return suite;
52     }
53     
54     //-------------------- AbstractContextTest overrides ------------------------------------------
55

56     protected Context JavaDoc makeInitialContext() {
57         try {
58             FileDirContext fdc = new FileDirContext();
59             fdc.setDocBase(".");
60             return fdc;
61         } catch (Exception JavaDoc ex) {
62             fail("Failed to create Initial Context");
63         }
64         return null;
65     }
66     
67     /**
68      * Just verify that the bound object is a resource
69      */

70     protected void verifyLookup(Object JavaDoc boundObject, Object JavaDoc returnedObject) {
71         assertTrue(returnedObject instanceof Resource);
72     }
73     
74     /**
75      * Verify that the listed bound names match expectation and
76      * that the bound objects are Resources
77      */

78     protected void verifyListBindings(Map JavaDoc expected, Map JavaDoc returned) {
79         super.verifyListBindings(expected, returned);
80         Iterator JavaDoc iterator = returned.values().iterator();
81         while (iterator.hasNext()) {
82             assertTrue(iterator.next() instanceof Resource);
83         }
84     }
85     
86     protected boolean isGetNameInNamespaceSupported() {
87         return true;
88     }
89     
90     protected boolean isWritable() {
91         return true;
92     }
93     
94     //-------------------- AbstractDirContextTest overrides ---------------------------------------
95

96     protected boolean isSchemaSupported() {
97         return false;
98     }
99     
100     protected boolean isAttributeModificationSupported() {
101         return false;
102     }
103     
104     /**
105      * Verify file attributes
106      */

107     public void testAttributes() throws Exception JavaDoc {
108         super.testAttributes();
109         DirContext JavaDoc context = (DirContext JavaDoc) initialContext.lookup(firstContextName()+ "/" + secondContextName());
110         Attributes JavaDoc attrs = (Attributes JavaDoc) context.getAttributes(firstBoundName());
111         Date JavaDoc creationDate = (Date JavaDoc) attrs.get(ResourceAttributes.CREATION_DATE).get();
112         assertTrue(creationDate.before(new Date JavaDoc()));
113         Date JavaDoc modifiedDate = (Date JavaDoc) attrs.get(ResourceAttributes.LAST_MODIFIED).get();
114         assertTrue(modifiedDate.equals(creationDate));
115         String JavaDoc displayName = (String JavaDoc) attrs.get(ResourceAttributes.NAME).get();
116         assertEquals(displayName, firstBoundName());
117         long contentLength = ((Long JavaDoc) attrs.get(ResourceAttributes.CONTENT_LENGTH).get()).longValue();
118         assertEquals(contentLength, bytes.length);
119         String JavaDoc resourceType = (String JavaDoc) attrs.get(ResourceAttributes.TYPE).get();
120         assertEquals(resourceType, ""); // Present, but empty -- is this the correct?
121
assertNull(attrs.get(ResourceAttributes.CONTENT_TYPE)); // Not present -- is this correct?
122
assertNull(attrs.get(ResourceAttributes.SOURCE)); // "" ""
123
assertNull(attrs.get(ResourceAttributes.ETAG)); // "" ""
124
}
125     
126     public void testGetNameInNamespace() throws Exception JavaDoc {
127         super.testGetNameInNamespace();
128         Name JavaDoc name = nameParser.parse(firstContext.getNameInNamespace());
129         assertTrue(name.endsWith(nameParser.parse(firstContextName())));
130         name = nameParser.parse(secondContext.getNameInNamespace());
131         assertTrue(name.endsWith(nameParser.parse(secondContextName())));
132     }
133 }
134
Popular Tags