1 17 package org.apache.excalibur.source.test; 18 19 import java.io.File ; 20 import java.io.OutputStream ; 21 import java.io.PrintWriter ; 22 import java.util.Collection ; 23 import java.util.ConcurrentModificationException ; 24 25 import junit.framework.TestCase; 26 27 import org.apache.excalibur.source.ModifiableSource; 28 import org.apache.excalibur.source.Source; 29 import org.apache.excalibur.source.SourceUtil; 30 import org.apache.excalibur.source.SourceValidity; 31 import org.apache.excalibur.source.impl.FileSource; 32 33 39 public class FileSourceTestCase extends TestCase 40 { 41 42 private File m_tempDir; 43 44 public FileSourceTestCase() 45 { 46 this("FileSource"); 47 } 48 49 public FileSourceTestCase(String name) 50 { 51 super(name); 52 } 53 54 protected void setUp() throws Exception 55 { 56 m_tempDir = File.createTempFile("filesource", "test"); 58 m_tempDir.delete(); 60 m_tempDir.mkdir(); 61 } 62 63 public void testDirExistence() throws Exception 64 { 65 m_tempDir.mkdirs(); 66 long time = m_tempDir.lastModified(); 67 FileSource src = new FileSource("file", m_tempDir); 68 assertTrue("Temp dir doesn't exist", src.exists()); 69 assertTrue("Temp dir is not traversable", src.isCollection()); 70 assertEquals("Wrong creation date", time, src.getLastModified()); 72 73 assertTrue("Temp dir is not empty", src.getChildren().isEmpty()); 74 } 75 76 public void testChildCreation() throws Exception 77 { 78 final String text = "Writing to a source"; 79 80 FileSource src = new FileSource("file", m_tempDir); 81 82 FileSource child = (FileSource) src.getChild("child.txt"); 83 assertTrue("New file already exists", !child.exists()); 84 85 assertNull("New file has a validity", child.getValidity()); 87 88 assertEquals("Wrong name", "child.txt", child.getName()); 90 91 fillSource(child, text); 93 94 assertEquals( 96 "Wrong length", 97 text.length() + System.getProperty("line.separator").length(), 98 child.getContentLength()); 99 assertEquals("Wrong content-type", "text/plain", child.getMimeType()); 100 assertTrue("New file is traversable", !child.isCollection()); 101 102 Collection children = src.getChildren(); 104 assertEquals("Wrong number of children", 1, children.size()); 105 106 Source parent = child.getParent(); 108 assertEquals("Wrong parent URI", src.getURI(), parent.getURI()); 109 110 } 111 112 public void testMove() throws Exception 113 { 114 final String text = "Original text"; 115 116 FileSource src = new FileSource("file", m_tempDir); 117 118 FileSource child = (FileSource) src.getChild("child.txt"); 119 assertTrue("New file already exists", !child.exists()); 120 121 fillSource(child, text); 122 assertTrue("New file doesn't exist", child.exists()); 123 long length = child.getContentLength(); 124 125 FileSource child2 = (FileSource) src.getChild("child2.txt"); 126 assertTrue("Second file already exist", !child2.exists()); 127 128 SourceUtil.move(child, child2); 129 assertTrue("First file still exists", !child.exists()); 130 assertTrue("Second file doesn't exist", child2.exists()); 131 assertEquals("Wrong length of second file", length, child2.getContentLength()); 132 } 133 134 public void testCopy() throws Exception 135 { 136 final String text = "Original text"; 137 138 FileSource src = new FileSource("file", m_tempDir); 139 140 FileSource child = (FileSource) src.getChild("child.txt"); 141 assertTrue("New file already exists", !child.exists()); 142 143 fillSource(child, text); 144 assertTrue("New file doesn't exist", child.exists()); 145 long length = child.getContentLength(); 146 147 FileSource child2 = (FileSource) src.getChild("child2.txt"); 148 assertTrue("Second file already exist", !child2.exists()); 149 150 SourceUtil.copy(child, child2); 151 152 assertTrue("First file doesn't exist", child.exists()); 153 assertTrue("Second file doesn't exist", child2.exists()); 154 assertEquals("Wrong length of second file", length, child2.getContentLength()); 155 156 } 157 158 public void testDelete() throws Exception 159 { 160 final String text = "Original text"; 161 162 FileSource src = new FileSource("file", m_tempDir); 163 164 FileSource child = (FileSource) src.getChild("child.txt"); 165 assertTrue("New file already exists", !child.exists()); 166 fillSource(child, text); 167 assertTrue("New file doesn't exist", child.exists()); 168 169 child.delete(); 170 assertTrue("File still exists", !child.exists()); 171 } 172 173 public void testConcurrentAccess() throws Exception 174 { 175 FileSource src = new FileSource("file", m_tempDir); 176 177 FileSource child = (FileSource) src.getChild("child.txt"); 178 assertTrue("New file already exists", !child.exists()); 179 180 child.getOutputStream(); 181 182 try 183 { 184 child.getOutputStream(); 186 } 187 catch (ConcurrentModificationException cme) 188 { 189 return; } 191 fail("Undedected concurrent modification"); 192 193 } 194 195 public void testAtomicUpdate() throws Exception 196 { 197 final String text = "Blah, blah"; 198 FileSource src = new FileSource("file", m_tempDir); 199 200 FileSource child = (FileSource) src.getChild("child.txt"); 201 assertTrue("New file already exists", !child.exists()); 202 fillSource(child, text + " and blah!"); 203 204 long length = child.getContentLength(); 205 206 SourceValidity validity = child.getValidity(); 207 assertEquals("Validity is not valid", 1, validity.isValid()); 208 209 Thread.sleep(2 * 1000L); 211 212 PrintWriter pw = new PrintWriter (child.getOutputStream()); 214 pw.write(text); 215 216 assertEquals("File length modified", length, child.getContentLength()); 217 218 pw.close(); 219 220 assertTrue("File length not modified", length != child.getContentLength()); 221 222 assertEquals("Validity is valid", -1, validity.isValid()); 223 } 224 225 protected void tearDown() throws Exception 226 { 227 deleteAll(m_tempDir); 228 } 229 230 private void deleteAll(File f) 232 { 233 if (f.isDirectory()) 234 { 235 File [] children = f.listFiles(); 236 for (int i = 0; i < children.length; i++) 237 { 238 deleteAll(children[i]); 239 } 240 } 241 242 f.delete(); 243 } 244 245 private void fillSource(ModifiableSource src, String text) throws Exception 246 { 247 OutputStream os = src.getOutputStream(); 248 PrintWriter pw = new PrintWriter (os); 249 250 pw.println(text); 251 pw.close(); 252 } 253 254 } 255 | Popular Tags |