Skip to content

Instantly share code, notes, and snippets.

@ittaiz
Last active December 20, 2015 18:18
Show Gist options
  • Save ittaiz/6174553 to your computer and use it in GitHub Desktop.
Save ittaiz/6174553 to your computer and use it in GitHub Desktop.
A test with guava and scala modules which fails due to order of module registration
package com.wixpress.report
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.datatype.guava.GuavaModule
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.junit._
import Assert._
/**
* @author: ittaiz
* @since: 8/7/13
*/
class JacksonMultiMapTest {
@Test
def blowsUpWhenScalaIsSecond() = {
val objectMapper = new ObjectMapper
objectMapper.registerModule(new GuavaModule)
objectMapper.registerModule(new DefaultScalaModule)
val objectReader = objectMapper.reader.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
val source: String = "{\"headers\":{\"key1\": [\"value1\"] }}"
val pojoWithMultiMap : PojoWithMultiMap = objectReader.readValue(objectReader.treeAsTokens(objectReader.readTree(source)),
objectMapper.getTypeFactory.constructType(classOf[PojoWithMultiMap]))
assertNotNull(pojoWithMultiMap)
}
@Test
def worksFineWhenScalaIsFirst() = {
val objectMapper = new ObjectMapper
objectMapper.registerModule(new DefaultScalaModule)
objectMapper.registerModule(new GuavaModule)
val objectReader = objectMapper.reader.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
val source: String = "{\"headers\":{\"key1\": [\"value1\"] }}"
val pojoWithMultiMap : PojoWithMultiMap = objectReader.readValue(objectReader.treeAsTokens(objectReader.readTree(source)),
objectMapper.getTypeFactory.constructType(classOf[PojoWithMultiMap]))
assertNotNull(pojoWithMultiMap)
}
@Test
def worksFineWhenNotPropertyBased() = {
val objectMapper = new ObjectMapper
objectMapper.registerModule(new DefaultScalaModule)
objectMapper.registerModule(new GuavaModule)
val objectReader = objectMapper.reader.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
val source: String = "{\"key1\": [\"value1\"] }"
val multiMap : com.google.common.collect.Multimap[String,String] = objectReader.readValue(objectReader.treeAsTokens(objectReader.readTree(source)),
objectMapper.getTypeFactory.constructMapLikeType(classOf[com.google.common.collect.Multimap[String,String]],classOf[String],classOf[String]))
assertNotNull(multiMap)
}
}
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Multimap;
/**
* @author: ittaiz
* @since: 8/7/13
*/
public class PojoWithMultiMap {
private final Multimap<String, String> headers;
@JsonCreator
public PojoWithMultiMap(@JsonProperty("headers") Multimap<String, String> headers) {
this.headers = headers;
}
public Multimap<String, String> getHeaders() {
return headers;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jacksonGuavaMultiMap</groupId>
<artifactId>jacksonGuavaMultiMap</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.10.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.2.2</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>2.2.2</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-scala_2.10</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.4</version>
<configuration>
<recompileMode>incremental</recompileMode>
<useZincServer>false</useZincServer>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<forkMode>once</forkMode>
<useSystemClassLoader>true</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment