// // ======================================================================== // Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd. // ------------------------------------------------------------------------ // All rights reserved. This program and the accompanying materials // are made available under the terms of the Eclipse Public License v1.0 // and Apache License v2.0 which accompanies this distribution. // // The Eclipse Public License is available at // http://www.eclipse.org/legal/epl-v10.html // // The Apache License v2.0 is available at // http://www.opensource.org/licenses/apache2.0.php // // You may elect to redistribute this code under either of these licenses. // ======================================================================== // package org.eclipse.jetty.websocket.common.test; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.is; import java.util.LinkedList; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.websocket.api.BatchMode; import org.eclipse.jetty.websocket.api.WriteCallback; import org.eclipse.jetty.websocket.api.extensions.Frame; import org.eclipse.jetty.websocket.api.extensions.OutgoingFrames; import org.eclipse.jetty.websocket.common.OpCode; import org.eclipse.jetty.websocket.common.WebSocketFrame; import org.junit.Assert; public class OutgoingFramesCapture implements OutgoingFrames { private LinkedList<WebSocketFrame> frames = new LinkedList<>(); public void assertFrameCount(int expectedCount) { Assert.assertThat("Captured frame count",frames.size(),is(expectedCount)); } public void assertHasFrame(byte op) { Assert.assertThat(OpCode.name(op),getFrameCount(op),greaterThanOrEqualTo(1)); } public void assertHasFrame(byte op, int expectedCount) { Assert.assertThat(OpCode.name(op),getFrameCount(op),is(expectedCount)); } public void assertHasNoFrames() { Assert.assertThat("Has no frames",frames.size(),is(0)); } public void dump() { System.out.printf("Captured %d outgoing writes%n",frames.size()); for (int i = 0; i < frames.size(); i++) { Frame frame = frames.get(i); System.out.printf("[%3d] %s%n",i,frame); System.out.printf(" %s%n",BufferUtil.toDetailString(frame.getPayload())); } } public int getFrameCount(byte op) { int count = 0; for (WebSocketFrame frame : frames) { if (frame.getOpCode() == op) { count++; } } return count; } public LinkedList<WebSocketFrame> getFrames() { return frames; } @Override public void outgoingFrame(Frame frame, WriteCallback callback, BatchMode batchMode) { frames.add(WebSocketFrame.copy(frame)); if (callback != null) { callback.writeSuccess(); } } }