1 17 package org.alfresco.repo.content.filestore; 18 19 import java.io.File ; 20 import java.nio.ByteBuffer ; 21 import java.nio.channels.FileChannel ; 22 import java.nio.channels.ReadableByteChannel ; 23 import java.nio.channels.WritableByteChannel ; 24 25 import org.alfresco.repo.content.AbstractContentReadWriteTest; 26 import org.alfresco.repo.content.ContentStore; 27 import org.alfresco.repo.content.RandomAccessContent; 28 import org.alfresco.service.cmr.repository.ContentReader; 29 import org.alfresco.service.cmr.repository.ContentWriter; 30 import org.alfresco.util.TempFileProvider; 31 32 40 public class FileContentReadWriteTest extends AbstractContentReadWriteTest 41 { 42 private File file; 43 44 public FileContentReadWriteTest() 45 { 46 super(); 47 } 48 49 @Override 50 public void setUp() throws Exception 51 { 52 super.setUp(); 53 file = TempFileProvider.createTempFile(getName(), ".txt"); 54 file.deleteOnExit(); 55 } 56 57 @Override 58 protected ContentStore getStore() 59 { 60 return null; 61 } 62 63 @Override 64 protected ContentReader getReader() 65 { 66 return new FileContentReader(file); 67 } 68 69 @Override 70 protected ContentWriter getWriter() 71 { 72 return new FileContentWriter(file); 73 } 74 75 78 public void testCallbackClass() throws Exception 79 { 80 ContentWriter writer = getWriter(); 81 WritableByteChannel writeChannel = writer.getWritableChannel(); 82 assertTrue("Channel not of correct callback type", writeChannel instanceof FileChannel ); 83 84 writeChannel.write(ByteBuffer.wrap("ABC".getBytes())); 86 assertFalse("Writer should not be closed", writer.isClosed()); 87 writeChannel.close(); 89 assertTrue("Writer should be closed", writer.isClosed()); 90 91 ContentReader reader = writer.getReader(); 93 ReadableByteChannel readChannel = reader.getReadableChannel(); 94 assertTrue("Channel not of correct callback type", readChannel instanceof FileChannel ); 95 } 96 97 101 public void testRandomAccessWriteCopy() throws Exception 102 { 103 String content = "ABC"; 104 byte[] bytes = content.getBytes(); 105 106 ContentWriter seedWriter = getWriter(); 107 seedWriter.putContent(content); 108 File file = TempFileProvider.createTempFile(getName(), ".txt"); 110 ContentWriter writer = new FileContentWriter(file, seedWriter.getReader()); 111 RandomAccessContent randAccessContent = (RandomAccessContent) writer; 113 FileChannel channel = randAccessContent.getChannel(); 114 115 ByteBuffer buffer = ByteBuffer.allocate(bytes.length); 117 int read = channel.read(buffer); 118 assertEquals("Not enough bytes read", bytes.length, read); 119 byte[] checkBytes = new byte[bytes.length]; 120 buffer.rewind(); 121 buffer.get(checkBytes); 122 String checkContent = new String (checkBytes); 123 assertEquals("Content is not exactly the same", content, checkContent); 124 125 channel.close(); 127 } 128 } 129 | Popular Tags |