Import atomix/{storage,utils}
[controller.git] / third-party / atomix / storage / src / test / java / io / atomix / storage / buffer / FileBufferTest.java
1 /*
2  * Copyright 2015-present Open Networking Foundation
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 package io.atomix.storage.buffer;
17
18 import org.junit.AfterClass;
19 import org.junit.Test;
20
21 import java.io.File;
22 import java.nio.file.Files;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
28 /**
29  * File buffer test.
30  *
31  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
32  */
33 public class FileBufferTest extends BufferTest {
34   @AfterClass
35   public static void afterTest() {
36     FileTesting.cleanFiles();
37   }
38
39   @Override
40   protected Buffer createBuffer(int capacity) {
41     return FileBuffer.allocate(FileTesting.createFile(), capacity);
42   }
43
44   @Override
45   protected Buffer createBuffer(int capacity, int maxCapacity) {
46     return FileBuffer.allocate(FileTesting.createFile(), capacity, maxCapacity);
47   }
48
49   @Test
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());
59     }
60   }
61
62   /**
63    * Rests reopening a file that has been closed.
64    */
65   @Test
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());
72     }
73     try (FileBuffer buffer = FileBuffer.allocate(file, 16)) {
74       assertEquals(10, buffer.readLong());
75       assertEquals(11, buffer.readLong());
76     }
77   }
78
79   /**
80    * Tests deleting a file.
81    */
82   @Test
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()));
90     buffer.delete();
91     assertFalse(Files.exists(file.toPath()));
92   }
93
94 }