Guest User

Untitled

a guest
May 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1.  
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import static org.junit.Assert.*;
  5. import java.sql.DriverManager;
  6. import org.junit.Test;
  7.  
  8.  
  9. public class UnitTest
  10. {
  11.     private static final String[] connectionInfo = new String[] {
  12.         "jdbc:mysql://localhost:3306/utp root root",
  13.         "jdbc:mysql://localhost:3306/utp wrong bad"
  14.     };
  15.    
  16.     private static final String[] userTest = new String[] {
  17.         "testuser1 testpass1",
  18.         "omg testpass2"
  19.     };
  20.    
  21.     private static final ActionManager actionMgr = new ActionManager();
  22.     private static final Connection con = actionMgr.conn;
  23.  
  24.     @Test
  25.     public void connectTest()
  26.     {
  27.         Connection conn;
  28.         for(int i = 0; i < 2; i++)
  29.         {
  30.             String str[] = connectionInfo[i].split(" ");
  31.            
  32.             try {
  33.                 conn = DriverManager.getConnection(str[0], str[1], str[2]);
  34.                 conn.close();
  35.             }
  36.             catch (SQLException e) {
  37.                 System.err.println("Wrong connection info given");
  38.             }
  39.         }
  40.     }
  41.    
  42.     @Test
  43.     public void insertTest() throws SQLException
  44.     {
  45.         con.setAutoCommit(false); // Make actionMgr not send changes
  46.         String[] t = userTest[0].split(" ");
  47.         System.err.println("before addition : " + actionMgr.getRowCount("users"));
  48.         actionMgr.add(new WrapperUser(t[0], t[1]));
  49.         System.err.println("after addition : " + actionMgr.getRowCount("users"));
  50.         con.rollback(); // Rollback all changes
  51.         System.err.println("after rollback: " + actionMgr.getRowCount("users"));
  52.     }
  53.    
  54.     @Test
  55.     public void selectTest()
  56.     {
  57.         WrapperSet<WrapperGroup> wrapSet = actionMgr.FindGroupWith("test");
  58.         assertEquals(3, wrapSet.size());
  59.     }
  60.    
  61.     @Test
  62.     public void countTest()
  63.     {
  64.         int userCount = actionMgr.getRowCount("users");
  65.         int groupsCount = actionMgr.getRowCount("groups");
  66.        
  67.         assertEquals(12, userCount);
  68.         assertEquals(4, groupsCount);
  69.     }
  70.    
  71.     @Test
  72.     public void deleteTest() throws SQLException
  73.     {
  74.         System.err.println("before removal : " + actionMgr.getRowCount("users"));
  75.         con.setAutoCommit(false); // Make actionMgr not send changes
  76.         String[] t = userTest[1].split(" ");
  77.         actionMgr.remove(new WrapperUser(t[0], t[1]));
  78.         System.err.println("after removal : " + actionMgr.getRowCount("users"));
  79.         con.rollback(); // Rollback all changes
  80.         System.err.println("after rollback: " + actionMgr.getRowCount("users"));
  81.     }
  82. }
Add Comment
Please, Sign In to add comment