2 * Copyright 2015-present Open Networking Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.atomix.storage.buffer;
18 import org.junit.AfterClass;
19 import org.junit.Test;
22 import java.nio.file.Files;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
31 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
33 public class FileBufferTest extends BufferTest {
35 public static void afterTest() {
36 FileTesting.cleanFiles();
40 protected Buffer createBuffer(int capacity) {
41 return FileBuffer.allocate(FileTesting.createFile(), capacity);
45 protected Buffer createBuffer(int capacity, int maxCapacity) {
46 return FileBuffer.allocate(FileTesting.createFile(), capacity, maxCapacity);
50 public void testFileToHeapBuffer() {
51 File file = FileTesting.createFile();
52 try (FileBuffer buffer = FileBuffer.allocate(file, 16)) {
53 buffer.writeLong(10).writeLong(11).flip();
54 byte[] bytes = new byte[16];
55 buffer.read(bytes).rewind();
56 HeapBuffer heapBuffer = HeapBuffer.wrap(bytes);
57 assertEquals(buffer.readLong(), heapBuffer.readLong());
58 assertEquals(buffer.readLong(), heapBuffer.readLong());
63 * Rests reopening a file that has been closed.
66 public void testPersist() {
67 File file = FileTesting.createFile();
68 try (FileBuffer buffer = FileBuffer.allocate(file, 16)) {
69 buffer.writeLong(10).writeLong(11).flip();
70 assertEquals(10, buffer.readLong());
71 assertEquals(11, buffer.readLong());
73 try (FileBuffer buffer = FileBuffer.allocate(file, 16)) {
74 assertEquals(10, buffer.readLong());
75 assertEquals(11, buffer.readLong());
80 * Tests deleting a file.
83 public void testDelete() {
84 File file = FileTesting.createFile();
85 FileBuffer buffer = FileBuffer.allocate(file, 16);
86 buffer.writeLong(10).writeLong(11).flip();
87 assertEquals(10, buffer.readLong());
88 assertEquals(11, buffer.readLong());
89 assertTrue(Files.exists(file.toPath()));
91 assertFalse(Files.exists(file.toPath()));