Skip to content

Commit

Permalink
ISPN-8038 java.lang.ClassCastException with compatibility mode and te…
Browse files Browse the repository at this point in the history
…xt content
  • Loading branch information
gustavonalle authored and anistor committed Jul 13, 2017
1 parent bade9dd commit ffec57c
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 10 deletions.
Expand Up @@ -5,15 +5,14 @@
import static org.infinispan.server.hotrod.test.HotRodTestingUtil.hotRodCacheConfiguration;
import static org.testng.AssertJUnit.assertEquals;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

import org.infinispan.client.hotrod.event.EventLogListener;
import org.infinispan.client.hotrod.test.InternalRemoteCacheManager;
import org.infinispan.client.hotrod.test.MultiHotRodServersTest;
import org.infinispan.commons.marshall.StringMarshaller;
import org.infinispan.commons.marshall.UTF8StringMarshaller;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.testng.AssertJUnit;
Expand Down Expand Up @@ -47,7 +46,7 @@ protected void createCacheManagers() throws Throwable {
private RemoteCacheManager createExecClient() {
org.infinispan.client.hotrod.configuration.ConfigurationBuilder clientBuilder =
super.createHotRodClientConfigurationBuilder(servers.get(0).getPort());
clientBuilder.marshaller(new StringMarshaller(Charset.forName("UTF-8")));
clientBuilder.marshaller(new UTF8StringMarshaller());
return new InternalRemoteCacheManager(clientBuilder.build());
}

Expand Down
Expand Up @@ -19,6 +19,7 @@ public BinaryEncoder(StreamingMarshaller marshaller) {
this.marshaller = marshaller;
}

@Override
public Object toStorage(Object content) {
try {
return skipEncoding(content) ? content : marshall(content);
Expand All @@ -27,6 +28,7 @@ public Object toStorage(Object content) {
}
}

@Override
public Object fromStorage(Object stored) {
try {
if (isTypeExcluded(stored.getClass())) {
Expand Down
Expand Up @@ -19,6 +19,7 @@ public CompatModeEncoder(Marshaller marshaller) {
this.marshaller = marshaller == null ? new GenericJBossMarshaller() : marshaller;
}

@Override
public Object toStorage(Object content) {
if (content instanceof byte[]) {
try {
Expand All @@ -30,10 +31,11 @@ public Object toStorage(Object content) {
return content;
}

@Override
public Object fromStorage(Object content) {
try {
return marshall(content);
} catch (InterruptedException | IOException e) {
} catch (Exception e) {
throw new CacheException(e);
}
}
Expand All @@ -47,7 +49,9 @@ protected Object unmarshall(byte[] source) throws IOException, ClassNotFoundExce
return marshaller.objectFromByteBuffer(source);
}

protected byte[] marshall(Object source) throws IOException, InterruptedException {
return marshaller.objectToByteBuffer(source);
protected Object marshall(Object source) throws Exception {
if (marshaller.isMarshallable(source))
return marshaller.objectToByteBuffer(source);
return source;
}
}
Expand Up @@ -19,6 +19,7 @@ public MarshallerEncoder(Marshaller marshaller) {
this.marshaller = marshaller;
}

@Override
public Object toStorage(Object content) {
try {
return marshall(content);
Expand All @@ -27,6 +28,7 @@ public Object toStorage(Object content) {
}
}

@Override
public Object fromStorage(Object stored) {
try {
return stored instanceof byte[] ? marshaller.objectFromByteBuffer((byte[]) stored) : stored;
Expand Down
Expand Up @@ -13,11 +13,13 @@ public class UTF8Encoder implements Encoder {

public static final UTF8Encoder INSTANCE = new UTF8Encoder();

@Override
public Object toStorage(Object content) {
if (content instanceof String) return String.class.cast(content).getBytes(CHARSET_UTF8);
throw new EncodingException("Cannot encode " + content);
}

@Override
public Object fromStorage(Object stored) {
return new String((byte[]) stored, CHARSET_UTF8);
}
Expand Down
Expand Up @@ -6,7 +6,7 @@
import org.infinispan.commons.io.ByteBuffer;
import org.infinispan.commons.io.ByteBufferImpl;

public final class StringMarshaller extends AbstractMarshaller {
public class StringMarshaller extends AbstractMarshaller {

final Charset charset;

Expand Down
@@ -0,0 +1,11 @@
package org.infinispan.commons.marshall;

import static java.nio.charset.StandardCharsets.UTF_8;

public final class UTF8StringMarshaller extends StringMarshaller {

public UTF8StringMarshaller() {
super(UTF_8);
}

}
Expand Up @@ -8,12 +8,11 @@
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.charset.Charset;
import java.util.Arrays;

import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.commons.marshall.StringMarshaller;
import org.infinispan.commons.marshall.UTF8StringMarshaller;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.test.AbstractInfinispanTest;
import org.testng.annotations.AfterClass;
Expand All @@ -37,7 +36,7 @@ public class CustomMemcachedHotRodTest extends AbstractInfinispanTest {
@BeforeClass
protected void setup() throws Exception {
cacheFactory = new CompatibilityCacheFactory<String, String>(
CACHE_NAME, new StringMarshaller(Charset.forName("UTF-8")), CacheMode.LOCAL).setup();
CACHE_NAME, new UTF8StringMarshaller(), CacheMode.LOCAL).setup();
}

@AfterClass
Expand Down
@@ -0,0 +1,63 @@
package org.infinispan.it.compatibility;

import static org.testng.AssertJUnit.assertEquals;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.commons.marshall.UTF8StringMarshaller;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.test.AbstractInfinispanTest;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

@Test(groups = "functional", testName = "it.compatibility.EmbeddedRestHotRodWithStringTest")
public class EmbeddedRestHotRodWithStringTest extends AbstractInfinispanTest {

CompatibilityCacheFactory<String, Object> cacheFactory;

@BeforeClass
protected void setup() throws Exception {
cacheFactory = new CompatibilityCacheFactory<String, Object>("testCache", new UTF8StringMarshaller(), CacheMode.LOCAL).setup();
}

@AfterClass
protected void teardown() {
CompatibilityCacheFactory.killCacheFactories(cacheFactory);
}

public void testRestPutStringHotRodGet() throws Exception {
final String key = "1";

// 1. Put text content with REST
EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key);
put.setRequestEntity(new StringRequestEntity("<hey>ho</hey>", "application/xml", "UTF-8"));
HttpClient restClient = cacheFactory.getRestClient();
restClient.executeMethod(put);
assertEquals(HttpStatus.SC_OK, put.getStatusCode());
assertEquals("", put.getResponseBodyAsString().trim());

// 3. Get with Hot Rod
assertEquals("<hey>ho</hey>", cacheFactory.getHotRodCache().get(key));

final String newKey = "2";
final String newValue = "<let's>go</let's>";

//4. Put text content with Hot Rod
RemoteCache<String, Object> hotRodCache = cacheFactory.getHotRodCache();
hotRodCache.put(newKey, newValue);

//5. Read with rest
HttpMethod get = new GetMethod(cacheFactory.getRestUrl() + "/" + newKey);
cacheFactory.getRestClient().executeMethod(get);
assertEquals(HttpStatus.SC_OK, get.getStatusCode());
assertEquals(newValue, get.getResponseBodyAsString());
}

}

0 comments on commit ffec57c

Please sign in to comment.