A friend just told me about a nifty little feature in commons-lang:
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
public String toString(){
return ToStringBuilder.reflectionToString(this, ToStringStyle.SIMPLE_STYLE);
}
What that does is spit out all the properties of your object when you call a toString on it.
Another little gem I have ended up using a lot is when I write test cases with spring. When you extend the AbstractDependencyInjectionSpringContextTests object one of the methods you need to override is the protected String[] getConfigLocations() method.
Getting your spring context to load correctly is critical to getting your spring app to run and can cause a lot of head scratching and frustration here is a little trick to get it to load correctly - it is a bit hackish but in some scenarios it works well.
import java.io.File;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
public class AbstractTestCase extends AbstractDependencyInjectionSpringContextTests {
public final String RELATIVE_LOCATION = "src/test/resources";
public final String FILE_NAME = "persistence_ioc.xml";
@Override
protected String[] getConfigLocations() {
File aNewFile = new File("./");
StringBuilder contextLocation =
new StringBuilder(
aNewFile.getAbsolutePath().substring(0,
(aNewFile.getAbsolutePath().length() -1)
)
);
contextLocation.append(RELATIVE_LOCATION).append("/").append(FILE_NAME);
File test = new File(contextLocation.toString());
if (!test.exists()){
System.out.println("!!! file doesn't exist at: " +
test.getAbsolutePath());
}
return new String[]{"File:" + contextLocation.toString()};
}
}
Like I said a little bit hackish but effective nonetheless.
-Aaron
No comments:
Post a Comment