1 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 ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.OutputStream ; 35 import java.util.Collection ; 36 import java.util.Collections ; 37 38 41 public class JCRSourceTestCase extends ContainerTestCase { 42 43 private SourceResolver resolver; 44 45 private File tempDir; 46 47 protected void addContext(DefaultContext context) { 48 super.addContext(context); 49 try { 51 tempDir = File.createTempFile("jcr-test", null); 52 } catch (IOException e) { 53 throw new CascadingRuntimeException("Cannot setup temp dir", e); 54 } 55 tempDir.delete(); 57 tempDir.mkdir(); 58 tempDir.deleteOnExit(); 59 60 context.put("context-root", tempDir); 63 64 context.put("object-model", Collections.EMPTY_MAP); 66 } 67 68 protected void setUp() throws Exception { 69 super.setUp(); 70 resolver = (SourceResolver)getManager().lookup(SourceResolver.ROLE); 71 } 72 73 private void write(ModifiableSource src, String text) throws Exception { 74 byte[] data = text.getBytes("ISO-8859-1"); 75 OutputStream os = src.getOutputStream(); 76 os.write(data); 77 os.close(); 78 } 79 80 private String read(Source src) throws Exception { 81 byte[] data = new byte[(int)src.getContentLength()]; 82 InputStream is = src.getInputStream(); 83 assertEquals(data.length, is.read(data)); 84 is.close(); 85 return new String (data, "ISO-8859-1"); 86 } 87 88 protected void deleteFile(File file) { 89 File [] 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 { 99 super.tearDown(); 100 deleteFile(tempDir); 101 } 102 103 public void testJCRSourceInitialization() throws Exception { 104 ServiceSelector selector = (ServiceSelector)getManager().lookup(SourceFactory.ROLE + "Selector"); 105 Object 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 { 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 { 120 121 String 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 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 { 145 String 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 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 { 167 String 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 source = (ModifiableSource)resolver.resolveURI("jcr://yet/another/deep/file"); 175 assertTrue(source.exists()); 176 source.delete(); 177 assertFalse(source.exists()); 178 179 source = (ModifiableSource)resolver.resolveURI("jcr://yet/another/deep/file"); 181 assertFalse(source.exists()); 182 } 183 184 public void testDeleteDir() throws Exception { 185 String 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 source = (ModifiableTraversableSource)resolver.resolveURI("jcr://and/again/a/"); 193 assertTrue(source.isCollection()); 194 source.delete(); 195 assertFalse(source.exists()); 196 197 source = (ModifiableTraversableSource)resolver.resolveURI("jcr://and/again/a/"); 199 assertFalse(source.exists()); 200 201 source = (ModifiableTraversableSource)resolver.resolveURI("jcr://and/again/a/deep/node"); 203 assertFalse(source.exists()); 204 } 205 206 public void testTraverseDir() throws Exception { 207 String 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 dir = (ModifiableTraversableSource)resolver.resolveURI("jcr://path/to/dir"); 219 Collection 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 { 231 String text = "Look Pa, some more!"; 232 233 ModifiableTraversableSource src = (ModifiableTraversableSource)resolver.resolveURI("jcr://path/to/very/deep/content"); 234 write(src, text); 235 236 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 parent = (ModifiableTraversableSource)parent.getParent(); 261 assertNull(parent); 262 } 263 } 264 | Popular Tags |