Пример #1
0
 public void testNextId() {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   assertEquals("nextId should be initialized to 1", 1, iStack.getNextId());
   assertEquals("nextId should increment", 2, iStack.getNextId());
   assertEquals("nextId should increment again", 3, iStack.getNextId());
 }
Пример #2
0
 public void testPeekAtStackWithOneReturnsTop() throws Exception {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   Instance<?> ti = new TestInstance();
   iStack.pushInstance(ti, ti.getDescriptor());
   assertEquals("Expecting top of stack", ti, iStack.peek());
 }
Пример #3
0
 public void testGetAccessReturnsPushedAccess() throws Exception {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   Instance<?> ti1 = new TestInstance("path1");
   iStack.pushAccess(ti1);
   assertEquals(ti1, iStack.getAccess());
 }
Пример #4
0
 public void testGetAccessReturnsInstanceWhenNoAccessStack() throws Exception {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   Instance<?> ti1 = new TestInstance("path1");
   iStack.pushInstance(ti1, ti1.getDescriptor());
   assertEquals(ti1, iStack.getAccess());
 }
Пример #5
0
 /** Verify nothing serialized if no registered components */
 public void testSerializeAsPartNoComponents() throws Exception {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   JsonEncoder jsonMock = Mockito.mock(JsonEncoder.class);
   iStack.serializeAsPart(jsonMock);
   assertEquals(
       "Components should empty when no registered components", 0, iStack.getComponents().size());
   verifyZeroInteractions(jsonMock);
 }
Пример #6
0
 public void testPushThenPopAccessSuccess() throws Exception {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   Instance<?> ti1 = new TestInstance("path1");
   Instance<?> ti2 = new TestInstance("path2");
   iStack.pushAccess(ti1);
   iStack.pushAccess(ti2);
   iStack.popAccess(ti2);
   iStack.popAccess(ti1);
   assertNull("Stack should return null after popping only instance", iStack.getAccess());
 }
Пример #7
0
 public void testErrorSetIndexWithoutAttributeSet() {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   TestInstance ti = new TestInstance("instance");
   iStack.pushInstance(ti, ti.getDescriptor());
   try {
     iStack.setAttributeIndex(1);
     fail("Expected error when setting attribute index without setting attribute name first");
   } catch (Exception expected) {
     assertExceptionMessage(expected, AuraRuntimeException.class, "no name when index set");
   }
 }
Пример #8
0
 public void testErrorPushPopDifferentInstances() {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   TestInstance ti = new TestInstance("instance1");
   iStack.pushInstance(ti, ti.getDescriptor());
   try {
     iStack.popInstance(new TestInstance("instance2"));
     fail("Expected error when trying to pop different instance than previously pushed");
   } catch (Exception expected) {
     assertExceptionMessage(expected, AuraRuntimeException.class, "mismatched instance pop");
   }
 }
Пример #9
0
 public void testPopAccessWithDifferentInstanceThrowsError() throws Exception {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   Instance<?> ti1 = new TestInstance("path1");
   Instance<?> ti2 = new TestInstance("path2");
   try {
     iStack.pushAccess(ti1);
     iStack.popAccess(ti2);
     fail("Expected exception when popping access with mismatched instances");
   } catch (Exception e) {
     assertExceptionMessage(e, AuraRuntimeException.class, "mismatched access pop");
   }
 }
Пример #10
0
 public void testErrorSetAttributeNameWithoutClearing() {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   TestInstance ti = new TestInstance("instance");
   iStack.pushInstance(ti, ti.getDescriptor());
   iStack.setAttributeName("first");
   try {
     iStack.setAttributeName("second");
     fail("Expected error when setting second attribute without clearing first");
   } catch (Exception expected) {
     assertExceptionMessage(expected, AuraRuntimeException.class, "Setting name illegally");
   }
 }
Пример #11
0
 public void testPopAccessPastEmptyThrowsError() throws Exception {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   Instance<?> ti1 = new TestInstance("path1");
   try {
     iStack.pushAccess(ti1);
     iStack.popAccess(ti1);
     iStack.popAccess(ti1);
     fail("Expected exception when popping access twice but only pushed instance once");
   } catch (Exception e) {
     assertExceptionMessage(e, AuraRuntimeException.class, "mismatched access pop");
   }
 }
Пример #12
0
  @Override
  public DefDescriptor<?> getCurrentDescriptor() {
    DefDescriptor<?> caller = getCurrentCallingDescriptor();
    if (caller == null) {
      InstanceStack istack = getInstanceStack();
      Instance<?> instance = istack.peek();
      if (instance != null) {
        caller = instance.getDescriptor();
      }
    }

    return caller;
  }
