1 16 package org.apache.commons.vfs.test; 17 18 import org.apache.commons.vfs.Capability; 19 import org.apache.commons.vfs.FileObject; 20 import org.apache.commons.vfs.RandomAccessContent; 21 import org.apache.commons.vfs.Selectors; 22 import org.apache.commons.vfs.util.RandomAccessMode; 23 24 29 public class ProviderRandomReadWriteTests 30 extends AbstractProviderTestCase 31 { 32 private final String TEST_DATA = "This is a test file."; 33 34 37 protected Capability[] getRequiredCaps() 38 { 39 return new Capability[] 40 { 41 Capability.GET_TYPE, 42 Capability.CREATE, 43 Capability.RANDOM_ACCESS_READ, 44 Capability.RANDOM_ACCESS_WRITE 45 }; 46 } 47 48 51 protected FileObject createScratchFolder() throws Exception 52 { 53 FileObject scratchFolder = getWriteFolder(); 54 55 scratchFolder.delete(Selectors.EXCLUDE_SELF); 57 scratchFolder.createFolder(); 58 59 return scratchFolder; 60 } 61 62 65 public void testRandomWrite() throws Exception 66 { 67 FileObject file = null; 68 try 69 { 70 file = createScratchFolder().resolveFile("random_write.txt"); 71 file.createFile(); 72 RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE); 73 74 ra.writeByte(TEST_DATA.charAt(0)); 76 77 ra.seek(3); 79 ra.writeByte(TEST_DATA.charAt(3)); 80 ra.writeByte(TEST_DATA.charAt(4)); 81 82 ra.seek(3); 84 ra.writeByte(TEST_DATA.charAt(7)); 85 ra.writeByte(TEST_DATA.charAt(8)); 86 87 ra.seek(10); 89 ra.writeByte(TEST_DATA.charAt(10)); 90 ra.writeByte(TEST_DATA.charAt(11)); 91 92 ra.seek(0); 94 assertEquals(ra.readByte(), TEST_DATA.charAt(0)); 95 96 ra.seek(3); 97 assertEquals(ra.readByte(), TEST_DATA.charAt(7)); 98 assertEquals(ra.readByte(), TEST_DATA.charAt(8)); 99 100 ra.seek(10); 101 assertEquals(ra.readByte(), TEST_DATA.charAt(10)); 102 assertEquals(ra.readByte(), TEST_DATA.charAt(11)); 103 } 104 finally 105 { 106 if (file != null) 107 { 108 file.close(); 109 } 110 } 111 } 112 } 113 | Popular Tags |