com.tc.websocket.junit.tests.RhinoClientTest.java Source code

Java tutorial

Introduction

Here is the source code for com.tc.websocket.junit.tests.RhinoClientTest.java

Source

/*
 *  Copyright Tek Counsel LLC 2014
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package com.tc.websocket.junit.tests;

import java.io.InputStream;
import java.net.URI;
import java.util.Date;

import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;

import com.tc.scriptrunner.guice.ScriptRunnerModule;
import com.tc.scriptrunner.runners.IScriptRunner;
import com.tc.utils.CloseUtils;
import com.tc.websocket.embeded.clients.IScriptClient;
import com.tc.websocket.embeded.clients.RhinoClient;
import com.tc.websocket.embeded.clients.Script;
import com.tc.websocket.valueobjects.IUser;
import com.tc.websocket.valueobjects.SocketMessage;
import com.tc.websocket.valueobjects.User;

public class RhinoClientTest {

    private RhinoClient client;

    @Before
    public void setUp() throws Exception {

        //set this value to your local system's path.
        System.setProperty("java.library.path", "C:/Domino");

        IScriptRunner runner = new ScriptRunnerModule().provideScriptRunner();

        URI uri = URI.create("ws://localhost:8889/websocket/rhinotest/rhinoclient");
        client = new RhinoClient(uri);

        IUser user = new User();
        user.setUserId("rhinoclient");
        user.setSessionId("rhinoclient");
        client.setUser(user);

        // set the runner
        client.setScriptRunner(runner);

        //load the test script
        InputStream in = RhinoClientTest.class.getResourceAsStream("rhinotest.js");
        String javascript = IOUtils.toString(in);
        CloseUtils.close(in);

        //build the script with appropriate target event.
        Script script = new Script();
        script.setScript(javascript);
        script.setEvent(IScriptClient.ON_MESSAGE);

        //add the script
        client.addScript(script);

        // connect the client.
        client.connect();

    }

    @Test
    public void testRhinoClient() throws InterruptedException {

        System.out.println("testRhinoClient...");

        SocketMessage msg = new SocketMessage();

        String text = "all work and no play makes mark a dull boy.";

        for (int i = 0; i < 1; i++) {

            String uri = "/rhinotest";

            msg.setTo(uri);

            msg.setText(text + " " + i);
            msg.setFrom("RhinoClient");

            msg.setDate(new Date());

            //client will echo the message sent.
            client.sendMsg(msg);

            Thread.sleep(1000);

            if (client.hasError()) {
                throw new RuntimeException("RhinoClient has an internal error.  Please check the log.");
            }

        }
    }

}