下面列出了怎么用com.fasterxml.jackson.databind.Module.SetupContext的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void setupModule() throws JsonMappingException {
SetupContext context = mock(SetupContext.class);
underTest.setupModule(context);
ArgumentCaptor<SimpleSerializers> serializersCaptor = ArgumentCaptor.forClass(SimpleSerializers.class);
ArgumentCaptor<SimpleDeserializers> deserializersCaptor = ArgumentCaptor.forClass(SimpleDeserializers.class);
verify(context).addSerializers(serializersCaptor.capture());
verify(context).addDeserializers(deserializersCaptor.capture());
JavaType javaType = new ObjectMapper().constructType(Schema.class);
JsonSerializer<?> serializer = serializersCaptor.getValue().findSerializer(null, javaType, null);
assertThat(serializer, is(instanceOf(SchemaSerializer.class)));
JsonDeserializer<?> deserializer = deserializersCaptor.getValue().findBeanDeserializer(javaType, null, null);
assertThat(deserializer, is(instanceOf(SchemaDeserializer.class)));
}
@Test
public void testBuildWithModules_manyWithAll() {
doModuleSetup(moduleA, "moduleA");
doModuleSetup(moduleB, "moduleB");
doModuleSetup(moduleC, "moduleC");
ConfiguredObjectMapper result1 = builder
.addRegisteredModules(ImmutableList.of(moduleA, moduleB))
.addRegisteredModules(ImmutableList.of(moduleC))
.build();
Mockito.verify(moduleA, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.verify(moduleB, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.verify(moduleC, atLeastOnce()).setupModule(any(SetupContext.class));
assertEquals(1, cache.size());
ConfiguredObjectMapper result2 = builder
.addRegisteredModules(ImmutableList.of(moduleA, moduleB, moduleC))
.build();
assertEquals(1, cache.size());
assertSame(result1, result2);
}
@Test
public void testEviction() {
doModuleSetup(moduleA, "moduleA");
builder = new ConfiguredObjectMapper.Builder(cache, 1);
builder.addRegisteredModules(ImmutableList.of(moduleA)).build();
assertEquals(1, cache.size());
Mockito.verify(moduleA, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.reset();
cache = Maps.newLinkedHashMap();
doModuleSetup(moduleB, "moduleB");
doModuleSetup(moduleA, "moduleA");
// Evict the other entries
new ConfiguredObjectMapper.Builder(cache, 1)
.addRegisteredModules(ImmutableList.of(moduleB))
.build();
// Now this is a miss
new ConfiguredObjectMapper.Builder(cache, 1)
.addRegisteredModules(ImmutableList.of(moduleA))
.build();
assertEquals(1, cache.size());
Mockito.verify(moduleA, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.verify(moduleB, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.reset();
}
public void setupModule(final SetupContext context, final boolean includeStacktrace,
final boolean stacktraceAsString) {
// JRE classes: we cannot edit those with Jackson annotations
context.setMixInAnnotations(StackTraceElement.class, StackTraceElementMixIn.class);
// Log4j API classes: we do not want to edit those with Jackson annotations because the API module should not
// depend on Jackson.
context.setMixInAnnotations(Marker.class, MarkerMixIn.class);
context.setMixInAnnotations(Level.class, LevelMixIn.class);
context.setMixInAnnotations(Instant.class, InstantMixIn.class);
context.setMixInAnnotations(LogEvent.class, LogEventJsonMixIn.class); // different ThreadContext handling
// Log4j Core classes: we do not want to bring in Jackson at runtime if we do not have to.
context.setMixInAnnotations(ExtendedStackTraceElement.class, ExtendedStackTraceElementMixIn.class);
context.setMixInAnnotations(ThrowableProxy.class, includeStacktrace
? (stacktraceAsString ? ThrowableProxyWithStacktraceAsStringMixIn.class : ThrowableProxyMixIn.class)
: ThrowableProxyWithoutStacktraceMixIn.class);
}
public void setupModule(final SetupContext context, final boolean includeStacktrace,
final boolean stacktraceAsString) {
// JRE classes: we cannot edit those with Jackson annotations
context.setMixInAnnotations(StackTraceElement.class, StackTraceElementMixIn.class);
// Log4j API classes: we do not want to edit those with Jackson annotations because the API module should not
// depend on Jackson.
context.setMixInAnnotations(Marker.class, MarkerMixIn.class);
context.setMixInAnnotations(Level.class, LevelMixIn.class);
context.setMixInAnnotations(Instant.class, InstantMixIn.class);
context.setMixInAnnotations(LogEvent.class, LogEventWithContextListMixIn.class);
// Log4j Core classes: we do not want to bring in Jackson at runtime if we do not have to.
context.setMixInAnnotations(ExtendedStackTraceElement.class, ExtendedStackTraceElementMixIn.class);
context.setMixInAnnotations(ThrowableProxy.class, includeStacktrace
? (stacktraceAsString ? ThrowableProxyWithStacktraceAsStringMixIn.class : ThrowableProxyMixIn.class)
: ThrowableProxyWithoutStacktraceMixIn.class);
}
public void setupModule(final SetupContext context, final boolean includeStacktrace,
final boolean stacktraceAsString) {
// JRE classes: we cannot edit those with Jackson annotations
context.setMixInAnnotations(StackTraceElement.class, StackTraceElementXmlMixIn.class);
// Log4j API classes: we do not want to edit those with Jackson annotations because the API module should not
// depend on Jackson.
context.setMixInAnnotations(Marker.class, MarkerXmlMixIn.class);
context.setMixInAnnotations(Level.class, LevelMixIn.class);
context.setMixInAnnotations(Instant.class, InstantXmlMixIn.class);
context.setMixInAnnotations(LogEvent.class, LogEventWithContextListXmlMixIn.class);
// Log4j Core classes: we do not want to bring in Jackson at runtime if we do not have to.
context.setMixInAnnotations(ExtendedStackTraceElement.class, ExtendedStackTraceElementXmlMixIn.class);
context.setMixInAnnotations(ThrowableProxy.class, includeStacktrace
? (stacktraceAsString ? ThrowableProxyWithStacktraceAsStringXmlMixIn.class : ThrowableProxyXmlMixIn.class)
: ThrowableProxyWithoutStacktraceXmlMixIn.class);
}
public void setupModule(final SetupContext context, final boolean includeStacktrace,
final boolean stacktraceAsString) {
// JRE classes: we cannot edit those with Jackson annotations
context.setMixInAnnotations(StackTraceElement.class, StackTraceElementMixIn.class);
// Log4j API classes: we do not want to edit those with Jackson annotations because the API module should not
// depend on Jackson.
context.setMixInAnnotations(Marker.class, MarkerMixIn.class);
context.setMixInAnnotations(Level.class, LevelMixIn.class);
context.setMixInAnnotations(Instant.class, InstantMixIn.class);
context.setMixInAnnotations(LogEvent.class, LogEventJsonMixIn.class); // different ThreadContext handling
// Log4j Core classes: we do not want to bring in Jackson at runtime if we do not have to.
context.setMixInAnnotations(ExtendedStackTraceElement.class, ExtendedStackTraceElementMixIn.class);
context.setMixInAnnotations(ThrowableProxy.class, includeStacktrace
? (stacktraceAsString ? ThrowableProxyWithStacktraceAsStringMixIn.class : ThrowableProxyMixIn.class)
: ThrowableProxyWithoutStacktraceMixIn.class);
}
@Test
public void testBuildWithModules_oneWithAll() {
doModuleSetup(moduleA, "moduleA");
doModuleSetup(moduleB, "moduleB");
doModuleSetup(moduleC, "moduleC");
builder.addRegisteredModules(ImmutableList.of(moduleA, moduleB, moduleC)).build();
Mockito.verify(moduleA, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.verify(moduleB, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.verify(moduleC, atLeastOnce()).setupModule(any(SetupContext.class));
assertEquals(1, cache.size());
}
@Test
public void testBuildComplex() {
// Cache miss
doModuleSetup(moduleA, "moduleA");
doModuleSetup(moduleB, "moduleB");
builder = new ConfiguredObjectMapper.Builder(cache, 100);
ConfiguredObjectMapper firstResultAB = builder
.addRegisteredModules(ImmutableList.of(moduleA, moduleB))
.build();
Mockito.verify(moduleA, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.verify(moduleB, atLeastOnce()).setupModule(any(SetupContext.class));
assertEquals(1, cache.size());
Mockito.reset();
// Cache miss
doModuleSetup(moduleA, "moduleA");
builder = new ConfiguredObjectMapper.Builder(cache, 100);
ConfiguredObjectMapper firstResultA = builder
.addRegisteredModules(ImmutableList.of(moduleA))
.apiSerializationConfig(fooConfig)
.build();
assertEquals(2, cache.size());
Mockito.verify(moduleA, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.reset();
// Cache miss
doModuleSetup(moduleB, "moduleB");
builder = new ConfiguredObjectMapper.Builder(cache, 100);
ConfiguredObjectMapper firstResultB =
builder.addRegisteredModules(ImmutableList.of(moduleB)).build();
assertEquals(3, cache.size());
// Cache hit
builder = new ConfiguredObjectMapper.Builder(cache, 100);
ConfiguredObjectMapper secondResultAB = builder
.addRegisteredModules(ImmutableList.of(moduleB, moduleA))
.build();
assertEquals(3, cache.size());
assertSame(firstResultAB, secondResultAB);
Mockito.verify(moduleB, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.reset();
// Cache hit, with config
builder = new ConfiguredObjectMapper.Builder(cache, 100);
ConfiguredObjectMapper secondResultA = builder
.apiSerializationConfig(fooConfig)
.addRegisteredModules(ImmutableList.of(moduleA))
.build();
assertEquals(3, cache.size());
assertSame(firstResultA, secondResultA);
Mockito.reset();
// Cache miss
doModuleSetup(moduleA, "moduleA");
doModuleSetup(moduleC, "moduleC");
builder = new ConfiguredObjectMapper.Builder(cache, 100);
ConfiguredObjectMapper firstResultAC = builder
.addRegisteredModules(ImmutableList.of(moduleA, moduleC))
.build();
assertEquals(4, cache.size());
Mockito.verify(moduleA, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.verify(moduleC, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.reset();
// Cache miss, config doesn't match
doModuleSetup(moduleA, "moduleA");
builder = new ConfiguredObjectMapper.Builder(cache, 100);
ConfiguredObjectMapper otherResultA = builder
.addRegisteredModules(ImmutableList.of(moduleA))
.apiSerializationConfig(barConfig)
.build();
assertEquals(5, cache.size());
assertNotSame(firstResultA, otherResultA);
Mockito.verify(moduleA, atLeastOnce()).setupModule(any(SetupContext.class));
Mockito.reset();
// Check the cache contents
assertThat(cache.values())
.containsExactly(firstResultA, firstResultAB, firstResultB, firstResultAC, otherResultA);
assertEquals(
5, ImmutableSet.of(firstResultA.delegate, firstResultAB.delegate, firstResultB.delegate,
firstResultAC.delegate, otherResultA.delegate).size());
}