six2six / fixture-factory

Generator fake objects from a template
Apache License 2.0
445 stars 88 forks source link

Processor return processed object instead of void #53

Closed nykolaslima closed 9 years ago

nykolaslima commented 10 years ago

I have a Father class that has one Son class.

We defined Father and Son templates.

Fixture.of(Father.class).addTemplate("valid", new Rule() {{
...
add("son", one(Son.class, "valid");
...
}});

Fixture.of(Son.class).addTemplate("valid", new Rule() {{
add("id", 1);
...
}});

I created a custom HibernateProcessor that persist generated fixtures to database but if generated fixture has id property different from null, instead of persist I want to load it from database.

public void execute(Object result) {
if(isIdDifferentFromNull()) {
result = session.load(result.class, id);
} else {
session.save(result);
}
}

The problem is that the loaded result is not used by Fixture-Factory, because result parameter has a reference copy of original result and applying loaded result to result parameter does no affect the generated result used internal by Fixture-Factory. (reference http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.html)

We can change Processor interface to return the processed object instead of return void.

public Object execute(Object result);

And in ObjectFactory we can use the processed object instead of generated object. Current implementation:

protected Object createObject(Rule rule) {
...
if (processor != null) {
processor.execute(result);
}
return result;
}

Suggested implementation

protected Object createObject(Rule rule) {
...
if (processor != null) {
return processor.execute(result);
} else {
return result;
}
}

@aparra @ahirata @bjornnborg @douglasrodrigo what do you guys think about it?

nykolaslima commented 9 years ago

Guys, any opinion about it? @aparra @ahirata

nykolaslima commented 9 years ago

I think we can close it because anyone share this need.