/*
 * 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.server.netty4;

import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThat;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;

import java.net.URI;

import org.atmosphere.vibe.platform.Action;
import org.atmosphere.vibe.platform.server.ServerWebSocket;
import org.atmosphere.vibe.platform.test.server.ServerWebSocketTestTemplate;
import org.junit.Test;

public class NettyServerWebSocketTest extends ServerWebSocketTestTemplate {

    EventLoopGroup bossGroup;
    EventLoopGroup workerGroup;

    @Override
    protected void startServer() {
        bossGroup = new NioEventLoopGroup();
        workerGroup = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup)
        .channel(NioServerSocketChannel.class)
        .childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new HttpServerCodec())
                .addLast(new VibeServerCodec() {
                    @Override
                    protected boolean accept(HttpRequest req) {
                        return URI.create(req.getUri()).getPath().equals("/test");
                    }
                    
                    @Override
                    public Action<ServerWebSocket> wsAction() {
                        return new Action<ServerWebSocket>() {
                            @Override
                            public void on(ServerWebSocket ws) {
                                performer.serverAction().on(ws);
                            }
                        };
                    }
                });
            }
        });
        bootstrap.bind(port);
    }

    @Override
    protected void stopServer() {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

    @Test
    public void unwrap() {
        performer.serverAction(new Action<ServerWebSocket>() {
            @Override
            public void on(ServerWebSocket ws) {
                assertThat(ws.unwrap(ChannelHandlerContext.class), instanceOf(ChannelHandlerContext.class));
                assertThat(ws.unwrap(WebSocketServerHandshaker.class), instanceOf(WebSocketServerHandshaker.class));
                assertThat(ws.unwrap(FullHttpRequest.class), instanceOf(FullHttpRequest.class));
                performer.start();
            }
        })
        .connect();
    }

}

@@ -945,32 +945,127 @@
 EventLoopGroup;%0A
+import io.netty.channel.group.ChannelGroup;%0Aimport io.netty.channel.group.DefaultChannelGroup;%0A
 import io.netty.
@@ -1424,16 +1424,69 @@
 dshaker;
+%0Aimport io.netty.util.concurrent.GlobalEventExecutor;
 %0A%0Aimport
@@ -1844,16 +1844,43 @@
 erGroup;
+%0A    ChannelGroup channels;
 %0A%0A    @O
@@ -2006,32 +2006,106 @@
 entLoopGroup();%0A
+        channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);%0A
         ServerBo
@@ -2302,24 +2302,190 @@
 hannel%3E() %7B%0A
+            @Override%0A            public void channelActive(ChannelHandlerContext ctx) throws Exception %7B%0A                channels.add(ctx.channel());%0A            %7D%0A%0A
             
@@ -3400,32 +3400,45 @@
     %7D);%0A        
+channels.add(
 bootstrap.bind(p
@@ -3441,16 +3441,27 @@
 nd(port)
+.channel())
 ;%0A    %7D%0A
@@ -3501,32 +3501,58 @@
  stopServer() %7B%0A
+        channels.close();%0A
         workerGr

Apply the same fix to ServerWebSocketTest #3