Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Suite.SuiteClasses( { ItemDaoTest.class, MySqlDaoFactoryTest.class} )
- @RunWith(Suite.class)
- public class AllTests {
- }
- @RunWith(Parameterized.class)
- public abstract class ItemDaoTest{
- protected abstract ItemDao<Entity> dao();
- protected Class<? extends Entity> daoClass;
- protected Entity dto;
- public ItemDaoTest(Class<? extends Entity> clazz, Entity dto) {
- this.daoClass = clazz;
- this.dto = dto;
- }
- @Test
- public void testGetById() throws Exception {
- dto = dao().getById(1);
- Assert.assertNotNull(dto);
- }
- @Test
- public void testGetAll() throws Exception{
- List<? extends Entity> list = dao().getAll();
- Assert.assertNotNull(list);
- Assert.assertTrue(list.size()>0);
- }
- @Test
- public void testDelete() throws Exception{
- List<? extends Entity> list = dao().getAll();
- Assert.assertNotNull(list);
- int oldSize = list.size();
- Assert.assertTrue(oldSize>0);
- dao().delete(list.get(0));
- list = dao().getAll();
- Assert.assertNotNull(list);
- int newSize = list.size();
- Assert.assertEquals(1, oldSize-newSize);
- }
- }
- public class MySqlDaoFactoryTest extends ItemDaoTest{
- private static final Logger LOG = Logger.getLogger(MySqlDaoFactoryTest.class);
- public MySqlDaoFactoryTest(Class<? extends Entity> clazz, Entity dto) {
- super(clazz, dto);
- }
- private Connection connection;
- private ItemDao<? extends Entity> dao;
- @BeforeClass
- public static void init() throws DBException{
- try {
- // Create initial context
- System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
- "org.apache.naming.java.javaURLContextFactory");
- System.setProperty(Context.URL_PKG_PREFIXES,
- "org.apache.naming");
- InitialContext ic = new InitialContext();
- ic.createSubcontext("java:");
- ic.createSubcontext("java:/comp");
- ic.createSubcontext("java:/comp/env");
- ic.createSubcontext("java:/comp/env/jdbc");
- // Construct DataSource
- MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
- ds.setURL("jdbc:mysql://localhost:3306/hospital");
- ds.setUser("root");
- ds.setPassword(" ");
- ic.bind("java:/comp/env/jdbc/hospital", ds);
- } catch (NamingException ex) {
- LOG.trace(ex);
- }
- }
- @Parameterized.Parameters
- public static Collection<Object[]> getParameters() {
- return Arrays.asList(new Object[][]{
- {Patient.class, new Patient()}
- {User.class, new User()},
- {Reception.class, new Reception()},
- {Task.class, new Task()},
- {Procedure.class, new Procedure()}
- });
- }
- @Before
- public void setUp() throws DBException, SQLException {
- DaoFactory factory = new MySqlDaoFactory();
- connection = factory.getConnection();
- LOG.trace("Obtain connection: "+connection);
- connection.setAutoCommit(false);
- dao = factory.getDao(connection, daoClass);
- LOG.trace("Obtain dao: "+dao);
- }
- @After
- public void tearDown() throws SQLException {
- connection.rollback();
- connection.close();
- }
- @Override
- public ItemDao<Entity> dao() {
- return (ItemDao<Entity>) dao;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement