Blame view

src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java 2.82 KB
292aec6f   jcartign   First version bef...
1
2
3
4
5
6
7
8
9
10
  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;
7a831760   msahmane   Version où les te...
11
12
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
292aec6f   jcartign   First version bef...
13
14
15
  
  public abstract class AbstractServiceSupport {
  
7a831760   msahmane   Version où les te...
16
17
      private static final Logger logger = LoggerFactory.getLogger(AbstractServiceSupport.class);
      
292aec6f   jcartign   First version bef...
18
19
20
      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))";
  
7a831760   msahmane   Version où les te...
21
      private static final String create_table_right = "CREATE TABLE RIGHT_T (RIGHT_ID INT NOT NULL AUTO_INCREMENT, "+ 
60e63100   msahmane   Test des Right à ...
22
              "NAME_R VARCHAR(50) NOT NULL, PRIMARY KEY(RIGHT_ID), PARENT INT, FOREIGN KEY(PARENT) REFERENCES RIGHT_T(RIGHT_ID))";
7a831760   msahmane   Version où les te...
23
24
25
26
      
      private static final String create_table_user = "CREATE TABLE USER_T (USER_ID INT NOT NULL AUTO_INCREMENT, "+
              "NAME_U VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(USER_ID), GROUP_U INT, FOREIGN KEY(GROUP_U) REFERENCES GROUP_T(GROUP_ID)) ";        
      
4e3e24c9   msahmane   Amélioration des ...
27
28
29
30
31
      private static final String create_table_rg = "CREATE TABLE GROUP_RIGHTS (G_ID INT NOT NULL, R_ID INT NOT NULL,"+
                    "PRIMARY KEY(G_ID,R_ID),"+
                    "FOREIGN KEY(G_ID) REFERENCES GROUP_T(GROUP_ID),"+
                    "FOREIGN KEY(R_ID) REFERENCES RIGHT_T(RIGHT_ID))";
      
292aec6f   jcartign   First version bef...
32
      private static final String drop_table_group = "DROP TABLE GROUP_T";
7a831760   msahmane   Version où les te...
33
34
35
36
      
      private static final String drop_table_right = "DROP TABLE RIGHT_T";
      
      private static final String drop_table_user = "DROP TABLE USER_T";
4e3e24c9   msahmane   Amélioration des ...
37
38
      
      private static final String drop_table_rg = "DROP TABLE GROUP_RIGHTS";
292aec6f   jcartign   First version bef...
39
40
41
42
43
44
45
46
47
48
  
      @Autowired
      private DataSource dataSource;
  
      private Connection connection;
  
      private Statement stmt;
  
      @Before
      public void createTables() throws SQLException {
7a831760   msahmane   Version où les te...
49
          logger.debug("createTables");
292aec6f   jcartign   First version bef...
50
51
          openConnection();
          stmt.executeUpdate(create_table_group);
7a831760   msahmane   Version où les te...
52
53
          stmt.executeUpdate(create_table_right);
          stmt.executeUpdate(create_table_user);
4e3e24c9   msahmane   Amélioration des ...
54
          stmt.executeUpdate(create_table_rg);
292aec6f   jcartign   First version bef...
55
56
57
58
59
          closeConnection();
      }
  
      @After
      public void cleanupDatabase() throws SQLException {
7a831760   msahmane   Version où les te...
60
          logger.debug("cleanupDatabase");        
292aec6f   jcartign   First version bef...
61
62
          openConnection();
          stmt.executeUpdate(drop_table_group);
7a831760   msahmane   Version où les te...
63
64
          stmt.executeUpdate(drop_table_right);
          stmt.executeUpdate(drop_table_user);
4e3e24c9   msahmane   Amélioration des ...
65
          stmt.executeUpdate(drop_table_rg);
292aec6f   jcartign   First version bef...
66
67
68
69
70
71
72
73
74
75
76
77
78
          closeConnection();
      }
  
      private void closeConnection() throws SQLException {
          stmt.close();
          connection.close();
      }
  
      private void openConnection() throws SQLException {
          connection = dataSource.getConnection();
          stmt = connection.createStatement();
      }
  }