Ejemplo n.º 1
0
 TestCaseDef getTestCase(TestSuiteDef testSuite, String testName) throws QuickFixException {
   for (TestCaseDef currentTestDef : testSuite.getTestCaseDefs()) {
     if (testName.equals(currentTestDef.getName())) {
       currentTestDef.validateDefinition();
       return currentTestDef;
     }
   }
   throw new DefinitionNotFoundException(
       definitionService.getDefDescriptor(testName, TestCaseDef.class));
 }
Ejemplo n.º 2
0
  private void writeJsTestScript(
      PrintWriter out, DefDescriptor<?> targetDescriptor, String testName) throws IOException {
    TestSuiteDef suiteDef;
    TestCaseDef testDef;
    try {
      suiteDef = getTestSuite(targetDescriptor);
      testDef = getTestCase(suiteDef, testName);
      testDef.validateDefinition();
    } catch (QuickFixException e) {
      out.append(String.format("$A.test.run('%s',{},1,'%s');", testName, e.getMessage()));
      return;
    }

    // TODO: Inject test framework here, before the test suite code, separately from framework code.
    final int testTimeout = 90; // TODO: support TestCaseDef override for timeout
    out.append(
        String.format("(function(suite){$A.test.run('%s',suite,%s);})(", testName, testTimeout));
    out.append(suiteDef.getCode());
    out.append("\n);"); // handle trailing single-line comments with newline
  }
 private ComponentTestCase(ComponentTestSuite suite, TestCaseDef caseDef) {
   super("testRun");
   this.name =
       String.format(
           "%s$%s",
           suite.descriptor.getQualifiedName().replaceAll("://", "_"), caseDef.getName());
   this.suite = suite;
   this.caseDef = caseDef;
   for (String browser : caseDef.getBrowsers()) {
     String token = browser.trim().toUpperCase();
     Set<BrowserType> set;
     if (token.charAt(0) == '-') {
       token = token.substring(1);
       set = excludedBrowsers;
     } else {
       set = targetBrowsers;
     }
     try {
       set.add(BrowserType.valueOf(token));
     } catch (IllegalArgumentException e) {
       fail("Unknown BrowserType: " + browser);
     }
   }
 }
Ejemplo n.º 4
0
  private String buildJsTestTargetUri(DefDescriptor<?> targetDescriptor, TestCaseDef testDef)
      throws QuickFixException {

    Map<String, Object> targetAttributes = testDef.getAttributeValues();

    // Force "legacy" style tests until ready
    if (!ENABLE_FREEFORM_TESTS && targetAttributes == null) {
      targetAttributes = ImmutableMap.of();
    }

    if (targetAttributes != null) {
      // The test has attributes specified, so request for the target component with the test's
      // attributes.
      String hash = "";
      List<NameValuePair> newParams = Lists.newArrayList();
      for (Entry<String, Object> entry : targetAttributes.entrySet()) {
        String key = entry.getKey();
        String value;
        if (entry.getValue() instanceof Map<?, ?> || entry.getValue() instanceof List<?>) {
          value = JsonEncoder.serialize(entry.getValue());
        } else {
          value = entry.getValue().toString();
        }
        if (key.equals("__layout")) {
          hash = value;
        } else {
          newParams.add(new BasicNameValuePair(key, value));
        }
      }
      String qs = URLEncodedUtils.format(newParams, "UTF-8") + hash;
      return createURI(
          targetDescriptor.getNamespace(),
          targetDescriptor.getName(),
          targetDescriptor.getDefType(),
          null,
          Authentication.AUTHENTICATED.name(),
          qs);
    } else {
      // Free-form tests will load only the target component's template.
      // TODO: Allow specifying the template on the test.
      // TODO: Load proxy app for cmps, apps must loadApplication.
      final BaseComponentDef originalDef = (BaseComponentDef) targetDescriptor.getDef();
      final ComponentDef targetTemplate = originalDef.getTemplateDef();
      String newDescriptorString =
          String.format("%s$%s", targetDescriptor.getDescriptorName(), testDef.getName());
      final DefDescriptor<ApplicationDef> newDescriptor =
          Aura.getDefinitionService().getDefDescriptor(newDescriptorString, ApplicationDef.class);
      final ApplicationDef dummyDef =
          Aura.getDefinitionService().getDefinition("aurajstest:blank", ApplicationDef.class);
      BaseComponentDef targetDef =
          (BaseComponentDef)
              Proxy.newProxyInstance(
                  originalDef.getClass().getClassLoader(),
                  new Class<?>[] {ApplicationDef.class},
                  new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args)
                        throws Throwable {
                      switch (method.getName()) {
                        case "getDescriptor":
                          return newDescriptor;
                        case "getTemplateDef":
                          return targetTemplate;
                        case "isLocallyRenderable":
                          return method.invoke(originalDef, args);
                        default:
                          return method.invoke(dummyDef, args);
                      }
                    }
                  });
      TestContext testContext =
          Aura.get(TestContextAdapter.class).getTestContext(testDef.getQualifiedName());
      testContext.getLocalDefs().add(targetDef);
      return createURI(
          newDescriptor.getNamespace(),
          newDescriptor.getName(),
          newDescriptor.getDefType(),
          null,
          Authentication.AUTHENTICATED.name(),
          null);
    }
  }
Ejemplo n.º 5
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);
  }