Skip to content

Commit

Permalink
ISPN-16317 Unit Test Remote Encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
karesti committed Jul 24, 2024
1 parent 5aa803c commit 445d3be
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 25 deletions.
5 changes: 5 additions & 0 deletions infinispan-remote/encoding/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-hotrod</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,18 @@

public class InfinispanEncodingCaches {

public static void main(String[] args) throws Exception {
ConfigurationBuilder builder = TutorialsConnectorHelper.connectionConfig();

URI textCacheURI = InfinispanEncodingCaches.class.getClassLoader().getResource("textCache.xml").toURI();
URI jsonCacheURI = InfinispanEncodingCaches.class.getClassLoader().getResource("jsonCache.xml").toURI();
URI xmlCacheURI = InfinispanEncodingCaches.class.getClassLoader().getResource("xmlCache.xml").toURI();
static RemoteCacheManager cacheManager;
static RemoteCache<String, String> textCache;
static RemoteCache<String, String> jsonCache;
static RemoteCache<String, String> xmlCache;

builder.remoteCache("textCache").configurationURI(textCacheURI);
builder.remoteCache("jsonCache").configurationURI(jsonCacheURI);
builder.remoteCache("xmlCache").configurationURI(xmlCacheURI);

RemoteCacheManager cacheManager = TutorialsConnectorHelper.connect(builder);
RemoteCache<String, String> textCache = cacheManager.getCache("textCache");
RemoteCache<String, String> jsonCache = cacheManager.getCache("jsonCache")
.withDataFormat(DataFormat.builder()
.keyMarshaller(new UTF8StringMarshaller())
.valueMarshaller(new UTF8StringMarshaller())
.keyType(MediaType.APPLICATION_JSON)
.valueType(MediaType.APPLICATION_JSON).build());
RemoteCache<String, String> xmlCache = cacheManager.getCache("xmlCache")
.withDataFormat(DataFormat.builder()
.keyMarshaller(new UTF8StringMarshaller())
.valueMarshaller(new UTF8StringMarshaller())
.keyType(MediaType.APPLICATION_XML)
.valueType(MediaType.APPLICATION_XML).build());
public static void main(String[] args) throws Exception {
connectToInfinispan();
manipulateCachesAndPrint();
disconnect(false);
}

static void manipulateCachesAndPrint() {
System.out.println("== Cache with text encoding and string marshaller.");
textCache.put("text", "诶, 你好.");
System.out.println("Get key from text cache: " + textCache.get("text"));
Expand All @@ -49,8 +35,41 @@ public static void main(String[] args) throws Exception {
System.out.println("== Cache with xml encoding and string marshaller.");
xmlCache.put("xml", "<name>infinispan</name>");
System.out.println("Get key from xml cache: " + xmlCache.get("xml"));
}

public static void connectToInfinispan() throws Exception {
ConfigurationBuilder builder = TutorialsConnectorHelper.connectionConfig();

URI textCacheURI = InfinispanEncodingCaches.class.getClassLoader().getResource("textCache.xml").toURI();
URI jsonCacheURI = InfinispanEncodingCaches.class.getClassLoader().getResource("jsonCache.xml").toURI();
URI xmlCacheURI = InfinispanEncodingCaches.class.getClassLoader().getResource("xmlCache.xml").toURI();

builder.remoteCache("textCache").configurationURI(textCacheURI);
builder.remoteCache("jsonCache").configurationURI(jsonCacheURI);
builder.remoteCache("xmlCache").configurationURI(xmlCacheURI);

cacheManager.stop();
cacheManager = TutorialsConnectorHelper.connect(builder);
textCache = cacheManager.getCache("textCache");
jsonCache = cacheManager.getCache("jsonCache")
.withDataFormat(DataFormat.builder()
.keyMarshaller(new UTF8StringMarshaller())
.valueMarshaller(new UTF8StringMarshaller())
.keyType(MediaType.APPLICATION_JSON)
.valueType(MediaType.APPLICATION_JSON).build());
xmlCache = cacheManager.getCache("xmlCache")
.withDataFormat(DataFormat.builder()
.keyMarshaller(new UTF8StringMarshaller())
.valueMarshaller(new UTF8StringMarshaller())
.keyType(MediaType.APPLICATION_XML)
.valueType(MediaType.APPLICATION_XML).build());
}

public static void disconnect(boolean removeCaches) {
if (removeCaches) {
cacheManager.administration().removeCache(textCache.getName());
cacheManager.administration().removeCache(jsonCache.getName());
cacheManager.administration().removeCache(xmlCache.getName());
}
TutorialsConnectorHelper.stop(cacheManager);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.infinispan.tutorial.simple.encoding;


import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class InfinispanEncodingCachesTest {

@BeforeAll
public static void start() throws Exception {
InfinispanEncodingCaches.connectToInfinispan();
}

@AfterAll
public static void stop() {
InfinispanEncodingCaches.disconnect(true);
}

@Test
public void testEncoding() {
assertNotNull(InfinispanEncodingCaches.cacheManager);
assertNotNull(InfinispanEncodingCaches.xmlCache);
assertNotNull(InfinispanEncodingCaches.textCache);
assertNotNull(InfinispanEncodingCaches.jsonCache);

InfinispanEncodingCaches.manipulateCachesAndPrint();

assertEquals("<name>infinispan</name>", InfinispanEncodingCaches.xmlCache.get("xml"));
assertEquals("{\"name\": \"infinispan\"}", InfinispanEncodingCaches.jsonCache.get("\"json\""));
assertEquals("诶, 你好.", InfinispanEncodingCaches.textCache.get("text"));
}
}

0 comments on commit 445d3be

Please sign in to comment.