You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by st...@apache.org on 2012/04/30 17:47:23 UTC

svn commit: r1332260 - in /hbase/trunk/src/test/java/org/apache/hadoop/hbase: TestCheckTestClasses.java TestHColumnDescriptor.java

Author: stack
Date: Mon Apr 30 15:47:23 2012
New Revision: 1332260

URL: http://svn.apache.org/viewvc?rev=1332260&view=rev
Log:
HBASE-5903 Detect the test classes without categories

Added:
    hbase/trunk/src/test/java/org/apache/hadoop/hbase/TestCheckTestClasses.java
Modified:
    hbase/trunk/src/test/java/org/apache/hadoop/hbase/TestHColumnDescriptor.java

Added: hbase/trunk/src/test/java/org/apache/hadoop/hbase/TestCheckTestClasses.java
URL: http://svn.apache.org/viewvc/hbase/trunk/src/test/java/org/apache/hadoop/hbase/TestCheckTestClasses.java?rev=1332260&view=auto
==============================================================================
--- hbase/trunk/src/test/java/org/apache/hadoop/hbase/TestCheckTestClasses.java (added)
+++ hbase/trunk/src/test/java/org/apache/hadoop/hbase/TestCheckTestClasses.java Mon Apr 30 15:47:23 2012
@@ -0,0 +1,141 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase;
+
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runners.Suite;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+import static junit.framework.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+
+/**
+ * Checks tests are categorized.
+ */
+@Category(SmallTests.class)
+public class TestCheckTestClasses {
+
+  /**
+   * Throws an assertion if we find a test class without category (small/medium/large).
+   * List all the test classes without category in the assertion message.
+   */
+  @Test
+  public void checkClasses() throws Exception {
+    List<Class<?>> badClasses = new java.util.ArrayList<Class<?>>();
+
+    for (Class<?> c : findTestClasses()) {
+      if (!existCategoryAnnotation(c)) {
+        badClasses.add(c);
+      }
+    }
+
+    assertTrue("There are " + badClasses.size() + " test classes without category: "
+      + badClasses, badClasses.isEmpty());
+  }
+
+
+  private boolean existCategoryAnnotation(Class<?> c) {
+    return (c.getAnnotation(Category.class) != null);
+  }
+
+  /*
+  * A class is considered as a test class if:
+   *  - it's not Abstract AND
+   *  - one or more of its methods is annotated with org.junit.Test OR
+   *  - the class is annotated with Suite.SuiteClasses
+  * */
+  private boolean isTestClass(Class<?> c) {
+    if (Modifier.isAbstract(c.getModifiers())) {
+      return false;
+    }
+
+    if (c.getAnnotation(Suite.SuiteClasses.class) != null) {
+      return true;
+    }
+
+    for (Method met : c.getMethods()) {
+      if (met.getAnnotation(Test.class) != null) {
+        return true;
+      }
+    }
+
+    return false;
+  }
+
+
+  private List<Class<?>> findTestClasses() throws ClassNotFoundException, IOException {
+    final String packageName = "org.apache.hadoop.hbase";
+    final String path = packageName.replace('.', '/');
+
+    Enumeration<URL> resources = this.getClass().getClassLoader().getResources(path);
+    List<File> dirs = new ArrayList<File>();
+
+    while (resources.hasMoreElements()) {
+      URL resource = resources.nextElement();
+      dirs.add(new File(resource.getFile()));
+    }
+
+    List<Class<?>> classes = new ArrayList<Class<?>>();
+    for (File directory : dirs) {
+      classes.addAll(findTestClasses(directory, packageName));
+    }
+
+    return classes;
+  }
+
+
+  private List<Class<?>> findTestClasses(File baseDirectory, String packageName)
+    throws ClassNotFoundException {
+    List<Class<?>> classes = new ArrayList<Class<?>>();
+    if (!baseDirectory.exists()) {
+      return classes;
+    }
+
+    File[] files = baseDirectory.listFiles();
+    assertNotNull(files);
+
+    for (File file : files) {
+      final String fileName = file.getName();
+      if (file.isDirectory()) {
+        classes.addAll(findTestClasses(file, packageName + "." + fileName));
+      } else if (fileName.endsWith(".class") && fileName.startsWith("Test")) {
+        Class<?> c = Class.forName(
+          packageName + '.' + fileName.substring(0, fileName.length() - 6),
+          false,
+          this.getClass().getClassLoader());
+
+        if (isTestClass(c)) {
+          classes.add(c);
+        }
+      }
+    }
+
+    return classes;
+  }
+}

Modified: hbase/trunk/src/test/java/org/apache/hadoop/hbase/TestHColumnDescriptor.java
URL: http://svn.apache.org/viewvc/hbase/trunk/src/test/java/org/apache/hadoop/hbase/TestHColumnDescriptor.java?rev=1332260&r1=1332259&r2=1332260&view=diff
==============================================================================
--- hbase/trunk/src/test/java/org/apache/hadoop/hbase/TestHColumnDescriptor.java (original)
+++ hbase/trunk/src/test/java/org/apache/hadoop/hbase/TestHColumnDescriptor.java Mon Apr 30 15:47:23 2012
@@ -18,10 +18,12 @@
 package org.apache.hadoop.hbase;
 
 import static org.junit.Assert.assertEquals;
+import org.junit.experimental.categories.Category;
 
 import org.junit.Test;
 
 /** Tests the HColumnDescriptor with appropriate arguments */
+@Category(SmallTests.class)
 public class TestHColumnDescriptor {
 
   @Test