1 17 package org.alfresco.repo.content.filestore; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 import java.io.OutputStream ; 23 import java.nio.ByteBuffer ; 24 import java.nio.channels.FileChannel ; 25 26 import junit.framework.TestCase; 27 28 33 public class FileIOTest extends TestCase 34 { 35 private static final String TEST_CONTENT = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 36 37 private File file; 38 39 public FileIOTest(String name) 40 { 41 super(name); 42 } 43 44 public void setUp() throws Exception 45 { 46 file = File.createTempFile(getName(), ".txt"); 47 OutputStream os = new FileOutputStream (file); 48 os.write(TEST_CONTENT.getBytes()); 49 os.flush(); 50 os.close(); 51 } 52 53 56 public void testConcurrentFileReads() throws Exception 57 { 58 FileInputStream isA = new FileInputStream (file); 60 FileInputStream isB = new FileInputStream (file); 61 62 FileChannel channelA = isA.getChannel(); 64 FileChannel channelB = isB.getChannel(); 65 66 ByteBuffer bufferA = ByteBuffer.allocate(10); 68 ByteBuffer bufferB = ByteBuffer.allocate(10); 69 70 int countA = 0; 72 int countB = 0; 73 do 74 { 75 countA = channelA.read((ByteBuffer )bufferA.clear()); 76 countB = channelB.read((ByteBuffer )bufferB.clear()); 77 assertEquals("Should read same number of bytes", countA, countB); 78 } while (countA > 6); 79 80 assertEquals("BufferA marker incorrect", 6, bufferA.position()); 82 assertEquals("BufferB marker incorrect", 6, bufferB.position()); 83 } 84 85 public void testConcurrentReadWrite() throws Exception 86 { 87 FileInputStream isRead = new FileInputStream (file); 89 FileOutputStream osWrite = new FileOutputStream (file); 91 92 FileChannel channelRead = isRead.getChannel(); 94 FileChannel channelWrite = osWrite.getChannel(); 95 96 ByteBuffer bufferRead = ByteBuffer.allocate(26); 98 ByteBuffer bufferWrite = ByteBuffer.wrap(TEST_CONTENT.getBytes()); 99 100 int countRead = channelRead.read(bufferRead); 102 assertEquals("Expected nothing to be read", -1, countRead); 103 int countWrite = channelWrite.write(bufferWrite); 105 assertEquals("Not all characters written", 26, countWrite); 106 107 channelWrite.close(); 109 110 countRead = channelRead.read(bufferRead); 112 assertEquals("Expected full read", 26, countRead); 113 } 114 } 115 | Popular Tags |