// Path: action/src/main/java/org/atmosphere/vibe/platform/action/Action.java
// public interface Action<T> {
// 
//     /**
//      * Some action is taken.
//      */
//     void on(T object);
// 
// }
// 
// Path: action/src/main/java/org/atmosphere/vibe/platform/action/VoidAction.java
// public abstract class VoidAction implements Action<Void> {
// 
//     @Override
//     public void on(Void _) {
//         on();
//     }
// 
//     public abstract void on();
// 
// }
// 
// Path: websocket/src/main/java/org/atmosphere/vibe/platform/websocket/ServerWebSocket.java
// public interface ServerWebSocket {
// 
//     /**
//      * The URI used to connect.
//      */
//     String uri();
// 
//     /**
//      * Closes the connection. This method has no side effect if called more than
//      * once.
//      */
//     void close();
// 
//     /**
//      * Sends a text frame through the connection.
//      */
//     ServerWebSocket send(String data);
// 
//     /**
//      * Sends a binary frame through the connection.
//      */
//     ServerWebSocket send(ByteBuffer byteBuffer);
// 
//     /**
//      * Attaches an action for the text frame.
//      */
//     ServerWebSocket ontext(Action<String> action);
// 
//     /**
//      * Attaches an action for the binary frame.
//      */
//     ServerWebSocket onbinary(Action<ByteBuffer> action);
// 
//     /**
//      * Attaches an action for the close event. After this event, the instance
//      * shouldn't be used and all the other events will be disabled.
//      */
//     ServerWebSocket onclose(Action<Void> action);
// 
//     /**
//      * Attaches an action to handle error from various things. Its exact
//      * behavior is platform-specific and error created by the platform is
//      * propagated.
//      */
//     ServerWebSocket onerror(Action<Throwable> action);
// 
//     /**
//      * Returns the provider-specific component.
//      */
//     <T> T unwrap(Class<T> clazz);
// 
// }


import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.net.ServerSocket;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch;
import org.atmosphere.vibe.platform.action.Action;
import org.atmosphere.vibe.platform.action.VoidAction;
import org.atmosphere.vibe.platform.websocket.ServerWebSocket;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.eclipse.jetty.websocket.api.WebSocketListener;
import org.eclipse.jetty.websocket.api.WriteCallback;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
/*
 * Copyright 2014 The Vibe Project
 *
 * 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.atmosphere.vibe.platform.test;




/**
 * Template class to test {@link ServerWebSocket}.
 *
 * @author Donghwan Kim
 */
public abstract class ServerWebSocketTest {

    @Rule
    public Timeout globalTimeout = Timeout.seconds(10);
    protected Performer performer;
    protected int port;

    @Before
    public void before() throws Exception {
        performer = new Performer();
        try (ServerSocket serverSocket = new ServerSocket(0)) {
            port = serverSocket.getLocalPort();
        }
        startServer();
    }

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

    /**
     * Starts the server listening port {@link ServerWebSocketTest#port}
     * and if WebSocket's path is {@code /test}, create {@link ServerWebSocket}
     * and pass it to {@code performer.serverAction()}. This method is executed
     * following {@link Before}.
     */
    protected abstract void startServer() throws Exception;

    /**
     * Stops the server started in
     * {@link ServerWebSocketTest#startServer()}. This method is
     * executed following {@link After}.
     *
     * @throws Exception
     */
    protected abstract void stopServer() throws Exception;

    @Test
    public void uri() {
// Path: action/src/main/java/org/atmosphere/vibe/platform/action/Action.java
// public interface Action<T> {
// 
//     /**
//      * Some action is taken.
//      */
//     void on(T object);
// 
// }
// 
// Path: action/src/main/java/org/atmosphere/vibe/platform/action/VoidAction.java
// public abstract class VoidAction implements Action<Void> {
// 
//     @Override
//     public void on(Void _) {
//         on();
//     }
// 
//     public abstract void on();
// 
// }
// 
// Path: websocket/src/main/java/org/atmosphere/vibe/platform/websocket/ServerWebSocket.java
// public interface ServerWebSocket {
// 
//     /**
//      * The URI used to connect.
//      */
//     String uri();
// 
//     /**
//      * Closes the connection. This method has no side effect if called more than
//      * once.
//      */
//     void close();
// 
//     /**
//      * Sends a text frame through the connection.
//      */
//     ServerWebSocket send(String data);
// 
//     /**
//      * Sends a binary frame through the connection.
//      */
//     ServerWebSocket send(ByteBuffer byteBuffer);
// 
//     /**
//      * Attaches an action for the text frame.
//      */
//     ServerWebSocket ontext(Action<String> action);
// 
//     /**
//      * Attaches an action for the binary frame.
//      */
//     ServerWebSocket onbinary(Action<ByteBuffer> action);
// 
//     /**
//      * Attaches an action for the close event. After this event, the instance
//      * shouldn't be used and all the other events will be disabled.
//      */
//     ServerWebSocket onclose(Action<Void> action);
// 
//     /**
//      * Attaches an action to handle error from various things. Its exact
//      * behavior is platform-specific and error created by the platform is
//      * propagated.
//      */
//     ServerWebSocket onerror(Action<Throwable> action);
// 
//     /**
//      * Returns the provider-specific component.
//      */
//     <T> T unwrap(Class<T> clazz);
// 
// }


// Path: test/src/main/java/org/atmosphere/vibe/platform/test/ServerWebSocketTest.java
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.net.ServerSocket;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch;
import org.atmosphere.vibe.platform.action.Action;
import org.atmosphere.vibe.platform.action.VoidAction;
import org.atmosphere.vibe.platform.websocket.ServerWebSocket;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.eclipse.jetty.websocket.api.WebSocketListener;
import org.eclipse.jetty.websocket.api.WriteCallback;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;

/*
 * Copyright 2014 The Vibe Project
 *
 * 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.atmosphere.vibe.platform.test;




/**
 * Template class to test {@link ServerWebSocket}.
 *
 * @author Donghwan Kim
 */
public abstract class ServerWebSocketTest {

    @Rule
    public Timeout globalTimeout = Timeout.seconds(10);
    protected Performer performer;
    protected int port;

    @Before
    public void before() throws Exception {
        performer = new Performer();
        try (ServerSocket serverSocket = new ServerSocket(0)) {
            port = serverSocket.getLocalPort();
        }
        startServer();
    }

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

    /**
     * Starts the server listening port {@link ServerWebSocketTest#port}
     * and if WebSocket's path is {@code /test}, create {@link ServerWebSocket}
     * and pass it to {@code performer.serverAction()}. This method is executed
     * following {@link Before}.
     */
    protected abstract void startServer() throws Exception;

    /**
     * Stops the server started in
     * {@link ServerWebSocketTest#startServer()}. This method is
     * executed following {@link After}.
     *
     * @throws Exception
     */
    protected abstract void stopServer() throws Exception;

    @Test
    public void uri() {
        performer.onserver(new Action<ServerWebSocket>() {