Browse Source

AMBARI-8413 Unit Testing Improvments : Examine AmbariManagementControllerTest (dsen)

Dmytro Sen 10 years ago
parent
commit
8559983833

+ 27 - 14
ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java

@@ -244,7 +244,7 @@ public class ControllerModule extends AbstractModule {
 
     requestStaticInjection(ExecutionCommandWrapper.class);
 
-    bindByAnnotation();
+    bindByAnnotation(null);
   }
 
 
@@ -334,28 +334,39 @@ public class ControllerModule extends AbstractModule {
    * A second example of where this is needed is when classes require static
    * members that are available via injection.
    * <p/>
-   * This currently scans {@code org.apache.ambari.server} for any
-   * {@link EagerSingleton} or {@link StaticallyInject} or {@link AmbariService}
-   * instances.
+   * If {@code beanDefinitions} is empty or null this will scan 
+   * {@code org.apache.ambari.server} (currently) for any {@link EagerSingleton}
+   * or {@link StaticallyInject} or {@link AmbariService} instances.
+   *
+   * @param beanDefinitions the set of bean definitions. If it is empty or
+   *                        {@code null} scan will occur.
+   *
+   * @return the set of bean definitions that was found during scan if
+   *         {@code beanDefinitions} was null or empty. Else original
+   *         {@code beanDefinitions} will be returned.
+   *
    */
+  // Method is protected and returns a set of bean definitions for testing convenience.
   @SuppressWarnings("unchecked")
-  private void bindByAnnotation() {
-    ClassPathScanningCandidateComponentProvider scanner =
-        new ClassPathScanningCandidateComponentProvider(false);
-
+  protected Set<BeanDefinition> bindByAnnotation(Set<BeanDefinition> beanDefinitions) {
     List<Class<? extends Annotation>> classes = Arrays.asList(
         EagerSingleton.class, StaticallyInject.class, AmbariService.class);
 
-    // match only singletons that are eager listeners
-    for (Class<? extends Annotation> cls : classes) {
-      scanner.addIncludeFilter(new AnnotationTypeFilter(cls));
-    }
+    if (null == beanDefinitions || beanDefinitions.size() == 0) {
+      ClassPathScanningCandidateComponentProvider scanner =
+          new ClassPathScanningCandidateComponentProvider(false);
 
-    Set<BeanDefinition> beanDefinitions = scanner.findCandidateComponents("org.apache.ambari.server");
+      // match only singletons that are eager listeners
+      for (Class<? extends Annotation> cls : classes) {
+        scanner.addIncludeFilter(new AnnotationTypeFilter(cls));
+      }
+
+      beanDefinitions = scanner.findCandidateComponents("org.apache.ambari.server");
+    }
 
     if (null == beanDefinitions || beanDefinitions.size() == 0) {
       LOG.warn("No instances of {} found to register", classes);
-      return;
+      return beanDefinitions;
     }
 
     Set<com.google.common.util.concurrent.Service> services =
@@ -404,5 +415,7 @@ public class ControllerModule extends AbstractModule {
 
     ServiceManager manager = new ServiceManager(services);
     bind(ServiceManager.class).toInstance(manager);
+
+    return beanDefinitions;
   }
 }

+ 30 - 1
ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java

@@ -21,10 +21,39 @@ package org.apache.ambari.server.orm;
 import com.google.inject.AbstractModule;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.ControllerModule;
+import org.springframework.beans.factory.config.BeanDefinition;
 
+import java.util.Collections;
 import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
 
 public class InMemoryDefaultTestModule extends AbstractModule {
+
+  /**
+   * Saves all {@link ControllerModule} logic, but changes bean discovery mechanism.
+   * In this implementation scan for {@link org.apache.ambari.server.EagerSingleton}
+   * and {@link org.apache.ambari.server.StaticallyInject} and
+   * {@link org.apache.ambari.server.AmbariService} annotations will not be run for every test.
+   */
+  private static class BeanDefinitionsCachingTestControllerModule extends ControllerModule {
+
+    // Access should be synchronised to allow concurrent test runs.
+    private static final AtomicReference<Set<BeanDefinition>> foundBeanDefinitions
+        = new AtomicReference<Set<BeanDefinition>>(null);
+
+    public BeanDefinitionsCachingTestControllerModule(Properties properties) throws Exception {
+      super(properties);
+    }
+
+    @Override
+    protected Set<BeanDefinition> bindByAnnotation(Set<BeanDefinition> beanDefinitions) {
+      Set<BeanDefinition> newBeanDefinitions = super.bindByAnnotation(foundBeanDefinitions.get());
+      foundBeanDefinitions.compareAndSet(null, Collections.unmodifiableSet(newBeanDefinitions));
+      return null;
+    }
+  }
+
   Properties properties = new Properties();
 
   @Override
@@ -39,7 +68,7 @@ public class InMemoryDefaultTestModule extends AbstractModule {
     properties.setProperty(Configuration.SHARED_RESOURCES_DIR_KEY, "src/test/resources/");
 
     try {
-      install(new ControllerModule(properties));
+      install(new BeanDefinitionsCachingTestControllerModule(properties));
     } catch (Exception e) {
       throw new RuntimeException(e);
     }