Skip to content

Commit

Permalink
Improve testing to try to reproduce #57
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 13, 2015
1 parent 5327842 commit 3201e3f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
public class JsonSchemaGenerator
{
protected final ObjectMapper _mapper;
private final WrapperFactory _wrapperFactory;

private final WrapperFactory _wrapperFactory;

public JsonSchemaGenerator(ObjectMapper mapper) {
this(mapper, null);
}

public JsonSchemaGenerator(ObjectMapper mapper, WrapperFactory wrapperFactory) {
_mapper = mapper;
_wrapperFactory = wrapperFactory == null ? new WrapperFactory() : wrapperFactory;
_wrapperFactory = wrapperFactory == null ? new WrapperFactory() : wrapperFactory;
}

public JsonSchema generateSchema(Class<?> type) throws JsonMappingException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.fasterxml.jackson.module.jsonSchema;

import java.util.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class EnumGenerationTest extends SchemaTestBase
{
public enum Enumerated {
A, B, C;

// add this; should NOT matter but...
@Override
public String toString() {
return "ToString:"+name();
}
}
public static class LetterBean {

public Enumerated letter;
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

private final ObjectMapper MAPPER = new ObjectMapper();

public void testEnumDefault() throws Exception
{
JsonSchemaGenerator generator = new JsonSchemaGenerator(MAPPER);
JsonSchema jsonSchema = generator.generateSchema(LetterBean.class);
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) MAPPER.convertValue(jsonSchema, Map.class);
assertNotNull(result);
assertTrue(jsonSchema.isObjectSchema());
assertEquals(expectedAsMap(false), result);
}

public void testEnumWithToString() throws Exception
{
final ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);

JsonSchemaGenerator generator = new JsonSchemaGenerator(mapper);
JsonSchema jsonSchema = generator.generateSchema(LetterBean.class);
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) mapper.convertValue(jsonSchema, Map.class);
assertNotNull(result);
assertTrue(jsonSchema.isObjectSchema());
assertEquals(expectedAsMap(true), result);
}

@SuppressWarnings("serial")
private Map<String,Object> expectedAsMap(final boolean useToString)
{
return new LinkedHashMap<String, Object>() {
{
put("type", "object");
put("id", "urn:jsonschema:com:fasterxml:jackson:module:jsonSchema:EnumGenerationTest:LetterBean");
put("properties",
new LinkedHashMap<String, Object>() {
{
put("letter",
new LinkedHashMap<String, Object>() {
{
put("type", "string");
put("enum", new ArrayList<String>() {
{
add(useToString ? "ToString:A" : "A");
add(useToString ? "ToString:B" : "B");
add(useToString ? "ToString:C" : "C");
}
});
}
});
}
});
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,6 @@ public void setObvious(String s) {
public static FilterProvider secretFilterProvider = new SimpleFilterProvider()
.addFilter("filteredBean", SimpleBeanPropertyFilter.filterOutAllExcept(new String[]{"obvious"}));

public enum Enumerated {

A, B, C;
}

public static class LetterBean {

public Enumerated letter;
}

static class StringMap extends HashMap<String, String> {
}

Expand Down Expand Up @@ -253,38 +243,6 @@ public void testSchemaId() throws Exception {
}, result);
}

public void testWithEnum() throws Exception {
JsonSchemaGenerator generator = new JsonSchemaGenerator(MAPPER);
JsonSchema jsonSchema = generator.generateSchema(LetterBean.class);
Map<String, Object> result = writeAndMap(MAPPER, jsonSchema);
assertNotNull(result);

assertEquals(new HashMap<String, Object>() {
{
put("type", "object");
put("id", "urn:jsonschema:com:fasterxml:jackson:module:jsonSchema:TestGenerateJsonSchema:LetterBean");
put("properties",
new HashMap<String, Object>() {
{
put("letter",
new HashMap<String, Object>() {
{
put("type", "string");
put("enum", new ArrayList<String>() {
{
add("A");
add("B");
add("C");
}
});
}
});
}
});
}
}, result);
}

public void testSimpleMap() throws Exception {
JsonSchemaGenerator generator = new JsonSchemaGenerator(MAPPER);
JsonSchema jsonSchema = generator.generateSchema(StringMap.class);
Expand Down

0 comments on commit 3201e3f

Please sign in to comment.