1   package test.tuigwaa;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.ArrayList;
6   import java.util.HashMap;
7   import java.util.List;
8   
9   import junit.framework.TestCase;
10  
11  import org.jfree.chart.ChartUtilities;
12  import org.jfree.chart.JFreeChart;
13  import org.seasar.tuigwaa.database.DataRow;
14  import org.seasar.tuigwaa.database.DataTable;
15  import org.seasar.tuigwaa.database.DataTableImpl;
16  import org.seasar.tuigwaa.database.ExtraRowDataTable;
17  import org.seasar.tuigwaa.database.SelectedColumnDataTableImpl;
18  import org.seasar.tuigwaa.database.SpreadSheet;
19  import org.seasar.tuigwaa.database.chart.TCharFactory;
20  import org.seasar.tuigwaa.database.function.criteria.DataTableCriteria;
21  import org.seasar.tuigwaa.plugin.database.component.EditTableDto;
22  import org.seasar.tuigwaa.util.functor.Op;
23  
24  public class DataTableTest extends TestCase {
25  
26  	private DataTable SIMPLE_DATA_TABLE;
27  
28  	private DataTable SELECTED_DATA_TABLE;
29  
30  	protected void setUp() throws Exception {
31  		// simple data table
32  		List data = new ArrayList();
33  		data.add(new Bean("hoge", "hoge2", new Integer(1)));
34  		data.add(new Bean("hoge3", "hoge4", new Integer(2)));
35  		SIMPLE_DATA_TABLE = new DataTableImpl("a", Bean.headers(), Bean
36  				.headers(), data, DataTable.BEAN_DATA, new HashMap());
37  
38  		// selected data table
39  		List header = new ArrayList();
40  		header.add("prop1");
41  		header.add("prop3");
42  		SELECTED_DATA_TABLE = new SelectedColumnDataTableImpl(
43  				SIMPLE_DATA_TABLE, header);
44  	}
45  
46  	public void testSimple() {
47  		DataTable table = SIMPLE_DATA_TABLE;
48  		assertEquals(2, table.getRowSize());
49  		assertEquals(3, table.getColumnSize());
50  	}
51  
52  	public void testSelectedColumn() {
53  		DataTable sTable = SELECTED_DATA_TABLE;
54  
55  		DataRow row0 = (DataRow) sTable.next();
56  		assertEquals("hoge", row0.next());
57  		assertTrue(row0.hasNext());
58  		assertEquals(new Integer(1), row0.next());
59  
60  		assertFalse(row0.hasNext());
61  		assertTrue(sTable.hasNext());
62  
63  		DataRow row1 = (DataRow) sTable.next();
64  
65  		assertEquals("hoge3", row1.next());
66  		assertEquals(new Integer(2), row1.next());
67  
68  		assertFalse(sTable.hasNext());
69  	}
70  
71  	public void testExtraRow(){
72  		DataTable dataTable = new ExtraRowDataTable(SIMPLE_DATA_TABLE, Op.SUM);
73  		System.out.println("---------------------------------------");
74  		while(dataTable.hasNext()){
75  			DataRow dataRow = dataTable.nextRow();
76  			
77  			while(dataRow.hasNext()){
78  				System.out.print(dataRow.next());
79  				System.out.print("\t");
80  			}
81  			System.out.println("");
82  		}
83  		System.out.println("---------------------------------------");
84  	}
85  	
86  	
87  	public void testNotSelectedColumn() {
88  		DataRow row = (DataRow)SIMPLE_DATA_TABLE.next();
89  		assertEquals("hoge2", row.getCell("prop2"));
90  	}
91  
92  	public void testFilter(){
93  		DataTableCriteria criteria = new DataTableCriteria(SIMPLE_DATA_TABLE);
94  		criteria.addEq(Bean.HEADER0, "hoge");
95  		assertEquals(1,criteria.list().getRowSize());
96  	}
97  	
98  	
99  	
100 	public void test1DCreator() {
101 
102 		List list = new ArrayList();
103 		list.add(new Object[] { "aaa1", new Integer(2) });
104 		list.add(new Object[] { "aaa2", new Integer(2) });
105 		list.add(new Object[] { "bbb3", new Integer(3) });
106 		list.add(new Object[] { "bbb4", new Integer(4) });
107 
108 		List headers = new ArrayList();
109 		headers.add("row");
110 		headers.add("num");
111 		DataTable dataTable = new DataTableImpl("ق", headers, list,
112 				DataTable.PRIMITIVE_DATA);
113 
114 		JFreeChart pieChart = TCharFactory.createPieChart(dataTable);
115 		// JFreeChart pieChart = TCharFactory.createBarChart(dataTable);
116 		// JFreeChart pieChart = TCharFactory.createLineChart(dataTable);
117 		File outFile = new File("./piechart.png");
118 		try {
119 			ChartUtilities.saveChartAsPNG(outFile, pieChart, 500, 500);
120 		} catch (IOException e) {
121 			e.printStackTrace();
122 		}
123 	}
124 
125 	public void test2DCreator() {
126 		Object[] row1 = new Object[] { "aaa", "hoge", new Integer(1) };
127 		Object[] row2 = new Object[] { "aaa", "hage", new Integer(2) };
128 		Object[] row3 = new Object[] { "bbb", "hoge", new Integer(3) };
129 		Object[] row4 = new Object[] { "bbb", "hage", new Integer(4) };
130 		List list = new ArrayList();
131 		list.add(row1);
132 		list.add(row2);
133 		list.add(row3);
134 		list.add(row4);
135 
136 		List headers = new ArrayList();
137 		headers.add("row");
138 		headers.add("column");
139 		headers.add("num");
140 
141 		DataTable dataTable = new DataTableImpl("b", headers, list,
142 				DataTable.PRIMITIVE_DATA);
143 		SpreadSheet sheet = ((DataTableImpl) dataTable).createSpreadSheet();
144 
145 		// while(sheet.hasNext()){
146 		// DataRow dataRow = sheet.nextRow();
147 		// while(dataRow.hasNext()){
148 		// System.out.print(dataRow.next());
149 		// System.out.print("\t");
150 		// }
151 		// System.out.println("");
152 		// }
153 
154 		//DataTableComponent comp = new DataTableComponent(sheet);
155 		//System.out.println(comp.getHtml());
156 
157 	}
158 
159 	public boolean isSpreadSheet() {
160 		return this instanceof SpreadSheet;
161 	}
162 	
163 	public void test2DChart() {
164 		Object[] row1 = new Object[] { "aaa", "hoge", new Integer(1) };
165 		Object[] row2 = new Object[] { "aaa", "hage", new Integer(2) };
166 		Object[] row3 = new Object[] { "bbb", "hoge", new Integer(3) };
167 		Object[] row4 = new Object[] { "bbb", "hage", new Integer(4) };
168 		List list = new ArrayList();
169 		list.add(row1);
170 		list.add(row2);
171 		list.add(row3);
172 		list.add(row4);
173 
174 		List headers = new ArrayList();
175 		headers.add("row");
176 		headers.add("column");
177 		headers.add("num");
178 
179 		DataTable dataTable = new DataTableImpl("b", headers, list,
180 				DataTable.PRIMITIVE_DATA);
181 		SpreadSheet sheet = ((DataTableImpl) dataTable).createSpreadSheet();
182 
183 		// JFreeChart pieChart = TCharFactory.createPieChart(dataTable);
184 		// JFreeChart pieChart = TCharFactory.createBarChart(sheet);
185 		JFreeChart pieChart = TCharFactory.createLineChart(sheet);
186 		File outFile = new File("./piechart.png");
187 		try {
188 			ChartUtilities.saveChartAsPNG(outFile, pieChart, 500, 500);
189 		} catch (IOException e) {
190 			e.printStackTrace();
191 		}
192 
193 	}
194 
195 	public static class Bean {
196 
197 		private String prop1;
198 
199 		private String prop2;
200 
201 		private Integer prop3;
202 
203 		private static final String HEADER0 = "prop1";
204 
205 		private static final String HEADER1 = "prop2";
206 		
207 		private static final String HEADER2 = "prop3";
208 		
209 		
210 		
211 		public Bean(String prop1, String prop2, Integer prop3) {
212 			this.prop1 = prop1;
213 			this.prop2 = prop2;
214 			this.prop3 = prop3;
215 		}
216 
217 		public static List headers() {
218 			List headers = new ArrayList();
219 			headers.add(HEADER0);
220 			headers.add(HEADER1);
221 			headers.add(HEADER2);
222 			return headers;
223 		}
224 
225 		public String getProp1() {
226 			return prop1;
227 		}
228 
229 		public void setProp1(String prop1) {
230 			this.prop1 = prop1;
231 		}
232 
233 		public String getProp2() {
234 			return prop2;
235 		}
236 
237 		public void setProp2(String prop2) {
238 			this.prop2 = prop2;
239 		}
240 
241 		public void setProp3(Integer prop3) {
242 			this.prop3 = prop3;
243 		}
244 
245 		public Integer getProp3() {
246 			return prop3;
247 		}
248 	}
249 }