View Javadoc
1   /*
2   Copyright (c) 2012 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.util;
18  
19  import java.io.File;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.nio.ByteBuffer;
23  import java.nio.channels.FileChannel;
24  import java.nio.channels.NonWritableChannelException;
25  import java.util.Arrays;
26  
27  import junit.framework.TestCase;
28  
29  import com.healthmarketscience.jackcess.TestUtil;
30  
31  /**
32   *
33   * @author James Ahlborn
34   */
35  public class MemFileChannelTest extends TestCase 
36  {
37  
38    public MemFileChannelTest(String name) {
39      super(name);
40    }
41  
42    public void testReadOnlyChannel() throws Exception
43    {
44      File testFile = new File("src/test/data/V1997/compIndexTestV1997.mdb");
45      MemFileChannel ch = MemFileChannel.newChannel(testFile, "r");
46      assertEquals(testFile.length(), ch.size());
47      assertEquals(0L, ch.position());
48  
49      try {
50        ByteBuffer bb = ByteBuffer.allocate(1024);
51        ch.write(bb);
52        fail("NonWritableChannelException should have been thrown");
53      } catch(NonWritableChannelException ignored) {
54        // success
55      }
56      
57      try {
58        ch.truncate(0L);
59        fail("NonWritableChannelException should have been thrown");
60      } catch(NonWritableChannelException ignored) {
61        // success
62      }
63      
64      try {
65        ch.transferFrom(null, 0L, 10L);
66        fail("NonWritableChannelException should have been thrown");
67      } catch(NonWritableChannelException ignored) {
68        // success
69      }
70  
71      assertEquals(testFile.length(), ch.size());
72      assertEquals(0L, ch.position());
73  
74      ch.close();
75    }
76  
77    public void testChannel() throws Exception
78    {
79      ByteBuffer bb = ByteBuffer.allocate(1024);
80  
81      MemFileChannel ch = MemFileChannel.newChannel();
82      assertTrue(ch.isOpen());
83      assertEquals(0L, ch.size());
84      assertEquals(0L, ch.position());
85      assertEquals(-1, ch.read(bb));
86      
87      ch.close();
88  
89      assertFalse(ch.isOpen());
90  
91      File testFile = new File("src/test/data/V1997/compIndexTestV1997.mdb");
92      ch = MemFileChannel.newChannel(testFile, "r");
93      assertEquals(testFile.length(), ch.size());
94      assertEquals(0L, ch.position());
95  
96      try {
97        ch.position(-1);
98        fail("IllegalArgumentException should have been thrown");
99      } catch(IllegalArgumentException ignored) {
100       // success
101     }
102     
103     MemFileChannel ch2 = MemFileChannel.newChannel();
104     ch.transferTo(ch2);
105     ch2.force(true);
106     assertEquals(testFile.length(), ch2.size());
107     assertEquals(testFile.length(), ch2.position());
108 
109     try {
110       ch2.truncate(-1L);
111       fail("IllegalArgumentException should have been thrown");
112     } catch(IllegalArgumentException ignored) {
113       // success
114     }
115     
116     long trucSize = ch2.size()/3;
117     ch2.truncate(trucSize);
118     assertEquals(trucSize, ch2.size());
119     assertEquals(trucSize, ch2.position());
120     ch2.position(0L);
121     copy(ch, ch2, bb);
122 
123     File tmpFile = File.createTempFile("chtest_", ".dat");
124     tmpFile.deleteOnExit();
125     FileOutputStream fc = new FileOutputStream(tmpFile);
126 
127     ch2.transferTo(fc);
128 
129     fc.close();
130 
131     assertEquals(testFile.length(), tmpFile.length());
132 
133     assertTrue(Arrays.equals(TestUtil.toByteArray(testFile),
134                              TestUtil.toByteArray(tmpFile)));
135 
136     ch2.truncate(0L);
137     assertTrue(ch2.isOpen());
138     assertEquals(0L, ch2.size());
139     assertEquals(0L, ch2.position());
140     assertEquals(-1, ch2.read(bb));
141 
142     ch2.close();
143     assertFalse(ch2.isOpen());
144   }
145 
146   private static void copy(FileChannel src, FileChannel dst, ByteBuffer bb)
147     throws IOException
148   {
149     src.position(0L);
150     while(true) {
151       bb.clear();
152       if(src.read(bb) < 0) {
153         break;
154       }
155       bb.flip();
156       dst.write(bb);
157     }
158   }
159 
160 }