@Override
 public void tearDown() throws Exception {
   ContextService contextService = Aura.getContextService();
   AuraContext context = contextService.getCurrentContext();
   if (context != null) {
     contextService.endContext();
   }
   super.tearDown();
 }
    @SuppressWarnings("unchecked")
    public NamespacePerfTestSuite(String namespace) throws Exception {
      super(namespace);
      ContextService contextService = Aura.getContextService();
      DefinitionService definitionService = Aura.getDefinitionService();

      boolean contextStarted = false;
      if (!contextService.isEstablished()) {
        contextStarted = true;
        contextService.startContext(Mode.PTEST, Format.JSON, Authentication.AUTHENTICATED);
      }

      Map<String, TestSuite> subSuites = Maps.newHashMap();

      try {
        DescriptorFilter matcher =
            new DescriptorFilter(String.format("markup://%s:*", namespace), DefType.COMPONENT);

        Set<DefDescriptor<?>> descriptors = definitionService.find(matcher);

        for (DefDescriptor<?> descriptor : descriptors) {
          if (((ComponentDef) descriptor.getDef()).isAbstract()
              || getBlacklistedComponents().contains(descriptor.getQualifiedName())) {
            continue;
          }

          Test test;
          try {
            test = new ComponentTestSuite((DefDescriptor<ComponentDef>) descriptor);
          } catch (Throwable t) {
            test = new FailTestCase(descriptor, t);
          }

          String testNamespace = descriptor.getNamespace();
          if (namespace.equals(testNamespace)) {
            addTest(test);
          } else {
            TestSuite subSuite = subSuites.get(testNamespace);
            if (subSuite == null) {
              subSuite = new TestSuite(testNamespace);
              subSuites.put(testNamespace, subSuite);
              addTest(subSuite);
            }
            subSuite.addTest(test);
          }
        }
      } catch (Throwable t) {
        LOG.log(Level.WARNING, "Failed to load component tests for namespace: " + namespace, t);
      } finally {
        if (contextStarted) {
          contextService.endContext();
        }
      }
    }
 /**
  * @return Returns the code.
  * @throws QuickFixException
  */
 public String getCode() throws QuickFixException {
   ContextService contextService = Aura.getContextService();
   boolean isEstablished = contextService.isEstablished();
   if (!isEstablished) {
     contextService.startContext(Mode.AUTOJSTEST, Format.JSON, Authentication.AUTHENTICATED);
   }
   try {
     return descriptor.getDef().getCode();
   } finally {
     if (!isEstablished) {
       contextService.endContext();
     }
   }
 }
    public NamespaceTestSuite(String namespace) throws Exception {
      super(namespace);
      ContextService contextService = Aura.getContextService();
      DefinitionService definitionService = Aura.getDefinitionService();

      boolean contextStarted = false;
      if (!contextService.isEstablished()) {
        contextStarted = true;
        contextService.startContext(Mode.JSTEST, Format.JSON, Authentication.AUTHENTICATED);
      }

      Map<String, TestSuite> subSuites = new HashMap<String, TestSuite>();
      try {
        DefDescriptor<TestSuiteDef> matcher =
            definitionService.getDefDescriptor(
                String.format("js://%s.*", namespace), TestSuiteDef.class);
        Set<DefDescriptor<TestSuiteDef>> descriptors = definitionService.find(matcher);

        for (DefDescriptor<TestSuiteDef> descriptor : descriptors) {
          Test test;
          try {
            test = new ComponentTestSuite(descriptor.getDef());
          } catch (Throwable t) {
            test = new FailTestCase(descriptor, t);
          }
          String testNamespace = descriptor.getNamespace();
          if (namespace.equals(testNamespace)) {
            addTest(test);
          } else {
            TestSuite subSuite = subSuites.get(testNamespace);
            if (subSuite == null) {
              subSuite = new TestSuite(testNamespace);
              subSuites.put(testNamespace, subSuite);
              addTest(subSuite);
            }
            subSuite.addTest(test);
          }
        }
      } catch (Throwable t) {
        System.err.println("Failed to load component tests for namespace: " + namespace);
        t.printStackTrace();
      } finally {
        if (contextStarted) {
          contextService.endContext();
        }
      }
    }