### Question:
DefaultHandlerRegistry implements HandlerRegistry { public RpcHandler getRpcHandler(JSONObject rpc) { try { String key = rpc.getString("method"); RpcInvocationHandler rpcHandler = rpcOperations.get(key); if (rpcHandler == null) { return new ErrorRpcHandler(new ProtocolException(HttpServletResponse.SC_NOT_IMPLEMENTED, "The method " + key + " is not implemented")); } return new RpcInvocationWrapper(rpcHandler, rpc); } catch (JSONException je) { return new ErrorRpcHandler(new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "No method requested in RPC")); } } @Inject  DefaultHandlerRegistry(Injector injector,
                                BeanJsonConverter beanJsonConverter,
                                HandlerExecutionListener executionListener); void addHandlers(Set<Object> handlers); RpcHandler getRpcHandler(JSONObject rpc); RestHandler getRestHandler(String path, String method); Set<String> getSupportedRestServices(); Set<String> getSupportedRpcServices();  }### Answer:
@Test public void testGetHandlerRPC() throws Exception { assertNotNull(registry.getRpcHandler(new JSONObject("{method : test.get}"))); }
@Test public void testOverrideHandlerRPC() throws Exception { assertNotNull(registry.getRpcHandler(new JSONObject("{method : test.overidden}"))); }
@Test public void testOverrideHandlerRPCName() throws Exception { assertNotNull(registry.getRpcHandler(new JSONObject("{method : test.override.rpcname}"))); }
@Test public void testRpcHandler_serviceDoesntExist() throws Exception { JSONObject rpc = new JSONObject("{method : makebelieve.get}"); RpcHandler rpcHandler = registry.getRpcHandler(rpc); try { Future<?> future = rpcHandler.execute(null, null, null); future.get(); fail("Expect exception for missing method"); } catch (ExecutionException t) { assertSame(t.getCause().getClass(), ProtocolException.class); Assert.assertEquals(HttpServletResponse.SC_NOT_IMPLEMENTED, ((ProtocolException) t.getCause()).getCode()); } catch (Throwable t) { fail("Unexpected exception " + t.toString()); } }
@Test public void testRpcWithInputClassThatIsntRequestItem() throws Exception { JSONObject rpc = new JSONObject("{ method : test.echo, params: {value: 'Bob' }}"); RpcHandler handler = registry.getRpcHandler(rpc); Future<?> future = handler.execute(null, null, converter); assertEquals(future.get(), TestHandler.ECHO_PREFIX + "Bob"); }
@Test public void testNoArgumentClass() throws Exception { JSONObject rpc = new JSONObject("{ method : test.noArg }"); RpcHandler handler = registry.getRpcHandler(rpc); Future<?> future = handler.execute(null, null, converter); assertEquals(TestHandler.NO_ARG_RESPONSE, future.get()); }
@Test public void testNonFutureException() throws Exception { JSONObject rpc = new JSONObject("{ method : test.exception }"); RpcHandler handler = registry.getRpcHandler(rpc); Future<?> future = handler.execute(null, null, null); try { future.get(); fail("Service method did not produce NullPointerException from Future"); } catch (ExecutionException ee) { assertSame(ee.getCause().getClass(), NullPointerException.class); } }
@Test public void testFutureException() throws Exception { JSONObject rpc = new JSONObject("{ method : test.futureException }"); RpcHandler handler = registry.getRpcHandler(rpc); Future<?> future = handler.execute(null, null, null); try { future.get(); fail("Service method did not produce ExecutionException from Future"); } catch (ExecutionException ee) { assertSame(ee.getCause().getClass(), ProtocolException.class); } }