@Test public void testHexConversion() throws Exception { assertNull(bytesToHex(null)); assertNull(hexToBytes(null)); assertEquals("", bytesToHex(new byte[]{})); assertArrayEquals(new byte[]{}, hexToBytes("")); JavaSerializationMarshaller marshaller = new JavaSerializationMarshaller(); byte[] before = marshaller.objectToByteBuffer(Calendar.getInstance()); byte[] after = hexToBytes(bytesToHex(before)); assertArrayEquals(before, after); assertEquals("0x010203", bytesToHex(new byte[]{1, 2, 3})); assertEquals("0x808080", bytesToHex(new byte[]{-128, -128, -128})); } }
@Test public void shouldGetAllEntriesConvertedToBytes() throws Exception { //given putStringValueInCache("default", "key1", "test1"); putStringValueInCache("default", "key2", "test2"); //when ContentResponse response = client .newRequest(String.format("http://localhost:%d/rest/%s", restServer().getPort(), "default")) .header(HttpHeader.ACCEPT, MediaType.APPLICATION_OCTET_STREAM_TYPE) .send(); //then ResponseAssertion.assertThat(response).isOk(); ResponseAssertion.assertThat(response).hasContentType(MediaType.APPLICATION_OCTET_STREAM_TYPE); // keys can be returned in any order ResponseAssertion.assertThat(response).hasReturnedText( String.format("%s\n%s", bytesToHex("key1".getBytes()), bytesToHex("key2".getBytes())), String.format("%s\n%s", bytesToHex("key2".getBytes()), bytesToHex("key1".getBytes()))); }
@Test public void testCustomKeysAndByteValues() throws Exception { String customKeyType = "application/x-java-object; type=ByteArray"; CustomKey objectKey = new CustomKey("a", 1.0d, 1.0f, true); byte[] key = new GenericJBossMarshaller().objectToByteBuffer(objectKey); byte[] value = {12}; // Write <byte[], byte[]> via Hot Rod (the HR client is configured with a no-op marshaller) defaultRemoteCache.put(key, value); assertEquals(value, defaultRemoteCache.get(key)); String restKey = StandardConversions.bytesToHex(key); // Read via Rest Object bytesFromRest = new RestRequest().cache(DEFAULT_CACHE_NAME) .key(restKey, customKeyType).accept(APPLICATION_OCTET_STREAM) .read(); assertArrayEquals((byte[]) bytesFromRest, value); }