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