整理单元测试文件结构,设置统一的数据库单元测试配置文件

This commit is contained in:
datagear 2019-05-28 12:33:29 +08:00
parent b712fbef93
commit 5f3584359f
22 changed files with 238 additions and 206 deletions

View File

@ -13,17 +13,5 @@
<name>datagear-connection</name>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-log4j12.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,9 +0,0 @@
#
# Set root category priority to WARN and its only appender to FILE.
#
log4j.rootLogger=DEBUG,A0
#==============appender 0==========================
log4j.appender.A0=org.apache.log4j.ConsoleAppender
log4j.appender.A0.layout=org.apache.log4j.PatternLayout
log4j.appender.A0.layout.ConversionPattern=[%d{HH:mm:ss,SSS}] [%p] %-x [%C.%M()] %m%n

View File

@ -0,0 +1,73 @@
/*
* Copyright 2018 datagear.tech. All Rights Reserved.
*/
package org.datagear.dbinfo;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
/**
* 数据库测试支持类
*
* @author datagear@163.com
*
*/
public abstract class DBTestSupport
{
private static final Properties JDBC_PROPERTIES = new Properties();
static
{
File jdbcConfigFile = new File("test/config/jdbc.properties");
if (!jdbcConfigFile.exists())
jdbcConfigFile = new File("../test/config/jdbc.properties");
try
{
Reader reader = new FileReader(jdbcConfigFile);
JDBC_PROPERTIES.load(reader);
reader.close();
}
catch (Exception e)
{
if (e instanceof RuntimeException)
throw (RuntimeException) e;
else
throw new RuntimeException(e);
}
}
protected Connection getConnection() throws Exception
{
return DriverManager.getConnection(JDBC_PROPERTIES.getProperty("jdbc.url"),
JDBC_PROPERTIES.getProperty("jdbc.user"), JDBC_PROPERTIES.getProperty("jdbc.password"));
}
protected Connection getConnection(Properties properties) throws Exception
{
properties.setProperty("user", JDBC_PROPERTIES.getProperty("jdbc.user"));
properties.setProperty("password", JDBC_PROPERTIES.getProperty("jdbc.password"));
return DriverManager.getConnection(JDBC_PROPERTIES.getProperty("jdbc.url"), properties);
}
protected void println()
{
System.out.println();
}
protected void println(Object o)
{
System.out.println((o == null ? "null" : o.toString()));
}
protected void print(Object o)
{
System.out.print((o == null ? "null" : o.toString()));
}
}

View File

@ -1,44 +0,0 @@
/*
* Copyright 2018 datagear.tech. All Rights Reserved.
*/
package org.datagear.dbinfo;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* 单元测试支持类
*
* @author datagear@163.com
*
*/
public class DatabaseTestSupport
{
public DatabaseTestSupport()
{
super();
}
protected Connection getMysqlConnection() throws Exception
{
String url = "jdbc:mysql://127.0.0.1:3306/datagear?useUnicode=true&amp;characterEncoding=UTF-8";
return DriverManager.getConnection(url, "root", "");
}
protected void println()
{
System.out.println();
}
protected void println(Object o)
{
System.out.println((o == null ? "null" : o.toString()));
}
protected void print(Object o)
{
System.out.print((o == null ? "null" : o.toString()));
}
}

View File

