Skip to content

Commit

Permalink
Minor changes to test, move to misc tests package
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 25, 2024
1 parent 91581fe commit 81632e0
Showing 1 changed file with 38 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
package com.fasterxml.jackson.dataformat.xml;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.module.SimpleModule;
package com.fasterxml.jackson.dataformat.xml.misc;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.List;

import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;

/**
* A regression test for https://github.com/FasterXML/jackson-databind/issues/4595
*/
public class CustomAnnotationIntrospectorNoWrapperTest extends XmlTestBase {
private final XmlMapper MAPPER = newMapper();

public void testNoWrapper() throws Exception {
Foo foo = new Foo(Arrays.asList("Value1", "Value2"));

assertEquals("<Foo><bar><bar>Value1</bar><bar>Value2</bar></bar></Foo>", MAPPER.writeValueAsString(foo));

MAPPER
.registerModule(new SimpleModule("NoWrapperModule") {
@Override
public void setupModule(SetupContext context) {
context.insertAnnotationIntrospector(new NoWrapperIntrospector());
super.setupModule(context);
}
});

// TODO: update after fixing https://github.com/FasterXML/jackson-databind/issues/4595
// Should be assertEquals("<Foo><bar>Value1</bar><bar>Value2</bar></Foo>", MAPPER.writeValueAsString(foo));
assertEquals("<Foo><bar><bar>Value1</bar><bar>Value2</bar></bar></Foo>", MAPPER.writeValueAsString(foo));
}

public class CustomAnnotationIntrospectorNoWrapperTest extends XmlTestBase
{
public static class Foo {
private final List<String> bar;

Expand All @@ -49,11 +30,8 @@ public List<String> getBar() {
}
}

public static class NoWrapperIntrospector extends AnnotationIntrospector {
@Override
public Version version() {
return com.fasterxml.jackson.databind.cfg.PackageVersion.VERSION;
}
public static class NoWrapperIntrospector extends NopAnnotationIntrospector {
private static final long serialVersionUID = 1L;

@Override
public PropertyName findWrapperName(Annotated ann) {
Expand All @@ -67,4 +45,30 @@ public PropertyName findWrapperName(Annotated ann) {
@Retention(RetentionPolicy.RUNTIME)
public @interface NoWrapper {
}

private final XmlMapper VANILLA_MAPPER = newMapper();

public void testNoWrapper() throws Exception {
Foo foo = new Foo(Arrays.asList("Value1", "Value2"));

assertEquals("<Foo><bar><bar>Value1</bar><bar>Value2</bar></bar></Foo>",
VANILLA_MAPPER.writeValueAsString(foo));

XmlMapper customMapper = mapperBuilder()
.addModule(new SimpleModule("NoWrapperModule") {
private static final long serialVersionUID = 1L;

@Override
public void setupModule(SetupContext context) {
context.insertAnnotationIntrospector(new NoWrapperIntrospector());
super.setupModule(context);
}
}).build();

// After fixing https://github.com/FasterXML/jackson-databind/issues/4595
assertEquals("<Foo><bar>Value1</bar><bar>Value2</bar></Foo>", customMapper.writeValueAsString(foo));

// before fix had:
//assertEquals("<Foo><bar><bar>Value1</bar><bar>Value2</bar></bar></Foo>", customMapper.writeValueAsString(foo));
}
}

0 comments on commit 81632e0

Please sign in to comment.