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 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
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
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
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
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
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 }