Skip to content

Commit a4780c2

Browse files
committed
Add DatabaseUtils unit tests
1 parent 34b4417 commit a4780c2

File tree

5 files changed

+232
-63
lines changed

5 files changed

+232
-63
lines changed

.gitignore

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
.settings
2-
target
3-
.classpath
4-
.project
5-
*~
6-
*.tmproj
7-
*.iml
8-
.idea
9-
.scala_dependencies
10-
integration/bundle/
11-
integration/felix-cache/
12-
runner
13-
.DS_Store
14-
test-output
1+
.settings
2+
target
3+
.classpath
4+
.project
5+
*~
6+
*.tmproj
7+
*.iml
8+
.idea
9+
.scala_dependencies
10+
integration/bundle/
11+
integration/felix-cache/
12+
runner
13+
.DS_Store
14+
test-output
15+
@TestDB
16+
TestDB
17+
derby*

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<gpg-plugin.version>1.6</gpg-plugin.version>
3636
<staging-plugin.version>1.6.7</staging-plugin.version>
3737
<release-plugin.version>2.5.3</release-plugin.version>
38+
<apache-derby.version>10.14.1.0</apache-derby.version>
3839
<skipSigning>true</skipSigning>
3940
</properties>
4041

@@ -63,6 +64,11 @@
6364
<artifactId>testng</artifactId>
6465
<version>${testng.version}</version>
6566
</dependency>
67+
<dependency>
68+
<groupId>org.apache.derby</groupId>
69+
<artifactId>derby</artifactId>
70+
<version>${apache-derby.version}</version>
71+
</dependency>
6672
</dependencies>
6773
</dependencyManagement>
6874

@@ -72,6 +78,11 @@
7278
<artifactId>testng</artifactId>
7379
<scope>test</scope>
7480
</dependency>
81+
<dependency>
82+
<groupId>org.apache.derby</groupId>
83+
<artifactId>derby</artifactId>
84+
<scope>test</scope>
85+
</dependency>
7586
</dependencies>
7687

7788
<build>

src/main/java/com/nordstrom/common/jdbc/DatabaseUtils.java

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
package com.nordstrom.common.jdbc;
22

33
import java.sql.Connection;
4+
import java.sql.Driver;
45
import java.sql.DriverManager;
56
import java.sql.ResultSet;
67
import java.sql.SQLException;
78
import java.util.Arrays;
9+
import java.util.Iterator;
10+
import java.util.ServiceLoader;
811

912
import com.nordstrom.common.base.UncheckedThrow;
1013

1114
import java.sql.PreparedStatement;
1215

1316
/**
14-
* This utility class provides facilities that enable you to define collections of Oracle database queries and
17+
* This utility class provides facilities that enable you to define collections of database queries and
1518
* execute them easily. Query collections are defined as Java enumeration that implement the {@link QueryAPI}
1619
* interface: <ul>
1720
* <li>{@link QueryAPI#getQueryStr() getQueryStr} - Get the query string for this constant. This is the actual query
1821
* that's sent to the database.</li>
1922
* <li>{@link QueryAPI#getArgNames() getArgNames} - Get the names of the arguments for this query. This provides
2023
* diagnostic information if the incorrect number of arguments is specified by the client.</li>
2124
* <li>{@link QueryAPI#getArgCount() getArgCount} - Get the number of arguments required by this query. This enables
22-
* {@link #executeOracleQuery(Class, QueryAPI, Object[])} to verify that the correct number of arguments has been
25+
* {@link #executeQuery(Class, QueryAPI, Object[])} to verify that the correct number of arguments has been
2326
* specified by the client.</li>
2427
* <li>{@link QueryAPI#getConnection() getConnection} - Get the connection string associated with this query. This
2528
* eliminates the need for the client to provide this information.</li>
2629
* <li>{@link QueryAPI#getEnum() getEnum} - Get the enumeration to which this query belongs. This enables {@link
27-
* #executeOracleQuery(Class, QueryAPI, Object[])} to retrieve the name of the query's enumerated constant for
30+
* #executeQuery(Class, QueryAPI, Object[])} to retrieve the name of the query's enumerated constant for
2831
* diagnostic messages.</li>
2932
* </ul>
3033
*
@@ -173,10 +176,9 @@ private DatabaseUtils() {
173176
}
174177

175178
static {
176-
try {
177-
Class.forName("oracle.jdbc.driver.OracleDriver");
178-
} catch (ClassNotFoundException e) {
179-
throw new RuntimeException("Unable to load the Oracle JDBC driver", e);
179+
Iterator<Driver> iterator = ServiceLoader.load(Driver.class).iterator();
180+
while (iterator.hasNext()) {
181+
iterator.next();
180182
}
181183
}
182184

@@ -188,7 +190,7 @@ private DatabaseUtils() {
188190
* @return count of records updated
189191
*/
190192
public static int update(QueryAPI query, Object... queryArgs) {
191-
Integer result = (Integer) executeOracleQuery(null, query, queryArgs);
193+
Integer result = (Integer) executeQuery(null, query, queryArgs);
192194
return (result != null) ? result.intValue() : -1;
193195
}
194196

