com.seajas.search.utilities.remoting.JsonHttpSupport.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.utilities.remoting.JsonHttpSupport.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * 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 General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.utilities.remoting;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;

import org.springframework.remoting.support.RemoteInvocation;
import org.springframework.remoting.support.RemoteInvocationResult;

import org.junit.Test;

/**
 * Tests {@link JsonInvokerRequestExecutor} and {@link JsonInvokerServiceExporter}.
 @author Pascal S. de Kloe
 */
public class JsonHttpSupport {

    JsonInvokerServiceExporter exporter = new JsonInvokerServiceExporter();
    JsonInvokerRequestExecutor executor = new JsonInvokerRequestExecutor();

    @Test
    public void invocation() throws Exception {
        String method = "testMethodName";
        Class[] types = new Class[] { Boolean.class, String.class, Number.class };
        Object[] arguments = new Object[] { Boolean.TRUE, null, new Integer(2) };
        RemoteInvocation invocation = new RemoteInvocation(method, types, arguments);

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        executor.writeRemoteInvocation(invocation, buffer);
        ByteArrayInputStream stream = new ByteArrayInputStream(buffer.toByteArray());
        RemoteInvocation deserialized = exporter.readRemoteInvocation(null, stream);
        assertEquals("method", method, deserialized.getMethodName());
        assertArrayEquals("types", types, deserialized.getParameterTypes());
        assertArrayEquals("arguments", arguments, deserialized.getArguments());
    }

    @Test
    public void resultValue() throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("a", null);
        map.put("b", "text");
        // stack overflow: map.put("c", map);
        RemoteInvocationResult result = new RemoteInvocationResult(map);

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        exporter.writeRemoteInvocationResult(null, result, buffer);
        ByteArrayInputStream stream = new ByteArrayInputStream(buffer.toByteArray());
        RemoteInvocationResult deserialized = executor.readRemoteInvocationResult(stream, null);
        assertEquals(result.getValue(), deserialized.getValue());
        assertNull(deserialized.getException());
    }

    @Test
    public void resultError() throws Exception {
        Throwable error = new RuntimeException("test");
        RemoteInvocationResult result = new RemoteInvocationResult(error);

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        exporter.writeRemoteInvocationResult(null, result, buffer);
        ByteArrayInputStream stream = new ByteArrayInputStream(buffer.toByteArray());
        RemoteInvocationResult deserialized = executor.readRemoteInvocationResult(stream, null);
        assertNull(deserialized.getValue());
        assertNotNull(deserialized.getException());
        assertEquals(error.getClass(), deserialized.getException().getClass());
        assertEquals(error.getMessage(), deserialized.getException().getMessage());
    }

}