提交 91f283da authored 作者: Thomas Mueller's avatar Thomas Mueller

Improve code coverage.

上级 b95ea354
...@@ -18,7 +18,8 @@ Change Log ...@@ -18,7 +18,8 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>The org.h2.tools.Console no longer calls System.exit on shutdown <ul><li>The SLF4J 1.6 API is now used by default (this should not have any user visible effect).
</li><li>The org.h2.tools.Console no longer calls System.exit on shutdown
(this should not have any user visible effect, but should allow to integrate the tool easier into other applications). (this should not have any user visible effect, but should allow to integrate the tool easier into other applications).
</li><li>Improved date / time arithmetics. Adding and subtracting a </li><li>Improved date / time arithmetics. Adding and subtracting a
floating point value from a date or timestamp is now supported. floating point value from a date or timestamp is now supported.
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
package org.h2.message; package org.h2.message;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Writer; import java.io.Writer;
import java.sql.DriverManager; import java.sql.DriverManager;
...@@ -92,6 +93,7 @@ public class TraceSystem implements TraceWriter { ...@@ -92,6 +93,7 @@ public class TraceSystem implements TraceWriter {
private boolean closed; private boolean closed;
private boolean writingErrorLogged; private boolean writingErrorLogged;
private TraceWriter writer = this; private TraceWriter writer = this;
private PrintStream sysOut = System.out;
/** /**
* Create a new trace system object. * Create a new trace system object.
...@@ -107,6 +109,15 @@ public class TraceSystem implements TraceWriter { ...@@ -107,6 +109,15 @@ public class TraceSystem implements TraceWriter {
levelMax = Math.max(levelSystemOut, levelFile); levelMax = Math.max(levelSystemOut, levelFile);
} }
/**
* Set the print stream to use instead of System.out.
*
* @param out the new print stream
*/
public void setSysOut(PrintStream out) {
this.sysOut = out;
}
/** /**
* Write the exception to the driver manager log writer if configured. * Write the exception to the driver manager log writer if configured.
* *
...@@ -220,9 +231,9 @@ public class TraceSystem implements TraceWriter { ...@@ -220,9 +231,9 @@ public class TraceSystem implements TraceWriter {
if (level <= levelSystemOut || level > this.levelMax) { if (level <= levelSystemOut || level > this.levelMax) {
// level <= levelSystemOut: the system out level is set higher // level <= levelSystemOut: the system out level is set higher
// level > this.level: the level for this module is set higher // level > this.level: the level for this module is set higher
System.out.println(format(module, s)); sysOut.println(format(module, s));
if (t != null && levelSystemOut == DEBUG) { if (t != null && levelSystemOut == DEBUG) {
t.printStackTrace(); t.printStackTrace(sysOut);
} }
} }
if (fileName != null) { if (fileName != null) {
...@@ -279,7 +290,7 @@ public class TraceSystem implements TraceWriter { ...@@ -279,7 +290,7 @@ public class TraceSystem implements TraceWriter {
Exception se = DbException.get(ErrorCode.TRACE_FILE_ERROR_2, e, fileName, e.toString()); Exception se = DbException.get(ErrorCode.TRACE_FILE_ERROR_2, e, fileName, e.toString());
// print this error only once // print this error only once
fileName = null; fileName = null;
System.out.println(se); sysOut.println(se);
se.printStackTrace(); se.printStackTrace();
} }
......
...@@ -116,6 +116,7 @@ import org.h2.test.unit.TestBitField; ...@@ -116,6 +116,7 @@ import org.h2.test.unit.TestBitField;
import org.h2.test.unit.TestCache; import org.h2.test.unit.TestCache;
import org.h2.test.unit.TestClearReferences; import org.h2.test.unit.TestClearReferences;
import org.h2.test.unit.TestCompress; import org.h2.test.unit.TestCompress;
import org.h2.test.unit.TestConnectionInfo;
import org.h2.test.unit.TestDataPage; import org.h2.test.unit.TestDataPage;
import org.h2.test.unit.TestDate; import org.h2.test.unit.TestDate;
import org.h2.test.unit.TestDateIso8601; import org.h2.test.unit.TestDateIso8601;
...@@ -128,6 +129,7 @@ import org.h2.test.unit.TestFtp; ...@@ -128,6 +129,7 @@ import org.h2.test.unit.TestFtp;
import org.h2.test.unit.TestIntArray; import org.h2.test.unit.TestIntArray;
import org.h2.test.unit.TestIntIntHashMap; import org.h2.test.unit.TestIntIntHashMap;
import org.h2.test.unit.TestJmx; import org.h2.test.unit.TestJmx;
import org.h2.test.unit.TestTraceSystem;
import org.h2.test.unit.TestMathUtils; import org.h2.test.unit.TestMathUtils;
import org.h2.test.unit.TestNetUtils; import org.h2.test.unit.TestNetUtils;
import org.h2.test.unit.TestOldVersion; import org.h2.test.unit.TestOldVersion;
...@@ -154,7 +156,6 @@ import org.h2.test.unit.TestValueMemory; ...@@ -154,7 +156,6 @@ import org.h2.test.unit.TestValueMemory;
import org.h2.test.utils.OutputCatcher; import org.h2.test.utils.OutputCatcher;
import org.h2.test.utils.RecordingFileSystem; import org.h2.test.utils.RecordingFileSystem;
import org.h2.test.utils.SelfDestructor; import org.h2.test.utils.SelfDestructor;
import org.h2.test.db.TestConnectionInfo;
import org.h2.tools.DeleteDbFiles; import org.h2.tools.DeleteDbFiles;
import org.h2.tools.Server; import org.h2.tools.Server;
import org.h2.util.Profiler; import org.h2.util.Profiler;
...@@ -535,7 +536,6 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1` ...@@ -535,7 +536,6 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
new TestCases().runTest(this); new TestCases().runTest(this);
new TestCheckpoint().runTest(this); new TestCheckpoint().runTest(this);
new TestCluster().runTest(this); new TestCluster().runTest(this);
new TestConnectionInfo().runTest(this);
new TestCompatibility().runTest(this); new TestCompatibility().runTest(this);
new TestCsv().runTest(this); new TestCsv().runTest(this);
new TestDeadlock().runTest(this); new TestDeadlock().runTest(this);
...@@ -638,6 +638,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1` ...@@ -638,6 +638,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
new TestCache().runTest(this); new TestCache().runTest(this);
new TestClearReferences().runTest(this); new TestClearReferences().runTest(this);
new TestCompress().runTest(this); new TestCompress().runTest(this);
new TestConnectionInfo().runTest(this);
new TestDataPage().runTest(this); new TestDataPage().runTest(this);
new TestDate().runTest(this); new TestDate().runTest(this);
new TestDateIso8601().runTest(this); new TestDateIso8601().runTest(this);
...@@ -670,6 +671,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1` ...@@ -670,6 +671,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
new TestStringCache().runTest(this); new TestStringCache().runTest(this);
new TestStringUtils().runTest(this); new TestStringUtils().runTest(this);
new TestTools().runTest(this); new TestTools().runTest(this);
new TestTraceSystem().runTest(this);
new TestUpgrade().runTest(this); new TestUpgrade().runTest(this);
new TestUtils().runTest(this); new TestUtils().runTest(this);
new TestValue().runTest(this); new TestValue().runTest(this);
......
...@@ -4,11 +4,14 @@ ...@@ -4,11 +4,14 @@
* (http://h2database.com/html/license.html). * (http://h2database.com/html/license.html).
* Initial Developer: H2 Group * Initial Developer: H2 Group
*/ */
package org.h2.test.db; package org.h2.test.unit;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.constant.ErrorCode;
import org.h2.engine.ConnectionInfo; import org.h2.engine.ConnectionInfo;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties; import java.util.Properties;
/** /**
...@@ -28,6 +31,26 @@ public class TestConnectionInfo extends TestBase { ...@@ -28,6 +31,26 @@ public class TestConnectionInfo extends TestBase {
} }
public void test() throws Exception { public void test() throws Exception {
testConnectInitError();
testConnectionInfo();
}
private void testConnectInitError() throws Exception {
try {
DriverManager.getConnection("jdbc:h2:mem:;init=error");
fail();
} catch (SQLException e) {
assertEquals(ErrorCode.SYNTAX_ERROR_2, e.getErrorCode());
}
try {
DriverManager.getConnection("jdbc:h2:mem:;init=runscript from 'wrong.file'");
fail();
} catch (SQLException e) {
assertEquals(ErrorCode.IO_EXCEPTION_2, e.getErrorCode());
}
}
private void testConnectionInfo() throws Exception {
Properties info = new Properties(); Properties info = new Properties();
ConnectionInfo connectionInfo = new ConnectionInfo( ConnectionInfo connectionInfo = new ConnectionInfo(
"jdbc:h2:mem:testdb" + "jdbc:h2:mem:testdb" +
......
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.unit;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.h2.message.TraceSystem;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.util.IOUtils;
/**
* Tests the trace system
*/
public class TestTraceSystem extends TestBase {
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
TestBase.createCaller().init().test();
}
public void test() throws Exception {
testTraceDebug();
testReadOnly();
testAdapter();
}
private void testAdapter() {
TraceSystem ts = new TraceSystem(null);
ts.setLevelFile(TraceSystem.ADAPTER);
ts.getTrace("test").info("test");
ts.close();
}
private void testTraceDebug() {
TraceSystem ts = new TraceSystem(null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ts.setSysOut(new PrintStream(out));
ts.setLevelSystemOut(TraceSystem.DEBUG);
ts.getTrace("test").debug(new Exception("error"), "test");
ts.close();
String outString = new String(out.toByteArray());
assertContains(outString, "error");
assertContains(outString, "Exception");
assertContains(outString, "test");
}
private void testReadOnly() throws Exception {
String readOnlyFile = getBaseDir() + "/readOnly.log";
IOUtils.delete(readOnlyFile);
IOUtils.openFileOutputStream(readOnlyFile, false).close();
FileSystem.getInstance(getBaseDir()).setReadOnly(readOnlyFile);
TraceSystem ts = new TraceSystem(readOnlyFile);
ts.setLevelFile(TraceSystem.INFO);
ts.getTrace("test").info("test");
IOUtils.delete(readOnlyFile);
ts.close();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论