KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > filestore > FileContentReadWriteTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.content.filestore;
18
19 import java.io.File JavaDoc;
20 import java.nio.ByteBuffer JavaDoc;
21 import java.nio.channels.FileChannel JavaDoc;
22 import java.nio.channels.ReadableByteChannel JavaDoc;
23 import java.nio.channels.WritableByteChannel JavaDoc;
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 /**
33  * Tests that the file reader and writer work
34  *
35  * @see org.alfresco.repo.content.filestore.FileContentReader
36  * @see org.alfresco.repo.content.filestore.FileContentWriter
37  *
38  * @author Derek Hulley
39  */

40 public class FileContentReadWriteTest extends AbstractContentReadWriteTest
41 {
42     private File JavaDoc file;
43     
44     public FileContentReadWriteTest()
45     {
46         super();
47     }
48
49     @Override JavaDoc
50     public void setUp() throws Exception JavaDoc
51     {
52         super.setUp();
53         file = TempFileProvider.createTempFile(getName(), ".txt");
54         file.deleteOnExit();
55     }
56
57     @Override JavaDoc
58     protected ContentStore getStore()
59     {
60         return null;
61     }
62     
63     @Override JavaDoc
64     protected ContentReader getReader()
65     {
66         return new FileContentReader(file);
67     }
68
69     @Override JavaDoc
70     protected ContentWriter getWriter()
71     {
72         return new FileContentWriter(file);
73     }
74     
75     /**
76      * Checks that the channel is a wrapper for file channel
77      */

78     public void testCallbackClass() throws Exception JavaDoc
79     {
80         ContentWriter writer = getWriter();
81         WritableByteChannel JavaDoc writeChannel = writer.getWritableChannel();
82         assertTrue("Channel not of correct callback type", writeChannel instanceof FileChannel JavaDoc);
83         
84         // put some content
85
writeChannel.write(ByteBuffer.wrap("ABC".getBytes()));
86         assertFalse("Writer should not be closed", writer.isClosed());
87         // close
88
writeChannel.close();
89         assertTrue("Writer should be closed", writer.isClosed());
90         
91         // get the reader
92
ContentReader reader = writer.getReader();
93         ReadableByteChannel JavaDoc readChannel = reader.getReadableChannel();
94         assertTrue("Channel not of correct callback type", readChannel instanceof FileChannel JavaDoc);
95     }
96     
97     /**
98      * Ensures that the random writing of content takes pre-existing content
99      * into account, i.e. that the pre-existing content is copied.
100      */

101     public void testRandomAccessWriteCopy() throws Exception JavaDoc
102     {
103         String JavaDoc content = "ABC";
104         byte[] bytes = content.getBytes();
105         
106         ContentWriter seedWriter = getWriter();
107         seedWriter.putContent(content);
108         // create a new writer that uses the seed writer
109
File JavaDoc file = TempFileProvider.createTempFile(getName(), ".txt");
110         ContentWriter writer = new FileContentWriter(file, seedWriter.getReader());
111         // go the random access route
112
RandomAccessContent randAccessContent = (RandomAccessContent) writer;
113         FileChannel JavaDoc channel = randAccessContent.getChannel();
114         
115         // check that the previous contents are present in the file
116
ByteBuffer JavaDoc 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 JavaDoc checkContent = new String JavaDoc(checkBytes);
123         assertEquals("Content is not exactly the same", content, checkContent);
124         
125         // close channel
126
channel.close();
127     }
128 }
129
Popular Tags