com.fasterxml.jackson.databind.Module.SetupContext Java Examples

The following examples show how to use com.fasterxml.jackson.databind.Module.SetupContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SchemaSerializationModuleTest.java    From data-highway with Apache License 2.0 6 votes vote down vote up
@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)));
}
 
Example #2
Source File: ConfiguredObjectMapperTest.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: ConfiguredObjectMapperTest.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #4
Source File: YamlSetupContextInitializer.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #5
Source File: SetupContextInitializer.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #6
Source File: XmlSetupContextInitializer.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #7
Source File: JsonSetupContextInitializer.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #8
Source File: ConfiguredObjectMapperTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #9
Source File: ConfiguredObjectMapperTest.java    From endpoints-java with Apache License 2.0 4 votes vote down vote up
@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());
}