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