Ejemplo n.º 1
0
 private void addAttributes(StringBuilder builder, Map<String, Object> attributes) {
   //
   // This feels a lot like a hack.
   //
   if (attributes != null && !attributes.isEmpty()) {
     builder.append("?aura.attributes=");
     builder.append(AuraTextUtil.urlencode(JsonEncoder.serialize(attributes, false, false)));
   }
 }
Ejemplo n.º 2
0
 /**
  * Having duplicate controller methods What if there are two actions in the javascript controller
  * with the same name.
  */
 public void testDuplicateJSController() throws Exception {
   DefDescriptor<ControllerDef> descriptor =
       DefDescriptorImpl.getInstance("js://test.testDuplicateJSController", ControllerDef.class);
   Source<?> source = getJavascriptSourceLoader().getSource(descriptor);
   Definition controller = parser.parse(descriptor, source);
   assertTrue(controller instanceof JavascriptControllerDef);
   JavascriptControllerDef obj = (JavascriptControllerDef) controller;
   Map<String, JavascriptActionDef> controllerActions = obj.getActionDefs();
   assertTrue(controllerActions.containsKey("functionName"));
   // If we have more than one controller function with same name, the
   // later one will replace the previous one
   assertTrue(controllerActions.size() == 1);
   // Verify the only JavascriptAction Def we have
   JavascriptActionDef jsActionDef = null;
   jsActionDef = controllerActions.get("functionName");
   assertEquals(ActionType.CLIENT, jsActionDef.getActionType());
   String[] jsonres = (JsonEncoder.serialize(jsActionDef)).split("\"");
   // Verify the second function did replace the first one
   assertEquals(
       "second function didn't survive", ":function(component) {var v = 2;},", jsonres[10]);
 }
Ejemplo n.º 3
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);
    }
  }
  @Override
  public void write(T value, Map<String, Object> componentAttributes, Appendable out)
      throws IOException {
    try {

      AuraContext context = Aura.getContextService().getCurrentContext();
      InstanceService instanceService = Aura.getInstanceService();
      RenderingService renderingService = Aura.getRenderingService();
      BaseComponentDef def = value.getDescriptor().getDef();

      ComponentDef templateDef = def.getTemplateDef();
      Map<String, Object> attributes = Maps.newHashMap();

      StringBuilder sb = new StringBuilder();
      writeHtmlStyles(new ArrayList<>(Arrays.asList(Aura.getConfigAdapter().getResetCssURL())), sb);
      attributes.put("auraResetCss", sb.toString());
      sb.setLength(0);
      writeHtmlStyles(AuraServlet.getStyles(), sb);
      attributes.put("auraStyleTags", sb.toString());
      sb.setLength(0);
      writeHtmlScripts(AuraServlet.getScripts(), sb);
      DefDescriptor<StyleDef> styleDefDesc = templateDef.getStyleDescriptor();
      if (styleDefDesc != null) {
        attributes.put("auraInlineStyle", styleDefDesc.getDef().getCode());
      }

      String contextPath = context.getContextPath();
      Mode mode = context.getMode();

      if (mode.allowLocalRendering() && def.isLocallyRenderable()) {
        BaseComponent<?, ?> cmp =
            (BaseComponent<?, ?>) instanceService.getInstance(def, componentAttributes);

        attributes.put("body", Lists.<BaseComponent<?, ?>>newArrayList(cmp));
        attributes.put("bodyClass", "");
        attributes.put("autoInitialize", "false");

        Component template = instanceService.getInstance(templateDef.getDescriptor(), attributes);

        renderingService.render(template, out);
      } else {

        attributes.put("auraScriptTags", sb.toString());
        Map<String, Object> auraInit = Maps.newHashMap();
        if (componentAttributes != null && !componentAttributes.isEmpty()) {
          auraInit.put("attributes", componentAttributes);
        }
        auraInit.put("descriptor", def.getDescriptor());
        auraInit.put("deftype", def.getDescriptor().getDefType());
        auraInit.put("host", contextPath);

        attributes.put("autoInitialize", "false");
        attributes.put("autoInitializeSync", "true");

        auraInit.put("instance", value);
        auraInit.put("token", AuraBaseServlet.getToken());

        StringBuilder contextWriter = new StringBuilder();
        Aura.getSerializationService()
            .write(context, null, AuraContext.class, contextWriter, "JSON");
        auraInit.put("context", new Literal(contextWriter.toString()));

        attributes.put("auraInitSync", JsonEncoder.serialize(auraInit));

        Component template = instanceService.getInstance(templateDef.getDescriptor(), attributes);
        renderingService.render(template, out);
      }
    } catch (QuickFixException e) {
      throw new AuraRuntimeException(e);
    }
  }