/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.test.jbossnet.document;

// $Id: ObjectElementTestCase.java,v 1.1.4.3 2005/12/01 19:51:08 dimitris Exp $

import junit.framework.Test;
import org.jboss.axis.utils.DOM2Utils;
import org.jboss.test.jbossnet.JBossNetTestBase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import java.io.ByteArrayInputStream;
import java.net.URL;

/** An endpoint test for document centric message exchange
 *
 * It uses the the jva:RPC provider with xsd:anyType
 *
 * @author Thomas.Diesler@jboss.org
 */
public class ObjectElementTestCase extends JBossNetTestBase
{
   public static final String xmlstr = "<root><child>Hello</child><child>World</child></root>";

   private String namespaceURI = "http://jboss.net/document";
   private Service service;

   public ObjectElementTestCase(String name)
   {
      super(name);
   }

   protected void setUp() throws Exception
   {
      super.setUp();
      URL wsdlURL = new URL(SERVICES_LOCATION + "/ObjectElementServer?wsdl");
      service = createService(wsdlURL, new QName(namespaceURI, "ObjectElementServerService"));
   }

   /** Process an Element at the endpoint
    */
   public void testProcessElement() throws Exception
   {
      DocumentBuilder builder = DOM2Utils.getDocumentBuilder();
      Document doc = builder.parse(new ByteArrayInputStream(xmlstr.getBytes()));
      Element rootEl = doc.getDocumentElement();

      Call call = service.createCall(new QName(namespaceURI, "ObjectElementServer"), "processElement");

      Object retObj = call.invoke(new Object[]{rootEl});
      assertNotNull("Return obj is null", retObj);
      assertTrue("Return obj is not an Element: " + retObj.getClass().getName(), retObj instanceof Element);
   }

   /** Process an Object at the endpoint
    */
   public void testProcessObject() throws Exception
   {
      DocumentBuilder builder = DOM2Utils.getDocumentBuilder();
      Document doc = builder.parse(new ByteArrayInputStream(xmlstr.getBytes()));
      Element rootEl = doc.getDocumentElement();

      Call call = service.createCall(new QName(namespaceURI, "ObjectElementServer"), "processObject");

      Object retObj = call.invoke(new Object[]{rootEl});
      assertNotNull("Return obj is null", retObj);
      assertTrue("Return obj is not an Element: " + retObj.getClass().getName(), retObj instanceof Element);
   }

   /** Deploy the test ear */
   public static Test suite() throws Exception
   {
      return getDeploySetup(ObjectElementTestCase.class, "jbossnet-document.wsr");
   }

}
