1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //	 this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //	 this list of conditions and the following disclaimer in the documentation  
12  //	 and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //	 nor the names of its contributors may be used to endorse or promote products  
15  //	 derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  package org.objectledge.database;
29  
30  import java.io.IOException;
31  import java.io.Reader;
32  import java.sql.Connection;
33  import java.sql.ResultSet;
34  import java.sql.Statement;
35  
36  import javax.sql.DataSource;
37  
38  import junit.framework.TestCase;
39  
40  import org.jcontainer.dna.impl.DefaultConfiguration;
41  import org.objectledge.filesystem.FileSystem;
42  
43  /***
44   * 
45   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
46   * @version $Id: DatabaseUtilsTest.java,v 1.6 2004/10/25 14:54:54 rafal Exp $
47   */
48  public class DatabaseUtilsTest extends TestCase
49  {    
50      private FileSystem fs;
51  
52      private DataSource dataSource;
53  
54      public void setUp()
55          throws Exception
56      {
57          fs = FileSystem.getStandardFileSystem(".");
58          dataSource = getDataSource();    
59      }
60      
61      /***
62       * Constructor for DatabaseUtilsTest.
63       * @param arg0
64       */
65      public DatabaseUtilsTest(String arg0)
66      {
67          super(arg0);
68      }
69  
70      /*
71       * Test for void close(Connection)
72       */
73      public void testCloseConnection()
74          throws Exception
75      {
76          Connection conn = null;
77          DatabaseUtils.close(conn);
78          conn = dataSource.getConnection();
79          DatabaseUtils.close(conn);
80          DatabaseUtils.close(conn);
81      }
82  
83      /*
84       * Test for void close(Statement)
85       */
86      public void testCloseStatement()
87          throws Exception
88      {
89          Connection conn = dataSource.getConnection();
90          Statement stmt = null;
91          DatabaseUtils.close(stmt);
92          stmt = conn.createStatement();
93          DatabaseUtils.close(stmt);
94          DatabaseUtils.close(stmt);
95      }
96  
97      /*
98       * Test for void close(ResultSet)
99       */
100     public void testCloseResultSet()
101         throws Exception
102     {
103         Connection conn = dataSource.getConnection();
104         Statement stmt = conn.createStatement();
105         ResultSet rs = null;
106         DatabaseUtils.close(rs);
107         rs = stmt.executeQuery("CHECKPOINT");
108         DatabaseUtils.close(rs);
109         DatabaseUtils.close(rs);
110     }
111 
112     /*
113      * Test for void close(Connection, Statement, ResultSet)
114      */
115     public void testCloseConnectionStatementResultSet()
116         throws Exception
117     {
118         Connection conn = dataSource.getConnection();
119         Statement stmt = conn.createStatement();
120         ResultSet rs = stmt.executeQuery("CHECKPOINT");
121         DatabaseUtils.close(conn, stmt, rs);
122     }
123 
124     public void testUnescapeSqlString()
125         throws Exception
126     {
127         // Shalom!
128         assertEquals("!\u05E9\u05DC\u05D5\u05DD",
129             DatabaseUtils.unescapeSqlString("!\u05E9\u05DC\u05D5\u05DD"));
130     }
131 
132     public void testEscapeSqlString()
133         throws Exception
134     {
135         assertEquals("\u05E2\u05D6\u05E8\u05D0//'s home directory is "+
136             "c:////users////\u05E2\u05D6\u05E8\u05D0",
137             DatabaseUtils.escapeSqlString("\u05E2\u05D6\u05E8\u05D0's home directory is "+
138                 "c://users//\u05E2\u05D6\u05E8\u05D0"));
139     }
140 
141     public void testRunScript()
142         throws Exception
143     {
144         DatabaseUtils.runScript(dataSource, getScript("runScript.sql"));
145         try
146         {
147             DatabaseUtils.runScript(dataSource, getScript("runScriptUnterminated.sql"));
148             fail("exception expected");
149         }
150         catch(Exception e)
151         {
152             assertEquals("unterminated statement at line 34", e.getMessage());
153         }
154         try
155         {
156             DatabaseUtils.runScript(dataSource, getScript("runScriptFailing.sql"));
157             fail("exception expected");
158         }
159         catch(Exception e)
160         {
161             assertEquals("error executing statement at line 34", e.getMessage());
162         }
163     }
164     
165     /////////////////////////////////////////////////////////////////////////////////////////////
166     
167     private Reader getScript(String name)
168         throws IOException
169     {
170         return fs.getReader("sql/database/"+name, "UTF-8");
171     }
172     
173     private DataSource getDataSource()
174         throws Exception
175     {
176         DefaultConfiguration conf = new DefaultConfiguration("config","","/");
177         DefaultConfiguration url = new DefaultConfiguration("url","","/config");
178         url.setValue("jdbc:hsqldb:."); 
179         conf.addChild(url);    
180         DefaultConfiguration user = new DefaultConfiguration("user","","/config");
181         user.setValue("sa");
182         conf.addChild(user);
183         return new HsqldbDataSource(conf);    
184     }    
185 }