Exemple #1
0
  protected void getBeanElementProperties(Element element, Object bean, PropertyDescriptor pd)
      throws IntrospectionException, IllegalAccessException {
    Element propertyElement = null;
    Class classOfProperty = pd.getPropertyType();
    Object[] argsNone = {};
    // If the property is "class" and the type is java.lang.Class.class then
    // this is the class of the bean, which we've already encoded.
    // In this special case, return null.
    if (!((pd.getName().charAt(0) == 'c') && pd.getName().equals("class"))
        && !classOfProperty.equals(java.lang.Class.class)) {
      // Don't process property if it is in the list of fields to be ignored
      boolean omittedField = false;
      try {
        Field field = bean.getClass().getDeclaredField(pd.getName());
        omittedField = field.isAnnotationPresent(ObjectXmlOmitField.class);
      } catch (NoSuchFieldException nsfe) {
      }
      if (!omittedField) {
        String propertyName = formatName(pd.getName());
        // Hereafter, we're trying to create a representation of the property
        // based on the property's value.
        // The very first thing we need to do is get the value of the
        // property as an object. If we can't do that, we can't get any
        // representation of the property at all.
        Object propertyValue = null;
        try {
          Method getter = pd.getReadMethod();
          if (getter != null) {
            propertyValue = getter.invoke(bean, argsNone);
          }
        } catch (Exception ex) {
          // couldn't get value
          System.err.println(ex.getMessage());
        }
        // See if this property's value is something we can represent as a
        // standard data type that can be converted to an xsd type,
        // or if it's something that must be represented as a JavaBean.
        if (propertyElement == null) {

          PropertyEditor propEditor = PropertyEditorManager.findEditor(classOfProperty);
          // If the property editor is not null, pass the property's
          // value to the PropertyEditor, and then ask the PropertyEditor
          // for the object and then attempt to convert it to a standard type.

          if ((propEditor != null)
              || (classOfProperty == java.util.Calendar.class)
              || (classOfProperty == java.util.Date.class)) {
            // The object is a standard type
            // (e.g. a wrapper around a primitive type, a String, or a Date or Calendar object)
            try {
              Object object;
              if ((classOfProperty == java.util.Calendar.class)
                  || (classOfProperty == java.util.Date.class)) object = propertyValue;
              else {
                propEditor.setValue(propertyValue);
                object = propEditor.getValue();
              }
              Element childElement = getStandardObjectElement(document, object, propertyName);
              if (includeNullValues
                  || !childElement
                      .getAttribute(
                          NamespaceConstants.NSPREFIX_SCHEMA_XSI
                              + ":"
                              + org.apache.axis.Constants.ATTR_TYPE)
                      .equals("anyType")) {
                if (!includeTypeInfo) {
                  childElement.removeAttribute(
                      NamespaceConstants.NSPREFIX_SCHEMA_XSI
                          + ":"
                          + org.apache.axis.Constants.ATTR_TYPE);
                  childElement.removeAttribute(NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":null");
                }
                try {
                  Field field = bean.getClass().getDeclaredField(propertyName);
                  if (field.isAnnotationPresent(ObjectXmlAsAttribute.class)) {
                    String attrName = null;
                    if (includeTypeInfo) attrName = nsPrefix + ":" + propertyName;
                    else attrName = propertyName;
                    element.setAttribute(attrName, childElement.getFirstChild().getNodeValue());
                  } else if (field.isAnnotationPresent(ObjectXmlAsValue.class))
                    element.setTextContent(childElement.getFirstChild().getNodeValue());
                  else element.appendChild(childElement);
                } catch (NoSuchFieldException nfse) {
                  element.appendChild(childElement);
                }
              }
            } catch (Exception e) {
            }
          } else {
            // The object is not an XSD-encoded datatype, it must be a JavaBean
            try {
              String propertyTypeName = null;
              if (propertyValue != null) {
                // get object type
                Class propertyType = pd.getPropertyType();
                propertyTypeName = propertyType.getName();
              }
              getBeanElements(element, propertyName, propertyTypeName, propertyValue);
            } catch (Exception e) {
            }
          }
        }
      }
    }
  }
  public boolean runTest(Test test) throws Exception {
    String filename = test.file.toString();
    if (this.verbose) {
      System.out.println(
          "Running "
              + filename
              + " against "
              + this.host
              + ":"
              + this.port
              + " with "
              + this.browser);
    }
    this.document = parseDocument(filename);

    if (this.baseUrl == null) {
      NodeList links = this.document.getElementsByTagName("link");
      if (links.getLength() != 0) {
        Element link = (Element) links.item(0);
        setBaseUrl(link.getAttribute("href"));
      }
    }
    if (this.verbose) {
      System.out.println("Base URL=" + this.baseUrl);
    }

    Node body = this.document.getElementsByTagName("body").item(0);
    Element resultContainer = document.createElement("div");
    resultContainer.setTextContent("Result: ");
    Element resultElt = document.createElement("span");
    resultElt.setAttribute("id", "result");
    resultElt.setIdAttribute("id", true);
    resultContainer.appendChild(resultElt);
    body.insertBefore(resultContainer, body.getFirstChild());

    Element executionLogContainer = document.createElement("div");
    executionLogContainer.setTextContent("Execution Log:");
    Element executionLog = document.createElement("div");
    executionLog.setAttribute("id", "log");
    executionLog.setIdAttribute("id", true);
    executionLog.setAttribute("style", "white-space: pre;");
    executionLogContainer.appendChild(executionLog);
    body.appendChild(executionLogContainer);

    NodeList tableRows = document.getElementsByTagName("tr");
    Element theadRow = (Element) tableRows.item(0);
    test.name = theadRow.getTextContent();
    appendCellToRow(theadRow, "Result");

    this.commandProcessor =
        new HtmlCommandProcessor(this.host, this.port, this.browser, this.baseUrl);
    String resultState;
    String resultLog;
    test.result = true;
    try {
      this.commandProcessor.start();
      test.commands = new Command[tableRows.getLength() - 1];
      for (int i = 1; i < tableRows.getLength(); i++) {
        Element stepRow = (Element) tableRows.item(i);
        Command command = executeStep(stepRow);
        appendCellToRow(stepRow, command.result);
        test.commands[i - 1] = command;
        if (command.error) {
          test.result = false;
        }
        if (command.failure) {
          test.result = false;
          // break;
        }
      }
      resultState = test.result ? "PASSED" : "FAILED";
      resultLog = (test.result ? "Test Complete" : "Error");
      this.commandProcessor.stop();
    } catch (Exception e) {
      test.result = false;
      resultState = "ERROR";
      resultLog = "Failed to initialize session\n" + e;
      e.printStackTrace();
    }
    document.getElementById("result").setTextContent(resultState);
    Element log = document.getElementById("log");
    log.setTextContent(log.getTextContent() + resultLog + "\n");
    return test.result;
  }
 void appendCellToRow(Element tableRow, String content) {
   Element newCell = document.createElement("td");
   newCell.setTextContent(content);
   tableRow.appendChild(newCell);
 }
  public void addPackageClass(PackageClass pClass) {

    Document doc;
    try {
      doc = getDocument();
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }

    String name = pClass.getName();

    // check if such class exists and remove duplicates
    Element rootEl = doc.getDocumentElement();
    NodeList classEls = rootEl.getElementsByTagName(EL_CLASS);
    for (int i = 0; i < classEls.getLength(); i++) {
      Element nameEl = getElementByName((Element) classEls.item(i), EL_NAME);
      if (name.equals(nameEl.getTextContent())) {
        rootEl.removeChild(classEls.item(i));
      }
    }

    Element classNode = doc.createElement(EL_CLASS);
    doc.getDocumentElement().appendChild(classNode);
    classNode.setAttribute(ATR_TYPE, PackageClass.ComponentType.SCHEME.getXmlName());
    classNode.setAttribute(ATR_STATIC, "false");

    Element className = doc.createElement(EL_NAME);
    className.setTextContent(name);
    classNode.appendChild(className);

    Element desrc = doc.createElement(EL_DESCRIPTION);
    desrc.setTextContent(pClass.getDescription());
    classNode.appendChild(desrc);

    Element icon = doc.createElement(EL_ICON);
    icon.setTextContent(pClass.getIcon());
    classNode.appendChild(icon);

    // graphics
    classNode.appendChild(generateGraphicsNode(doc, pClass.getGraphics()));

    // ports
    List<Port> ports = pClass.getPorts();
    if (!ports.isEmpty()) {
      Element portsEl = doc.createElement(EL_PORTS);
      classNode.appendChild(portsEl);

      for (Port port : ports) {
        portsEl.appendChild(generatePortNode(doc, port));
      }
    }

    // fields
    Collection<ClassField> fields = pClass.getFields();
    if (!fields.isEmpty()) {
      Element fieldsEl = doc.createElement(EL_FIELDS);
      classNode.appendChild(fieldsEl);

      for (ClassField cf : fields) {
        fieldsEl.appendChild(generateFieldNode(doc, cf));
      }
    }

    // write
    try {
      writeDocument(doc, new FileOutputStream(xmlFile));
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }