/**
   * Test method for {@link
   * org.jvoicexml.interpreter.ObjectExecutorThread#execute(org.jvoicexml.interpreter.VoiceXmlInterpreterContext,
   * org.jvoicexml.interpreter.VoiceXmlInterpreter,
   * org.jvoicexml.interpreter.FormInterpretationAlgorithm,
   * org.jvoicexml.interpreter.formitem.ObjectFormItem)} .
   *
   * @exception Exception Test failed.
   * @exception JVoiceXMLEvent Test failed.
   */
  @Test
  public void testExecute() throws Exception, JVoiceXMLEvent {
    final VoiceXmlDocument doc = new VoiceXmlDocument();
    final Vxml vxml = doc.getVxml();
    final Form form = vxml.appendChild(Form.class);
    final Subdialog subdialog = form.appendChild(Subdialog.class);
    subdialog.setName("test");
    final Form subform = vxml.appendChild(Form.class);
    subform.setId("subid");
    final Var var = subform.appendChild(Var.class);
    var.setName("testparam");
    final Block block = subform.appendChild(Block.class);
    final Assign assign = block.appendChild(Assign.class);
    assign.setName("testparam");
    assign.setExpr("testparam * 2");
    final Return ret = block.appendChild(Return.class);
    ret.setNamelist("testparam");
    final SubdialogFormItem item = new SubdialogFormItem(context, subdialog);
    final Dialog dialog = new ExecutablePlainForm();
    dialog.setNode(form);
    final FormInterpretationAlgorithm fia = new FormInterpretationAlgorithm(context, null, dialog);
    final EventHandler handler =
        new org.jvoicexml.interpreter.event.JVoiceXmlEventHandler(null, null);
    final EventBus eventbus = context.getEventBus();
    eventbus.subscribe("", handler);
    handler.collect(context, null, fia, item);

    final URI uri = new URI("#subid");
    final Map<String, Object> params = new java.util.HashMap<String, Object>();
    params.put("testparam", new Integer(4));
    final JVoiceXmlApplication application = new JVoiceXmlApplication(null);
    application.addDocument(new URI("test"), doc);

    final SubdialogExecutorThread executor =
        new SubdialogExecutorThread(uri, context, application, params, eventbus);

    executor.start();
    executor.join();
    ReturnEvent event = null;
    try {
      handler.processEvent(item);
    } catch (ReturnEvent e) {
      event = e;
    }
    Assert.assertNotNull(event);
    final Map<String, Object> variables = event.getVariables();
    Assert.assertEquals(1, variables.size());
    Assert.assertEquals(8, variables.get("testparam"));
  }
示例#2
0
 /**
  * Creates a VoiceXML snippet that can be pasted into the content.
  *
  * @param xml the XML snippet
  * @return create VoiceXML document
  * @throws SAXException
  * @throws IOException
  * @throws TransformerException
  * @throws ParserConfigurationException
  */
 private Object convertContent(final String xml)
     throws SAXException, IOException, TransformerException, ParserConfigurationException {
   if (xml.startsWith("<")) {
     final Reader reader = new StringReader(xml);
     final InputSource source = new InputSource(reader);
     Document document = builder.parse(source);
     Transformer transformer = template.newTransformer();
     final Source domSource = new DOMSource(document);
     final ByteArrayOutputStream out = new ByteArrayOutputStream();
     final Result result = new StreamResult(out);
     transformer.transform(domSource, result);
     final InputStream in = new ByteArrayInputStream(out.toByteArray());
     final InputSource transformedSource = new InputSource(in);
     final VoiceXmlDocument doc = new VoiceXmlDocument(transformedSource);
     final Vxml vxml = doc.getVxml();
     final List<Form> forms = vxml.getForms();
     final Form form = forms.get(0);
     final Collection<Block> blocks = form.getChildNodes(Block.class);
     final Block block = blocks.iterator().next();
     return block.getFirstChild();
   } else {
     return xml;
   }
 }