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  private static class InValidParameterizedClass {
063    // Not valid because parameters method is private.
064    @Parameters
065    private static List<Object> parameters() {
066      return Arrays.asList(1, 2, 3, 4);
067    }
068
069    int dummy(int a) {
070      return 0;
071    }
072  }
073
074  @RunWith(Parameterized.class)
075  private static class ValidParameterizedClass1 {
076    @Parameters
077    public static List<Object> parameters() {
078      return Arrays.asList(1, 2, 3, 4, 5);
079    }
080
081    int dummy(int a) {
082      return 0;
083    }
084  }
085
086  @RunWith(Parameterized.class)
087  private static class ValidParameterizedClass2 {
088    @Parameters
089    public static Object[] parameters() {
090      return new Integer[] { 1, 2, 3, 4, 5, 6 };
091    }
092  }
093
094  @RunWith(Parameterized.class)
095  private static class ValidParameterizedClass3 {
096    @Parameters
097    public static Iterable<Integer> parameters() {
098      return Arrays.asList(1, 2, 3, 4, 5, 6, 7);
099    }
100  }
101
102  @RunWith(Parameterized.class)
103  private static class ValidParameterizedClass4 {
104    @Parameters
105    public static Collection<Integer> parameters() {
106      return Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
107    }
108  }
109
110  @RunWith(Parameterized.class)
111  private static class ExtendedParameterizedClass1 extends ValidParameterizedClass1 {
112    // Should be inferred from the parent class.
113    int dummy2(int a) {
114      return 0;
115    }
116  }
117
118  @RunWith(Parameterized.class)
119  private static class ExtendedParameterizedClass2 extends ValidParameterizedClass1 {
120    // Should override the parent parameters class.
121    @Parameters
122    public static List<Object> parameters() {
123      return Arrays.asList(1, 2, 3);
124    }
125  }
126
127  @Test
128  public void testNumParameters() {
129    // Invalid cases, expected to return 1.
130    assertEquals(HBaseClassTestRule.getNumParameters(NonParameterizedClass.class), 1);
131    assertEquals(
132      HBaseClassTestRule.getNumParameters(ParameterizedClassWithNoParametersMethod.class), 1);
133    assertEquals(HBaseClassTestRule.getNumParameters(InValidParameterizedClass.class), 1);
134    // Valid parameterized classes.
135    assertEquals(HBaseClassTestRule.getNumParameters(ValidParameterizedClass1.class),
136      ValidParameterizedClass1.parameters().size());
137    assertEquals(HBaseClassTestRule.getNumParameters(ValidParameterizedClass2.class),
138      ValidParameterizedClass2.parameters().length);
139    assertEquals(HBaseClassTestRule.getNumParameters(ValidParameterizedClass3.class),
140      Iterables.size(ValidParameterizedClass3.parameters()));
141    assertEquals(HBaseClassTestRule.getNumParameters(ValidParameterizedClass4.class),
142      ValidParameterizedClass4.parameters().size());
143    // Testing inheritance.
144    assertEquals(HBaseClassTestRule.getNumParameters(ExtendedParameterizedClass1.class),
145      ValidParameterizedClass1.parameters().size());
146    assertEquals(HBaseClassTestRule.getNumParameters(ExtendedParameterizedClass2.class),
147      ExtendedParameterizedClass2.parameters().size());
148  }
149}