Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. @Suite.SuiteClasses( { ItemDaoTest.class, MySqlDaoFactoryTest.class} )
  2. @RunWith(Suite.class)
  3. public class AllTests {
  4.  
  5. }
  6.  
  7. @RunWith(Parameterized.class)
  8. public abstract class ItemDaoTest{
  9.  
  10. protected abstract ItemDao<Entity> dao();
  11.  
  12. protected Class<? extends Entity> daoClass;
  13.  
  14. protected Entity dto;
  15.  
  16. public ItemDaoTest(Class<? extends Entity> clazz, Entity dto) {
  17. this.daoClass = clazz;
  18. this.dto = dto;
  19. }
  20.  
  21. @Test
  22. public void testGetById() throws Exception {
  23. dto = dao().getById(1);
  24. Assert.assertNotNull(dto);
  25. }
  26.  
  27.  
  28. @Test
  29. public void testGetAll() throws Exception{
  30. List<? extends Entity> list = dao().getAll();
  31. Assert.assertNotNull(list);
  32. Assert.assertTrue(list.size()>0);
  33. }
  34.  
  35. @Test
  36. public void testDelete() throws Exception{
  37. List<? extends Entity> list = dao().getAll();
  38. Assert.assertNotNull(list);
  39.  
  40. int oldSize = list.size();
  41. Assert.assertTrue(oldSize>0);
  42.  
  43. dao().delete(list.get(0));
  44.  
  45. list = dao().getAll();
  46. Assert.assertNotNull(list);
  47.  
  48. int newSize = list.size();
  49. Assert.assertEquals(1, oldSize-newSize);
  50. }
  51.  
  52. }
  53.  
  54. public class MySqlDaoFactoryTest extends ItemDaoTest{
  55.  
  56. private static final Logger LOG = Logger.getLogger(MySqlDaoFactoryTest.class);
  57.  
  58. public MySqlDaoFactoryTest(Class<? extends Entity> clazz, Entity dto) {
  59. super(clazz, dto);
  60. }
  61.  
  62. private Connection connection;
  63.  
  64. private ItemDao<? extends Entity> dao;
  65.  
  66.  
  67. @BeforeClass
  68. public static void init() throws DBException{
  69.  
  70. try {
  71. // Create initial context
  72. System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  73. "org.apache.naming.java.javaURLContextFactory");
  74. System.setProperty(Context.URL_PKG_PREFIXES,
  75. "org.apache.naming");
  76. InitialContext ic = new InitialContext();
  77.  
  78. ic.createSubcontext("java:");
  79. ic.createSubcontext("java:/comp");
  80. ic.createSubcontext("java:/comp/env");
  81. ic.createSubcontext("java:/comp/env/jdbc");
  82.  
  83. // Construct DataSource
  84. MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
  85. ds.setURL("jdbc:mysql://localhost:3306/hospital");
  86. ds.setUser("root");
  87. ds.setPassword(" ");
  88.  
  89. ic.bind("java:/comp/env/jdbc/hospital", ds);
  90.  
  91. } catch (NamingException ex) {
  92. LOG.trace(ex);
  93. }
  94. }
  95.  
  96. @Parameterized.Parameters
  97. public static Collection<Object[]> getParameters() {
  98. return Arrays.asList(new Object[][]{
  99. {Patient.class, new Patient()}
  100. {User.class, new User()},
  101. {Reception.class, new Reception()},
  102. {Task.class, new Task()},
  103. {Procedure.class, new Procedure()}
  104.  
  105. });
  106. }
  107.  
  108. @Before
  109. public void setUp() throws DBException, SQLException {
  110. DaoFactory factory = new MySqlDaoFactory();
  111. connection = factory.getConnection();
  112. LOG.trace("Obtain connection: "+connection);
  113. connection.setAutoCommit(false);
  114. dao = factory.getDao(connection, daoClass);
  115. LOG.trace("Obtain dao: "+dao);
  116. }
  117.  
  118. @After
  119. public void tearDown() throws SQLException {
  120. connection.rollback();
  121. connection.close();
  122. }
  123.  
  124. @Override
  125. public ItemDao<Entity> dao() {
  126. return (ItemDao<Entity>) dao;
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement