1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.sql;
35
36 import java.security.SecureRandom;
37 import java.sql.Statement;
38
39 import fr.paris.lutece.portal.service.database.AppConnectionService;
40 import fr.paris.lutece.portal.service.plugin.Plugin;
41 import fr.paris.lutece.portal.service.plugin.PluginDefaultImplementation;
42 import fr.paris.lutece.test.LuteceTestCase;
43
44 public class DAOUtilTest extends LuteceTestCase
45 {
46
47 private static final String SQL_INSERT = " INSERT INTO core_datastore ( entity_key, entity_value ) VALUES ( ? , ? ) ";
48 private static final String SQL_DELETE = " DELETE FROM core_datastore where entity_key = ? ";
49 private static final String TESTKEY = "daoutiltestkey";
50 private static final String TESTVALUE = "daoutiltestvalue";
51
52 public void testDAOUtil_str( )
53 {
54 DAOUtil daoUtil = new DAOUtil( SQL_INSERT );
55 doTest( daoUtil, false );
56 }
57
58 public void testDAOUtil_str_int( )
59 {
60 DAOUtil daoUtil = new DAOUtil( SQL_INSERT, Statement.RETURN_GENERATED_KEYS );
61 doTest( daoUtil, true );
62 }
63
64 public void testDAOUtil_str_plugin( )
65 {
66 Plugin p = new PluginDefaultImplementation( );
67 p.setName( "core" );
68 p.setConnectionService( AppConnectionService.getDefaultConnectionService( ) );
69
70 DAOUtil daoUtil = new DAOUtil( SQL_INSERT, p );
71 doTest( daoUtil, false );
72 }
73
74 public void testDAOUtil_str_int_plugin( )
75 {
76 Plugin p = new PluginDefaultImplementation( );
77 p.setName( "core" );
78 p.setConnectionService( AppConnectionService.getDefaultConnectionService( ) );
79
80 DAOUtil daoUtil = new DAOUtil( SQL_INSERT, Statement.RETURN_GENERATED_KEYS, p );
81 doTest( daoUtil, true );
82 }
83
84 private void doTest( DAOUtil daoUtil, boolean hasGeneratedKey )
85 {
86 String key = TESTKEY + new SecureRandom( ).nextLong( );
87 try
88 {
89 daoUtil.setString( 1, key );
90 daoUtil.setString( 2, TESTVALUE );
91 daoUtil.executeUpdate( );
92 if ( hasGeneratedKey )
93 {
94
95
96
97
98
99 assertNotNull( "There should be a generatedkey resultset", daoUtil.getGeneratedKeysResultSet( ) );
100 }
101 }
102 finally
103 {
104 daoUtil.free( );
105 }
106
107 try ( DAOUtil daoUtildelete = new DAOUtil( SQL_DELETE ) )
108 {
109 daoUtildelete.setString( 1, key );
110 daoUtildelete.executeUpdate( );
111 }
112 catch( Exception e )
113 {
114 fail( );
115 }
116 }
117
118 public void testDAOUtil_str_FAIL_NO_GENERATED_KEYS( )
119 {
120 String key = TESTKEY + new SecureRandom( ).nextLong( );
121 try ( DAOUtil daoUtil = new DAOUtil( SQL_INSERT, Statement.NO_GENERATED_KEYS ) )
122 {
123 daoUtil.setString( 1, key );
124 daoUtil.setString( 2, TESTVALUE );
125 daoUtil.executeUpdate( );
126 }
127 catch( Exception e )
128 {
129 fail( );
130 }
131
132 try ( DAOUtil daoUtildelete = new DAOUtil( SQL_DELETE ) )
133 {
134 daoUtildelete.setString( 1, key );
135 daoUtildelete.executeUpdate( );
136 }
137 catch( Exception e )
138 {
139 fail( );
140 }
141 }
142
143 }