mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -27,94 +27,94 @@ public class CRUDTest {
|
||||
|
||||
@Test
|
||||
public void findIsNullTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", "is null"));
|
||||
final List<Entity> results = db.findAll(Entity.create("user").set("age", "is null"));
|
||||
Assert.assertEquals(0, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findIsNullTest2() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", "= null"));
|
||||
final List<Entity> results = db.findAll(Entity.create("user").set("age", "= null"));
|
||||
Assert.assertEquals(0, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findIsNullTest3() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", null));
|
||||
final List<Entity> results = db.findAll(Entity.create("user").set("age", null));
|
||||
Assert.assertEquals(0, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findBetweenTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", "between '18' and '40'"));
|
||||
final List<Entity> results = db.findAll(Entity.create("user").set("age", "between '18' and '40'"));
|
||||
Assert.assertEquals(1, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByBigIntegerTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", new BigInteger("12")));
|
||||
final List<Entity> results = db.findAll(Entity.create("user").set("age", new BigInteger("12")));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByBigDecimalTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", new BigDecimal("12")));
|
||||
final List<Entity> results = db.findAll(Entity.create("user").set("age", new BigDecimal("12")));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findLikeTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("name", "like \"%三%\""));
|
||||
final List<Entity> results = db.findAll(Entity.create("user").set("name", "like \"%三%\""));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findLikeTest2() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("name", new Condition("name", "三", LikeType.Contains)));
|
||||
final List<Entity> results = db.findAll(Entity.create("user").set("name", new Condition("name", "三", LikeType.Contains)));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findLikeTest3() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("name", new Condition("name", null, LikeType.Contains)));
|
||||
final List<Entity> results = db.findAll(Entity.create("user").set("name", new Condition("name", null, LikeType.Contains)));
|
||||
Assert.assertEquals(0, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findInTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("id", "in 1,2,3"));
|
||||
final List<Entity> results = db.findAll(Entity.create("user").set("id", "in 1,2,3"));
|
||||
Console.log(results);
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findInTest2() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user")
|
||||
final List<Entity> results = db.findAll(Entity.create("user")
|
||||
.set("id", new Condition("id", new long[]{1, 2, 3})));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findInTest3() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user")
|
||||
final List<Entity> results = db.findAll(Entity.create("user")
|
||||
.set("id", new long[]{1, 2, 3}));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllTest() throws SQLException {
|
||||
List<Entity> results = db.findAll("user");
|
||||
final List<Entity> results = db.findAll("user");
|
||||
Assert.assertEquals(4, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTest() throws SQLException {
|
||||
List<Entity> find = db.find(CollUtil.newArrayList("name AS name2"), Entity.create("user"), new EntityListHandler());
|
||||
final List<Entity> find = db.find(CollUtil.newArrayList("name AS name2"), Entity.create("user"), new EntityListHandler());
|
||||
Assert.assertFalse(find.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findActiveTest() {
|
||||
ActiveEntity entity = new ActiveEntity(db, "user");
|
||||
final ActiveEntity entity = new ActiveEntity(db, "user");
|
||||
entity.setFieldNames("name AS name2").load();
|
||||
Assert.assertEquals("user", entity.getTableName());
|
||||
Assert.assertFalse(entity.isEmpty());
|
||||
@@ -130,64 +130,64 @@ public class CRUDTest {
|
||||
public void crudTest() throws SQLException {
|
||||
|
||||
// 增
|
||||
Long id = db.insertForGeneratedKey(Entity.create("user").set("name", "unitTestUser").set("age", 66));
|
||||
final Long id = db.insertForGeneratedKey(Entity.create("user").set("name", "unitTestUser").set("age", 66));
|
||||
Assert.assertTrue(id > 0);
|
||||
Entity result = db.get("user", "name", "unitTestUser");
|
||||
final Entity result = db.get("user", "name", "unitTestUser");
|
||||
Assert.assertSame(66, result.getInt("age"));
|
||||
|
||||
// 改
|
||||
int update = db.update(Entity.create().set("age", 88), Entity.create("user").set("name", "unitTestUser"));
|
||||
final int update = db.update(Entity.create().set("age", 88), Entity.create("user").set("name", "unitTestUser"));
|
||||
Assert.assertTrue(update > 0);
|
||||
Entity result2 = db.get("user", "name", "unitTestUser");
|
||||
final Entity result2 = db.get("user", "name", "unitTestUser");
|
||||
Assert.assertSame(88, result2.getInt("age"));
|
||||
|
||||
// 删
|
||||
int del = db.del("user", "name", "unitTestUser");
|
||||
final int del = db.del("user", "name", "unitTestUser");
|
||||
Assert.assertTrue(del > 0);
|
||||
Entity result3 = db.get("user", "name", "unitTestUser");
|
||||
final Entity result3 = db.get("user", "name", "unitTestUser");
|
||||
Assert.assertNull(result3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void insertBatchTest() throws SQLException {
|
||||
User user1 = new User();
|
||||
final User user1 = new User();
|
||||
user1.setName("张三");
|
||||
user1.setAge(12);
|
||||
user1.setBirthday("19900112");
|
||||
user1.setGender(true);
|
||||
|
||||
User user2 = new User();
|
||||
final User user2 = new User();
|
||||
user2.setName("李四");
|
||||
user2.setAge(12);
|
||||
user2.setBirthday("19890512");
|
||||
user2.setGender(false);
|
||||
|
||||
Entity data1 = Entity.parse(user1);
|
||||
final Entity data1 = Entity.parse(user1);
|
||||
data1.put("name", null);
|
||||
Entity data2 = Entity.parse(user2);
|
||||
final Entity data2 = Entity.parse(user2);
|
||||
|
||||
Console.log(data1);
|
||||
Console.log(data2);
|
||||
|
||||
int[] result = db.insert(CollUtil.newArrayList(data1, data2));
|
||||
final int[] result = db.insert(CollUtil.newArrayList(data1, data2));
|
||||
Console.log(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void insertBatchOneTest() throws SQLException {
|
||||
User user1 = new User();
|
||||
final User user1 = new User();
|
||||
user1.setName("张三");
|
||||
user1.setAge(12);
|
||||
user1.setBirthday("19900112");
|
||||
user1.setGender(true);
|
||||
|
||||
Entity data1 = Entity.parse(user1);
|
||||
final Entity data1 = Entity.parse(user1);
|
||||
|
||||
Console.log(data1);
|
||||
|
||||
int[] result = db.insert(CollUtil.newArrayList(data1));
|
||||
final int[] result = db.insert(CollUtil.newArrayList(data1));
|
||||
Console.log(result);
|
||||
}
|
||||
|
||||
|
@@ -31,10 +31,10 @@ public class ConcurentTest {
|
||||
public void findTest() {
|
||||
for(int i = 0; i < 10000; i++) {
|
||||
ThreadUtil.execute(() -> {
|
||||
List<Entity> find;
|
||||
final List<Entity> find;
|
||||
try {
|
||||
find = db.find(CollUtil.newArrayList("name AS name2"), Entity.create("user"), new EntityListHandler());
|
||||
} catch (SQLException e) {
|
||||
} catch (final SQLException e) {
|
||||
throw new DbRuntimeException(e);
|
||||
}
|
||||
Console.log(find);
|
||||
|
@@ -21,42 +21,42 @@ public class DbTest {
|
||||
|
||||
@Test
|
||||
public void queryTest() throws SQLException {
|
||||
List<Entity> find = Db.use().query("select * from user where age = ?", 18);
|
||||
final List<Entity> find = Db.use().query("select * from user where age = ?", 18);
|
||||
Assert.assertEquals("王五", find.get(0).get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTest() throws SQLException {
|
||||
List<Entity> find = Db.use().find(Entity.create("user").set("age", 18));
|
||||
final List<Entity> find = Db.use().find(Entity.create("user").set("age", 18));
|
||||
Assert.assertEquals("王五", find.get(0).get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pageTest() throws SQLException {
|
||||
// 测试数据库中一共4条数据,第0页有3条,第1页有1条
|
||||
List<Entity> page0 = Db.use().page(Entity.create("user"), 0, 3);
|
||||
final List<Entity> page0 = Db.use().page(Entity.create("user"), 0, 3);
|
||||
Assert.assertEquals(3, page0.size());
|
||||
List<Entity> page1 = Db.use().page(Entity.create("user"), 1, 3);
|
||||
final List<Entity> page1 = Db.use().page(Entity.create("user"), 1, 3);
|
||||
Assert.assertEquals(1, page1.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pageTest2() throws SQLException {
|
||||
String sql = "select * from user order by name";
|
||||
final String sql = "select * from user order by name";
|
||||
// 测试数据库中一共4条数据,第0页有3条,第1页有1条
|
||||
List<Entity> page0 = Db.use().page(
|
||||
final List<Entity> page0 = Db.use().page(
|
||||
sql, Page.of(0, 3));
|
||||
Assert.assertEquals(3, page0.size());
|
||||
|
||||
List<Entity> page1 = Db.use().page(
|
||||
final List<Entity> page1 = Db.use().page(
|
||||
sql, Page.of(1, 3));
|
||||
Assert.assertEquals(1, page1.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pageWithParamsTest() throws SQLException {
|
||||
String sql = "select * from user where name = ?";
|
||||
PageResult<Entity> result = Db.use().page(
|
||||
final String sql = "select * from user where name = ?";
|
||||
final PageResult<Entity> result = Db.use().page(
|
||||
sql, Page.of(0, 3), "张三");
|
||||
|
||||
Assert.assertEquals(2, result.getTotal());
|
||||
@@ -93,11 +93,11 @@ public class DbTest {
|
||||
|
||||
@Test
|
||||
public void findByTest() throws SQLException {
|
||||
List<Entity> find = Db.use().findBy("user",
|
||||
final List<Entity> find = Db.use().findBy("user",
|
||||
Condition.parse("age", "> 18"),
|
||||
Condition.parse("age", "< 100")
|
||||
);
|
||||
for (Entity entity : find) {
|
||||
for (final Entity entity : find) {
|
||||
StaticLog.debug("{}", entity);
|
||||
}
|
||||
Assert.assertEquals("unitTestUser", find.get(0).get("name"));
|
||||
@@ -118,7 +118,7 @@ public class DbTest {
|
||||
public void queryFetchTest() throws SQLException {
|
||||
// https://gitee.com/dromara/hutool/issues/I4JXWN
|
||||
Db.use().query((conn->{
|
||||
PreparedStatement ps = conn.prepareStatement("select * from table",
|
||||
final PreparedStatement ps = conn.prepareStatement("select * from table",
|
||||
ResultSet.TYPE_FORWARD_ONLY,
|
||||
ResultSet.CONCUR_READ_ONLY);
|
||||
ps.setFetchSize(Integer.MIN_VALUE);
|
||||
|
@@ -10,20 +10,20 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* Derby数据库单元测试
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class DerbyTest {
|
||||
|
||||
|
||||
private static final String DS_GROUP_NAME = "derby";
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws SQLException {
|
||||
Db db = Db.use(DS_GROUP_NAME);
|
||||
final Db db = Db.use(DS_GROUP_NAME);
|
||||
try{
|
||||
db.execute("CREATE TABLE test(a INTEGER, b BIGINT)");
|
||||
}catch (SQLException e){
|
||||
}catch (final SQLException e){
|
||||
// 数据库已存在
|
||||
return;
|
||||
}
|
||||
@@ -33,18 +33,18 @@ public class DerbyTest {
|
||||
db.insert(Entity.create("test").set("a", 3).set("b", 31));
|
||||
db.insert(Entity.create("test").set("a", 4).set("b", 41));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void queryTest() throws SQLException {
|
||||
List<Entity> query = Db.use(DS_GROUP_NAME).query("select * from test");
|
||||
final List<Entity> query = Db.use(DS_GROUP_NAME).query("select * from test");
|
||||
Assert.assertEquals(4, query.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void findTest() throws SQLException {
|
||||
List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
|
||||
final List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
|
||||
Assert.assertEquals(4, query.size());
|
||||
}
|
||||
}
|
||||
|
@@ -28,64 +28,64 @@ public class DsTest {
|
||||
|
||||
@Test
|
||||
public void defaultDsTest() throws SQLException {
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
final DataSource ds = DSFactory.get("test");
|
||||
final Db db = Db.use(ds);
|
||||
final List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hikariDsTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new HikariDSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
final DataSource ds = DSFactory.get("test");
|
||||
final Db db = Db.use(ds);
|
||||
final List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void druidDsTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new DruidDSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
final DataSource ds = DSFactory.get("test");
|
||||
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
final Db db = Db.use(ds);
|
||||
final List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatDsTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new TomcatDSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
final DataSource ds = DSFactory.get("test");
|
||||
final Db db = Db.use(ds);
|
||||
final List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beeCPDsTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new BeeDSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
final DataSource ds = DSFactory.get("test");
|
||||
final Db db = Db.use(ds);
|
||||
final List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dbcpDsTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new DbcpDSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
final DataSource ds = DSFactory.get("test");
|
||||
final Db db = Db.use(ds);
|
||||
final List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void c3p0DsTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new C3p0DSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
final DataSource ds = DSFactory.get("test");
|
||||
final Db db = Db.use(ds);
|
||||
final List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ public class DsTest {
|
||||
public void c3p0DsUserAndPassTest() {
|
||||
// https://gitee.com/dromara/hutool/issues/I4T7XZ
|
||||
DSFactory.setCurrentDSFactory(new C3p0DSFactory());
|
||||
ComboPooledDataSource ds = (ComboPooledDataSource) ((DataSourceWrapper) DSFactory.get("mysql")).getRaw();
|
||||
final ComboPooledDataSource ds = (ComboPooledDataSource) ((DataSourceWrapper) DSFactory.get("mysql")).getRaw();
|
||||
Assert.assertEquals("root", ds.getUser());
|
||||
Assert.assertEquals("123456", ds.getPassword());
|
||||
}
|
||||
@@ -101,9 +101,9 @@ public class DsTest {
|
||||
@Test
|
||||
public void hutoolPoolTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new PooledDSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
final DataSource ds = DSFactory.get("test");
|
||||
final Db db = Db.use(ds);
|
||||
final List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
}
|
||||
|
@@ -14,22 +14,22 @@ public class EntityTest {
|
||||
|
||||
@Test
|
||||
public void parseTest() {
|
||||
User user = new User();
|
||||
final User user = new User();
|
||||
user.setId(1);
|
||||
user.setName("test");
|
||||
|
||||
Entity entity = Entity.create("testTable").parseBean(user);
|
||||
final Entity entity = Entity.create("testTable").parseBean(user);
|
||||
Assert.assertEquals(Integer.valueOf(1), entity.getInt("id"));
|
||||
Assert.assertEquals("test", entity.getStr("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest2() {
|
||||
User user = new User();
|
||||
final User user = new User();
|
||||
user.setId(1);
|
||||
user.setName("test");
|
||||
|
||||
Entity entity = Entity.create().parseBean(user);
|
||||
final Entity entity = Entity.create().parseBean(user);
|
||||
Assert.assertEquals(Integer.valueOf(1), entity.getInt("id"));
|
||||
Assert.assertEquals("test", entity.getStr("name"));
|
||||
Assert.assertEquals("user", entity.getTableName());
|
||||
@@ -37,10 +37,10 @@ public class EntityTest {
|
||||
|
||||
@Test
|
||||
public void parseTest3() {
|
||||
User user = new User();
|
||||
final User user = new User();
|
||||
user.setName("test");
|
||||
|
||||
Entity entity = Entity.create().parseBean(user, false, true);
|
||||
final Entity entity = Entity.create().parseBean(user, false, true);
|
||||
|
||||
Assert.assertFalse(entity.containsKey("id"));
|
||||
Assert.assertEquals("test", entity.getStr("name"));
|
||||
@@ -49,8 +49,8 @@ public class EntityTest {
|
||||
|
||||
@Test
|
||||
public void entityToBeanIgnoreCaseTest() {
|
||||
Entity entity = Entity.create().set("ID", 2).set("NAME", "testName");
|
||||
User user = entity.toBeanIgnoreCase(User.class);
|
||||
final Entity entity = Entity.create().set("ID", 2).set("NAME", "testName");
|
||||
final User user = entity.toBeanIgnoreCase(User.class);
|
||||
|
||||
Assert.assertEquals(Integer.valueOf(2), user.getId());
|
||||
Assert.assertEquals("testName", user.getName());
|
||||
|
@@ -10,7 +10,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* Entity测试
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
@@ -25,42 +25,42 @@ public class FindBeanTest {
|
||||
|
||||
@Test
|
||||
public void findAllBeanTest() throws SQLException {
|
||||
List<User> results = db.findAll(Entity.create("user"), User.class);
|
||||
|
||||
final List<User> results = db.findAll(Entity.create("user"), User.class);
|
||||
|
||||
Assert.assertEquals(4, results.size());
|
||||
Assert.assertEquals(Integer.valueOf(1), results.get(0).getId());
|
||||
Assert.assertEquals("张三", results.get(0).getName());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void findAllListTest() throws SQLException {
|
||||
List<List> results = db.findAll(Entity.create("user"), List.class);
|
||||
|
||||
final List<List> results = db.findAll(Entity.create("user"), List.class);
|
||||
|
||||
Assert.assertEquals(4, results.size());
|
||||
Assert.assertEquals(1, results.get(0).get(0));
|
||||
Assert.assertEquals("张三", results.get(0).get(1));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void findAllArrayTest() throws SQLException {
|
||||
List<Object[]> results = db.findAll(Entity.create("user"), Object[].class);
|
||||
|
||||
final List<Object[]> results = db.findAll(Entity.create("user"), Object[].class);
|
||||
|
||||
Assert.assertEquals(4, results.size());
|
||||
Assert.assertEquals(1, results.get(0)[0]);
|
||||
Assert.assertEquals("张三", results.get(0)[1]);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void findAllStringTest() throws SQLException {
|
||||
List<String> results = db.findAll(Entity.create("user"), String.class);
|
||||
final List<String> results = db.findAll(Entity.create("user"), String.class);
|
||||
Assert.assertEquals(4, results.size());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void findAllStringArrayTest() throws SQLException {
|
||||
List<String[]> results = db.findAll(Entity.create("user"), String[].class);
|
||||
|
||||
final List<String[]> results = db.findAll(Entity.create("user"), String[].class);
|
||||
|
||||
Assert.assertEquals(4, results.size());
|
||||
Assert.assertEquals("1", results.get(0)[0]);
|
||||
Assert.assertEquals("张三", results.get(0)[1]);
|
||||
|
@@ -19,7 +19,7 @@ public class H2Test {
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws SQLException {
|
||||
Db db = Db.use(DS_GROUP_NAME);
|
||||
final Db db = Db.use(DS_GROUP_NAME);
|
||||
db.execute("CREATE TABLE test(a INTEGER, b BIGINT)");
|
||||
|
||||
db.insert(Entity.create("test").set("a", 1).set("b", 11));
|
||||
@@ -30,21 +30,21 @@ public class H2Test {
|
||||
|
||||
@Test
|
||||
public void queryTest() throws SQLException {
|
||||
List<Entity> query = Db.use(DS_GROUP_NAME).query("select * from test");
|
||||
final List<Entity> query = Db.use(DS_GROUP_NAME).query("select * from test");
|
||||
Assert.assertEquals(4, query.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTest() throws SQLException {
|
||||
List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
|
||||
final List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
|
||||
Assert.assertEquals(4, query.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void upsertTest() throws SQLException {
|
||||
Db db=Db.use(DS_GROUP_NAME);
|
||||
final Db db=Db.use(DS_GROUP_NAME);
|
||||
db.upsert(Entity.create("test").set("a",1).set("b",111),"a");
|
||||
Entity a1=db.get("test","a",1);
|
||||
final Entity a1=db.get("test","a",1);
|
||||
Assert.assertEquals(Long.valueOf(111),a1.getLong("b"));
|
||||
}
|
||||
}
|
||||
|
@@ -9,33 +9,33 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* HSQLDB数据库单元测试
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class HsqldbTest {
|
||||
|
||||
|
||||
private static final String DS_GROUP_NAME = "hsqldb";
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws SQLException {
|
||||
Db db = Db.use(DS_GROUP_NAME);
|
||||
final Db db = Db.use(DS_GROUP_NAME);
|
||||
db.execute("CREATE TABLE test(a INTEGER, b BIGINT)");
|
||||
db.insert(Entity.create("test").set("a", 1).set("b", 11));
|
||||
db.insert(Entity.create("test").set("a", 2).set("b", 21));
|
||||
db.insert(Entity.create("test").set("a", 3).set("b", 31));
|
||||
db.insert(Entity.create("test").set("a", 4).set("b", 41));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void connTest() throws SQLException {
|
||||
List<Entity> query = Db.use(DS_GROUP_NAME).query("select * from test");
|
||||
final List<Entity> query = Db.use(DS_GROUP_NAME).query("select * from test");
|
||||
Assert.assertEquals(4, query.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTest() throws SQLException {
|
||||
List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
|
||||
final List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
|
||||
Assert.assertEquals(4, query.size());
|
||||
}
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@ public class MySQLTest {
|
||||
@BeforeClass
|
||||
@Ignore
|
||||
public static void createTable() throws SQLException {
|
||||
Db db = Db.use("mysql");
|
||||
final Db db = Db.use("mysql");
|
||||
db.executeBatch("drop table if exists testuser", "CREATE TABLE if not exists `testuser` ( `id` int(11) NOT NULL, `account` varchar(255) DEFAULT NULL, `pass` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8");
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public class MySQLTest {
|
||||
@Ignore
|
||||
public void txTest() throws SQLException {
|
||||
Db.use("mysql").tx(db -> {
|
||||
int update = db.update(Entity.create("user").set("text", "描述100"), Entity.create().set("id", 100));
|
||||
final int update = db.update(Entity.create("user").set("text", "描述100"), Entity.create().set("id", 100));
|
||||
db.update(Entity.create("user").set("text", "描述101"), Entity.create().set("id", 101));
|
||||
if (1 == update) {
|
||||
// 手动指定异常,然后测试回滚触发
|
||||
@@ -58,8 +58,8 @@ public class MySQLTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void pageTest() throws SQLException {
|
||||
PageResult<Entity> result = Db.use("mysql").page(Entity.create("user"), new Page(2, 10));
|
||||
for (Entity entity : result) {
|
||||
final PageResult<Entity> result = Db.use("mysql").page(Entity.create("user"), new Page(2, 10));
|
||||
for (final Entity entity : result) {
|
||||
Console.log(entity.get("id"));
|
||||
}
|
||||
}
|
||||
@@ -74,11 +74,11 @@ public class MySQLTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void upsertTest() throws SQLException {
|
||||
Db db = Db.use("mysql");
|
||||
final Db db = Db.use("mysql");
|
||||
db.insert(Entity.create("testuser").set("id", 1).set("account", "ice").set("pass", "123456"));
|
||||
db.upsert(Entity.create("testuser").set("id", 1).set("account", "icefairy").set("pass", "a123456"));
|
||||
Entity user = db.get(Entity.create("testuser").set("id", 1));
|
||||
final Entity user = db.get(Entity.create("testuser").set("id", 1));
|
||||
System.out.println("user======="+user.getStr("account")+"___"+user.getStr("pass"));
|
||||
Assert.assertEquals(user.getStr("account"), new String("icefairy"));
|
||||
Assert.assertEquals(user.getStr("account"), "icefairy");
|
||||
}
|
||||
}
|
||||
|
@@ -14,15 +14,15 @@ public class NamedSqlTest {
|
||||
|
||||
@Test
|
||||
public void parseTest() {
|
||||
String sql = "select * from table where id=@id and name = @name1 and nickName = :subName";
|
||||
final String sql = "select * from table where id=@id and name = @name1 and nickName = :subName";
|
||||
|
||||
Map<String, Object> paramMap = MapUtil
|
||||
final Map<String, Object> paramMap = MapUtil
|
||||
.builder("name1", (Object)"张三")
|
||||
.put("age", 12)
|
||||
.put("subName", "小豆豆")
|
||||
.build();
|
||||
|
||||
NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
final NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
//未指定参数原样输出
|
||||
Assert.assertEquals("select * from table where id=@id and name = ? and nickName = ?", namedSql.getSql());
|
||||
Assert.assertEquals("张三", namedSql.getParams()[0]);
|
||||
@@ -31,16 +31,16 @@ public class NamedSqlTest {
|
||||
|
||||
@Test
|
||||
public void parseTest2() {
|
||||
String sql = "select * from table where id=@id and name = @name1 and nickName = :subName";
|
||||
final String sql = "select * from table where id=@id and name = @name1 and nickName = :subName";
|
||||
|
||||
Map<String, Object> paramMap = MapUtil
|
||||
final Map<String, Object> paramMap = MapUtil
|
||||
.builder("name1", (Object)"张三")
|
||||
.put("age", 12)
|
||||
.put("subName", "小豆豆")
|
||||
.put("id", null)
|
||||
.build();
|
||||
|
||||
NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
final NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
Assert.assertEquals("select * from table where id=? and name = ? and nickName = ?", namedSql.getSql());
|
||||
//指定了null参数的依旧替换,参数值为null
|
||||
Assert.assertNull(namedSql.getParams()[0]);
|
||||
@@ -51,35 +51,35 @@ public class NamedSqlTest {
|
||||
@Test
|
||||
public void parseTest3() {
|
||||
// 测试连续变量名出现是否有问题
|
||||
String sql = "SELECT to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as sysdate FROM dual";
|
||||
final String sql = "SELECT to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as sysdate FROM dual";
|
||||
|
||||
Map<String, Object> paramMap = MapUtil
|
||||
final Map<String, Object> paramMap = MapUtil
|
||||
.builder("name1", (Object)"张三")
|
||||
.build();
|
||||
|
||||
NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
final NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
Assert.assertEquals(sql, namedSql.getSql());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest4() {
|
||||
// 测试postgre中形如data_value::numeric是否出错
|
||||
String sql = "select device_key, min(data_value::numeric) as data_value from device";
|
||||
final String sql = "select device_key, min(data_value::numeric) as data_value from device";
|
||||
|
||||
Map<String, Object> paramMap = MapUtil
|
||||
final Map<String, Object> paramMap = MapUtil
|
||||
.builder("name1", (Object)"张三")
|
||||
.build();
|
||||
|
||||
NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
final NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
Assert.assertEquals(sql, namedSql.getSql());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseInTest(){
|
||||
String sql = "select * from user where id in (:ids)";
|
||||
final String sql = "select * from user where id in (:ids)";
|
||||
final HashMap<String, Object> paramMap = MapUtil.of("ids", new int[]{1, 2, 3});
|
||||
|
||||
NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
final NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
Assert.assertEquals("select * from user where id in (?,?,?)", namedSql.getSql());
|
||||
Assert.assertEquals(1, namedSql.getParams()[0]);
|
||||
Assert.assertEquals(2, namedSql.getParams()[1]);
|
||||
@@ -88,10 +88,10 @@ public class NamedSqlTest {
|
||||
|
||||
@Test
|
||||
public void queryTest() throws SQLException {
|
||||
Map<String, Object> paramMap = MapUtil
|
||||
final Map<String, Object> paramMap = MapUtil
|
||||
.builder("name1", (Object)"王五")
|
||||
.put("age1", 18).build();
|
||||
String sql = "select * from user where name = @name1 and age = @age1";
|
||||
final String sql = "select * from user where name = @name1 and age = @age1";
|
||||
|
||||
List<Entity> query = Db.use().query(sql, paramMap);
|
||||
Assert.assertEquals(1, query.size());
|
||||
|
@@ -20,20 +20,20 @@ public class OracleTest {
|
||||
|
||||
@Test
|
||||
public void oraclePageSqlTest() {
|
||||
Page page = new Page(0, 10);
|
||||
Entity where = Entity.create("PMCPERFORMANCEINFO").set("yearPI", "2017");
|
||||
final Page page = new Page(0, 10);
|
||||
final Entity where = Entity.create("PMCPERFORMANCEINFO").set("yearPI", "2017");
|
||||
final Query query = new Query(SqlUtil.buildConditions(where), where.getTableName());
|
||||
query.setPage(page);
|
||||
|
||||
SqlBuilder find = SqlBuilder.create(null).query(query).orderBy(page.getOrders());
|
||||
final SqlBuilder find = SqlBuilder.create(null).query(query).orderBy(page.getOrders());
|
||||
final int[] startEnd = page.getStartEnd();
|
||||
SqlBuilder builder = SqlBuilder.create(null).append("SELECT * FROM ( SELECT row_.*, rownum rownum_ from ( ")//
|
||||
final SqlBuilder builder = SqlBuilder.create(null).append("SELECT * FROM ( SELECT row_.*, rownum rownum_ from ( ")//
|
||||
.append(find)//
|
||||
.append(" ) row_ where rownum <= ").append(startEnd[1])//
|
||||
.append(") table_alias")//
|
||||
.append(" where table_alias.rownum_ >= ").append(startEnd[0]);//
|
||||
|
||||
String ok = "SELECT * FROM "//
|
||||
final String ok = "SELECT * FROM "//
|
||||
+ "( SELECT row_.*, rownum rownum_ from ( SELECT * FROM PMCPERFORMANCEINFO WHERE yearPI = ? ) row_ "//
|
||||
+ "where rownum <= 10) table_alias where table_alias.rownum_ >= 0";//
|
||||
Assert.assertEquals(ok, builder.toString());
|
||||
@@ -55,8 +55,8 @@ public class OracleTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void pageTest() throws SQLException {
|
||||
PageResult<Entity> result = Db.use("orcl").page(Entity.create("T_USER"), new Page(2, 10));
|
||||
for (Entity entity : result) {
|
||||
final PageResult<Entity> result = Db.use("orcl").page(Entity.create("T_USER"), new Page(2, 10));
|
||||
for (final Entity entity : result) {
|
||||
Console.log(entity.get("ID"));
|
||||
}
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ public class PageTest {
|
||||
|
||||
@Test
|
||||
public void addOrderTest() {
|
||||
Page page = new Page();
|
||||
final Page page = new Page();
|
||||
page.addOrder(new Order("aaa"));
|
||||
Assert.assertEquals(page.getOrders().length, 1);
|
||||
page.addOrder(new Order("aaa"));
|
||||
|
@@ -26,9 +26,9 @@ public class PicTransferTest {
|
||||
);
|
||||
}
|
||||
|
||||
private static void save(ResultSet rs) throws SQLException{
|
||||
String destDir = "d:/test/pic";
|
||||
String path = StrUtil.format("{}/{}-{}.jpg", destDir, rs.getString("NAME"), rs.getString("GROUP"));
|
||||
private static void save(final ResultSet rs) throws SQLException{
|
||||
final String destDir = "d:/test/pic";
|
||||
final String path = StrUtil.format("{}/{}-{}.jpg", destDir, rs.getString("NAME"), rs.getString("GROUP"));
|
||||
FileUtil.writeFromStream(rs.getBlob("PIC").getBinaryStream(), path);
|
||||
}
|
||||
}
|
||||
|
@@ -29,8 +29,8 @@ public class PostgreTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void pageTest() throws SQLException {
|
||||
PageResult<Entity> result = Db.use("postgre").page(Entity.create("user"), new Page(2, 10));
|
||||
for (Entity entity : result) {
|
||||
final PageResult<Entity> result = Db.use("postgre").page(Entity.create("user"), new Page(2, 10));
|
||||
for (final Entity entity : result) {
|
||||
Console.log(entity.get("id"));
|
||||
}
|
||||
}
|
||||
@@ -38,12 +38,12 @@ public class PostgreTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void upsertTest() throws SQLException {
|
||||
Db db = Db.use("postgre");
|
||||
final Db db = Db.use("postgre");
|
||||
db.executeBatch("drop table if exists ctest",
|
||||
"create table if not exists \"ctest\" ( \"id\" serial4, \"t1\" varchar(255) COLLATE \"pg_catalog\".\"default\", \"t2\" varchar(255) COLLATE \"pg_catalog\".\"default\", \"t3\" varchar(255) COLLATE \"pg_catalog\".\"default\", CONSTRAINT \"ctest_pkey\" PRIMARY KEY (\"id\") ) ");
|
||||
db.insert(Entity.create("ctest").set("id", 1).set("t1", "111").set("t2", "222").set("t3", "333"));
|
||||
db.upsert(Entity.create("ctest").set("id", 1).set("t1", "new111").set("t2", "new222").set("t3", "bew333"),"id");
|
||||
Entity et=db.get(Entity.create("ctest").set("id", 1));
|
||||
final Entity et=db.get(Entity.create("ctest").set("id", 1));
|
||||
Assert.assertEquals("new111",et.getStr("t1"));
|
||||
}
|
||||
}
|
||||
|
@@ -11,20 +11,20 @@ import java.sql.SQLException;
|
||||
*
|
||||
*/
|
||||
public class SessionTest {
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void transTest() {
|
||||
Session session = Session.create("test");
|
||||
final Session session = Session.create("test");
|
||||
try {
|
||||
session.beginTransaction();
|
||||
session.update(Entity.create().set("age", 76), Entity.create("user").set("name", "unitTestUser"));
|
||||
session.commit();
|
||||
} catch (SQLException e) {
|
||||
} catch (final SQLException e) {
|
||||
session.quietRollback();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void txTest() throws SQLException {
|
||||
|
@@ -35,8 +35,8 @@ public class SqlServerTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void pageTest() throws SQLException {
|
||||
PageResult<Entity> result = Db.use("sqlserver").page(Entity.create("T_USER"), new Page(2, 10));
|
||||
for (Entity entity : result) {
|
||||
final PageResult<Entity> result = Db.use("sqlserver").page(Entity.create("T_USER"), new Page(2, 10));
|
||||
for (final Entity entity : result) {
|
||||
Console.log(entity.get("ID"));
|
||||
}
|
||||
}
|
||||
|
@@ -8,17 +8,17 @@ import org.junit.Test;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class UpdateTest {
|
||||
|
||||
|
||||
Db db;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
db = Db.use("test");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 对更新做单元测试
|
||||
*
|
||||
*
|
||||
* @throws SQLException SQL异常
|
||||
*/
|
||||
@Test
|
||||
@@ -26,9 +26,9 @@ public class UpdateTest {
|
||||
public void updateTest() throws SQLException {
|
||||
|
||||
// 改
|
||||
int update = db.update(Entity.create("user").set("age", 88), Entity.create().set("name", "unitTestUser"));
|
||||
final int update = db.update(Entity.create("user").set("age", 88), Entity.create().set("name", "unitTestUser"));
|
||||
Assert.assertTrue(update > 0);
|
||||
Entity result2 = db.get("user", "name", "unitTestUser");
|
||||
final Entity result2 = db.get("user", "name", "unitTestUser");
|
||||
Assert.assertSame(88, result2.getInt("age"));
|
||||
}
|
||||
}
|
||||
|
@@ -7,8 +7,8 @@ public class DriverUtilTest {
|
||||
|
||||
@Test
|
||||
public void identifyDriverTest(){
|
||||
String url = "jdbc:h2:file:./db/test;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL";
|
||||
String driver = DriverUtil.identifyDriver(url); // driver 返回 mysql 的 driver
|
||||
final String url = "jdbc:h2:file:./db/test;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL";
|
||||
final String driver = DriverUtil.identifyDriver(url); // driver 返回 mysql 的 driver
|
||||
Assert.assertEquals("org.h2.Driver", driver);
|
||||
}
|
||||
}
|
||||
|
@@ -20,25 +20,25 @@ public class MetaUtilTest {
|
||||
|
||||
@Test
|
||||
public void getTablesTest() {
|
||||
List<String> tables = MetaUtil.getTables(ds);
|
||||
final List<String> tables = MetaUtil.getTables(ds);
|
||||
Assert.assertEquals("user", tables.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTableMetaTest() {
|
||||
Table table = MetaUtil.getTableMeta(ds, "user");
|
||||
final Table table = MetaUtil.getTableMeta(ds, "user");
|
||||
Assert.assertEquals(CollUtil.newHashSet("id"), table.getPkNames());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getColumnNamesTest() {
|
||||
String[] names = MetaUtil.getColumnNames(ds, "user");
|
||||
final String[] names = MetaUtil.getColumnNames(ds, "user");
|
||||
Assert.assertArrayEquals(StrUtil.splitToArray("id,name,age,birthday,gender", ','), names);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTableIndexInfoTest() {
|
||||
Table table = MetaUtil.getTableMeta(ds, "user_1");
|
||||
final Table table = MetaUtil.getTableMeta(ds, "user_1");
|
||||
Assert.assertEquals(table.getIndexInfoList().size(), 2);
|
||||
}
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ public class MongoDBTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void mongoDSTest() {
|
||||
MongoDatabase db = MongoFactory.getDS("master").getDb("test");
|
||||
final MongoDatabase db = MongoFactory.getDS("master").getDb("test");
|
||||
Assert.assertEquals("test", db.getName());
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@ package cn.hutool.db.pojo;
|
||||
|
||||
/**
|
||||
* 测试用POJO,与测试数据库中的user表对应
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
@@ -17,7 +17,7 @@ public class User {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
public void setId(final Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class User {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public class User {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
public void setAge(final int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class User {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(String birthday) {
|
||||
public void setBirthday(final String birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public class User {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(boolean gender) {
|
||||
public void setGender(final boolean gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
|
@@ -7,10 +7,10 @@ public class ConditionBuilderTest {
|
||||
|
||||
@Test
|
||||
public void buildTest(){
|
||||
Condition c1 = new Condition("user", null);
|
||||
Condition c2 = new Condition("name", "!= null");
|
||||
final Condition c1 = new Condition("user", null);
|
||||
final Condition c2 = new Condition("name", "!= null");
|
||||
c2.setLinkOperator(LogicalOperator.OR);
|
||||
Condition c3 = new Condition("group", "like %aaa");
|
||||
final Condition c3 = new Condition("group", "like %aaa");
|
||||
|
||||
final ConditionBuilder builder = ConditionBuilder.of(c1, c2, c3);
|
||||
final String sql = builder.build();
|
||||
|
@@ -7,17 +7,17 @@ import org.junit.Test;
|
||||
public class ConditionGroupTest {
|
||||
@Test
|
||||
public void ConditionGroupToStringTest() {
|
||||
Condition condition1 = new Condition("a", "A");
|
||||
Condition condition2 = new Condition("b", "B");
|
||||
final Condition condition1 = new Condition("a", "A");
|
||||
final Condition condition2 = new Condition("b", "B");
|
||||
condition2.setLinkOperator(LogicalOperator.OR);
|
||||
Condition condition3 = new Condition("c", "C");
|
||||
Condition condition4 = new Condition("d", "D");
|
||||
final Condition condition3 = new Condition("c", "C");
|
||||
final Condition condition4 = new Condition("d", "D");
|
||||
|
||||
ConditionGroup cg = new ConditionGroup();
|
||||
final ConditionGroup cg = new ConditionGroup();
|
||||
cg.addConditions(condition1, condition2);
|
||||
|
||||
// 条件组嵌套情况
|
||||
ConditionGroup cg2 = new ConditionGroup();
|
||||
final ConditionGroup cg2 = new ConditionGroup();
|
||||
cg2.addConditions(cg, condition3);
|
||||
|
||||
final ConditionBuilder conditionBuilder = ConditionBuilder.of(cg2, condition4);
|
||||
|
@@ -9,48 +9,48 @@ public class ConditionTest {
|
||||
|
||||
@Test
|
||||
public void toStringTest() {
|
||||
Condition conditionNull = new Condition("user", null);
|
||||
final Condition conditionNull = new Condition("user", null);
|
||||
Assert.assertEquals("user IS NULL", conditionNull.toString());
|
||||
|
||||
Condition conditionNotNull = new Condition("user", "!= null");
|
||||
final Condition conditionNotNull = new Condition("user", "!= null");
|
||||
Assert.assertEquals("user IS NOT NULL", conditionNotNull.toString());
|
||||
|
||||
Condition condition2 = new Condition("user", "= zhangsan");
|
||||
final Condition condition2 = new Condition("user", "= zhangsan");
|
||||
Assert.assertEquals("user = ?", condition2.toString());
|
||||
|
||||
Condition conditionLike = new Condition("user", "like %aaa");
|
||||
final Condition conditionLike = new Condition("user", "like %aaa");
|
||||
Assert.assertEquals("user LIKE ?", conditionLike.toString());
|
||||
|
||||
Condition conditionIn = new Condition("user", "in 1,2,3");
|
||||
final Condition conditionIn = new Condition("user", "in 1,2,3");
|
||||
Assert.assertEquals("user IN (?,?,?)", conditionIn.toString());
|
||||
|
||||
Condition conditionBetween = new Condition("user", "between 12 and 13");
|
||||
final Condition conditionBetween = new Condition("user", "between 12 and 13");
|
||||
Assert.assertEquals("user BETWEEN ? AND ?", conditionBetween.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringNoPlaceHolderTest() {
|
||||
Condition conditionNull = new Condition("user", null);
|
||||
final Condition conditionNull = new Condition("user", null);
|
||||
conditionNull.setPlaceHolder(false);
|
||||
Assert.assertEquals("user IS NULL", conditionNull.toString());
|
||||
|
||||
Condition conditionNotNull = new Condition("user", "!= null");
|
||||
final Condition conditionNotNull = new Condition("user", "!= null");
|
||||
conditionNotNull.setPlaceHolder(false);
|
||||
Assert.assertEquals("user IS NOT NULL", conditionNotNull.toString());
|
||||
|
||||
Condition conditionEquals = new Condition("user", "= zhangsan");
|
||||
final Condition conditionEquals = new Condition("user", "= zhangsan");
|
||||
conditionEquals.setPlaceHolder(false);
|
||||
Assert.assertEquals("user = zhangsan", conditionEquals.toString());
|
||||
|
||||
Condition conditionLike = new Condition("user", "like %aaa");
|
||||
final Condition conditionLike = new Condition("user", "like %aaa");
|
||||
conditionLike.setPlaceHolder(false);
|
||||
Assert.assertEquals("user LIKE '%aaa'", conditionLike.toString());
|
||||
|
||||
Condition conditionIn = new Condition("user", "in 1,2,3");
|
||||
final Condition conditionIn = new Condition("user", "in 1,2,3");
|
||||
conditionIn.setPlaceHolder(false);
|
||||
Assert.assertEquals("user IN (1,2,3)", conditionIn.toString());
|
||||
|
||||
Condition conditionBetween = new Condition("user", "between 12 and 13");
|
||||
final Condition conditionBetween = new Condition("user", "between 12 and 13");
|
||||
conditionBetween.setPlaceHolder(false);
|
||||
Assert.assertEquals("user BETWEEN 12 AND 13", conditionBetween.toString());
|
||||
}
|
||||
|
@@ -7,22 +7,22 @@ public class SqlBuilderTest {
|
||||
|
||||
@Test
|
||||
public void queryNullTest() {
|
||||
SqlBuilder builder = SqlBuilder.create().select().from("user").where(new Condition("name", "= null"));
|
||||
final SqlBuilder builder = SqlBuilder.create().select().from("user").where(new Condition("name", "= null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NULL", builder.build());
|
||||
|
||||
SqlBuilder builder2 = SqlBuilder.create().select().from("user").where(new Condition("name", "is null"));
|
||||
final SqlBuilder builder2 = SqlBuilder.create().select().from("user").where(new Condition("name", "is null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NULL", builder2.build());
|
||||
|
||||
SqlBuilder builder3 = SqlBuilder.create().select().from("user").where(new Condition("name", "!= null"));
|
||||
final SqlBuilder builder3 = SqlBuilder.create().select().from("user").where(new Condition("name", "!= null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NOT NULL", builder3.build());
|
||||
|
||||
SqlBuilder builder4 = SqlBuilder.create().select().from("user").where(new Condition("name", "is not null"));
|
||||
final SqlBuilder builder4 = SqlBuilder.create().select().from("user").where(new Condition("name", "is not null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NOT NULL", builder4.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void orderByTest() {
|
||||
SqlBuilder builder = SqlBuilder.create().select("id", "username").from("user")
|
||||
final SqlBuilder builder = SqlBuilder.create().select("id", "username").from("user")
|
||||
.join("role", SqlBuilder.Join.INNER)
|
||||
.on("user.id = role.user_id")
|
||||
.where(new Condition("age", ">=", 18),
|
||||
@@ -34,14 +34,14 @@ public class SqlBuilderTest {
|
||||
|
||||
@Test
|
||||
public void likeTest() {
|
||||
Condition conditionEquals = new Condition("user", "123", Condition.LikeType.Contains);
|
||||
final Condition conditionEquals = new Condition("user", "123", Condition.LikeType.Contains);
|
||||
conditionEquals.setPlaceHolder(false);
|
||||
|
||||
SqlBuilder sqlBuilder = new SqlBuilder();
|
||||
final SqlBuilder sqlBuilder = new SqlBuilder();
|
||||
sqlBuilder.select("id");
|
||||
sqlBuilder.from("user");
|
||||
sqlBuilder.where(conditionEquals);
|
||||
String s1 = sqlBuilder.build();
|
||||
final String s1 = sqlBuilder.build();
|
||||
Assert.assertEquals("SELECT id FROM user WHERE user LIKE '%123%'", s1);
|
||||
}
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ public class SqlFormatterTest {
|
||||
public void formatTest(){
|
||||
// issue#I3XS44@Gitee
|
||||
// 测试是否空指针错误
|
||||
String sql = "(select 1 from dual) union all (select 1 from dual)";
|
||||
final String sql = "(select 1 from dual) union all (select 1 from dual)";
|
||||
SqlFormatter.format(sql);
|
||||
}
|
||||
}
|
||||
|
@@ -22,49 +22,49 @@ remarks = true
|
||||
|
||||
|
||||
# 测试数据源
|
||||
[test]
|
||||
[test]=
|
||||
url = jdbc:sqlite:test.db
|
||||
remarks = true
|
||||
|
||||
# 测试用HSQLDB数据库
|
||||
[hsqldb]
|
||||
[hsqldb]=
|
||||
url = jdbc:hsqldb:mem:mem_hutool
|
||||
user = SA
|
||||
pass =
|
||||
remarks = true
|
||||
|
||||
# 测试用HSQLDB数据库
|
||||
[h2]
|
||||
[h2]=
|
||||
url = jdbc:h2:mem:h2_hutool
|
||||
user = sa
|
||||
pass =
|
||||
remarks = true
|
||||
|
||||
# 测试用HSQLDB数据库
|
||||
[derby]
|
||||
[derby]=
|
||||
url = jdbc:derby:.derby/test_db;create=true
|
||||
remarks = true
|
||||
|
||||
# 测试用Oracle数据库
|
||||
[orcl]
|
||||
[orcl]=
|
||||
url = jdbc:oracle:thin:@//looly.centos:1521/XE
|
||||
user = looly
|
||||
pass = 123456
|
||||
remarks = true
|
||||
|
||||
[mysql]
|
||||
[mysql]=
|
||||
url = jdbc:mysql://looly.centos8:3306/hutool_test?useSSL=false
|
||||
user = root
|
||||
pass = 123456
|
||||
remarks = true
|
||||
|
||||
[postgre]
|
||||
[postgre]=
|
||||
url = jdbc:postgresql://looly.centos:5432/test_hutool
|
||||
user = postgres
|
||||
pass = 123456
|
||||
remarks = true
|
||||
|
||||
[sqlserver]
|
||||
[sqlserver]=
|
||||
url = jdbc:sqlserver://looly.database.chinacloudapi.cn:1433;database=test;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.chinacloudapi.cn;loginTimeout=30;
|
||||
user = looly@looly
|
||||
pass = 123
|
||||
|
@@ -21,9 +21,9 @@ pass = 123456
|
||||
database = test
|
||||
|
||||
#---------------------------------- MongoDB实例连接
|
||||
[master]
|
||||
[master]=
|
||||
host = 127.0.0.1:27017
|
||||
|
||||
[slave]
|
||||
[slave]=
|
||||
host = 127.0.0.1:27017
|
||||
#-----------------------------------------------------
|
||||
#-----------------------------------------------------
|
||||
|
@@ -12,9 +12,9 @@ socketTimeout=0
|
||||
socketKeepAlive=false
|
||||
|
||||
#---------------------------------- MongoDB实例连接
|
||||
[master]
|
||||
[master]=
|
||||
host = localhost:27017
|
||||
|
||||
[slave]
|
||||
[slave]=
|
||||
host = localhost:27018
|
||||
#-----------------------------------------------------
|
||||
|
@@ -27,7 +27,7 @@ clientName = Hutool
|
||||
ssl = false;
|
||||
|
||||
#----- 自定义分组的连接
|
||||
[custom]
|
||||
[custom]=
|
||||
# 地址,默认localhost
|
||||
host = localhost
|
||||
# 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
|
||||
|
Reference in New Issue
Block a user