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;
019
020import static junit.framework.TestCase.assertEquals;
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.List;
024import org.apache.hadoop.hbase.testclassification.SmallTests;
025import org.junit.ClassRule;
026import org.junit.Test;
027import org.junit.experimental.categories.Category;
028import org.junit.runner.RunWith;
029import org.junit.runners.Parameterized;
030import org.junit.runners.Parameterized.Parameters;
031import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
032
033/**
034 * Tests HBaseClassTestRule.
035 */
036@Category(SmallTests.class)
037public class TestHBaseClassTestRule {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(
041      TestHBaseClassTestRule.class);
042
043  // Test input classes of various kinds.
044  private static class NonParameterizedClass {
045    void dummy() {
046    }
047    int dummy(int a) {
048      return 0;
049    }
050  }
051
052  @RunWith(Parameterized.class)
053  private static class ParameterizedClassWithNoParametersMethod {
054    void dummy() {
055    }
056  }
057
058  @RunWith(Parameterized.class)
059  private static class InValidParameterizedClass {
060    // Not valid because parameters method is private.
061    @Parameters
062    private static List<Object> parameters() {
063      return Arrays.asList(1, 2, 3, 4);
064    }
065    int dummy(int a) {
066      return 0;
067    }
068  }
069
070  @RunWith(Parameterized.class)
071  private static class ValidParameterizedClass1 {
072    @Parameters
073    public static List<Object> parameters() {
074      return Arrays.asList(1, 2, 3, 4, 5);
075    }
076    int dummy(int a) {
077      return 0;
078    }
079  }
080
081  @RunWith(Parameterized.class)
082  private static class ValidParameterizedClass2 {
083    @Parameters
084    public static Object[] parameters() {
085      return new Integer[] {1, 2, 3, 4, 5, 6};
086    }
087  }
088
089  @RunWith(Parameterized.class)
090  private static class ValidParameterizedClass3 {
091    @Parameters
092    public static Iterable<Integer> parameters() {
093      return Arrays.asList(1, 2, 3, 4, 5, 6, 7);
094    }
095  }
096
097  @RunWith(Parameterized.class)
098  private static class ValidParameterizedClass4 {
099    @Parameters
100    public static Collection<Integer> parameters() {
101      return Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
102    }
103  }
104
105
106  @RunWith(Parameterized.class)
107  private static class ExtendedParameterizedClass1 extends ValidParameterizedClass1 {
108    // Should be inferred from the parent class.
109    int dummy2(int a) {
110      return 0;
111    }
112  }
113
114  @RunWith(Parameterized.class)
115  private static class ExtendedParameterizedClass2 extends ValidParameterizedClass1 {
116    // Should override the parent parameters class.
117    @Parameters
118    public static List<Object> parameters() {
119      return Arrays.asList(1, 2, 3);
120    }
121  }
122
123  @Test
124  public void testNumParameters() {
125    // Invalid cases, expected to return 1.
126    assertEquals(HBaseClassTestRule.getNumParameters(NonParameterizedClass.class), 1);
127    assertEquals(HBaseClassTestRule.getNumParameters(
128        ParameterizedClassWithNoParametersMethod.class), 1);
129    assertEquals(HBaseClassTestRule.getNumParameters(InValidParameterizedClass.class), 1);
130    // Valid parameterized classes.
131    assertEquals(HBaseClassTestRule.getNumParameters(ValidParameterizedClass1.class),
132        ValidParameterizedClass1.parameters().size());
133    assertEquals(HBaseClassTestRule.getNumParameters(ValidParameterizedClass2.class),
134        ValidParameterizedClass2.parameters().length);
135    assertEquals(HBaseClassTestRule.getNumParameters(ValidParameterizedClass3.class),
136        Iterables.size(ValidParameterizedClass3.parameters()));
137    assertEquals(HBaseClassTestRule.getNumParameters(ValidParameterizedClass4.class),
138        ValidParameterizedClass4.parameters().size());
139    // Testing inheritance.
140    assertEquals(HBaseClassTestRule.getNumParameters(ExtendedParameterizedClass1.class),
141        ValidParameterizedClass1.parameters().size());
142    assertEquals(HBaseClassTestRule.getNumParameters(ExtendedParameterizedClass2.class),
143        ExtendedParameterizedClass2.parameters().size());
144  }
145}