/*
 * Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * $Id: URLClient.java 62542 2011-04-15 16:55:08Z adf $
 */

package com.sun.ts.tests.saaj.api.javax_xml_soap.Name;

import com.sun.ts.lib.util.*;
import com.sun.ts.lib.porting.*;
import com.sun.ts.lib.harness.*;

import java.io.*;
import java.net.*;
import java.util.*;
import java.rmi.*;

import com.sun.javatest.Status;

public class URLClient extends EETest
{
    private static final String PROTOCOL = "http";
    private static final String HOSTNAME = "localhost";
    private static final int PORTNUM  = 8000;
    private static final String TESTSERVLET  = 
		"/Name_web/NameTestServlet";

    private static final String WEBSERVERHOSTPROP = "webServerHost";
    private static final String WEBSERVERPORTPROP = "webServerPort";

    private TSURL tsurl = new TSURL();
    private URL url = null;
    private URLConnection     urlConn = null;
    private Properties props = null;
    private String hostname = HOSTNAME;
    private int portnum = PORTNUM;

    public static void main(String[] args) 
    {
	URLClient theTests = new URLClient();
	Status s = theTests.run(args, System.out, System.err);
	s.exit();
    }


/*  Test setup */

  /*
   *   @class.setup_props: webServerHost;
   *                       webServerPort;
   */

    public void setup(String[] args, Properties p) throws Fault
    {
        props = p;
	boolean pass = true;

        try {
	    hostname = p.getProperty(WEBSERVERHOSTPROP);
	    if (hostname == null)
		      pass = false;
	    else if (hostname.equals(""))
		      pass = false;
	    try { 
		portnum = Integer.parseInt(p.getProperty(WEBSERVERPORTPROP));
	    } catch (Exception e) { 
		      pass = false; 
	    }
         } catch (Exception e) {
	    throw new Fault("setup failed:", e);
         }
	 if (!pass) {
	    TestUtil.logErr("Please specify host & port of web server " +
			    "in config properties: " + WEBSERVERHOSTPROP +
			    ", " + WEBSERVERPORTPROP);
	    throw new Fault("setup failed:");
	 }
        logMsg("setup ok");
    }

    public void cleanup()  throws Fault {
        logMsg("cleanup ok");
    }

    /*
     *   @testName:  getPrefixTest
     *
     *   @assertion_ids:  SAAJ:JAVADOC:308;
     *
     *   @test_Strategy: Name.getPrefix() will return the prefix
     *			associated with this namespace object.
     *
     *   Description: get prefix associated with Name
     */
    public void getPrefixTest() throws Fault {
	boolean pass = true;
        try {

	    TestUtil.logMsg("getPrefixTest: get prefix associated with Name");
	    TestUtil.logMsg("Creating url to test servlet.....");
    	    url = tsurl.getURL(PROTOCOL, hostname, portnum, TESTSERVLET);
	    TestUtil.logMsg(url.toString());
	    for(int i=0; i<2; i++) {
	        TestUtil.logMsg("Sending post request to test servlet.....");
	        props.setProperty("TESTNAME", "getPrefixTest");
	        if(i == 0)
	             props.setProperty("SOAPVERSION", "soap11");
	        else
	             props.setProperty("SOAPVERSION", "soap12");
	        urlConn = TestUtil.sendPostData(props, url);
	        TestUtil.logMsg("Getting response from test servlet.....");
	        Properties resProps = TestUtil.getResponseProperties(urlConn);
	        if(!resProps.getProperty("TESTRESULT").equals("pass"))
		      pass = false;
	    }

        } catch (Exception e) {
	    TestUtil.logErr("Caught exception: " + e.getMessage());
            e.printStackTrace();
	    throw new Fault("getPrefixTest failed", e);
        }

	if (!pass)
	    throw new Fault("getPrefixTest failed");
    }

