/*
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * $Id: WSCClient.java 74522 2015-06-02 12:05:10Z jsupol $
 */

package com.sun.ts.tests.websocket.negdep.onmessage.client.pongduplicate;

import javax.websocket.DeploymentException;

import com.sun.ts.lib.util.TestUtil;
import com.sun.ts.tests.websocket.negdep.NegativeDeploymentClient;

/*
 * @class.setup_props: webServerHost;
 *                     webServerPort;
 *                     ts_home;
 *                     ws_wait;
 *                     tslib.name;
 */
/**
 * @OnMessage can contain one pong message handling method. This test checks two
 *            such methods fail the deployment.
 * @since 1.11
 */
public class WSCClient extends NegativeDeploymentClient {

	private static final long serialVersionUID = 111L;

	public WSCClient() {
		setContextRoot("wsc_negdep_onmessage_client_pongduplicate_web");
	}

	public static void main(String[] args) {
		new WSCClient().run(args);
	}

	/*
	 * @testName: duplicateOnMessageTest
	 * @assertion_ids:  WebSocket:SPEC:WSC-5.2.1-3;WebSocket:SPEC:WSC-4.7-4;
	 * 					WebSocket:SPEC:WSC-5.2.1-4;
	 * @test_Strategy: In both cases, a deployment error raised during the 
	 * deployment process must halt the deployment of the application, any 
	 * well formed endpoints deployed prior to the error being raised must be 
	 * removed from service and no more websocket endpoints from that 
	 * application may be deployed by the container, even if they are valid.
	 * 
	 * Each websocket endpoint may only have one message handling method for 
	 * each of the native websocket message formats: text, binary and pong. 
	 * The websocket implementation must not deploy such an endpoint and must 
	 * raise a deployment error if an attempt is made to deploy such an 
	 * annotated endpoint. [WSC-4.7-4]
	 * 
	 * If the deployment error occurs under the programmatic control of the 
	 * developer, for example, when using the WebSocketContainer API to deploy 
	 * a client endpoint, deployment errors must be reported by the container 
	 * to the developer by using an instance of the DeploymentException. 
	 * [WSC-5.2.1-4]
	 * 
	 * To check the test fails when deployment pass, comment out of the 
	 * @OnMessage of OnMessageServerEndpoint 
	 */
	public void duplicateOnMessageTest() throws Fault {
		setEntity("anything");
		OnMessageClientEndpoint endpoint = new OnMessageClientEndpoint();
		setAnnotatedClientEndpoint(endpoint);
		setProperty(Property.REQUEST, buildRequest("echo"));
		setProperty(Property.STATUS_CODE, "-1");
		logExceptionOnInvocation(false);
		printClientCall(false);
		boolean thrown = true;
		try {
			invoke(false);
			thrown = false;
		} catch (Fault tfe) {
			// DeploymentException
			assertCause(tfe, DeploymentException.class,
					"The DeploymentException has not been thrown!");
			tfe.printStackTrace();
			logMsg("--\n\n");
			logMsg("DeploymentException has been thrown as expected");
		}
		if (!thrown)
			super.throwDeploymentDidNotFail();

		boolean isSession = false;
		try {
			getSession();
			isSession = true;
		} catch (Fault f) {
			// did not succeeded
			TestUtil.logMsg("The session is closed as expected");
		}
		if (isSession) {
			TestUtil.logErr("Session is not null!");
			throw new Fault("Session is not null! Deployment succeeded");
		}
	}
}
