KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileInputStream JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.nio.ByteBuffer JavaDoc;
24 import java.nio.channels.FileChannel JavaDoc;
25
26 import junit.framework.TestCase;
27
28 /**
29  * Some tests to check out the </code>java.lang.nio</code> functionality
30  *
31  * @author Derek Hulley
32  */

33 public class FileIOTest extends TestCase
34 {
35     private static final String JavaDoc TEST_CONTENT = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
36
37     private File JavaDoc file;
38     
39     public FileIOTest(String JavaDoc name)
40     {
41         super(name);
42     }
43     
44     public void setUp() throws Exception JavaDoc
45     {
46         file = File.createTempFile(getName(), ".txt");
47         OutputStream JavaDoc os = new FileOutputStream JavaDoc(file);
48         os.write(TEST_CONTENT.getBytes());
49         os.flush();
50         os.close();
51     }
52     
53     /**
54      * Attempt to read the same file using multiple channels concurrently
55      */

56     public void testConcurrentFileReads() throws Exception JavaDoc
57     {
58         // open the file for a read
59
FileInputStream JavaDoc isA = new FileInputStream JavaDoc(file);
60         FileInputStream JavaDoc isB = new FileInputStream JavaDoc(file);
61         
62         // get the channels
63
FileChannel JavaDoc channelA = isA.getChannel();
64         FileChannel JavaDoc channelB = isB.getChannel();
65         
66         // buffers for reading
67
ByteBuffer JavaDoc bufferA = ByteBuffer.allocate(10);
68         ByteBuffer JavaDoc bufferB = ByteBuffer.allocate(10);
69         
70         // read file into both buffers
71
int countA = 0;
72         int countB = 0;
73         do
74         {
75             countA = channelA.read((ByteBuffer JavaDoc)bufferA.clear());
76             countB = channelB.read((ByteBuffer JavaDoc)bufferB.clear());
77             assertEquals("Should read same number of bytes", countA, countB);
78         } while (countA > 6);
79         
80         // both buffers should be at the same marker 6
81
assertEquals("BufferA marker incorrect", 6, bufferA.position());
82         assertEquals("BufferB marker incorrect", 6, bufferB.position());
83     }
84     
85     public void testConcurrentReadWrite() throws Exception JavaDoc
86     {
87         // open file for a read
88
FileInputStream JavaDoc isRead = new FileInputStream JavaDoc(file);
89         // open file for write
90
FileOutputStream JavaDoc osWrite = new FileOutputStream JavaDoc(file);
91         
92         // get channels
93
FileChannel JavaDoc channelRead = isRead.getChannel();
94         FileChannel JavaDoc channelWrite = osWrite.getChannel();
95         
96         // buffers
97
ByteBuffer JavaDoc bufferRead = ByteBuffer.allocate(26);
98         ByteBuffer JavaDoc bufferWrite = ByteBuffer.wrap(TEST_CONTENT.getBytes());
99         
100         // read - nothing will be read
101
int countRead = channelRead.read(bufferRead);
102         assertEquals("Expected nothing to be read", -1, countRead);
103         // write
104
int countWrite = channelWrite.write(bufferWrite);
105         assertEquals("Not all characters written", 26, countWrite);
106         
107         // close the write side
108
channelWrite.close();
109         
110         // reread
111
countRead = channelRead.read(bufferRead);
112         assertEquals("Expected full read", 26, countRead);
113     }
114 }
115
Popular Tags