Blame view

src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java 2.34 KB
292aec6f   jcartign   First version bef...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  package fr.plil.sio.persistence.jdbc;
  
  import org.junit.After;
  import org.junit.Before;
  import org.springframework.beans.factory.annotation.Autowired;
  
  import javax.sql.DataSource;
  import java.sql.Connection;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  public abstract class AbstractServiceSupport {
  
      private static final String create_table_group = "CREATE TABLE GROUP_T (GROUP_ID INT NOT NULL AUTO_INCREMENT, " +
              "NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(GROUP_ID))";
e1ed8749   Rémi Vangrevelynghe   save before merge
16
17
      
      private static final String create_table_right = "CREATE TABLE RIGHT_T (RIGHT_ID INT NOT NULL AUTO_INCREMENT, " + 
fa02d31b   rvangrev   Commit avant le m...
18
              "NAME_R VARCHAR(50) NOT NULL, PARENT_ID INT, PRIMARY KEY(RIGHT_ID))";
e1ed8749   Rémi Vangrevelynghe   save before merge
19
20
  
      private static final String create_table_user = "CREATE TABLE USER_T (USER_ID INT NOT NULL AUTO_INCREMENT, " + 
fa02d31b   rvangrev   Commit avant le m...
21
22
23
24
              "NAME_U VARCHAR(50) UNIQUE NOT NULL, GROUP_ID INT, PRIMARY KEY(USER_ID))";
      
      private static final String create_table_link = "CREATE TABLE LINK_T (RIGHT_ID INT NOT NULL, GROUP_ID INT NOT NULL, " +
              "PRIMARY KEY(RIGHT_ID, GROUP_ID))";
e1ed8749   Rémi Vangrevelynghe   save before merge
25
      
292aec6f   jcartign   First version bef...
26
      private static final String drop_table_group = "DROP TABLE GROUP_T";
e1ed8749   Rémi Vangrevelynghe   save before merge
27
28
      private static final String drop_table_right = "DROP TABLE RIGHT_T";
      private static final String drop_table_user = "DROP TABLE USER_T";
fa02d31b   rvangrev   Commit avant le m...
29
      private static final String drop_table_link = "DROP TABLE LINK_T";
292aec6f   jcartign   First version bef...
30
31
32
33
34
35
36
37
38
39
40
41
  
      @Autowired
      private DataSource dataSource;
  
      private Connection connection;
  
      private Statement stmt;
  
      @Before
      public void createTables() throws SQLException {
          openConnection();
          stmt.executeUpdate(create_table_group);
e1ed8749   Rémi Vangrevelynghe   save before merge
42
43
          stmt.executeUpdate(create_table_right);
          stmt.executeUpdate(create_table_user);
fa02d31b   rvangrev   Commit avant le m...
44
          stmt.executeUpdate(create_table_link);
292aec6f   jcartign   First version bef...
45
46
47
48
49
50
51
          closeConnection();
      }
  
      @After
      public void cleanupDatabase() throws SQLException {
          openConnection();
          stmt.executeUpdate(drop_table_group);
e1ed8749   Rémi Vangrevelynghe   save before merge
52
53
          stmt.executeUpdate(drop_table_right);
          stmt.executeUpdate(drop_table_user);
fa02d31b   rvangrev   Commit avant le m...
54
          stmt.executeUpdate(drop_table_link);
292aec6f   jcartign   First version bef...
55
56
57
58
59
60
61
62
63
64
65
66
67
          closeConnection();
      }
  
      private void closeConnection() throws SQLException {
          stmt.close();
          connection.close();
      }
  
      private void openConnection() throws SQLException {
          connection = dataSource.getConnection();
          stmt = connection.createStatement();
      }
  }