DefaultResourceHandler implements IResourceHandler { private byte[] asByteArray(Object value) { if (value instanceof byte[]) { return (byte[]) value; } else if (value instanceof String) { try { return ((String) value).getBytes("utf-8"); } catch (UnsupportedEncodingException e) { return ((String) value).getBytes(); } } else { throw new RuntimeException("Can't convert value to byte array, value:" + value); } }  @Override byte[] getBinaryContent(IResource resource, IResourceContext context); @Override String getContent(IResource resource, IResourceContext context); @Override String getUrl(IResource resource, IResourceContext context);  }
@Test public void testAsByteArray() throws NoSuchMethodException, SecurityException, ReflectiveOperationException, IllegalArgumentException, InvocationTargetException, UnsupportedEncodingException { Class<DefaultResourceHandler> clazz = DefaultResourceHandler.class; Method method = clazz.getDeclaredMethod("asByteArray", Object.class); method.setAccessible(true); String str = "test string for asByteArray"; Assert.assertEquals(str, new String((byte[])method.invoke(m_handler, str))); byte[] byteArray = str.getBytes("utf-8"); Assert.assertEquals(str, new String((byte[])method.invoke(m_handler, byteArray))); try { method.invoke(m_handler, new Object()); } catch (Exception e) { Assert.assertTrue(e instanceof Exception); } }