You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ds...@apache.org on 2014/11/24 14:24:41 UTC

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

Repository: ambari
Updated Branches:
  refs/heads/trunk 88a872215 -> 855998383


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


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/85599838
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/85599838
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/85599838

Branch: refs/heads/trunk
Commit: 855998383381d80966428b481d4448216956eb6f
Parents: 88a8722
Author: Dmytro Sen <ds...@apache.org>
Authored: Mon Nov 24 15:10:05 2014 +0200
Committer: Dmytro Sen <ds...@apache.org>
Committed: Mon Nov 24 15:10:05 2014 +0200

----------------------------------------------------------------------
 .../server/controller/ControllerModule.java     | 41 +++++++++++++-------
 .../server/orm/InMemoryDefaultTestModule.java   | 31 ++++++++++++++-
 2 files changed, 57 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/85599838/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
index a02f49d..9662669 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
+++ b/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;
   }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/85599838/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java
index 1484698..7c33bba 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java
+++ b/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);
     }