001 package com.khubla.pragmatach.dbtestsuite; 002 003 import java.util.List; 004 005 import org.testng.Assert; 006 007 import com.khubla.pragmatach.dbtestsuite.pojo.ExamplePOJO; 008 import com.khubla.pragmatach.framework.dao.DAO; 009 010 /** 011 * @author tome 012 */ 013 public class BasicTests { 014 public static void testBasicFunctionality(DAO<ExamplePOJO, Long> dao) { 015 try { 016 /* 017 * empty table 018 */ 019 Assert.assertTrue(dao.count() == 0); 020 /* 021 * create and save an object 022 */ 023 final ExamplePOJO examplePOJO1 = new ExamplePOJO(); 024 examplePOJO1.setName("abc123"); 025 examplePOJO1.setDoubleNumber(12.2); 026 examplePOJO1.setIntNumber(34); 027 dao.save(examplePOJO1); 028 /* 029 * 1 row 030 */ 031 Assert.assertTrue(dao.count() == 1); 032 /* 033 * check that the id was generated 034 */ 035 Assert.assertNotNull(examplePOJO1.getId()); 036 /* 037 * get the created object 038 */ 039 ExamplePOJO retrievedPojo = dao.findById(examplePOJO1.getId()); 040 Assert.assertNotNull(retrievedPojo); 041 Assert.assertNotNull(retrievedPojo.getId()); 042 Assert.assertTrue(retrievedPojo.getId().longValue() == examplePOJO1.getId().longValue()); 043 Assert.assertTrue(retrievedPojo.getName().compareTo(examplePOJO1.getName()) == 0); 044 Assert.assertTrue(retrievedPojo.getDoubleNumber() == examplePOJO1.getDoubleNumber()); 045 Assert.assertTrue(retrievedPojo.getIntNumber() == examplePOJO1.getIntNumber()); 046 /* 047 * there is 1 object in the DB table 048 */ 049 List<ExamplePOJO> allObjects = dao.getAll(); 050 Assert.assertNotNull(allObjects); 051 Assert.assertTrue(allObjects.size() == 1, "found '" + allObjects.size() + "' objects."); 052 /* 053 * cant fetch object with wrong id 054 */ 055 final ExamplePOJO noPojo = dao.findById(new Long(909090)); 056 Assert.assertNull(noPojo); 057 /* 058 * delete it 059 */ 060 dao.delete(examplePOJO1); 061 /* 062 * make sure it's gone 063 */ 064 retrievedPojo = dao.findById(examplePOJO1.getId()); 065 Assert.assertNull(retrievedPojo); 066 /* 067 * count is zero 068 */ 069 Assert.assertTrue(dao.count() == 0); 070 /* 071 * no objects 072 */ 073 allObjects = dao.getAll(); 074 Assert.assertNotNull(allObjects); 075 Assert.assertTrue(allObjects.size() == 0); 076 } catch (final Exception e) { 077 e.printStackTrace(); 078 Assert.fail(); 079 } 080 } 081 }