1   /*
2    * Created on 2005/08/20
3    *
4    * TODO To change the template for this generated file go to
5    * Window - Preferences - Java - Code Style - Code Temgplates
6    */
7   package test.tuigwaa;
8   
9   import java.lang.reflect.InvocationTargetException;
10  import java.sql.SQLException;
11  import java.util.ArrayList;
12  import java.util.HashSet;
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.Set;
16  
17  import javax.sql.DataSource;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  import org.apache.commons.beanutils.PropertyUtils;
24  import org.seasar.framework.container.S2Container;
25  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
26  import org.seasar.tuigwaa.database.BasicDatabaseService;
27  import org.seasar.tuigwaa.database.CriteriaFunctionFactory;
28  import org.seasar.tuigwaa.database.DataTable;
29  import org.seasar.tuigwaa.database.function.CriteriaExeFunction;
30  import org.seasar.tuigwaa.database.function.DaoMethod;
31  import org.seasar.tuigwaa.database.function.aggregation.AggregationFunction;
32  import org.seasar.tuigwaa.database.function.aggregation.AvgProjectionFunction;
33  import org.seasar.tuigwaa.database.function.aggregation.GroupProjectionFunction;
34  import org.seasar.tuigwaa.database.function.aggregation.ProjectionFunction;
35  import org.seasar.tuigwaa.database.function.aggregation.RowcountProjectionFunction;
36  import org.seasar.tuigwaa.database.function.criteria.AbstractCriteriaFunction;
37  import org.seasar.tuigwaa.database.function.criteria.CriteriaFunction;
38  import org.seasar.tuigwaa.database.function.criteria.CriteriaListFunction;
39  import org.seasar.tuigwaa.database.function.criteria.EqCriteriaFunction;
40  import org.seasar.tuigwaa.database.function.criteria.FirstResultCriteriaFunction;
41  import org.seasar.tuigwaa.database.function.criteria.LeCriteriaFunction;
42  import org.seasar.tuigwaa.database.function.criteria.SubcriteriaFunction;
43  import org.seasar.tuigwaa.database.function.criteria.UnaryCriteriaFunction;
44  import org.seasar.tuigwaa.model.DAOService;
45  import org.seasar.tuigwaa.model.ModelService;
46  import org.seasar.tuigwaa.model.common.EntityDAO;
47  import org.seasar.tuigwaa.model.core.TgwAttribute;
48  import org.seasar.tuigwaa.model.core.TgwEntity;
49  import org.seasar.tuigwaa.model.core.impl.FileAttribute;
50  import org.seasar.tuigwaa.model.core.impl.FkAttribute;
51  import org.seasar.tuigwaa.model.core.impl.IntegerAttribute;
52  import org.seasar.tuigwaa.model.core.impl.SelfAttribute;
53  import org.seasar.tuigwaa.model.core.impl.SetAttribute;
54  import org.seasar.tuigwaa.model.core.impl.StringAttribute;
55  import org.seasar.tuigwaa.model.core.impl.TgwEntityImpl;
56  import org.seasar.tuigwaa.model.core.impl.TimestampAttribute;
57  import org.seasar.tuigwaa.util.TgwUtils;
58  
59  import com.isenshi.util.extlib.HSQLDBUtil;
60  
61  /***
62   * @author nishioka
63   */
64  public class EntityTest extends TestCase {
65  
66  	private static final String SCHEMA_NAME = "junit";
67  
68  	private static final String TABLE_NAME = "sample";
69  
70  	private static final String FKTABLE_NAME = "fktable";
71  
72  	private static final String INSERT_VALUE = "sample";
73  
74  	private static final String UPDATE_VALUE = "sample2";
75  
76  	private static final String INSERT_COLUMN = "name2";
77  
78  	private static final String NUMBER_COLUMN = "numcol";
79  
80  	private static final String ID_COLUMN = "id";
81  
82  	private static final String FK_FK_COLUMN = "fk";
83  
84  	private static final String FK_NAME_COLUMN = "name";
85  
86  	private static TgwEntity SAMPLE_ENTITY;
87  
88  	private static TgwEntity FILE_ENTITY;
89  
90  	private static S2Container container;
91  
92  	private static final TgwEntity FK_ENTITY;
93  
94  	private static final CriteriaListFunction SAMPLE_FILTER = new CriteriaListFunction();
95  
96  	private static final CriteriaListFunction NUM_LE_FILTER = new CriteriaListFunction();
97  
98  	private static final CriteriaListFunction FK_EQ_FILTER = new CriteriaListFunction();
99  
100 	
101 	private static BasicDatabaseService db;
102 
103 	private static DAOService dbLogic_;
104 
105 	private static ModelService model;
106 
107 	static {
108 		SingletonS2ContainerFactory.init();
109 		container = SingletonS2ContainerFactory.getContainer();
110 	
111 		dbLogic_ = (DAOService) container
112 				.getComponent(DAOService.class);
113 		db = (BasicDatabaseService) container
114 				.getComponent(BasicDatabaseService.class);
115 
116 		model = (ModelService) container
117 				.getComponent(ModelService.class);
118 
119 		model.createDomain("junit");
120 
121 		SAMPLE_ENTITY = new TgwEntityImpl(SCHEMA_NAME, TABLE_NAME);
122 		StringAttribute sattr = new StringAttribute();
123 		sattr.setName("fieldName");
124 		SAMPLE_ENTITY.addField(sattr);
125 		
126 		FK_ENTITY = new TgwEntityImpl(SCHEMA_NAME, FKTABLE_NAME);
127 		StringAttribute sattr2 = new StringAttribute();
128 		sattr2.setName(FK_NAME_COLUMN);
129 		FK_ENTITY.addField(sattr2);
130 		
131 		FkAttribute fk = new FkAttribute();
132 		fk.setRefEntity(SAMPLE_ENTITY);
133 		fk.setName(FK_FK_COLUMN);
134 		FK_ENTITY.addField(fk);
135 		
136 		CriteriaExeFunction sampleFilter = createCriteriaExe(SAMPLE_ENTITY,
137 				"sample", SAMPLE_FILTER);
138 		CriteriaFunction eqFunction = new EqCriteriaFunction(INSERT_COLUMN,
139 				UPDATE_VALUE);
140 		SAMPLE_FILTER.addFunction((AbstractCriteriaFunction) eqFunction);
141 		dbLogic_.registerDaoMethod(SAMPLE_ENTITY, sampleFilter);
142 
143 		CriteriaExeFunction numleFilter = createCriteriaExe(SAMPLE_ENTITY,
144 				"numle", NUM_LE_FILTER);
145 		UnaryCriteriaFunction leFunction = new LeCriteriaFunction(
146 				NUMBER_COLUMN, new Integer(2));
147 		NUM_LE_FILTER.addFunction((AbstractCriteriaFunction) leFunction);
148 		dbLogic_.registerDaoMethod(SAMPLE_ENTITY, numleFilter);
149 
150 		CriteriaExeFunction firstFilter = createCriteriaExe(SAMPLE_ENTITY,
151 				"firstResult", new FirstResultCriteriaFunction(new Integer(1)));
152 		dbLogic_.registerDaoMethod(SAMPLE_ENTITY, firstFilter);
153 
154 		CriteriaExeFunction fkFilter = createCriteriaExe(FK_ENTITY, "fk",
155 				FK_EQ_FILTER);
156 		SubcriteriaFunction subFunction = new SubcriteriaFunction(FK_FK_COLUMN);
157 		subFunction.setSequentialFunction(eqFunction);
158 		FK_EQ_FILTER.addFunction(subFunction);
159 		dbLogic_.registerDaoMethod(FK_ENTITY, fkFilter);
160 
161 		FILE_ENTITY = new TgwEntityImpl("junit", "fileclass");
162 		FileAttribute fattr = new FileAttribute();
163 		fattr.setName("file");
164 		FILE_ENTITY.addField(fattr);
165 	}
166 
167 	public EntityTest(String method) {
168 		super(method);
169 	}
170 
171 	public static Test suite() {
172 		TestSuite suite = new TestSuite();
173 
174 		// Basic Test Group
175 		suite.addTest(new EntityTest("testCreate"));
176 		suite.addTest(new EntityTest("testFileEntityCreate"));
177 		suite.addTest(new EntityTest("testTimestampEntityCreate"));
178 
179 		suite.addTest(new EntityTest("testAlter"));
180 		suite.addTest(new EntityTest("testInsert"));
181 		suite.addTest(new EntityTest("testUpdate"));
182 		suite.addTest(new EntityTest("testLoad"));
183 		suite.addTest(new EntityTest("testFindAll"));
184 
185 		// Foreign Key Test Group
186 		suite.addTest(new EntityTest("testFkCreate"));
187 		suite.addTest(new EntityTest("testFkInsert"));
188 
189 		// Data is below
190 		// --------------
191 		// 1| sample | 1 | -- FK (hoge)
192 		// 2| hoge | 2 |
193 		// 3| hoge | 3 | -- FK (hoge2)
194 		// --------------
195 
196 		// Data Filter Test Group
197 		suite.addTest(new EntityTest("testFilter"));
198 		suite.addTest(new EntityTest("testNumFilter"));
199 		suite.addTest(new EntityTest("testSavedFilter"));
200 		suite.addTest(new EntityTest("testFkFilter"));
201 		suite.addTest(new EntityTest("testFirstResult"));
202 
203 		// Aggregation Test
204 		suite.addTest(new EntityTest("testRowCountAggregation"));
205 		suite.addTest(new EntityTest("testSavedRowCountAggregation"));
206 		suite.addTest(new EntityTest("testGroupAggregation"));
207 		suite.addTest(new EntityTest("testGroupAggregationFilter"));
208 		suite.addTest(new EntityTest("testGroupRowCountAggregation"));
209 
210 		// One to many
211 		suite.addTest(new EntityTest("testOneToMany"));
212 
213 		// One to many
214 		suite.addTest(new EntityTest("testSelf"));
215 
216 		// Drop
217 		suite.addTest(new EntityTest("testDrop"));
218 
219 		suite.addTest(new EntityTest("testShutdown"));
220 		return suite;
221 	}
222 
223 	
224 
225 	public void testShutdown() {
226 		DataSource dataSource = (DataSource) SingletonS2ContainerFactory
227 				.getContainer().getComponent(DataSource.class);
228 		try {
229 			HSQLDBUtil.shutdown(dataSource);
230 		} catch (SQLException e) {
231 			// TODO Auto-generated catch block
232 			e.printStackTrace();
233 		}
234 	}
235 
236 	public void testFilter() {
237 		DaoMethod method = dbLogic_.getDAO(SAMPLE_ENTITY).getMethod("sample");
238 		DataTable table = (DataTable) method.evaluate();
239 		assertEquals(1, table.getRowSize());
240 	}
241 
242 	public void testFirstResult() {
243 		DaoMethod method = dbLogic_.getDAO(SAMPLE_ENTITY).getMethod(
244 				"firstResult");
245 		DataTable table = (DataTable) method.evaluate();
246 		assertEquals(2, table.getRowSize());
247 	}
248 
249 	public void testFkFilter() {
250 		DaoMethod method = dbLogic_.getDAO(FK_ENTITY).getMethod("fk");
251 		DataTable table = (DataTable) method.evaluate();
252 		assertEquals(2, table.getRowSize());
253 	}
254 
255 	public void testNumFilter() {
256 		DaoMethod method = dbLogic_.getDAO(SAMPLE_ENTITY).getMethod("numle");
257 		DataTable table = (DataTable) method.evaluate();
258 		assertEquals(2, table.getRowSize());
259 	}
260 
261 	public void testSavedFilter() {
262 		dbLogic_.saveAllFunctions();
263 		dbLogic_.resetAllFunctions();
264 		dbLogic_.loadFunctions(SAMPLE_ENTITY);
265 		dbLogic_.loadFunctions(FK_ENTITY);
266 		testFilter();
267 	}
268 
269 	public void testCreate() {
270 		try {
271 			// Create
272 			model.createEntity(SAMPLE_ENTITY);
273 		} catch (Exception e) {
274 			e.printStackTrace();
275 			fail();
276 		}
277 		assertEquals(true, db.existTable(SCHEMA_NAME, TgwUtils
278 				.toTableName(SAMPLE_ENTITY)));
279 	}
280 
281 	public void testFileEntityCreate() {
282 
283 		try {
284 			// Create
285 			model.createEntity(FILE_ENTITY);
286 			Object bean = FILE_ENTITY.getJavaClass().newInstance();
287 			PropertyUtils.setProperty(bean, "file", "hoge".getBytes());
288 			saveOrUpdate(FILE_ENTITY, bean);
289 			System.out.println(bean);
290 		} catch (Exception e) {
291 			e.printStackTrace();
292 			fail();
293 		}
294 		assertEquals(true, db.existTable(SCHEMA_NAME, TgwUtils
295 				.toTableName(FILE_ENTITY)));
296 	}
297 
298 	public void testTimestampEntityCreate() {
299 		String entityName = "timehoge";
300 
301 		TgwEntity entity = new TgwEntityImpl("junit", entityName);
302 		TimestampAttribute tattr = new TimestampAttribute();
303 		tattr.setName("field");
304 		entity.addField(tattr);
305 
306 				
307 		try {
308 			// Create
309 			model.createEntity(entity);			
310 			Object bean = entity.getJavaClass().newInstance();
311 
312 			assertEquals(true, PropertyUtils.isWriteable(bean, "field"));
313 
314 			PropertyUtils.setProperty(bean, "field", new java.util.Date());
315 			saveOrUpdate(entity, bean);
316 			System.out.println(bean);
317 		} catch (Exception e) {
318 			e.printStackTrace();
319 			fail();
320 		}
321 		assertEquals(true, db.existTable(SCHEMA_NAME, TgwUtils
322 				.toTableName(entity)));
323 	}
324 
325 	public void testFkCreate() {
326 		try {
327 			// Create
328 			model.createEntity(FK_ENTITY);
329 		} catch (Exception e) {
330 			e.printStackTrace();
331 			fail();
332 		}
333 		assertEquals(true, db.existTable(SCHEMA_NAME, TgwUtils
334 				.toTableName(FK_ENTITY)));
335 	}
336 
337 	public void testAlter() {
338 		alterField(SAMPLE_ENTITY);
339 
340 		try {
341 			// Alter
342 			model.alterEntity(SAMPLE_ENTITY, true);
343 		} catch (Exception e) {
344 			e.printStackTrace();
345 			fail();
346 		}
347 		assertEquals(true, db.existTable(SCHEMA_NAME, TgwUtils
348 				.toTableName(SAMPLE_ENTITY)));
349 	}
350 
351 	public void testOneToMany() throws Exception {
352 		int rowSize = getDataTable(FK_ENTITY).getRowSize();
353 
354 		String setFieldName = "setdayo";
355 
356 		SetAttribute setField = new SetAttribute();
357 		setField.setEntity(SAMPLE_ENTITY);
358 		setField.setRefEntity(FK_ENTITY);
359 		setField.setName(setFieldName);
360 		SAMPLE_ENTITY.addField(setField);
361 
362 		model.alterEntity(SAMPLE_ENTITY, true);
363 		
364 		Object sampleObj = SAMPLE_ENTITY.newInstance();
365 		Set set = new HashSet();
366 
367 		Object fk1 = FK_ENTITY.newInstance();
368 		// PropertyUtils.setProperty(fk1, "id", new Long(1));
369 		PropertyUtils.setProperty(fk1, FK_NAME_COLUMN, "hoge");
370 		PropertyUtils.setProperty(fk1, FK_FK_COLUMN, sampleObj);
371 
372 		Object fk2 = FK_ENTITY.newInstance();
373 		// PropertyUtils.setProperty(fk2, "id", new Long(2));
374 		PropertyUtils.setProperty(fk2, FK_NAME_COLUMN, "hoge2");
375 		PropertyUtils.setProperty(fk2, FK_FK_COLUMN, sampleObj);
376 
377 		set.add(fk1);
378 		set.add(fk2);
379 
380 		PropertyUtils.setProperty(sampleObj, setFieldName, set);
381 
382 		dbLogic_.getDAO(SAMPLE_ENTITY).saveOrUpdate(sampleObj);
383 
384 		Object sampleObj2 = SAMPLE_ENTITY.newInstance();
385 		dbLogic_.getDAO(SAMPLE_ENTITY).saveOrUpdate(sampleObj2);
386 
387 		Object fk3 = FK_ENTITY.newInstance();
388 		dbLogic_.getDAO(FK_ENTITY).saveOrUpdate(fk3);
389 
390 		assertEquals(rowSize + 3, getDataTable(FK_ENTITY).getRowSize());
391 	}
392 
393 	public void testInsert() {
394 		try {
395 			// Insert
396 			Object sample1 = getSampleInstance(INSERT_VALUE, 1);
397 			Object sample2 = getSampleInstance("hoge", 2);
398 			Object sample3 = getSampleInstance("hoge", 3);
399 
400 			saveOrUpdate(SAMPLE_ENTITY, sample1);
401 			saveOrUpdate(SAMPLE_ENTITY, sample2);
402 			saveOrUpdate(SAMPLE_ENTITY, sample3);
403 
404 			DataTable table = getDataTable(SAMPLE_ENTITY);
405 			assertEquals(3, table.getRowSize());
406 		} catch (Exception e) {
407 			e.printStackTrace();
408 			fail();
409 		}
410 	}
411 
412 	private void saveOrUpdate(TgwEntity entity, Object obj) {
413 		DaoMethod method = dbLogic_.getDAO(entity).getMethod(
414 				EntityDAO.SAVEUPDATE);
415 		method.evaluate(obj);
416 	}
417 
418 	public void testFkInsert() {
419 		try {
420 
421 			// Insert
422 			Object sample = SAMPLE_ENTITY.newInstance();
423 			PropertyUtils.setProperty(sample, ID_COLUMN, new Long(1));
424 
425 			Object fkObj = FK_ENTITY.newInstance();
426 			PropertyUtils.setProperty(fkObj, FK_FK_COLUMN, sample);
427 			PropertyUtils.setProperty(fkObj, FK_NAME_COLUMN, "hoge");
428 
429 			Object fkObj2 = FK_ENTITY.newInstance();
430 			PropertyUtils.setProperty(fkObj2, FK_FK_COLUMN, sample);
431 			PropertyUtils.setProperty(fkObj2, FK_NAME_COLUMN, "hoge2");
432 
433 			saveOrUpdate(SAMPLE_ENTITY, fkObj);
434 			saveOrUpdate(SAMPLE_ENTITY, fkObj2);
435 
436 			assertEquals(2, getDataTable(FK_ENTITY).getRowSize());
437 		} catch (Exception e) {
438 			e.printStackTrace();
439 			fail();
440 		}
441 	}
442 
443 	private DataTable getDataTable(TgwEntity entity) {
444 		DataTable table = (DataTable) dbLogic_.getDAO(entity).getMethod(
445 				EntityDAO.LIST).evaluate();
446 		return table;
447 	}
448 
449 	public void testUpdate() {
450 		try {
451 
452 			// Update
453 			Object obj = getSampleInstance(UPDATE_VALUE, 1);
454 			PropertyUtils.setProperty(obj, ID_COLUMN, new Long(1));
455 
456 			EntityDAO dao = dbLogic_.getDAO(SAMPLE_ENTITY);
457 			DaoMethod method = dao.getMethod(EntityDAO.SAVEUPDATE);
458 			method.evaluate(obj);
459 
460 			assertEquals(3, getDataTable(SAMPLE_ENTITY).getRowSize());
461 		} catch (Exception e) {
462 			e.printStackTrace();
463 			fail();
464 		}
465 	}
466 
467 	public void testLoad() {
468 		String value = null;
469 		try {
470 			// Load
471 			Object obj = dbLogic_.getDAO(SAMPLE_ENTITY).load(new Long(1));
472 			// Object obj = method.evaluate(new Long(1));
473 			value = (String) PropertyUtils.getProperty(obj, INSERT_COLUMN);
474 		} catch (Exception e) {
475 			e.printStackTrace();
476 			fail();
477 		}
478 		assertEquals(UPDATE_VALUE, value);
479 	}
480 
481 	public void testFindAll() {
482 		DataTable table = getDataTable(SAMPLE_ENTITY);
483 		int rowSize = table.getRowSize();
484 		int columnSize = table.getColumnSize();
485 		int count = 0;
486 		while (table.hasNext()) {
487 			count++;
488 			table.next();
489 			int columnCount = 0;
490 			while (table.hasNextColumn()) {
491 				columnCount++;
492 				table.nextColumn();
493 				assertNotNull(table.getData());
494 			}
495 			assertEquals(columnSize, columnCount);
496 		}
497 		assertEquals(rowSize, count);
498 
499 		Iterator itr = (Iterator) table;
500 		while (itr.hasNext()) {
501 			Iterator itr2 = (Iterator) itr.next();
502 			while (itr2.hasNext()) {
503 				// Object data =
504 				itr2.next();
505 			}
506 		}
507 	}
508 
509 	public void testSavedRowCountAggregation() {
510 		CriteriaExeFunction exe = getRowCountAggExe();
511 		dbLogic_.registerDaoMethod(SAMPLE_ENTITY, exe);
512 		dbLogic_.saveFunction(SAMPLE_ENTITY);
513 		dbLogic_.resetAllFunctions();
514 		dbLogic_.loadFunctions(SAMPLE_ENTITY);
515 		dbLogic_.loadFunctions(FK_ENTITY);
516 
517 		DaoMethod method = dbLogic_.getDAO(SAMPLE_ENTITY).getMethod(
518 				exe.getName());
519 
520 		DataTable table = (DataTable) method.evaluate();
521 		assertEquals(3, ((Integer) table.getData(0, "rowcount")).intValue());
522 		assertEquals(1, table.getRowSize());
523 		Class clazz = table.getData(0, 1).getClass();
524 		System.out.println(clazz);
525 
526 	}
527 
528 	private CriteriaExeFunction getGroupAggExe() {
529 		return getGroupAggExe(false);
530 	}
531 
532 	private CriteriaExeFunction getGroupAggExe(boolean isFilter) {
533 		CriteriaListFunction listFunction = new CriteriaListFunction();
534 
535 		CriteriaExeFunction exefunction = createCriteriaExe(SAMPLE_ENTITY,
536 				"froup", listFunction);
537 		exefunction.addProjection("group");
538 
539 		GroupProjectionFunction groupFunction = new GroupProjectionFunction();
540 		groupFunction.setField(INSERT_COLUMN);
541 		groupFunction.setAlias(INSERT_COLUMN);
542 
543 		AggregationFunction function = new AggregationFunction();
544 		function.addFunction(groupFunction);
545 
546 		// OrderCriteriaFunction order = new OrderCriteriaFunction("group");
547 
548 		listFunction.addFunction(function);
549 		// listFunction.addFunction(order);
550 
551 		if (isFilter) {
552 			EqCriteriaFunction eq = new EqCriteriaFunction();
553 			eq.setField(INSERT_COLUMN);
554 			eq.setValue("hoge");
555 			exefunction.setDataTableFilter(eq);
556 		}
557 
558 		dbLogic_.registerDaoMethod(SAMPLE_ENTITY, exefunction);
559 		return exefunction;
560 	}
561 
562 	private static CriteriaExeFunction createCriteriaExe(TgwEntity entity,
563 			String name, UnaryCriteriaFunction function) {
564 		return CriteriaFunctionFactory.createCriteriaExeFunction(entity, name,
565 				function);
566 	}
567 
568 	private CriteriaExeFunction getGroupRowCountAggExe() {
569 		AggregationFunction function = new AggregationFunction();
570 
571 		CriteriaExeFunction aggfunction = createCriteriaExe(SAMPLE_ENTITY,
572 				"hogehogea", function);
573 		aggfunction.addProjection("group");
574 		aggfunction.addProjection("rowcount");
575 
576 		ProjectionFunction groupFunction = new GroupProjectionFunction();
577 		groupFunction.setField(INSERT_COLUMN);
578 		groupFunction.setAlias(INSERT_COLUMN);
579 
580 		ProjectionFunction rowcount = new RowcountProjectionFunction();
581 		rowcount.setAlias("rowcount");
582 
583 		function.addFunction(groupFunction);
584 		function.addFunction(rowcount);
585 
586 		dbLogic_.registerDaoMethod(SAMPLE_ENTITY, aggfunction);
587 
588 		return aggfunction;
589 	}
590 
591 	private CriteriaExeFunction getRowCountAggExe() {
592 		AggregationFunction function = new AggregationFunction();
593 
594 		CriteriaExeFunction aggFunction = createCriteriaExe(SAMPLE_ENTITY,
595 				"simplerowcon", function);
596 		aggFunction.addProjection("rowcount");
597 		aggFunction.addProjection("heikin");
598 
599 		ProjectionFunction rowcount = new RowcountProjectionFunction();
600 		rowcount.setAlias("rowcount");
601 
602 		AvgProjectionFunction heikin = new AvgProjectionFunction();
603 		heikin.setField(NUMBER_COLUMN);
604 		heikin.setAlias("hoge");
605 
606 		function.addFunction(rowcount);
607 		function.addFunction(heikin);
608 
609 		dbLogic_.registerDaoMethod(SAMPLE_ENTITY, aggFunction);
610 
611 		return aggFunction;
612 	}
613 
614 	public void testRowCountAggregation() {
615 
616 		DataTable table = (DataTable) getRowCountAggExe().evaluate();
617 		assertEquals(3, ((Integer) table.getData(0, "rowcount")).intValue());
618 		while (table.hasNext()) {
619 			table.next();
620 			assertNotNull(table.getData("rowcount"));
621 		}
622 	}
623 
624 	public void testGroupAggregation() {
625 		DataTable table = (DataTable) getGroupAggExe().evaluate();
626 		assertEquals(2, table.getRowSize());
627 		assertNotNull(table.getData(1, 0));
628 		while (table.hasNext()) {
629 			table.next();
630 			// assertNotNull(table.getData("group"));
631 		}
632 	}
633 
634 	public void testGroupAggregationFilter() {
635 		DataTable table = (DataTable) getGroupAggExe(true).evaluate();
636 		assertEquals(1, table.getRowSize());
637 	}
638 
639 	public void testGroupRowCountAggregation() {
640 		DataTable table = (DataTable) getGroupRowCountAggExe().evaluate();
641 		assertEquals(2, table.getRowSize());
642 		assertNotNull(table.getData(1, 1));
643 		while (table.hasNext()) {
644 			table.next();
645 			// assertNotNull(table.getData("group"));
646 			// assertNotNull(table.getData("rowcount"));
647 		}
648 	}
649 
650 	public void testSelf() throws Exception {
651 		TgwEntity entity = new TgwEntityImpl("junit", "selftable");
652 		String name = "name";
653 		String self = "selffield";
654 		
655 		StringAttribute sattr = new StringAttribute();
656 		sattr.setName(name);
657 		entity.addField(sattr);
658 
659 		SelfAttribute selfAttr = new SelfAttribute();
660 		selfAttr.setName(self);
661 		entity.addField(selfAttr);
662 
663 		model.createEntity(entity);
664 						
665 		Object obj1 = entity.newInstance();
666 		Object obj2 = entity.newInstance();
667 
668 		PropertyUtils.setProperty(obj1, name, "hoge");
669 
670 		PropertyUtils.setProperty(obj2, name, "hage");
671 		PropertyUtils.setProperty(obj2, self, obj1);
672 
673 		saveOrUpdate(entity, obj1);
674 		saveOrUpdate(entity, obj2);
675 
676 	}
677 
678 	public void testDrop() {
679 		try {
680 			model.dropEntity(SAMPLE_ENTITY.getDomainName(), SAMPLE_ENTITY.getName());
681 		} catch (Exception e) {
682 			e.printStackTrace();
683 			fail();
684 		}
685 		assertEquals(false, db.existTable(SCHEMA_NAME, TABLE_NAME));
686 	}
687 
688 	private Object getSampleInstance(String strValue, int num)
689 			throws InstantiationException, ClassNotFoundException,
690 			IllegalAccessException, InvocationTargetException,
691 			NoSuchMethodException {
692 		Object obj = SAMPLE_ENTITY.newInstance();
693 		PropertyUtils.setProperty(obj, INSERT_COLUMN, strValue);
694 		PropertyUtils.setProperty(obj, NUMBER_COLUMN, new Integer(num));
695 		return obj;
696 	}
697 
698 	private void alterField(TgwEntity entity) {
699 		List list = new ArrayList();
700 		StringAttribute field = new StringAttribute();
701 		field.setName(INSERT_COLUMN);
702 		list.add(field);
703 
704 		IntegerAttribute integerField = new IntegerAttribute();
705 		integerField.setName(NUMBER_COLUMN);
706 		list.add(integerField);
707 
708 		entity.resetAttributes();
709 		for (Iterator i = list.iterator(); i.hasNext();) {
710 			entity.addField((TgwAttribute) i.next());
711 		}
712 	}
713 }