由于好久没来这里了,发现有几个朋友想要代码参考一下,所以上传到这里,感兴趣的朋友可以下来参考
https://files.cnblogs.com/hjzhang/data.rar
在实现DataTable的基本功能后,我们对我们实现的对象进行一下测试,测试代码如下:
1: import java.util.List;
2: import org.junit.After;
3: import org.junit.Assert;
4: import org.junit.Before;
5: import org.junit.Test;
6:
7:
8: /**
9: * 此类描述的是: 测试数据表对象DataTable对象相关功能是否正常
10: * @author: James Cheung
11: * @version: 2.0
12: */
13: public class DataTableTest {
14:
15: @Before
16: public void init() {
17:
18: }
19:
20: @After
21: public void dispose() {
22:
23: }
24:
25:
26: @Test
27: public void tableInitTest() {
28: DataTable table = new DataTable(); //新建一个数据表对象,不带构造函数参数
29: fillTable(table);//填充数据
30: Assert.assertNotNull(table);
31: Assert.assertEquals(20, table.getColumns().size());
32: Assert.assertEquals(100, table.getRows().size());
33: //printTable(table);
34: System.out.print("------------------------------------------------------------------------------------------------------------------------\n");
35: table = new DataTable("TestTable");//新建一个数据表对象,带表名为参数
36: //测试添加数据列
37: fillTable(table);//填充数据
38: Assert.assertNotNull(table);
39: //printTable(table);
40: }
41:
42: @Test
43: public void tableSelectTest() throws Exception {
44: //测试数据表的选择过滤方法
45: DataTable table = new DataTable(); //新建一个数据表对象,不带构造函数参数
46: fillTable(table);//填充数据
47: List<DataRow> rows = table.select("col_1> 2 && col_2<10");
48: Assert.assertNotNull(rows);
49: Assert.assertEquals(2, rows.size());
50: //测试查询指定列
51: DataTable filterResult = table.select("col_1>2 && col_2<10",
52: "col_1,col_2,col_3,col_4".split(","),
53: true);
54: printTable(filterResult);
55:
56: }
57:
58: private void printTable(DataTable table) {
59:
60: for (int i = 0; i < table.getTotalCount(); i++) {
61: for (int j = 0; j < table.getColumns().size(); j++) {
62: System.out.print(table.getValue(i, j));
63: if (j < 19)
64: System.out.print(" , ");
65: }
66: System.out.print("\n");
67: }
68: }
69:
70: private void fillTable(DataTable table) {
71: try {
72: //填充20列数据
73: for (int i = 0; i < 20; i++) {
74: table.addColumn("Col_" + i, i % 13);
75: }
76: //填充100行数据
77: for (int i = 0; i < 100; i++) {
78: DataRow row = table.newRow();
79: for (int j = 0; j < 20; j++) {
80: //循环每列加入数据
81: row.setValue(j, i * j);
82: }
83: table.addRow(row);
84: }
85: } catch (Exception e) {
86: e.printStackTrace();
87: }
88: }
89: }
后记:仔细看的朋友可能发现了,DataTable里还有几个函数没有实现,那几个函数是我们后面要实现的基于DataTable进行分组汇总计算所需的功能,有兴趣的朋友也可以自己来实现一套试试,我这里可以提个醒给大家,下面是类似功能的测试代码:
newTable = dataTable.Select("tableName", "count(Name),sum(11) aaa,sum(222) name,avg(aa),last,first,max,min aa,Name", "", "Name");