    /*
     *   @testName:  getURITest
     *
     *   @assertion_ids:  SAAJ:JAVADOC:309;
     *
     *   @test_Strategy: Name.getURI() will return the URI
     *			associated with this namespace object.
     *
     *   Description: get URI associated with Name
     */
    public void getURITest() throws Fault {
	boolean pass = true;
        try {
	    TestUtil.logMsg("getURITest: get URI associated with Name");
	    TestUtil.logMsg("Creating url to test servlet.....");
    	    url = tsurl.getURL(PROTOCOL, hostname, portnum, TESTSERVLET);
	    TestUtil.logMsg(url.toString());
	    for(int i=0; i<2; i++) {
	        TestUtil.logMsg("Sending post request to test servlet.....");
	        props.setProperty("TESTNAME", "getURITest");
	        if(i == 0)
	             props.setProperty("SOAPVERSION", "soap11");
	        else
	             props.setProperty("SOAPVERSION", "soap12");
	        urlConn = TestUtil.sendPostData(props, url);
	        TestUtil.logMsg("Getting response from test servlet.....");
	        Properties resProps = TestUtil.getResponseProperties(urlConn);
	        if(!resProps.getProperty("TESTRESULT").equals("pass"))
		      pass = false;
	    }

        } catch (Exception e) {
	    TestUtil.logErr("Caught exception: " + e.getMessage());
            e.printStackTrace();
	    throw new Fault("getURITest failed", e);
        }

	if (!pass)
	    throw new Fault("getURITest failed");
    }

    /*
     *   @testName:  getLocalNameTest
     *
     *   @assertion_ids:  SAAJ:JAVADOC:306;
     *
     *   @test_Strategy: Name.getLocalName() will return the local name
     *			associated with this namespace object.
     *
     *   Description: get local name associated with Name
     */
    public void getLocalNameTest() throws Fault {
	boolean pass = true;
        try {
	    TestUtil.logMsg("getLocalNameTest: get local name associated with Name");
	    TestUtil.logMsg("Creating url to test servlet.....");
    	    url = tsurl.getURL(PROTOCOL, hostname, portnum, TESTSERVLET);
	    TestUtil.logMsg(url.toString());
	    for(int i=0; i<2; i++) {
	        TestUtil.logMsg("Sending post request to test servlet.....");
	        props.setProperty("TESTNAME", "getLocalNameTest");
	        if(i == 0)
	             props.setProperty("SOAPVERSION", "soap11");
	        else
	             props.setProperty("SOAPVERSION", "soap12");
	        urlConn = TestUtil.sendPostData(props, url);
	        TestUtil.logMsg("Getting response from test servlet.....");
	        Properties resProps = TestUtil.getResponseProperties(urlConn);
	        if(!resProps.getProperty("TESTRESULT").equals("pass"))
		      pass = false;
	    }

        } catch (Exception e) {
	    TestUtil.logErr("Caught exception: " + e.getMessage());
            e.printStackTrace();
	    throw new Fault("getLocalNameTest failed", e);
        }

	if (!pass)
	    throw new Fault("getLocalNameTest failed");
    }

    /*
     *   @testName:  getQualifiedNameTest
     *
     *   @assertion_ids:  SAAJ:JAVADOC:307;
     *
     *   @test_Strategy: Name.getQualified() will return the qualified name
     *			associated with this namespace object.
     *
     *   Description: get qualified name associated with Name
     */
    public void getQualifiedNameTest() throws Fault {
	boolean pass = true;
        try {
	    TestUtil.logMsg("getQualifiedNameTest: get qualified name associated with Name");
	    TestUtil.logMsg("Creating url to test servlet.....");
    	    url = tsurl.getURL(PROTOCOL, hostname, portnum, TESTSERVLET);
	    TestUtil.logMsg(url.toString());
	    for(int i=0; i<2; i++) {
	        TestUtil.logMsg("Sending post request to test servlet.....");
	        props.setProperty("TESTNAME", "getQualifiedNameTest");
	        if(i == 0)
	             props.setProperty("SOAPVERSION", "soap11");
	        else
	             props.setProperty("SOAPVERSION", "soap12");
	        urlConn = TestUtil.sendPostData(props, url);
	        TestUtil.logMsg("Getting response from test servlet.....");
	        Properties resProps = TestUtil.getResponseProperties(urlConn);
	        if(!resProps.getProperty("TESTRESULT").equals("pass"))
		      pass = false;
	    }

        } catch (Exception e) {
	    TestUtil.logErr("Caught exception: " + e.getMessage());
            e.printStackTrace();
	    throw new Fault("getQualifiedNameTest failed", e);
        }

	if (!pass)
	    throw new Fault("getQualifiedNameTest failed");
    }
}
