Skip to content

Commit

Permalink
Simplify failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 27, 2015
1 parent c568ff2 commit 48617c8
Showing 1 changed file with 12 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,9 @@
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class RaceCondition738Test extends BaseMapTest
{
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({
@JsonSubTypes.Type(value = TypeOne.class, name = "one"),
@JsonSubTypes.Type(value = TypeTwo.class, name = "two"),
@JsonSubTypes.Type(value = TypeThree.class, name = "three")
})
static abstract class AbstractHasSubTypes implements HasSubTypes { }

static class TypeOne extends AbstractHasSubTypes {
Expand All @@ -35,36 +28,10 @@ public String getType() {
}
}

static class TypeTwo extends AbstractHasSubTypes {
private final String id;
public TypeTwo(String id) {
this.id = id;
}
@JsonProperty
public String getId() {
return id;
}
@Override
public String getType() {
return TypeTwo.class.getSimpleName();
}
}

static class TypeThree extends AbstractHasSubTypes {
private final String id;
public TypeThree(String id) {
this.id = id;
}
@JsonProperty
public String getId() {
return id;
}
@Override
public String getType() {
return TypeThree.class.getSimpleName();
}
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({
@JsonSubTypes.Type(value = TypeOne.class, name = "one")
})
public interface HasSubTypes {
String getType();
}
Expand All @@ -89,12 +56,13 @@ public HasSubTypes getHasSubTypes() {
*/

public void testRepeatedly() throws Exception {
for (int i = 0; i < 1000; i++) {
runOnce();
final int COUNT = 50;
for (int i = 0; i < COUNT; i++) {
runOnce(i, COUNT);
}
}

void runOnce() throws Exception {
void runOnce(int round, int max) throws Exception {
final ObjectMapper mapper = getObjectMapper();
Callable<String> writeJson = new Callable<String>() {
@Override
Expand All @@ -120,17 +88,14 @@ public String call() throws Exception {
JsonNode wrapped = tree.get("hasSubTypes");

if (!wrapped.has("one")) {
throw new IllegalStateException("Missing 'one', source: "+json);
System.out.println("JSON wrong: "+json);
throw new IllegalStateException("Round #"+round+"/"+max+" ; missing property 'one', source: "+json);
}
System.out.println("JSON fine: "+json);
}
}

private static ObjectMapper getObjectMapper() {
SimpleModule module = new SimpleModule("subTypeRace");
module.setMixInAnnotation(HasSubTypes.class, AbstractHasSubTypes.class);

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
return mapper;
return new ObjectMapper();
}
}

0 comments on commit 48617c8

Please sign in to comment.