View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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      // Any table would be ok here
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" ); // DAOUtil.DEFAULT_MODULE_NAME
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" ); // DAOUtil.DEFAULT_MODULE_NAME
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                  // For now let's just test that there is no generated key
95                  // we don't have a table in the core that is created
96                  // with a generated key correctly in the different databases
97                  // With mysql and hsql, the result set is empty.
98                  // With postgresql, the result set has all the inserted values.
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 }