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: package org.apache.cocoon.jcr.source;
018:
019: import org.apache.avalon.framework.CascadingRuntimeException;
020: import org.apache.avalon.framework.context.DefaultContext;
021: import org.apache.avalon.framework.service.ServiceSelector;
022:
023: import org.apache.cocoon.core.container.ContainerTestCase;
024:
025: import org.apache.excalibur.source.ModifiableSource;
026: import org.apache.excalibur.source.ModifiableTraversableSource;
027: import org.apache.excalibur.source.Source;
028: import org.apache.excalibur.source.SourceFactory;
029: import org.apache.excalibur.source.SourceResolver;
030: import org.apache.excalibur.source.TraversableSource;
031:
032: import java.io.File;
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.io.OutputStream;
036: import java.util.Collection;
037: import java.util.Collections;
038:
039: /**
040: * @version $Id: JCRSourceTestCase.java 467931 2006-10-26 09:27:56Z cziegeler $
041: */
042: public class JCRSourceTestCase extends ContainerTestCase {
043:
044: private SourceResolver resolver;
045:
046: private File tempDir;
047:
048: /**
049: * @see org.apache.cocoon.core.container.ContainerTestCase#addContext(org.apache.avalon.framework.context.DefaultContext)
050: */
051: protected void addContext(DefaultContext context) {
052: super .addContext(context);
053: // Create a temp file
054: try {
055: tempDir = File.createTempFile("jcr-test", null);
056: } catch (IOException e) {
057: throw new CascadingRuntimeException(
058: "Cannot setup temp dir", e);
059: }
060: // and turn it to a directory
061: tempDir.delete();
062: tempDir.mkdir();
063: tempDir.deleteOnExit();
064:
065: // Setup context root as the temp dir so that relative URI used in the
066: // repository configuration go there
067: context.put("context-root", tempDir);
068:
069: // Make VariableResolver used in repository configuration happy
070: context.put("object-model", Collections.EMPTY_MAP);
071: }
072:
073: /**
074: * @see org.apache.cocoon.core.container.ContainerTestCase#setUp()
075: */
076: protected void setUp() throws Exception {
077: super .setUp();
078: resolver = (SourceResolver) getManager().lookup(
079: SourceResolver.ROLE);
080: }
081:
082: private void write(ModifiableSource src, String text)
083: throws Exception {
084: byte[] data = text.getBytes("ISO-8859-1");
085: OutputStream os = src.getOutputStream();
086: os.write(data);
087: os.close();
088: }
089:
090: private String read(Source src) throws Exception {
091: byte[] data = new byte[(int) src.getContentLength()];
092: InputStream is = src.getInputStream();
093: assertEquals(data.length, is.read(data));
094: is.close();
095: return new String(data, "ISO-8859-1");
096: }
097:
098: protected void deleteFile(File file) {
099: File[] children = file.listFiles();
100: if (children != null) {
101: for (int i = 0; i < children.length; i++) {
102: deleteFile(children[i]);
103: }
104: }
105: file.delete();
106: }
107:
108: /**
109: * @see org.apache.cocoon.core.container.ContainerTestCase#tearDown()
110: */
111: protected void tearDown() throws Exception {
112: super .tearDown();
113: deleteFile(tempDir);
114: }
115:
116: public void testJCRSourceInitialization() throws Exception {
117: ServiceSelector selector = (ServiceSelector) getManager()
118: .lookup(SourceFactory.ROLE + "Selector");
119: Object jcrSourceFactory = selector.select("jcr");
120:
121: assertEquals("Wrong class name for jcr protocol",
122: jcrSourceFactory.getClass(), JCRSourceFactory.class);
123: }
124:
125: public void testGetRootNode() throws Exception {
126:
127: JCRNodeSource source = (JCRNodeSource) resolver
128: .resolveURI("jcr://");
129:
130: assertTrue("Root node should exist", source.exists());
131: System.err.println("Root node type = "
132: + source.getNode().getPrimaryNodeType().getName());
133: assertTrue("Root node should be a collection", source
134: .isCollection());
135: }
136:
137: public void testCreateFirstLevelFile() throws Exception {
138:
139: String someText = "Some text";
140:
141: JCRNodeSource root = (JCRNodeSource) resolver
142: .resolveURI("jcr://");
143:
144: JCRNodeSource firstChild = (JCRNodeSource) root
145: .getChild("child1");
146:
147: assertFalse(firstChild.exists());
148: assertEquals(firstChild.getURI(), "jcr://child1");
149:
150: write(firstChild, someText);
151:
152: assertTrue(firstChild.exists());
153:
154: // Check content
155: Source child1 = resolver.resolveURI("jcr://child1");
156: assertTrue(child1.exists());
157:
158: int len = (int) child1.getContentLength();
159: assertEquals(someText.length(), len);
160: assertEquals(someText, read(child1));
161:
162: }
163:
164: public void testCreateDeepFile() throws Exception {
165: String anotherText = "another text";
166:
167: JCRNodeSource source = (JCRNodeSource) resolver
168: .resolveURI("jcr://some/deep/path/to/file");
169: assertFalse(source.exists());
170:
171: write(source, anotherText);
172:
173: // Lookup again, using the parent, doing some traversal
174: TraversableSource dir = (TraversableSource) resolver
175: .resolveURI("jcr://some/deep");
176: assertTrue(dir.isCollection());
177: dir = (TraversableSource) dir.getChild("path");
178: assertTrue(dir.isCollection());
179: dir = (TraversableSource) dir.getChild("to");
180: assertTrue(dir.isCollection());
181:
182: source = (JCRNodeSource) dir.getChild("file");
183: assertTrue(source.exists());
184:
185: assertEquals(anotherText, read(source));
186: }
187:
188: public void testDeleteFile() throws Exception {
189: String text = "Yeah! Some content!";
190: ModifiableSource source = (ModifiableSource) resolver
191: .resolveURI("jcr://yet/another/deep/file");
192:
193: assertFalse(source.exists());
194: write(source, text);
195:
196: // Lookup a fresh source
197: source = (ModifiableSource) resolver
198: .resolveURI("jcr://yet/another/deep/file");
199: assertTrue(source.exists());
200: source.delete();
201: assertFalse(source.exists());
202:
203: // Lookup again to check it was really deleted
204: source = (ModifiableSource) resolver
205: .resolveURI("jcr://yet/another/deep/file");
206: assertFalse(source.exists());
207: }
208:
209: public void testDeleteDir() throws Exception {
210: String text = "Wow, a lot of data going there";
211: ModifiableTraversableSource source = (ModifiableTraversableSource) resolver
212: .resolveURI("jcr://and/again/a/deep/node");
213:
214: assertFalse(source.exists());
215: write(source, text);
216:
217: // Lookup 'a' node
218: source = (ModifiableTraversableSource) resolver
219: .resolveURI("jcr://and/again/a/");
220: assertTrue(source.isCollection());
221: source.delete();
222: assertFalse(source.exists());
223:
224: // Double check with a fresh source
225: source = (ModifiableTraversableSource) resolver
226: .resolveURI("jcr://and/again/a/");
227: assertFalse(source.exists());
228:
229: // Check on children
230: source = (ModifiableTraversableSource) resolver
231: .resolveURI("jcr://and/again/a/deep/node");
232: assertFalse(source.exists());
233: }
234:
235: public void testTraverseDir() throws Exception {
236: String text = "Look Ma, more data!";
237:
238: ModifiableTraversableSource dir = (ModifiableTraversableSource) resolver
239: .resolveURI("jcr://path/to/dir");
240: dir.makeCollection();
241:
242: for (int i = 0; i < 10; i++) {
243: ModifiableTraversableSource src = (ModifiableTraversableSource) dir
244: .getChild("file" + i);
245: write(src, text + i);
246: }
247:
248: // Lookup dir again, and inspect children
249: dir = (ModifiableTraversableSource) resolver
250: .resolveURI("jcr://path/to/dir");
251: Collection children = dir.getChildren();
252:
253: assertEquals(10, children.size());
254:
255: for (int i = 0; i < 10; i++) {
256: Source src = dir.getChild("file" + i);
257: assertTrue(src.exists());
258: assertEquals(text + i, read(src));
259: }
260: }
261:
262: public void testCrawlUp() throws Exception {
263: String text = "Look Pa, some more!";
264:
265: ModifiableTraversableSource src = (ModifiableTraversableSource) resolver
266: .resolveURI("jcr://path/to/very/deep/content");
267: write(src, text);
268:
269: // Do a fresh lookup
270: src = (ModifiableTraversableSource) resolver
271: .resolveURI("jcr://path/to/very/deep/content");
272:
273: ModifiableTraversableSource parent = (ModifiableTraversableSource) src
274: .getParent();
275: assertTrue(parent.exists());
276: assertEquals("jcr://path/to/very/deep", parent.getURI());
277:
278: parent = (ModifiableTraversableSource) parent.getParent();
279: assertTrue(parent.exists());
280: assertEquals("jcr://path/to/very", parent.getURI());
281:
282: parent = (ModifiableTraversableSource) parent.getParent();
283: assertTrue(parent.exists());
284: assertEquals("jcr://path/to", parent.getURI());
285:
286: parent = (ModifiableTraversableSource) parent.getParent();
287: assertTrue(parent.exists());
288: assertEquals("jcr://path", parent.getURI());
289:
290: parent = (ModifiableTraversableSource) parent.getParent();
291: assertTrue(parent.exists());
292: assertEquals("jcr://", parent.getURI());
293:
294: // Root node has no parent
295: parent = (ModifiableTraversableSource) parent.getParent();
296: assertNull(parent);
297: }
298: }
|