Пример #13
0
 public void testInstanceStack() {
   ActionDef def = Mockito.mock(ActionDef.class);
   Action test = new MyAction(null, def, null);
   InstanceStack iStack = test.getInstanceStack();
   assertEquals(
       "Instance stack should be initialized without action ID as path",
       "/*[0]",
       iStack.getPath());
   assertEquals(
       "Subsequent calls to getInstanceStack should return same InstanceStack",
       iStack,
       test.getInstanceStack());
 }
Пример #14
0
 public void testErrorClearIndexWithoutSettingIndex() {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   TestInstance ti = new TestInstance();
   iStack.pushInstance(ti, ti.getDescriptor());
   iStack.setAttributeName("attribute");
   try {
     iStack.clearAttributeIndex(1);
     fail("Expected error when clearing attribute index before setting an index");
   } catch (Exception expected) {
     assertExceptionMessage(
         expected, AuraRuntimeException.class, "mismatched clearAttributeIndex");
   }
 }
Пример #15
0
 public void testPopInstanceToTopIncrementsIndex() {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   assertEquals("InstanceStack constructor should set path to base", "/*[0]", iStack.getPath());
   TestInstance ti = new TestInstance();
   iStack.pushInstance(ti, ti.getDescriptor());
   iStack.popInstance(ti);
   assertEquals("Popping to top of stack should increment index", "/*[1]", iStack.getPath());
   iStack.pushInstance(ti, ti.getDescriptor());
   iStack.popInstance(ti);
   assertEquals("Popping to top of stack should increment index", "/*[2]", iStack.getPath());
 }
Пример #16
0
  /** Verify registered components are serialized in alphabetical order */
  public void testSerializeAsPart() throws Exception {
    InstanceStack iStack = new InstanceStack();
    iStack.setConfigAdapter(mci);
    JsonEncoder jsonMock = Mockito.mock(JsonEncoder.class);
    BaseComponent<?, ?> a = getComponentWithPath("a");
    BaseComponent<?, ?> b = getComponentWithPath("b");
    BaseComponent<?, ?> c = getComponentWithPath("c");
    iStack.registerComponent(b);
    iStack.registerComponent(c);
    iStack.registerComponent(a);
    iStack.serializeAsPart(jsonMock);

    List<BaseComponent<?, ?>> sorted = Lists.newArrayList();
    sorted.add(a);
    sorted.add(b);
    sorted.add(c);
    verify(jsonMock).writeMapKey("components");
    verify(jsonMock).writeArray(sorted);
  }
Пример #17
0
 public void testMarkParentNoCurrentInstance() {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   assertEquals("InstanceStack constructor should set path to base", "/*[0]", iStack.getPath());
   TestInstance parent = new TestInstance("parentBase");
   iStack.markParent(parent);
   assertEquals(
       "Marking parent should set path to the parent's path", "parentBase", iStack.getPath());
   iStack.clearParent(parent);
   assertEquals(
       "Clearing parent should reset path to original base path", "/*[0]", iStack.getPath());
 }
Пример #18
0
 @Override
 public void registerComponent(BaseComponent<?, ?> component) {
   InstanceStack iStack = getInstanceStack();
   if (iStack.isUnprivileged()) {
     if (componentCount++ > MAX_COMPONENT_COUNT) {
       //
       // This is bad, try to give the poor user an idea of what happened.
       //
       Action tmp = getCurrentAction();
       StringBuffer sb = new StringBuffer();
       if (tmp != null) {
         sb.append(tmp);
         sb.append("(");
         tmp.logParams(new SBKeyValueLogger(sb));
         sb.append(")");
       } else {
         sb.append("request");
       }
       throw new SystemErrorException("Too many components for " + sb.toString());
     }
   }
   iStack.registerComponent(component);
 }
Пример #19
0
  public void testPeekAtStackAfterPopReturnsTop() throws Exception {
    InstanceStack iStack = new InstanceStack();
    iStack.setConfigAdapter(mci);
    Instance<?> ti1 = new TestInstance();
    iStack.pushInstance(ti1, ti1.getDescriptor());

    Instance<?> ti2 = new TestInstance();
    iStack.pushInstance(ti2, ti2.getDescriptor());

    iStack.popInstance(ti2);

    assertEquals("Expecting top of stack", ti1, iStack.peek());
  }
