コード例 #1
0
ファイル: DataTypes_013.java プロジェクト: theoyoung/patches
  /**
   * Assign a java byte array to a JavaScript variable in the following ways:
   *
   * <ul>
   *   <li>Call a static method
   *   <li>Get the value of a static field
   *   <li>Call an instance method
   *   <li>Get the value of an instance field.
   *
   * @param command the command to eval to get the byte array
   */
  public void doArrayTest(String command, boolean shouldEqual) {
    int array[] = DataTypeClass.PUB_STATIC_ARRAY_INT;
    int jsArray[];
    int jsArray_length;

    try {
      //	assign the array to	a JavaScript variable
      global.eval("var jsArray =	" + command);

      // get the jsArray object, which should be the java object
      jsArray = (int[]) global.getMember("jsArray");

      // get the length of the array from JavaScript
      jsArray_length = ((Double) global.eval("jsArray.length")).intValue();

      //  iterate through jsArray in JavaScript. verify that the type and
      //  value of each object in the array is correct.

      for (int i = 0; i < jsArray_length; i++) {
        //  verify that the array item is the same object as in the
        //  original array

        Double item = (Double) global.eval("jsArray[" + i + "];");

        addTestCase(
            "[jsArray = "
                + command
                + "] "
                + "global.eval(\"jsArray["
                + i
                + "]\").equals( array["
                + i
                + "])",
            "true",
            (item.equals(new Double(array[i]))) + "",
            "");
      }

    } catch (Exception e) {
      e.printStackTrace();
      file.exception = e.toString();
      jsArray_length = 0;
      jsArray = null;
    }

    // verify that jsArray is the same as the original array

    addTestCase(
        "[jsArray = "
            + command
            + "] "
            + "jsArray = global.getMember( \"jsArray\"); "
            + "jsArray == array",
        (shouldEqual) ? "true" : "false",
        (jsArray == array) + "",
        "");
  }
コード例 #2
0
ファイル: DataTypes_017.java プロジェクト: unktomi/rhino
  /**
   * This tests calls a Java setter method from JavaScript. It verifies that the setter was called
   * properly in two ways: by checking the return value of the getter method, and by checking the
   * value of the public field that was set.
   *
   * @param setter java method that takes an argument, and sets a value
   * @param jsValue JavaScript value that is passed to the setter
   * @param getter java method that returns the value set by setter
   * @param field java field that setter changed
   * @param eResult expected result, which should be of some Number type
   */
  public void doSetterTest(
      String jsValue, String setter, String getter, String field, Object eResult) {
    String setMethod = setter + "(" + jsValue + ");";
    String getMethod = getter + "();";
    String setterResult = "No exception thrown";
    Double getterResult = null;
    Double fieldResult = null;
    Object expectedResult = null;
    boolean eq = false;

    try {
      eq = eResult.getClass().equals(Class.forName("java.lang.String"));
    } catch (ClassNotFoundException e) {
      addTestCase(setMethod + " driver error.", "very", "bad", file.exception);
    }

    if (eq) {
      try {
        global.eval(setMethod);
      } catch (Exception e) {
        setterResult = EXCEPTION;
        file.exception = e.toString();
        e.printStackTrace();
      } finally {
        addTestCase(
            setMethod + " should throw a JSException", EXCEPTION, setterResult, file.exception);
      }
    } else {

      try {
        // From	JavaScript,	call the setter
        global.eval(setMethod);

        // From	JavaScript,	call the getter
        getterResult = (Double) global.eval(getMethod);

        // From	JavaSript, access the field
        fieldResult = (Double) global.eval(field);

      } catch (Exception e) {
        e.printStackTrace();
        file.exception = e.toString();
      } finally {
        addTestCase(
            "[value: "
                + getterResult
                + "; expected:	"
                + expectedResult
                + "] "
                + setMethod
                + getMethod
                + "( "
                + expectedResult
                + ").equals("
                + getterResult
                + ")",
            "true",
            expectedResult.equals(getterResult) + "",
            file.exception);

        addTestCase(
            "[value: "
                + fieldResult
                + "; expected: "
                + expectedResult
                + "] "
                + setMethod
                + field
                + "; ("
                + expectedResult
                + ").equals("
                + fieldResult
                + ")",
            "true",
            expectedResult.equals(fieldResult) + "",
            file.exception);
      }
    }
  }