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 MappedBufferTest extends BufferTest {
35 public static void afterTest() {
36 FileTesting.cleanFiles();
40 protected Buffer createBuffer(int capacity) {
41 return MappedBuffer.allocate(FileTesting.createFile(), capacity);
45 protected Buffer createBuffer(int capacity, int maxCapacity) {
46 return MappedBuffer.allocate(FileTesting.createFile(), capacity, maxCapacity);
50 * Rests reopening a file that has been closed.
53 public void testPersist() {
54 File file = FileTesting.createFile();
55 try (MappedBuffer buffer = MappedBuffer.allocate(file, 16)) {
56 buffer.writeLong(10).writeLong(11).flip();
57 assertEquals(10, buffer.readLong());
58 assertEquals(11, buffer.readLong());
60 try (MappedBuffer buffer = MappedBuffer.allocate(file, 16)) {
61 assertEquals(10, buffer.readLong());
62 assertEquals(11, buffer.readLong());
67 * Tests deleting a file.
70 public void testDelete() {
71 File file = FileTesting.createFile();
72 MappedBuffer buffer = MappedBuffer.allocate(file, 16);
73 buffer.writeLong(10).writeLong(11).flip();
74 assertEquals(10, buffer.readLong());
75 assertEquals(11, buffer.readLong());
76 assertTrue(Files.exists(file.toPath()));
78 assertFalse(Files.exists(file.toPath()));