View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.io.hfile.bucket;
20  
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.nio.ByteBuffer;
26  
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   * Basic test for {@link FileIOEngine}
33   */
34  @Category(SmallTests.class)
35  public class TestFileIOEngine {
36    @Test
37    public void testFileIOEngine() throws IOException {
38      int size = 2 * 1024 * 1024; // 2 MB
39      String filePath = "testFileIOEngine";
40      try {
41        FileIOEngine fileIOEngine = new FileIOEngine(filePath, size);
42        for (int i = 0; i < 50; i++) {
43          int len = (int) Math.floor(Math.random() * 100);
44          long offset = (long) Math.floor(Math.random() * size % (size - len));
45          byte[] data1 = new byte[len];
46          for (int j = 0; j < data1.length; ++j) {
47            data1[j] = (byte) (Math.random() * 255);
48          }
49          byte[] data2 = new byte[len];
50          fileIOEngine.write(ByteBuffer.wrap(data1), offset);
51          fileIOEngine.read(ByteBuffer.wrap(data2), offset);
52          for (int j = 0; j < data1.length; ++j) {
53            assertTrue(data1[j] == data2[j]);
54          }
55        }
56      } finally {
57        File file = new File(filePath);
58        if (file.exists()) {
59          file.delete();
60        }
61      }
62  
63    }
64  }