@Override
 public void tearDown() throws Exception {
   ContextService contextService = Aura.getContextService();
   AuraContext context = contextService.getCurrentContext();
   if (context != null) {
     contextService.endContext();
   }
   super.tearDown();
 }
 /** Start a context and set up default values. */
 protected AuraContext setupContext(
     Mode mode, Format format, DefDescriptor<? extends BaseComponentDef> desc)
     throws QuickFixException {
   ContextService contextService = Aura.getContextService();
   AuraContext ctxt =
       contextService.startContext(mode, format, Authentication.AUTHENTICATED, desc);
   ctxt.setFrameworkUID(Aura.getConfigAdapter().getAuraFrameworkNonce());
   String uid = ctxt.getDefRegistry().getUid(null, desc);
   ctxt.addLoaded(desc, uid);
   return ctxt;
 }
    @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();
        }
      }
    }
 @Override
 public void setUp() throws Exception {
   super.setUp();
   ContextService contextService = Aura.getContextService();
   AuraContext context = contextService.getCurrentContext();
   if (context == null) {
     contextService.startContext(
         AuraContext.Mode.SELENIUM,
         AuraContext.Format.HTML,
         AuraContext.Authentication.AUTHENTICATED);
   }
   clientLibraryService = Aura.getClientLibraryService();
 }
 /**
  * @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();
        }
      }
    }
Exemple #7
0
  @Override
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
      throws ServletException, IOException {

    if (!Aura.getConfigAdapter().isTestAllowed()) {
      chain.doFilter(req, res);
      return;
    }

    TestContextAdapter testContextAdapter = Aura.get(TestContextAdapter.class);
    if (testContextAdapter == null) {
      chain.doFilter(req, res);
      return;
    }

    // Check for requests to execute a JSTest, i.e. initial component GETs with particular
    // parameters.
    HttpServletRequest request = (HttpServletRequest) req;
    if ("GET".equals(request.getMethod())) {
      String contextPath = request.getContextPath();
      String uri = request.getRequestURI();
      String browserType = request.getParameter("aura.browserType");
      if (browserType == null) {
        // read it from request header
        String ua = request.getHeader(HttpHeaders.USER_AGENT);
        if (ua != null) {
          ua = ua.toLowerCase();
          if (ua.contains("chrome")) {
            browserType = "GOOGLECHROME";
          } else if (ua.contains("safari")) {
            browserType = "SAFARI";
          } else if (ua.contains("firefox")) {
            browserType = "FIREFOX";
          } else if (ua.contains("ipad")) {
            browserType = "IPAD";
          } else if (ua.contains("iphone")) {
            browserType = "IPHONE";
          } else if (ua.contains("msie 10")) {
            browserType = "IE10";
          } else if (ua.contains("msie 9")) {
            browserType = "IE9";
          } else if (ua.contains("msie 8")) {
            browserType = "IE8";
          } else if (ua.contains("msie 7")) {
            browserType = "IE7";
          } else if (ua.contains("msie 6")) {
            browserType = "IE6";
          } else if (ua.contains("trident/7.0")) {
            browserType = "IE11";
          } else if (ua.contains("edge/12")) {
            browserType = "IE12";
          } else {
            browserType = "OTHER";
          }
        }
      }
      String path;
      if (uri.startsWith(contextPath)) {
        path = uri.substring(contextPath.length());
      } else {
        path = uri;
      }
      Matcher matcher = AuraRewriteFilter.DESCRIPTOR_PATTERN.matcher(path);
      if (matcher.matches()) {
        // Extract the target component since AuraContext usually does not have the app descriptor
        // set yet.
        DefType type = "app".equals(matcher.group(3)) ? DefType.APPLICATION : DefType.COMPONENT;
        String namespace = matcher.group(1);
        String name = matcher.group(2);
        DefDescriptor<?> targetDescriptor =
            Aura.getDefinitionService()
                .getDefDescriptor(
                    String.format("%s:%s", namespace, name), type.getPrimaryInterface());

        // Check if a single jstest is being requested.
        String testToRun = jstestToRun.get(request);
        if (testToRun != null && !testToRun.isEmpty()) {
          AuraContext context = Aura.getContextService().getCurrentContext();
          Format format = context.getFormat();
          switch (format) {
            case HTML:
              TestCaseDef testDef;
              TestContext testContext;
              String targetUri;
              try {
                TestSuiteDef suiteDef = getTestSuite(targetDescriptor);
                testDef = getTestCase(suiteDef, testToRun);
                testDef.validateDefinition();
                testDef.setCurrentBrowser(browserType);
                testContextAdapter.getTestContext(testDef.getQualifiedName());
                testContextAdapter.release();
                testContext = testContextAdapter.getTestContext(testDef.getQualifiedName());
                targetUri = buildJsTestTargetUri(targetDescriptor, testDef);
              } catch (QuickFixException e) {
                ((HttpServletResponse) res).setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                res.setCharacterEncoding(AuraBaseServlet.UTF_ENCODING);
                res.getWriter().append(e.getMessage());
                Aura.getExceptionAdapter().handleException(e);
                return;
              }

              // Load any test mocks.
              Collection<Definition> mocks = testDef.getLocalDefs();
              testContext.getLocalDefs().addAll(mocks);
              loadTestMocks(context, true, testContext.getLocalDefs());

              // Capture the response and inject tags to load jstest.
              String capturedResponse = captureResponse(req, res, targetUri);
              if (capturedResponse != null) {
                res.setCharacterEncoding(AuraBaseServlet.UTF_ENCODING);
                if (!Aura.getContextService().isEstablished()) {
                  // There was an error in the original response, so just write the response out.
                  res.getWriter().write(capturedResponse);
                } else {
                  String testTag =
                      buildJsTestScriptTag(targetDescriptor, testToRun, capturedResponse);
                  injectScriptTags(res.getWriter(), capturedResponse, testTag);
                }
                return;
              }
            case JS:
              res.setCharacterEncoding(AuraBaseServlet.UTF_ENCODING);
              writeJsTestScript(res.getWriter(), targetDescriptor, testToRun);
              return;
            default:
              // Pass it on.
          }
        }

        // aurajstest:jstest app is invokable in the following ways:
        // ?aura.mode=JSTEST - run all tests
        // ?aura.mode JSTEST&test=XXX - run single test
        // ?aura.jstest - run all tests
        // ?aura.jstest=XXX - run single test
        // ?aura.jstestrun - run all tests
        // TODO: delete JSTEST mode
        String jstestAppRequest = jstestAppFlag.get(request);
        Mode mode = AuraContextFilter.mode.get(request, Mode.PROD);
        if (mode == Mode.JSTEST
            || mode == Mode.JSTESTDEBUG
            || jstestAppRequest != null
            || testToRun != null) {

          mode = mode.toString().endsWith("DEBUG") ? Mode.AUTOJSTESTDEBUG : Mode.AUTOJSTEST;

          String qs = String.format("descriptor=%s:%s&defType=%s", namespace, name, type.name());
          String testName = null;
          if (jstestAppRequest != null && !jstestAppRequest.isEmpty()) {
            testName = jstestAppRequest;
          } else if (testToRun != null && !testToRun.isEmpty()) {
            testName = testToRun;
          }
          if (testName != null) {
            qs = qs + "&test=" + testName;
          }

          String newUri =
              createURI(
                  "aurajstest",
                  "jstest",
                  DefType.APPLICATION,
                  mode,
                  Authentication.AUTHENTICATED.name(),
                  qs);
          RequestDispatcher dispatcher =
              servletContext.getContext(newUri).getRequestDispatcher(newUri);
          if (dispatcher != null) {
            dispatcher.forward(req, res);
            return;
          }
        }
      }
    }

    // Handle mock definitions specified in the tests.
    TestContext testContext = getTestContext(request);
    if (testContext == null) {
      // During manual testing, the test context adapter may not always get cleared.
      testContextAdapter.clear();
    } else {
      ContextService contextService = Aura.getContextService();
      if (!contextService.isEstablished()) {
        LOG.error("Aura context is not established! New context will NOT be created.");
        chain.doFilter(req, res);
        return;
      }
      AuraContext context = contextService.getCurrentContext();

      // Reset mocks if requested, or for the initial GET.
      boolean doResetMocks = testReset.get(request, Format.HTML.equals(context.getFormat()));
      loadTestMocks(context, doResetMocks, testContext.getLocalDefs());
    }
    chain.doFilter(req, res);
  }