@@ -200,7 +202,7 @@ public static int update(QueryAPI query, Object... queryArgs) {
200202
* @return row 1 / column 1 as integer; -1 if no rows were returned
201203
*/
202204
public static int getInt(QueryAPI query, Object... queryArgs) {
203-
Integer result = (Integer) executeOracleQuery(Integer.class, query, queryArgs);
205+
Integer result = (Integer) executeQuery(Integer.class, query, queryArgs);
204206
return (result != null) ? result.intValue() : -1;
205207
}
206208

@@ -212,7 +214,7 @@ public static int getInt(QueryAPI query, Object... queryArgs) {
212214
* @return row 1 / column 1 as string; 'null' if no rows were returned
213215
*/
214216
public static String getString(QueryAPI query, Object... queryArgs) {
215-
return (String) executeOracleQuery(String.class, query, queryArgs);
217+
return (String) executeQuery(String.class, query, queryArgs);
216218
}
217219

218220
/**
@@ -223,7 +225,7 @@ public static String getString(QueryAPI query, Object... queryArgs) {
223225
* @return {@link ResultPackage} object
224226
*/
225227
public static ResultPackage getResultPackage(QueryAPI query, Object... queryArgs) {
226-
return (ResultPackage) executeOracleQuery(ResultPackage.class, query, queryArgs);
228+
return (ResultPackage) executeQuery(ResultPackage.class, query, queryArgs);
227229
}
228230

229231
/**
@@ -243,7 +245,7 @@ public static ResultPackage getResultPackage(QueryAPI query, Object... queryArgs
243245
* <b>NOTE</b>: If you specify {@link ResultPackage} as the result type, it's recommended that you close this object
244246
* when you're done with it to free up database and JDBC resources that were allocated for it.
245247
*/
246-
private static Object executeOracleQuery(Class<?> resultType, QueryAPI query, Object... queryArgs) {
248+
private static Object executeQuery(Class<?> resultType, QueryAPI query, Object... queryArgs) {
247249
int expectCount = query.getArgCount();
248250
int actualCount = queryArgs.length;
249251

@@ -260,7 +262,7 @@ private static Object executeOracleQuery(Class<?> resultType, QueryAPI query, Ob
260262
throw new IllegalArgumentException(message);
261263
}
262264

263-
return executeOracleQuery(resultType, query.getConnection(), query.getQueryStr(), queryArgs);
265+
return executeQuery(resultType, query.getConnection(), query.getQueryStr(), queryArgs);
264266
}
265267

266268
/**
@@ -274,14 +276,14 @@ private static Object executeOracleQuery(Class<?> resultType, QueryAPI query, Ob
274276
* <li>For other types, {@link ResultSet#getObject(int, Class)} to return row 1 / column 1 as that type</li></ul>
275277
*
276278
* @param resultType desired result type (see TYPES above)
277-
* @param connectionStr Oracle database connection string
279+
* @param connectionStr database connection string
278280
* @param queryStr a SQL statement that may contain one or more '?' IN parameter placeholders
279281
* @param param an array of objects containing the input parameter values
280282
* @return for update operations, the number of rows affected; for query operations, an object of the indicated type<br>
281283
* <b>NOTE</b>: If you specify {@link ResultPackage} as the result type, it's recommended that you close this object
282284
* when you're done with it to free up database and JDBC resources that were allocated for it.
283285
*/
284-
public static Object executeOracleQuery(Class<?> resultType, String connectionStr, String queryStr, Object... param) {
286+
public static Object executeQuery(Class<?> resultType, String connectionStr, String queryStr, Object... param) {
285287
Object result = null;
286288
boolean failed = false;
287289

@@ -290,7 +292,7 @@ public static Object executeOracleQuery(Class<?> resultType, String connectionSt
290292
ResultSet resultSet = null;
291293

292294
try {
293-
connection = getOracleConnection(connectionStr);
295+
connection = getConnection(connectionStr);
294296
statement = connection.prepareStatement(queryStr); //NOSONAR
295297

296298
for (int i = 0; i < param.length; i++) {
@@ -321,18 +323,24 @@ public static Object executeOracleQuery(Class<?> resultType, String connectionSt
321323
if (resultSet != null) {
322324
try {
323325
resultSet.close();
324-
} catch (SQLException e) { }
326+
} catch (SQLException e) {
327+
// Suppress shutdown failures
328+
}
325329
}
326330
if (statement != null) {
327331
try {
328332
statement.close();
329-
} catch (SQLException e) { }
333+
} catch (SQLException e) {
334+
// Suppress shutdown failures
335+
}
330336
}
331337
if (connection != null) {
332338
try {
333339
connection.commit();
334340
connection.close();
335-
} catch (SQLException e) { }
341+
} catch (SQLException e) {
342+
// Suppress shutdown failures
343+
}
336344
}
337345
}
338346
}
@@ -341,43 +349,19 @@ public static Object executeOracleQuery(Class<?> resultType, String connectionSt
341349
}
342350

343351
/**
344-
* Get a connection to the Oracle database associated with the specified connection string.
352+
* Get a connection to the database associated with the specified connection string.
345353
*
346-
* @param connectionString Oracle database connection string
347-
* @return Oracle database connection object
354+
* @param connectionString database connection string
355+
* @return database connection object
348356
*/
349-
private static Connection getOracleConnection(String connectionString) {
357+
private static Connection getConnection(String connectionString) {
350358
try {
351-
QueryCreds creds = new QueryCreds(connectionString);
352-
return DriverManager.getConnection(creds.url, creds.userId, creds.password);
359+
return DriverManager.getConnection(connectionString);
353360
} catch (SQLException e) {
354361
throw UncheckedThrow.throwUnchecked(e);
355362
}
356363
}
357364

358-
/**
359-
* This class encapsulated database query credentials.
360-
*/
361-
private static class QueryCreds {
362-
363-
private String url;
364-
private String userId;
365-
private String password;
366-
367-
/**
368-
* Constructor for database query credentials.
369-
*
370-
* @param connectionString database connection string
371-
*/
372-
private QueryCreds(String connectionString) {
373-
String[] bits = connectionString.split(";");
374-
375-
url = bits[0].trim();
376-
userId = bits[1].split("=")[1].trim();
377-
password = bits[2].split("=")[1].trim();
378-
}
379-
}
380-
381365
/**
382366
* This interface defines the API supported by database query collections
383367
*/
@@ -416,7 +400,7 @@ public interface QueryAPI {
416400
*
417401
* @return query object enumerated constant
418402
*/
419-
Enum<?> getEnum();
403+
Enum<? extends QueryAPI> getEnum(); //NOSONAR
420404
}
421405

422406
/**

0 commit comments

Comments
 (0)