Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * JBoss, Home of Professional Open Source
  * Copyright 2010 Red Hat Inc. and/or its affiliates and other
  * contributors as indicated by the @author tags. All rights reserved.
  * See the copyright.txt in the distribution for a full listing of
  * individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.infinispan.server.websocket.handlers;
import  org.json.JSONException;
import  org.json.JSONObject;

Author(s):
tom.fennelly@gmail.com
@Test (testName = "websocket.handlers.OpHandlerTest", groups = "unit")
public class OpHandlerTest {
	public void test() throws JSONException {
		MockChannel mockChannel = new MockChannel();
		MockClient firstCacheClient = new MockClient("firstCache"mockChannel);
      try {
         JSONObject jsonPayload;
         // Put...
         firstCacheClient.put("a""aVal");
         firstCacheClient.put("b""bVal");
         // Get...
         firstCacheClient.get("a");
         jsonPayload = mockChannel.getJSONPayload();
         Assert.assertEquals("firstCache"jsonPayload.get(.));
         Assert.assertEquals("a"jsonPayload.get(.));
         Assert.assertEquals("aVal"jsonPayload.get(.));
         Assert.assertEquals("text/plain"jsonPayload.get(.));
         firstCacheClient.get("b");
         jsonPayload = mockChannel.getJSONPayload();
         Assert.assertEquals("firstCache"jsonPayload.get(.));
         Assert.assertEquals("b"jsonPayload.get(.));
         Assert.assertEquals("bVal"jsonPayload.get(.));
         Assert.assertEquals("text/plain"jsonPayload.get(.));
         firstCacheClient.get("x"); // not in cache
         jsonPayload = mockChannel.getJSONPayload();
         Assert.assertEquals("firstCache"jsonPayload.get(.));
         Assert.assertEquals("x"jsonPayload.get(.));
         Assert.assertEquals(nulljsonPayload.get(.));
         // Notify...
         firstCacheClient.notify("a");
         // Call to notify immediately pushes the value and then pushes it again later on modify...
         jsonPayload = mockChannel.getJSONPayload(1000);
         Assert.assertEquals("aVal"jsonPayload.get(.));
         // Modify the value should result in a push notification...
         firstCacheClient.getCache().put("a""aNewValue");
         jsonPayload = mockChannel.getJSONPayload();
         Assert.assertEquals("aNewValue"jsonPayload.get(.));
         // Modify something we're not listening to... nothing should happen...
         firstCacheClient.getCache().put("b""bNewValue");
         try {
            mockChannel.getJSONPayload(500);
            Assert.fail("Expected timeout");
         } catch (RuntimeException e) {
            Assert.assertEquals("Timed out waiting for data to be pushed onto the channel."e.getMessage());
         }
         // Remove...
         firstCacheClient.remove("a");
         firstCacheClient.get("a");
         jsonPayload = mockChannel.getJSONPayload();
         Assert.assertEquals("firstCache"jsonPayload.get(.));
         Assert.assertEquals("a"jsonPayload.get(.));
         Assert.assertEquals(nulljsonPayload.get(.));
      } finally {
         firstCacheClient.stop();
      }
   }
New to GrepCode? Check out our FAQ X