001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.mapreduce;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.ArrayList;
023import java.util.List;
024import org.apache.hadoop.fs.FileStatus;
025import org.apache.hadoop.fs.LocatedFileStatus;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.testclassification.MapReduceTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.mockito.Mockito;
035
036@Category({ MapReduceTests.class, SmallTests.class })
037public class TestWALInputFormat {
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestWALInputFormat.class);
041
042  /**
043   * Test the primitive start/end time filtering.
044   */
045  @Test
046  public void testAddFile() {
047    List<FileStatus> lfss = new ArrayList<>();
048    LocatedFileStatus lfs = Mockito.mock(LocatedFileStatus.class);
049    long now = EnvironmentEdgeManager.currentTime();
050    Mockito.when(lfs.getPath()).thenReturn(new Path("/name." + now));
051    WALInputFormat.addFile(lfss, lfs, now, now);
052    assertEquals(1, lfss.size());
053    WALInputFormat.addFile(lfss, lfs, now - 1, now - 1);
054    assertEquals(1, lfss.size());
055    WALInputFormat.addFile(lfss, lfs, now - 2, now - 1);
056    assertEquals(1, lfss.size());
057    WALInputFormat.addFile(lfss, lfs, now - 2, now);
058    assertEquals(2, lfss.size());
059    WALInputFormat.addFile(lfss, lfs, Long.MIN_VALUE, now);
060    assertEquals(3, lfss.size());
061    WALInputFormat.addFile(lfss, lfs, Long.MIN_VALUE, Long.MAX_VALUE);
062    assertEquals(4, lfss.size());
063    WALInputFormat.addFile(lfss, lfs, now, now + 2);
064    assertEquals(5, lfss.size());
065    WALInputFormat.addFile(lfss, lfs, now + 1, now + 2);
066    assertEquals(5, lfss.size());
067    Mockito.when(lfs.getPath()).thenReturn(new Path("/name"));
068    WALInputFormat.addFile(lfss, lfs, Long.MIN_VALUE, Long.MAX_VALUE);
069    assertEquals(6, lfss.size());
070    Mockito.when(lfs.getPath()).thenReturn(new Path("/name.123"));
071    WALInputFormat.addFile(lfss, lfs, Long.MIN_VALUE, Long.MAX_VALUE);
072    assertEquals(7, lfss.size());
073    Mockito.when(lfs.getPath()).thenReturn(new Path("/name." + now + ".meta"));
074    WALInputFormat.addFile(lfss, lfs, now, now);
075    assertEquals(8, lfss.size());
076  }
077}