Java Reference
In-Depth Information
G ,
H
Once we find an invalid foreign key, we buffer it and fail later, with the failure message
containing all violations. Although it would be simpler to fail right away, this approach
is more helpful, because it requires just one run to spot all invalid names.
The meaning of a valid foreign key is up to the project. In our case, we define that it
has to start with FK_ and have two names (which should represent the tables involved
in the relationship) separated by an underscore ( _ ).
I
This test case relies on a new infrastructure method, analyzeSchema() , which is
defined in AbstractJpaTestCase ; listing 18.23 shows all changes required to imple-
ment it.
Listing 18.23
Changes on AbstractJpaTestCase to support analyzeSchema()
[...]
public abstract class AbstractJpaTestCase {
[...]
protected void analyzeSchema( SqlHandler handler ) {
ConfigurationCreator cfgCreator = new ConfigurationCreator();
Configuration cfg = cfgCreator.createConfiguration();
SchemaExport export = new SchemaExport(cfg);
B
C
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream oldOut = System.out;
PrintStream newOut = new PrintStream(outputStream);
System.setOut(newOut);
try {
export.create(true, true);
String sql = outputStream.toString();
handler.handle(sql);
} finally {
System.setOut(oldOut);
newOut.close();
}
}
D
protected interface SqlHandler {
void handle( String sql );
}
E
private class ConfigurationCreator extends JPAConfigurationTask {
@Override
protected Configuration createConfiguration() {
return super .createConfiguration();
}
}
analyzeSchema() is based on the Template Design Pattern (see section 17.7.1). It
knows how to create a Java string containing the database schema but doesn't
know what to do with it, so it passes this string to a SqlHandler , which was passed
as a parameter.
SchemaExport is the Hibernate class (part of the Hibernate Tools project) used to
export the schema. It requires a Hibernate Properties object, which unfortunately
B
C
 
Search WWH ::




Custom Search