Package org.cometd.javascript

Source Code of org.cometd.javascript.AbstractCometDTest

/*
* Copyright (c) 2008-2014 the original author or authors.
*
* 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 org.cometd.javascript;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.cometd.javascript.jquery.JQueryTestProvider;
import org.cometd.server.BayeuxServerImpl;
import org.cometd.server.CometDServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

public abstract class AbstractCometDTest
{
    @Rule
    public final TestWatcher testName = new TestWatcher()
    {
        @Override
        public void starting(Description description)
        {
            super.starting(description);
            String providerClass = getProviderClassName();
            System.err.printf("Running %s.%s() [%s]%n",
                    description.getClassName(),
                    description.getMethodName(),
                    providerClass.substring(providerClass.lastIndexOf('.') + 1));
        }
    };
    protected TestProvider provider;
    private HttpCookieStore cookies;
    protected Server server;
    protected ServerConnector connector;
    protected ServletContextHandler context;
    protected CometDServlet cometdServlet;
    protected int metaConnectPeriod = 5000;
    protected String cometdServletPath = "/cometd";
    protected int port;
    protected String contextURL;
    protected String cometdURL;
    protected BayeuxServerImpl bayeuxServer;
    protected int expirationPeriod = 2500;
    protected ThreadModel threadModel;
    private XMLHttpRequestClient xhrClient;
    private WebSocketConnector wsConnector;

    @Before
    public void initCometDServer() throws Exception
    {
        Map<String, String> options = new HashMap<>();
        initCometDServer(options);
    }

    protected void initCometDServer(Map<String, String> options) throws Exception
    {
        prepareAndStartServer(options);
        initPage();
    }

    protected void prepareAndStartServer(Map<String, String> options) throws Exception
    {
        String providerClass = getProviderClassName();
        provider = (TestProvider)Thread.currentThread().getContextClassLoader().loadClass(providerClass).newInstance();

        cookies = new HttpCookieStore();

        server = new Server();
        connector = new ServerConnector(server);
        server.addConnector(connector);

        HandlerCollection handlers = new HandlerCollection();
        server.setHandler(handlers);

        String contextPath = "/cometd";
        context = new ServletContextHandler(handlers, contextPath, ServletContextHandler.SESSIONS);

        WebSocketServerContainerInitializer.configureContext(context);

        // Setup default servlet to serve static files
        context.addServlet(DefaultServlet.class, "/");

        // Setup CometD servlet
        String cometdURLMapping = cometdServletPath + "/*";
        cometdServlet = new CometDServlet();
        ServletHolder cometdServletHolder = new ServletHolder(cometdServlet);
        for (Map.Entry<String, String> entry : options.entrySet())
            cometdServletHolder.setInitParameter(entry.getKey(), entry.getValue());
        cometdServletHolder.setInitParameter("timeout", String.valueOf(metaConnectPeriod));
        cometdServletHolder.setInitParameter("ws.cometdURLMapping", cometdURLMapping);
        context.addServlet(cometdServletHolder, cometdURLMapping);

        customizeContext(context);

        startServer();

        contextURL = "http://localhost:" + port + contextPath;
        cometdURL = contextURL + cometdServletPath;
    }

    protected void startServer() throws Exception
    {
        connector.setPort(port);
        server.start();
        port = connector.getLocalPort();
        bayeuxServer = cometdServlet.getBayeux();
    }

    @After
    public void destroyCometDServer() throws Exception
    {
        destroyPage();
        stopServer();
    }

    protected void stopServer() throws Exception
    {
        server.stop();
        server.join();
        cookies.clear();
    }

    private String getProviderClassName()
    {
        return System.getProperty("toolkitTestProvider", JQueryTestProvider.class.getName());
    }

    protected String getLogLevel()
    {
        String property = Log.getLogger("org.cometd.javascript").isDebugEnabled() ? "debug" : "info";
        return property.toLowerCase(Locale.ENGLISH);
    }

    protected void customizeContext(ServletContextHandler context) throws Exception
    {
        File baseDirectory = new File(System.getProperty("basedir", "."));
        File overlaidScriptDirectory = new File(baseDirectory, "target/scripts");
        File mainResourcesDirectory = new File(baseDirectory, "src/main/resources");
        File testResourcesDirectory = new File(baseDirectory, "src/test/resources");
        context.setBaseResource(new ResourceCollection(new String[]
                {
                        overlaidScriptDirectory.getCanonicalPath(),
                        mainResourcesDirectory.getCanonicalPath(),
                        testResourcesDirectory.getCanonicalPath()
                }));
    }

    protected void initPage() throws Exception
    {
        initJavaScript();
        initCometD();
    }

    protected void initCometD() throws Exception
    {
        provider.provideCometD(threadModel, contextURL);
    }

    protected void initJavaScript() throws Exception
    {
        // Initializes the thread model
        org.mozilla.javascript.Context jsContext = org.mozilla.javascript.Context.enter();
        try
        {
            ScriptableObject rootScope = jsContext.initStandardObjects();

            // TODO: not needed now that HttpClient supports cookies
            ScriptableObject.defineClass(rootScope, HttpCookieStore.class);
            jsContext.evaluateString(rootScope, "var cookies = new HttpCookieStore();", "cookies", 1, null);
            HttpCookieStore cookies = (HttpCookieStore)rootScope.get("cookies", rootScope);
            cookies.putAll(this.cookies);
            this.cookies = cookies;

            ScriptableObject.defineClass(rootScope, JavaScriptThreadModel.class);
            jsContext.evaluateString(rootScope, "var threadModel = new JavaScriptThreadModel(this);", "threadModel", 1, null);
            threadModel = (ThreadModel)rootScope.get("threadModel", rootScope);
            threadModel.init();

            ScriptableObject.defineClass(rootScope, XMLHttpRequestClient.class);
            ScriptableObject.defineClass(rootScope, XMLHttpRequestExchange.class);
            jsContext.evaluateString(rootScope, "var xhrClient = new XMLHttpRequestClient(2);", "xhrClient", 1, null);
            xhrClient = (XMLHttpRequestClient)rootScope.get("xhrClient", rootScope);
            xhrClient.start();

            ScriptableObject.defineClass(rootScope, WebSocketConnector.class);
            ScriptableObject.defineClass(rootScope, WebSocketConnection.class);
            jsContext.evaluateString(rootScope, "var wsConnector = new WebSocketConnector();", "wsConnector", 1, null);
            wsConnector = (WebSocketConnector)rootScope.get("wsConnector", rootScope);
            wsConnector.start();
        }
        finally
        {
            org.mozilla.javascript.Context.exit();
        }
    }

    protected void provideTimestampExtension() throws Exception
    {
        provider.provideTimestampExtension(threadModel, contextURL);
    }

    protected void provideTimesyncExtension() throws Exception
    {
        provider.provideTimesyncExtension(threadModel, contextURL);
    }

    protected void provideMessageAcknowledgeExtension() throws Exception
    {
        provider.provideMessageAcknowledgeExtension(threadModel, contextURL);
    }

    protected void provideReloadExtension() throws Exception
    {
        provider.provideReloadExtension(threadModel, contextURL);
    }

    protected void destroyPage() throws Exception
    {
        destroyJavaScript();
    }

    protected void destroyJavaScript() throws Exception
    {
        wsConnector.stop();
        xhrClient.stop();
        threadModel.destroy();
    }

    @SuppressWarnings("unchecked")
    protected <T> T evaluateScript(String script)
    {
        return evaluateScript(null, script);
    }

    @SuppressWarnings("unchecked")
    protected <T> T evaluateScript(String scriptName, String script)
    {
        return (T)threadModel.evaluate(scriptName, script);
    }

    protected void defineClass(Class<? extends Scriptable> clazz) throws InvocationTargetException, InstantiationException, IllegalAccessException
    {
        threadModel.define(clazz);
    }

    @SuppressWarnings("unchecked")
    protected <T> T get(String name)
    {
        return (T) threadModel.get(name);
    }

    protected void sleep(long time)
    {
        try
        {
            Thread.sleep(time);
        }
        catch (InterruptedException x)
        {
            Thread.currentThread().interrupt();
            throw new RuntimeException(x);
        }
    }

    protected void disconnect() throws InterruptedException
    {
        evaluateScript("var disconnectLatch = new Latch(1);");
        Latch disconnectLatch = get("disconnectLatch");
        evaluateScript("cometd.addListener('/meta/disconnect', disconnectLatch, disconnectLatch.countDown);");
        evaluateScript("cometd.disconnect();");
        Assert.assertTrue(disconnectLatch.await(5000));
        String status = evaluateScript("cometd.getStatus();");
        Assert.assertEquals("disconnected", status);
    }
}
TOP

Related Classes of org.cometd.javascript.AbstractCometDTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.