/** * Copyright 2007-2015, Kaazing Corporation. All rights reserved. * * 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.kaazing.k3po.driver.internal.behavior.handler.barrier; import static java.lang.Boolean.TRUE; import static org.jboss.netty.channel.ChannelState.OPEN; import static org.jboss.netty.channel.Channels.pipeline; import static org.junit.Assert.assertTrue; import org.jboss.netty.channel.ChannelDownstreamHandler; import org.jboss.netty.channel.ChannelEvent; import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelUpstreamHandler; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.local.DefaultLocalClientChannelFactory; import org.junit.Before; import org.junit.Test; import org.kaazing.k3po.driver.internal.behavior.Barrier; import org.kaazing.k3po.driver.internal.behavior.handler.prepare.PreparationEvent; import org.kaazing.k3po.driver.internal.jmock.Expectations; import org.kaazing.k3po.driver.internal.jmock.Mockery; public class NotifyBarrierHandlerTest { private Mockery context; private ChannelUpstreamHandler upstream; private ChannelDownstreamHandler downstream; private ChannelPipeline pipeline; private ChannelFactory channelFactory; private NotifyBarrierHandler handler; private Barrier barrier; @Before public void setUp() throws Exception { context = new Mockery() { { setThrowFirstErrorOnAssertIsSatisfied(true); } }; upstream = context.mock(ChannelUpstreamHandler.class); downstream = context.mock(ChannelDownstreamHandler.class); barrier = new Barrier("BARRIER"); handler = new NotifyBarrierHandler(barrier); pipeline = pipeline(new SimpleChannelHandler() { @Override public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent evt) throws Exception { downstream.handleDownstream(ctx, evt); super.handleDownstream(ctx, evt); } }, handler, new SimpleChannelHandler() { @Override public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception { upstream.handleUpstream(ctx, e); super.handleUpstream(ctx, e); } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { // prevent console error message } }); channelFactory = new DefaultLocalClientChannelFactory(); } @Test public void shouldPropagateDownstreamOpenAndBarrierFutureSuccess() throws Exception { context.checking(new Expectations() { { oneOf(upstream).handleUpstream(with(any(ChannelHandlerContext.class)), with(any(PreparationEvent.class))); oneOf(upstream).handleUpstream(with(any(ChannelHandlerContext.class)), with(channelState(OPEN, TRUE))); } }); channelFactory.newChannel(pipeline); ChannelFuture handlerFuture = handler.getHandlerFuture(); assertTrue(handlerFuture.isSuccess()); context.assertIsSatisfied(); } }