Skip to content

Commit

Permalink
Add test for #1223
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 25, 2016
1 parent ae22865 commit 1c68f96
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 32 deletions.
Expand Up @@ -127,7 +127,7 @@ protected POJOPropertiesCollector(MapperConfig<?> config, boolean forSerializati
_config.getDefaultVisibilityChecker()); _config.getDefaultVisibilityChecker());
} }
} }

/* /*
/********************************************************** /**********************************************************
/* Public API /* Public API
Expand Down
Expand Up @@ -241,11 +241,11 @@ public Std with(JsonAutoDetect ann)
{ {
Std curr = this; Std curr = this;
if (ann != null) { if (ann != null) {
curr = curr.withGetterVisibility(ann.getterVisibility()); curr = curr.withGetterVisibility(ann.getterVisibility());
curr = curr.withIsGetterVisibility(ann.isGetterVisibility()); curr = curr.withIsGetterVisibility(ann.isGetterVisibility());
curr = curr.withSetterVisibility(ann.setterVisibility()); curr = curr.withSetterVisibility(ann.setterVisibility());
curr = curr.withCreatorVisibility(ann.creatorVisibility()); curr = curr.withCreatorVisibility(ann.creatorVisibility());
curr = curr.withFieldVisibility(ann.fieldVisibility()); curr = curr.withFieldVisibility(ann.fieldVisibility());
} }
return curr; return curr;
} }
Expand Down
Expand Up @@ -5,22 +5,18 @@


import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.BasicBeanDescription;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;


/** /**
* Unit tests for checking whether JsonSerializerFactory.Feature * Unit tests for checking handling of some of {@link MapperFeature}s
* configuration works * and {@link SerializationFeature}s for serialization.
*/ */
public class TestFeatures public class SerializationFeaturesTest
extends BaseMapTest extends BaseMapTest
{ {
/*
/**********************************************************
/* Helper classes
/**********************************************************
*/

/** /**
* Class with one explicitly defined getter, one name-based * Class with one explicitly defined getter, one name-based
* auto-detectable getter. * auto-detectable getter.
Expand Down Expand Up @@ -192,49 +188,47 @@ public void testFlushingAutomatic() throws IOException
assertTrue(mapper.getSerializationConfig().isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)); assertTrue(mapper.getSerializationConfig().isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE));
// default is to flush after writeValue() // default is to flush after writeValue()
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
JsonGenerator jgen = mapper.getFactory().createGenerator(sw); JsonGenerator g = mapper.getFactory().createGenerator(sw);
mapper.writeValue(jgen, Integer.valueOf(13)); mapper.writeValue(g, Integer.valueOf(13));
assertEquals("13", sw.toString()); assertEquals("13", sw.toString());
jgen.close(); g.close();


// ditto with ObjectWriter // ditto with ObjectWriter
sw = new StringWriter(); sw = new StringWriter();
jgen = mapper.getFactory().createGenerator(sw); g = mapper.getFactory().createGenerator(sw);
ObjectWriter ow = mapper.writer(); ObjectWriter ow = mapper.writer();
ow.writeValue(jgen, Integer.valueOf(99)); ow.writeValue(g, Integer.valueOf(99));
assertEquals("99", sw.toString()); assertEquals("99", sw.toString());
jgen.close(); g.close();
} }


// Test for [JACKSON-401]
public void testFlushingNotAutomatic() throws IOException public void testFlushingNotAutomatic() throws IOException
{ {
// but should not occur if configured otherwise // but should not occur if configured otherwise
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, false); mapper.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, false);
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
JsonGenerator jgen = mapper.getFactory().createGenerator(sw); JsonGenerator g = mapper.getFactory().createGenerator(sw);


mapper.writeValue(jgen, Integer.valueOf(13)); mapper.writeValue(g, Integer.valueOf(13));
// no flushing now: // no flushing now:
assertEquals("", sw.toString()); assertEquals("", sw.toString());
// except when actually flushing // except when actually flushing
jgen.flush(); g.flush();
assertEquals("13", sw.toString()); assertEquals("13", sw.toString());
jgen.close(); g.close();
// Also, same should happen with ObjectWriter // Also, same should happen with ObjectWriter
sw = new StringWriter(); sw = new StringWriter();
jgen = mapper.getFactory().createGenerator(sw); g = mapper.getFactory().createGenerator(sw);
ObjectWriter ow = mapper.writer(); ObjectWriter ow = mapper.writer();
ow.writeValue(jgen, Integer.valueOf(99)); ow.writeValue(g, Integer.valueOf(99));
assertEquals("", sw.toString()); assertEquals("", sw.toString());
// except when actually flushing // except when actually flushing
jgen.flush(); g.flush();
assertEquals("99", sw.toString()); assertEquals("99", sw.toString());
jgen.close(); g.close();
} }


// Test for [JACKSON-805]
public void testSingleElementCollections() throws IOException public void testSingleElementCollections() throws IOException
{ {
final ObjectWriter writer = objectWriter().with(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED); final ObjectWriter writer = objectWriter().with(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
Expand Down Expand Up @@ -264,6 +258,38 @@ public void testSingleElementCollections() throws IOException
assertEquals("true", writer.writeValueAsString(new Boolean[] { Boolean.TRUE })); assertEquals("true", writer.writeValueAsString(new Boolean[] { Boolean.TRUE }));
assertEquals("3", writer.writeValueAsString(new int[] { 3 })); assertEquals("3", writer.writeValueAsString(new int[] { 3 }));
assertEquals(quote("foo"), writer.writeValueAsString(new String[] { "foo" })); assertEquals(quote("foo"), writer.writeValueAsString(new String[] { "foo" }));

}

static class TCls {
@JsonProperty("groupname")
private String groupname;

public void setName(String str) {
this.groupname = str;
}
public String getName() {
return groupname;
}
}

public void testVisibilityFeatures() throws Exception
{
ObjectMapper om = new ObjectMapper();
// Only use explicitly specified values to be serialized/deserialized (i.e., JSONProperty).
om.configure(MapperFeature.AUTO_DETECT_FIELDS, false);
om.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
om.configure(MapperFeature.AUTO_DETECT_SETTERS, false);
om.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
om.configure(MapperFeature.USE_GETTERS_AS_SETTERS, false);
om.configure(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
om.configure(MapperFeature.INFER_PROPERTY_MUTATORS, false);
om.configure(MapperFeature.USE_ANNOTATIONS, true);

JavaType javaType = om.getTypeFactory().constructType(TCls.class);
BeanDescription desc = (BeanDescription) om.getSerializationConfig().introspect(javaType);
List<BeanPropertyDefinition> props = desc.findProperties();
if (props.size() != 1) {
fail("Should find 1 property, not "+props.size()+"; properties = "+props);
}
} }
} }

0 comments on commit 1c68f96

Please sign in to comment.