@Before public void init() { notaBDAO = new NotaBDAO(); try { db = new DBAccess(); conn = db.getConnection(); notaB1 = new NotaB(); notaB1.setIdNotaB(1); notaB1.setDescrizione("asd"); notaB1.setData(cd.getTime()); notaB1.setIdStrumento(2); notaB1.setIdUtente(1); notaB2 = new NotaB(); // notaB2.setIdNotaB(2); notaB2.setDescrizione("asd"); // notaB2.setData(cd.getTime()); notaB2.setIdStrumento(2); notaB2.setIdUtente(1); } catch (DBAccessException e) { fail(e.getMessage()); } }
@Test public void testCreate() { try { notaB2.setData(cd.getTime()); notaBDAO.create(conn, notaB2); String query = NOTA_B_FIND_BYPK; Statement stmt2 = conn.createStatement(); ResultSet rs = stmt2.executeQuery( "SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'db_qualita' AND TABLE_NAME = 'nota_b'"); rs.next(); int id = rs.getInt(1); PreparedStatement stmt = conn.prepareStatement(query); stmt.setInt(1, id - 1); rs = stmt.executeQuery(); rs.next(); int a = rs.getInt(1); db.rollbackConnection(conn); assertTrue(rs.getInt(1) == (id - 1)); } catch (DAOException e) { fail(e.getMessage()); } catch (SQLException e) { fail(e.getMessage()); } catch (DBAccessException e) { e.printStackTrace(); } finally { try { db.closeConnection(conn); } catch (DBAccessException e) { e.printStackTrace(); } } // TEST 2 test null try { notaBDAO.create(null, notaB2); fail(); } catch (DAOException e) { // return; assertTrue(e.getMessageKey().equals(INV_OBJ_NOTA_B)); } finally { try { db.closeConnection(conn); } catch (DBAccessException e) { e.printStackTrace(); } } // TEST 3 test class exception try { notaBDAO.create(conn, new Allegato()); fail(); } catch (DAOException e) { assertTrue(e.getMessageKey().equals(INV_OBJ_NOTA_B)); } finally { try { db.closeConnection(conn); } catch (DBAccessException e) { e.printStackTrace(); } } }