Пример #20
0
 public void testErrorSetIndexWithoutClearingPreviousIndex() {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   TestInstance ti = new TestInstance();
   iStack.pushInstance(ti, ti.getDescriptor());
   iStack.setAttributeName("attribute");
   iStack.setAttributeIndex(42);
   try {
     iStack.setAttributeIndex(43);
     fail("Expected error when setting a new attribute index without clearing previous one");
   } catch (Exception expected) {
     assertExceptionMessage(expected, AuraRuntimeException.class, "missing clearAttributeIndex");
   }
 }
Пример #21
0
  public void testPeekAtEmptiedStackReturnsNull() throws Exception {
    InstanceStack iStack = new InstanceStack();
    iStack.setConfigAdapter(mci);
    Instance<?> ti1 = new TestInstance();
    iStack.pushInstance(ti1, ti1.getDescriptor());

    Instance<?> ti2 = new TestInstance();
    iStack.pushInstance(ti2, ti2.getDescriptor());

    iStack.popInstance(ti2);
    iStack.popInstance(ti1);

    assertEquals("Expecting null at top of empty stack", null, iStack.peek());
  }
Пример #22
0
  public void testComponents() {
    InstanceStack iStack = new InstanceStack();
    iStack.setConfigAdapter(mci);
    Map<String, BaseComponent<?, ?>> comps = iStack.getComponents();
    assertNotNull("Components should never be null", comps);
    assertEquals("Components should empty", 0, comps.size());

    BaseComponent<?, ?> x = getComponentWithPath("a");
    iStack.registerComponent(x);
    comps = iStack.getComponents();
    assertNotNull("Components should never be null", comps);
    assertEquals("Components should have one component", 1, comps.size());
    assertEquals("Components should have x", x, comps.get("a"));

    BaseComponent<?, ?> y = getComponentWithPath("b");
    iStack.registerComponent(y);
    comps = iStack.getComponents();
    assertNotNull("Components should never be null", comps);
    assertEquals("Components should have two components", 2, comps.size());
    assertEquals("Components should have x", x, comps.get("a"));
    assertEquals("Components should have y", y, comps.get("b"));
  }
Пример #23
0
  public void testErrorWrongParent() {
    // Mark a new parent without clearing the first
    InstanceStack iStack = new InstanceStack();
    iStack.setConfigAdapter(mci);
    iStack.markParent(new TestInstance("parent"));
    try {
      iStack.markParent(new TestInstance("different parent"));
      fail("Expected error when marking parent that's different than the current instance");
    } catch (Exception expected) {
      assertExceptionMessage(
          expected, AuraRuntimeException.class, "Don't know how to handle setAttribute here");
    }

    // Clear an instance that hasn't been marked as a parent
    iStack = new InstanceStack();
    iStack.setConfigAdapter(mci);
    iStack.markParent(new TestInstance("parent"));
    try {
      iStack.clearParent(new TestInstance("different parent"));
      fail("Expected error when clearing parent that's different than the current instance");
    } catch (Exception expected) {
      assertExceptionMessage(expected, AuraRuntimeException.class, "mismatched clear parent");
    }
  }
Пример #24
0
  /**
   * Handle an exception in the servlet.
   *
   * <p>This routine should be called whenever an exception has surfaced to the top level of the
   * servlet. It should not be overridden unless Aura is entirely subsumed. Most special cases can
   * be handled by the Aura user by implementing {@link ExceptionAdapter ExceptionAdapter}.
   *
   * @param t the throwable to write out.
   * @param quickfix is this exception a valid quick-fix
   * @param context the aura context.
   * @param request the request.
   * @param response the response.
   * @param written true if we have started writing to the output stream.
   * @throws IOException if the output stream does.
   * @throws ServletException if send404 does (should not generally happen).
   */
  @Override
  public void handleServletException(
      Throwable t,
      boolean quickfix,
      AuraContext context,
      HttpServletRequest request,
      HttpServletResponse response,
      boolean written)
      throws IOException {
    try {
      Throwable mappedEx = t;
      boolean map = !quickfix;
      Format format = context.getFormat();

      //
      // This seems to fail, though the documentation implies that you can do
      // it.
      //
      // if (written && !response.isCommitted()) {
      // response.resetBuffer();
      // written = false;
      // }
      if (!written) {
        // Should we only delete for JSON?
        setNoCache(response);
      }
      if (mappedEx instanceof IOException) {
        //
        // Just re-throw IOExceptions.
        //
        throw (IOException) mappedEx;
      } else if (mappedEx instanceof NoAccessException) {
        Throwable cause = mappedEx.getCause();
        String denyMessage = mappedEx.getMessage();

        map = false;
        if (cause != null) {
          //
          // Note that the exception handler can remap the cause here.
          //
          cause = exceptionAdapter.handleException(cause);
          denyMessage += ": cause = " + cause.getMessage();
        }
        //
        // Is this correct?!?!?!
        //
        if (format != Format.JSON) {
          this.send404(request.getServletContext(), request, response);
          if (!isProductionMode(context.getMode())) {
            // Preserve new lines and tabs in the stacktrace since this is directly being written on
            // to the
            // page
            denyMessage = "<pre>" + AuraTextUtil.escapeForHTML(denyMessage) + "</pre>";
            response.getWriter().println(denyMessage);
          }
          return;
        }
      } else if (mappedEx instanceof QuickFixException) {
        if (isProductionMode(context.getMode())) {
          //
          // In production environments, we want wrap the quick-fix. But be a little careful here.
          // We should never mark the top level as a quick-fix, because that means that we gack
          // on every mis-spelled app. In this case we simply send a 404 and bolt.
          //
          if (mappedEx instanceof DefinitionNotFoundException) {
            DefinitionNotFoundException dnfe = (DefinitionNotFoundException) mappedEx;

            if (dnfe.getDescriptor() != null
                && dnfe.getDescriptor().equals(context.getApplicationDescriptor())) {
              // We're in production and tried to hit an aura app that doesn't exist.
              // just show the standard 404 page.
              this.send404(request.getServletContext(), request, response);
              return;
            }
          }
          map = true;
          mappedEx = new AuraUnhandledException("404 Not Found (Application Error)", mappedEx);
        }
      }
      if (map) {
        mappedEx = exceptionAdapter.handleException(mappedEx);
      }

      PrintWriter out = response.getWriter();

      //
      // If we have written out data, We are kinda toast in this case.
      // We really want to roll it all back, but we can't, so we opt
      // for the best we can do. For HTML we can do nothing at all.
      //
      if (format == Format.JSON) {
        if (!written) {
          out.write(CSRF_PROTECT);
        }
        //
        // If an exception happened while we were emitting JSON, we want the
        // client to ignore the now-corrupt data structure. 404s and 500s
        // cause the client to prepend /*, so we can effectively erase the
        // bad data by appending a */ here and then serializing the exception
        // info.
        //
        out.write("*/");
        //
        // Unfortunately we can't do the following now. It might be possible
        // in some cases, but we don't want to go there unless we have to.
        //
      }
      if (format == Format.JS || format == Format.CSS) {
        // Make sure js and css doesn't get cached in browser, appcache, etc
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
      }
      if (format == Format.JSON
          || format == Format.HTML
          || format == Format.JS
          || format == Format.CSS) {
        //
        // We only write out exceptions for HTML or JSON.
        // Seems bogus, but here it is.
        //
        // Start out by cleaning out some settings to ensure we don't
        // check too many things, leading to a circular failure. Note
        // that this is still a bit dangerous, as we seem to have a lot
        // of magic in the serializer.
        //
        // Clear the InstanceStack before trying to serialize the exception since the Throwable has
        // likely
        // rendered the stack inaccurate, and may falsely trigger NoAccessExceptions.
        InstanceStack stack = this.contextService.getCurrentContext().getInstanceStack();
        List<String> list = stack.getStackInfo();
        for (int count = list.size(); count > 0; count--) {
          stack.popInstance(stack.peek());
        }

        serializationService.write(mappedEx, null, out);
        if (format == Format.JSON) {
          out.write("/*ERROR*/");
        }
      }
    } catch (IOException ioe) {
      throw ioe;
    } catch (Throwable death) {
      //
      // Catch any other exception and log it. This is actually kinda bad, because something has
      // gone horribly wrong. We should write out some sort of generic page other than a 404,
      // but at this point, it is unclear what we can do, as stuff is breaking right and left.
      //
      try {
        response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        exceptionAdapter.handleException(death);
        if (!isProductionMode(context.getMode())) {
          response.getWriter().println(death.getMessage());
        }
      } catch (IOException ioe) {
        throw ioe;
      } catch (Throwable doubleDeath) {
        // we are totally hosed.
        if (!isProductionMode(context.getMode())) {
          response.getWriter().println(doubleDeath.getMessage());
        }
      }
    } finally {
      this.contextService.endContext();
    }
  }
