Java tutorial
package com.conwet.silbops.connectors.comet; /* * #%L * SilboPS Service * %% * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.atmosphere.cpr.AtmosphereResource; import org.json.simple.JSONObject; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import com.conwet.silbops.api.EndPointException; import com.conwet.silbops.msg.Message; public class SubCometConnectionTest { private SubCometConnection instance; private String endpointID; @Mock private PrintWriter writer; @Mock private AtmosphereResource<HttpServletRequest, HttpServletResponse> resource; @Before public void setup() { MockitoAnnotations.initMocks(this); endpointID = "id"; instance = new SubCometConnection(endpointID, resource); } @Test public void shouldBeSubscriber() { assertThat(instance.isPublisher()).isFalse(); assertThat(instance.isRouter()).isFalse(); assertThat(instance.isSubscriber()).isTrue(); } @Test @SuppressWarnings("unchecked") public void shouldSendMessage() throws IOException, EndPointException { HttpServletResponse respose = mock(HttpServletResponse.class); when(resource.getResponse()).thenReturn(respose); when(respose.getWriter()).thenReturn(writer); final JSONObject payload = new JSONObject(); payload.put("key", "value"); Message message = new Message() { @Override public JSONObject getPayloadAsJSON() { return payload; } }; instance.sendMessage(message); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(writer).println(captor.capture()); verify(writer).flush(); assertThat(captor.getValue()).contains("SilboPS.recvMessage").contains(endpointID) .contains(payload.toJSONString().replaceAll("\"", "\\\\\"")); } }