KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > jcr > source > JCRSourceTestCase


1 /*
2  * Copyright 2005 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.cocoon.jcr.source;
17
18 import org.apache.avalon.framework.CascadingRuntimeException;
19 import org.apache.avalon.framework.context.DefaultContext;
20 import org.apache.avalon.framework.service.ServiceSelector;
21
22 import org.apache.cocoon.core.container.ContainerTestCase;
23
24 import org.apache.excalibur.source.ModifiableSource;
25 import org.apache.excalibur.source.ModifiableTraversableSource;
26 import org.apache.excalibur.source.Source;
27 import org.apache.excalibur.source.SourceFactory;
28 import org.apache.excalibur.source.SourceResolver;
29 import org.apache.excalibur.source.TraversableSource;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.Collections JavaDoc;
37
38 /**
39  * @version $Id: JCRSourceTestCase.java 240329 2005-08-26 20:04:15Z vgritsenko $
40  */

41 public class JCRSourceTestCase extends ContainerTestCase {
42
43     private SourceResolver resolver;
44
45     private File JavaDoc tempDir;
46
47     protected void addContext(DefaultContext context) {
48         super.addContext(context);
49         // Create a temp file
50
try {
51             tempDir = File.createTempFile("jcr-test", null);
52         } catch (IOException JavaDoc e) {
53             throw new CascadingRuntimeException("Cannot setup temp dir", e);
54         }
55         // and turn it to a directory
56
tempDir.delete();
57         tempDir.mkdir();
58         tempDir.deleteOnExit();
59
60         // Setup context root as the temp dir so that relative URI used in the
61
// repository configuration go there
62
context.put("context-root", tempDir);
63
64         // Make VariableResolver used in repository configuration happy
65
context.put("object-model", Collections.EMPTY_MAP);
66     }
67
68     protected void setUp() throws Exception JavaDoc {
69         super.setUp();
70         resolver = (SourceResolver)getManager().lookup(SourceResolver.ROLE);
71     }
72
73     private void write(ModifiableSource src, String JavaDoc text) throws Exception JavaDoc {
74         byte[] data = text.getBytes("ISO-8859-1");
75         OutputStream JavaDoc os = src.getOutputStream();
76         os.write(data);
77         os.close();
78     }
79
80     private String JavaDoc read(Source src) throws Exception JavaDoc {
81         byte[] data = new byte[(int)src.getContentLength()];
82         InputStream JavaDoc is = src.getInputStream();
83         assertEquals(data.length, is.read(data));
84         is.close();
85         return new String JavaDoc(data, "ISO-8859-1");
86     }
87
88     protected void deleteFile(File JavaDoc file) {
89         File JavaDoc[] children = file.listFiles();
90         if (children != null) {
91             for (int i = 0; i < children.length; i++) {
92                 deleteFile(children[i]);
93             }
94         }
95         file.delete();
96     }
97
98     protected void tearDown() throws Exception JavaDoc {
99         super.tearDown();
100         deleteFile(tempDir);
101     }
102
103     public void testJCRSourceInitialization() throws Exception JavaDoc {
104         ServiceSelector selector = (ServiceSelector)getManager().lookup(SourceFactory.ROLE + "Selector");
105         Object JavaDoc jcrSourceFactory = selector.select("jcr");
106
107         assertEquals("Wrong class name for jcr protocol", jcrSourceFactory.getClass(), JCRSourceFactory.class);
108     }
109
110     public void testGetRootNode() throws Exception JavaDoc {
111
112         JCRNodeSource source = (JCRNodeSource)resolver.resolveURI("jcr://");
113
114         assertTrue("Root node should exist", source.exists());
115         System.err.println("Root node type = " + source.getNode().getPrimaryNodeType().getName());
116         assertTrue("Root node should be a collection", source.isCollection());
117     }
118
119     public void testCreateFirstLevelFile() throws Exception JavaDoc {
120
121         String JavaDoc someText = "Some text";
122
123         JCRNodeSource root = (JCRNodeSource)resolver.resolveURI("jcr://");
124
125         JCRNodeSource firstChild = (JCRNodeSource)root.getChild("child1");
126
127         assertFalse(firstChild.exists());
128         assertEquals(firstChild.getURI(), "jcr://child1");
129
130         write(firstChild, someText);
131
132         assertTrue(firstChild.exists());
133
134         // Check content
135
Source child1 = resolver.resolveURI("jcr://child1");
136         assertTrue(child1.exists());
137
138         int len = (int)child1.getContentLength();
139         assertEquals(someText.length(), len);
140         assertEquals(someText, read(child1));
141
142     }
143
144     public void testCreateDeepFile() throws Exception JavaDoc {
145         String JavaDoc anotherText = "another text";
146
147         JCRNodeSource source = (JCRNodeSource)resolver.resolveURI("jcr://some/deep/path/to/file");
148         assertFalse(source.exists());
149
150         write(source, anotherText);
151
152         // Lookup again, using the parent, doing some traversal
153
TraversableSource dir = (TraversableSource)resolver.resolveURI("jcr://some/deep");
154         assertTrue(dir.isCollection());
155         dir = (TraversableSource)dir.getChild("path");
156         assertTrue(dir.isCollection());
157         dir = (TraversableSource)dir.getChild("to");
158         assertTrue(dir.isCollection());
159
160         source = (JCRNodeSource)dir.getChild("file");
161         assertTrue(source.exists());
162
163         assertEquals(anotherText, read(source));
164     }
165
166     public void testDeleteFile() throws Exception JavaDoc {
167         String JavaDoc text = "Yeah! Some content!";
168         ModifiableSource source = (ModifiableSource)resolver.resolveURI("jcr://yet/another/deep/file");
169
170         assertFalse(source.exists());
171         write(source, text);
172
173         // Lookup a fresh source
174
source = (ModifiableSource)resolver.resolveURI("jcr://yet/another/deep/file");
175         assertTrue(source.exists());
176         source.delete();
177         assertFalse(source.exists());
178
179         // Lookup again to check it was really deleted
180
source = (ModifiableSource)resolver.resolveURI("jcr://yet/another/deep/file");
181         assertFalse(source.exists());
182     }
183
184     public void testDeleteDir() throws Exception JavaDoc {
185         String JavaDoc text = "Wow, a lot of data going there";
186         ModifiableTraversableSource source = (ModifiableTraversableSource)resolver.resolveURI("jcr://and/again/a/deep/node");
187
188         assertFalse(source.exists());
189         write(source, text);
190
191         // Lookup 'a' node
192
source = (ModifiableTraversableSource)resolver.resolveURI("jcr://and/again/a/");
193         assertTrue(source.isCollection());
194         source.delete();
195         assertFalse(source.exists());
196
197         // Double check with a fresh source
198
source = (ModifiableTraversableSource)resolver.resolveURI("jcr://and/again/a/");
199         assertFalse(source.exists());
200
201         // Check on children
202
source = (ModifiableTraversableSource)resolver.resolveURI("jcr://and/again/a/deep/node");
203         assertFalse(source.exists());
204     }
205
206     public void testTraverseDir() throws Exception JavaDoc {
207         String JavaDoc text = "Look Ma, more data!";
208
209         ModifiableTraversableSource dir = (ModifiableTraversableSource)resolver.resolveURI("jcr://path/to/dir");
210         dir.makeCollection();
211
212         for (int i = 0; i < 10; i++) {
213             ModifiableTraversableSource src = (ModifiableTraversableSource)dir.getChild("file" + i);
214             write(src, text + i);
215         }
216
217         // Lookup dir again, and inspect children
218
dir = (ModifiableTraversableSource)resolver.resolveURI("jcr://path/to/dir");
219         Collection JavaDoc children = dir.getChildren();
220
221         assertEquals(10, children.size());
222
223         for (int i = 0; i < 10; i++) {
224             Source src = dir.getChild("file" + i);
225             assertTrue(src.exists());
226             assertEquals(text + i, read(src));
227         }
228     }
229
230     public void testCrawlUp() throws Exception JavaDoc {
231         String JavaDoc text = "Look Pa, some more!";
232
233         ModifiableTraversableSource src = (ModifiableTraversableSource)resolver.resolveURI("jcr://path/to/very/deep/content");
234         write(src, text);
235
236         // Do a fresh lookup
237
src = (ModifiableTraversableSource)resolver.resolveURI("jcr://path/to/very/deep/content");
238
239         ModifiableTraversableSource parent = (ModifiableTraversableSource)src.getParent();
240         assertTrue(parent.exists());
241         assertEquals("jcr://path/to/very/deep", parent.getURI());
242
243         parent = (ModifiableTraversableSource)parent.getParent();
244         assertTrue(parent.exists());
245         assertEquals("jcr://path/to/very", parent.getURI());
246
247         parent = (ModifiableTraversableSource)parent.getParent();
248         assertTrue(parent.exists());
249         assertEquals("jcr://path/to", parent.getURI());
250
251         parent = (ModifiableTraversableSource)parent.getParent();
252         assertTrue(parent.exists());
253         assertEquals("jcr://path", parent.getURI());
254
255         parent = (ModifiableTraversableSource)parent.getParent();
256         assertTrue(parent.exists());
257         assertEquals("jcr://", parent.getURI());
258
259         // Root node has no parent
260
parent = (ModifiableTraversableSource)parent.getParent();
261         assertNull(parent);
262     }
263 }
264
Popular Tags