@ -9,14 +9,6 @@ import java.util.ArrayList;
import java.util.List;
import org.datagear.connection.JdbcUtil;
import org.datagear.dbinfo.ColumnInfo;
import org.datagear.dbinfo.DatabaseInfo;
import org.datagear.dbinfo.DevotedDatabaseInfoResolver;
import org.datagear.dbinfo.ExportedKeyInfo;
import org.datagear.dbinfo.GenericDatabaseInfoResolver;
import org.datagear.dbinfo.ImportedKeyInfo;
import org.datagear.dbinfo.TableInfo;
import org.datagear.dbinfo.WildcardDevotedDatabaseInfoResolver;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@ -28,7 +20,7 @@ import org.junit.Test;
* @author datagear@163.com
*
*/
public class GenericDatabaseInfoResolverTest extends DatabaseTestSupport
public class GenericDatabaseInfoResolverTest extends DBTestSupport
{
private GenericDatabaseInfoResolver genericDatabaseInfoResolver;
@ -54,7 +46,7 @@ public class GenericDatabaseInfoResolverTest extends DatabaseTestSupport
@Test
public void getDatabaseInfoTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
DatabaseInfo databaseInfo = null;
@ -77,7 +69,7 @@ public class GenericDatabaseInfoResolverTest extends DatabaseTestSupport
@Test
public void getTableInfosTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
TableInfo[] tableInfos = null;
@ -101,7 +93,7 @@ public class GenericDatabaseInfoResolverTest extends DatabaseTestSupport
@Test
public void getTableInfoTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
TableInfo tableInfo = null;
@ -122,7 +114,7 @@ public class GenericDatabaseInfoResolverTest extends DatabaseTestSupport
@Test
public void getColumnInfosTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
ColumnInfo[] columnInfos = null;
@ -146,7 +138,7 @@ public class GenericDatabaseInfoResolverTest extends DatabaseTestSupport
@Test
public void getPrimaryKeyColumnNamesTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
String[] primaryKeyColumnNames = null;
@ -172,7 +164,7 @@ public class GenericDatabaseInfoResolverTest extends DatabaseTestSupport
@Test
public void getImportedKeyInfosTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
ImportedKeyInfo[] importedKeyInfos = null;
@ -196,7 +188,7 @@ public class GenericDatabaseInfoResolverTest extends DatabaseTestSupport
@Test
public void getExportedKeyInfosTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{

View File

@ -33,17 +33,5 @@
<artifactId>datagear-persistence</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-log4j12.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,73 @@
/*
* Copyright 2018 datagear.tech. All Rights Reserved.
*/
package org.datagear.dbmodel;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
/**
* 数据库测试支持类
*
* @author datagear@163.com
*
*/
public abstract class DBTestSupport
{
private static final Properties JDBC_PROPERTIES = new Properties();
static
{
File jdbcConfigFile = new File("test/config/jdbc.properties");
if (!jdbcConfigFile.exists())
jdbcConfigFile = new File("../test/config/jdbc.properties");
try
{
Reader reader = new FileReader(jdbcConfigFile);
JDBC_PROPERTIES.load(reader);
reader.close();
}
catch (Exception e)
{
if (e instanceof RuntimeException)
throw (RuntimeException) e;
else
throw new RuntimeException(e);
}
}
protected Connection getConnection() throws Exception
{
return DriverManager.getConnection(JDBC_PROPERTIES.getProperty("jdbc.url"),
JDBC_PROPERTIES.getProperty("jdbc.user"), JDBC_PROPERTIES.getProperty("jdbc.password"));
}
protected Connection getConnection(Properties properties) throws Exception
{
properties.setProperty("user", JDBC_PROPERTIES.getProperty("jdbc.user"));
properties.setProperty("password", JDBC_PROPERTIES.getProperty("jdbc.password"));
return DriverManager.getConnection(JDBC_PROPERTIES.getProperty("jdbc.url"), properties);
}
protected void println()
{
System.out.println();
}
protected void println(Object o)
{
System.out.println((o == null ? "null" : o.toString()));
}
protected void print(Object o)
{
System.out.print((o == null ? "null" : o.toString()));
}
}

View File

@ -6,7 +6,6 @@ package org.datagear.dbmodel;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
@ -22,7 +21,7 @@ import org.junit.Test;
* @author datagear@163.com
*
*/
public class DatabaseMetaDataTest extends TestSupport
public class DatabaseMetaDataTest extends DBTestSupport
{
@Before
public void setUp() throws Exception
@ -37,7 +36,7 @@ public class DatabaseMetaDataTest extends TestSupport
@Test
public void getIdentifierQuoteStringTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{
@ -58,7 +57,7 @@ public class DatabaseMetaDataTest extends TestSupport
@Test
public void getCatalogsTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{
@ -77,7 +76,7 @@ public class DatabaseMetaDataTest extends TestSupport
@Test
public void getSchemasTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{
@ -96,7 +95,7 @@ public class DatabaseMetaDataTest extends TestSupport
@Test
public void getUserNameTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{
@ -118,7 +117,7 @@ public class DatabaseMetaDataTest extends TestSupport
@Test
public void getTableTypesTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{
@ -140,7 +139,7 @@ public class DatabaseMetaDataTest extends TestSupport
Properties props = new Properties();
props.setProperty("useInformationSchema", "true");
Connection cn = getMysqlConnection(props);
Connection cn = getConnection(props);
try
{
@ -159,7 +158,7 @@ public class DatabaseMetaDataTest extends TestSupport
@Test
public void getPrimaryKeysTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{
@ -178,7 +177,7 @@ public class DatabaseMetaDataTest extends TestSupport
@Test
public void getColumnTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{
@ -197,7 +196,7 @@ public class DatabaseMetaDataTest extends TestSupport
@Test
public void getExportedKeysTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{
@ -216,7 +215,7 @@ public class DatabaseMetaDataTest extends TestSupport
@Test
public void getImportedKeysTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{
@ -235,7 +234,7 @@ public class DatabaseMetaDataTest extends TestSupport
@Test
public void getUniqueKeysTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{
@ -290,13 +289,4 @@ public class DatabaseMetaDataTest extends TestSupport
println("---" + label + "--------------------------------------------");
println("");
}
protected Connection getMysqlConnection(Properties properties) throws SQLException
{
properties.setProperty("user", "root");
properties.setProperty("password", "");
return DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/datagear?useUnicode=true&amp;characterEncoding=UTF-8", properties);
}
}

View File

@ -38,7 +38,7 @@ import org.junit.Test;
* @author datagear@163.com
*
*/
public class GenericDatabaseModelResolverTest extends TestSupport
public class GenericDatabaseModelResolverTest extends DBTestSupport
{
protected ConnectionSensor connectionSensor;
@ -84,7 +84,7 @@ public class GenericDatabaseModelResolverTest extends TestSupport
@Test
public void resolveTest() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
int id = 999999;
@ -113,7 +113,7 @@ public class GenericDatabaseModelResolverTest extends TestSupport
@Test
public void resolveResultset() throws Exception
{
Connection cn = getMysqlConnection();
Connection cn = getConnection();
try
{

View File

@ -1,44 +0,0 @@
/*
* Copyright 2018 datagear.tech. All Rights Reserved.
*/
package org.datagear.dbmodel;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* 单元测试支持类
*
* @author datagear@163.com
*
*/
public class TestSupport
{
public TestSupport()
{
super();
}
protected Connection getMysqlConnection() throws Exception
{
String url = "jdbc:mysql://127.0.0.1:3306/datagear?useUnicode=true&amp;characterEncoding=UTF-8";
return DriverManager.getConnection(url, "root", "");
}
protected void println()
{
System.out.println();
}
protected void println(Object o)
{
System.out.println((o == null ? "null" : o.toString()));
}
protected void print(Object o)
{
System.out.print((o == null ? "null" : o.toString()));
}
}

View File

@ -1,4 +0,0 @@
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/datagear?useUnicode=true&amp;characterEncoding=UTF-8
user=root
password=

View File

@ -1,9 +0,0 @@
#
# Set root category priority to WARN and its only appender to FILE.
#
log4j.rootLogger=DEBUG,A0
#==============appender 0==========================
log4j.appender.A0=org.apache.log4j.ConsoleAppender
log4j.appender.A0.layout=org.apache.log4j.PatternLayout
log4j.appender.A0.layout.ConversionPattern=[%d{HH:mm:ss,SSS}] [%p] %-x [%C.%M()] %m%n

View File

@ -39,23 +39,5 @@
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-log4j12.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,4 +0,0 @@
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/datagear?useUnicode=true&amp;characterEncoding=UTF-8
user=root
password=

View File

@ -1,9 +0,0 @@
#
# Set root category priority to WARN and its only appender to FILE.
#
log4j.rootLogger=DEBUG,A0
#==============appender 0==========================
log4j.appender.A0=org.apache.log4j.ConsoleAppender
log4j.appender.A0.layout=org.apache.log4j.PatternLayout
log4j.appender.A0.layout.ConversionPattern=[%d{HH:mm:ss,SSS}] [%p] %-x [%C.%M()] %m%n

View File

@ -45,6 +45,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,60 @@
/**
* 数据库测试支持类
*
* @author datagear@163.com
*
*/
public abstract class DBTestSupport
{
private static final Properties JDBC_PROPERTIES = new Properties();
static
{
File jdbcConfigFile = new File("test/config/jdbc.properties");
if (!jdbcConfigFile.exists())
jdbcConfigFile = new File("../test/config/jdbc.properties");
try
{
Reader reader = new FileReader(jdbcConfigFile);
JDBC_PROPERTIES.load(reader);
reader.close();
}
catch (Exception e)
{
if (e instanceof RuntimeException)
throw (RuntimeException) e;
else
throw new RuntimeException(e);
}
}
protected Connection getConnection() throws Exception
{
return DriverManager.getConnection(JDBC_PROPERTIES.getProperty("jdbc.url"),
JDBC_PROPERTIES.getProperty("jdbc.user"), JDBC_PROPERTIES.getProperty("jdbc.password"));
}
protected Connection getConnection(Properties properties) throws Exception
{
properties.setProperty("user", JDBC_PROPERTIES.getProperty("jdbc.user"));
properties.setProperty("password", JDBC_PROPERTIES.getProperty("jdbc.password"));
return DriverManager.getConnection(JDBC_PROPERTIES.getProperty("jdbc.url"), properties);
}
protected void println()
{
System.out.println();
}
protected void println(Object o)
{
System.out.println((o == null ? "null" : o.toString()));
}
protected void print(Object o)
{
System.out.print((o == null ? "null" : o.toString()));
}
}

View File

@ -0,0 +1,3 @@
jdbc.url=jdbc:mysql://127.0.0.1:3306/datagear?useUnicode=true&amp;characterEncoding=UTF-8
jdbc.user=root
jdbc.password=