Пример #25
0
  public void testPath() {
    InstanceStack iStack = new InstanceStack();
    iStack.setConfigAdapter(mci);
    assertEquals("InstanceStack constructor should set path to base", "/*[0]", iStack.getPath());

    Instance<?> ti = new TestInstance();
    iStack.pushInstance(ti, ti.getDescriptor());

    // Set and clear attributes
    iStack.setAttributeName("attr1");
    assertEquals(
        "Setting attribute name should append name to path", "/*[0]/attr1", iStack.getPath());
    iStack.clearAttributeName("attr1");
    assertEquals("Clearing attribute name should remove name from path", "/*[0]", iStack.getPath());
    iStack.setAttributeName("body");
    assertEquals(
        "Setting attribute name as body should append '*' to path", "/*[0]/*", iStack.getPath());
    iStack.clearAttributeName("body");
    assertEquals("/*[0]", iStack.getPath());
    iStack.setAttributeName("realbody");
    assertEquals(
        "Setting attribute name as realbody should append '+' to path",
        "/*[0]/+",
        iStack.getPath());
    iStack.clearAttributeName("realbody");
    assertEquals("/*[0]", iStack.getPath());

    // Set and clear indexes
    iStack.setAttributeName("attr2");
    iStack.setAttributeIndex(42);
    assertEquals("/*[0]/attr2[42]", iStack.getPath());
    iStack.clearAttributeIndex(42);
    assertEquals("/*[0]/attr2", iStack.getPath());
  }
Пример #26
0
 public void testMarkParentMultipleTimes() {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   assertEquals("InstanceStack constructor should set path to base", "/*[0]", iStack.getPath());
   TestInstance parent = new TestInstance("parent");
   iStack.markParent(parent);
   assertEquals("Marking parent should set path to the parent's path", "parent", iStack.getPath());
   iStack.markParent(parent);
   assertEquals("Marking additional parent should not update path", "parent", iStack.getPath());
   iStack.clearParent(parent);
   assertEquals("parent", iStack.getPath());
   iStack.clearParent(parent);
   assertEquals(
       "Clearing both parents should reset path to original base path", "/*[0]", iStack.getPath());
 }
Пример #27
0
 @Override
 public void serializeAsPart(Json json) throws IOException {
   if (fakeInstanceStack != null) {
     fakeInstanceStack.serializeAsPart(json);
   }
 }
Пример #28
0
 public void testPeekAtEmptyStackReturnsNull() throws Exception {
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   assertEquals("Expecting null at top of empty stack", null, iStack.peek());
 }
Пример #29
0
 public void testPrivileged() throws Exception {
   // setting up
   String namespace_Priv = "previlege";
   String namespace_UnPriv = "unprevilege";
   String name1 = "one";
   String name2 = "two";
   String name3 = "three";
   String name4 = "four";
   Mockito.when(mci.isPrivilegedNamespace(namespace_Priv)).thenReturn(true);
   Mockito.when(mci.isPrivilegedNamespace(namespace_UnPriv)).thenReturn(false);
   // create empty stack, sanity check
   InstanceStack iStack = new InstanceStack();
   iStack.setConfigAdapter(mci);
   assertFalse("stack should has topUnprivileged=null at the beginning", iStack.isUnprivileged());
   // start pushing
   TestInstance one = new TestInstance(namespace_Priv, name1);
   iStack.pushInstance(one, one.getDescriptor());
   assertFalse(
       "topUnprivileged is still null after pushing in one previleged instance:instance1",
       iStack.isUnprivileged());
   TestInstance two = new TestInstance(namespace_UnPriv, name2);
   iStack.pushInstance(two, two.getDescriptor());
   assertTrue(
       "topUnprivileged should become first unprivilege instance:instance2",
       iStack.isUnprivileged());
   TestInstance three = new TestInstance(namespace_Priv, name3);
   iStack.pushInstance(three, three.getDescriptor());
   assertTrue(
       "topUnprivileged should remain unchanged after pushing in a new privilege instance:instance3",
       iStack.isUnprivileged());
   TestInstance four = new TestInstance(namespace_UnPriv, name4);
   iStack.pushInstance(four, four.getDescriptor());
   assertTrue(
       "topUnprivileged should be unchanged after pushing in a new unprivilege instance:instance4",
       iStack.isUnprivileged());
   // start poping
   iStack.popInstance(four);
   assertTrue(
       "topUnprivileged should be unchanged after poping out unprivilege instance:instance4",
       iStack.isUnprivileged());
   iStack.popInstance(three);
   assertTrue(
       "topUnprivileged should be unchanged after poping out privilege instance:instance3",
       iStack.isUnprivileged());
   iStack.popInstance(two);
   assertFalse(
       "topUnprivileged should become null after poping out first unprivilege instance:instance2",
       iStack.isUnprivileged());
   iStack.popInstance(one);
   assertFalse(
       "topUnprivileged should be unchanged(null) after poping out instance1",
       iStack.isUnprivileged());
 }