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;
021import java.util.ArrayList;
022import java.util.List;
023import org.apache.hadoop.fs.FileStatus;
024import org.apache.hadoop.fs.LocatedFileStatus;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.testclassification.MapReduceTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033import org.mockito.Mockito;
034
035@Category({ MapReduceTests.class, SmallTests.class})
036public class TestWALInputFormat {
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039    HBaseClassTestRule.forClass(TestWALInputFormat.class);
040
041  /**
042   * Test the primitive start/end time filtering.
043   */
044  @Test
045  public void testAddFile() {
046    List<FileStatus> lfss = new ArrayList<>();
047    LocatedFileStatus lfs = Mockito.mock(LocatedFileStatus.class);
048    long now = EnvironmentEdgeManager.currentTime();
049    Mockito.when(lfs.getPath()).thenReturn(new Path("/name." + now));
050    WALInputFormat.addFile(lfss, lfs, now, now);
051    assertEquals(1, lfss.size());
052    WALInputFormat.addFile(lfss, lfs, now - 1, now - 1);
053    assertEquals(1, lfss.size());
054    WALInputFormat.addFile(lfss, lfs, now - 2, now - 1);
055    assertEquals(1, lfss.size());
056    WALInputFormat.addFile(lfss, lfs, now - 2, now);
057    assertEquals(2, lfss.size());
058    WALInputFormat.addFile(lfss, lfs, Long.MIN_VALUE, now);
059    assertEquals(3, lfss.size());
060    WALInputFormat.addFile(lfss, lfs, Long.MIN_VALUE, Long.MAX_VALUE);
061    assertEquals(4, lfss.size());
062    WALInputFormat.addFile(lfss, lfs, now, now + 2);
063    assertEquals(5, lfss.size());
064    WALInputFormat.addFile(lfss, lfs, now + 1, now + 2);
065    assertEquals(5, lfss.size());
066    Mockito.when(lfs.getPath()).thenReturn(new Path("/name"));
067    WALInputFormat.addFile(lfss, lfs, Long.MIN_VALUE, Long.MAX_VALUE);
068    assertEquals(6, lfss.size());
069    Mockito.when(lfs.getPath()).thenReturn(new Path("/name.123"));
070    WALInputFormat.addFile(lfss, lfs, Long.MIN_VALUE, Long.MAX_VALUE);
071    assertEquals(7, lfss.size());
072    Mockito.when(lfs.getPath()).thenReturn(new Path("/name." + now + ".meta"));
073    WALInputFormat.addFile(lfss, lfs, now, now);
074    assertEquals(8, lfss.size());
075  }
076}