import org.apache.derby.drda.NetworkServerControl; //导入方法依赖的package包/类
@BeforeClass
public static void startDatabase() throws Exception {
ServerSocket socket = new ServerSocket(0);
port = socket.getLocalPort();
socket.close();
LOG.info("Starting Derby database on {}", port);
System.setProperty("derby.stream.error.file", "target/derby.log");
derbyServer = new NetworkServerControl(InetAddress.getByName("localhost"), port);
StringWriter out = new StringWriter();
derbyServer.start(new PrintWriter(out));
boolean started = false;
int count = 0;
// Use two different methods to detect when server is started:
// 1) Check the server stdout for the "started" string
// 2) wait up to 15 seconds for the derby server to start based on a ping
// on faster machines and networks, this may return very quick, but on slower
// networks where the DNS lookups are slow, this may take a little time
while (!started && count < 30) {
if (out.toString().contains("started")) {
started = true;
} else {
count++;
Thread.sleep(500);
try {
derbyServer.ping();
started = true;
} catch (Throwable t) {
//ignore, still trying to start
}
}
}
dataSource = new ClientDataSource();
dataSource.setCreateDatabase("create");
dataSource.setDatabaseName("target/beam");
dataSource.setServerName("localhost");
dataSource.setPortNumber(port);
readTableName = JdbcTestHelper.getTableName("UT_READ");
JdbcTestHelper.createDataTable(dataSource, readTableName);
addInitialData(dataSource, readTableName);
}