Remove atomix.storage.statistics
[controller.git] / third-party / atomix / storage / src / test / java / io / atomix / storage / buffer / MappedBufferTest.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  * Mapped buffer test.
30  *
31  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
32  */
33 public class MappedBufferTest extends BufferTest {
34   @AfterClass
35   public static void afterTest() {
36     FileTesting.cleanFiles();
37   }
38
39   @Override
40   protected Buffer createBuffer(int capacity) {
41     return MappedBuffer.allocate(FileTesting.createFile(), capacity);
42   }
43
44   @Override
45   protected Buffer createBuffer(int capacity, int maxCapacity) {
46     return MappedBuffer.allocate(FileTesting.createFile(), capacity, maxCapacity);
47   }
48
49   /**
50    * Rests reopening a file that has been closed.
51    */
52   @Test
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());
59     }
60     try (MappedBuffer buffer = MappedBuffer.allocate(file, 16)) {
61       assertEquals(10, buffer.readLong());
62       assertEquals(11, buffer.readLong());
63     }
64   }
65
66   /**
67    * Tests deleting a file.
68    */
69   @Test
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()));
77     buffer.delete();
78     assertFalse(Files.exists(file.toPath()));
79   }
80
81 }