[java-support] branch dev/JSPT-111 updated: Move basic database testing helper into java-support.

Scott Cantor cantor.2 at osu.edu
Wed Jun 15 16:59:56 UTC 2022


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch dev/JSPT-111
in repository java-support.

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=96d080e77bd957da5b35586a369b09482ccf4ffc

The following commit(s) were added to refs/heads/dev/JSPT-111 by this push:
     new 96d080e  Move basic database testing helper into java-support.
96d080e is described below

commit 96d080e77bd957da5b35586a369b09482ccf4ffc
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Wed Jun 15 12:59:54 2022 -0400

    Move basic database testing helper into java-support.
---
 pom.xml                                            |   5 +
 .../support/testing/DatabaseTestingSupport.java    | 144 +++++++++++++++++++++
 2 files changed, 149 insertions(+)

diff --git a/pom.xml b/pom.xml
index 787fd32..0a5bbcc 100644
--- a/pom.xml
+++ b/pom.xml
@@ -160,6 +160,11 @@
             <artifactId>spring-web</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.hsqldb</groupId>
+            <artifactId>hsqldb</artifactId>
+            <scope>test</scope>
+        </dependency>
 
     </dependencies>
 
diff --git a/src/test/java/net/shibboleth/utilities/java/support/testing/DatabaseTestingSupport.java b/src/test/java/net/shibboleth/utilities/java/support/testing/DatabaseTestingSupport.java
new file mode 100644
index 0000000..2fd8156
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/testing/DatabaseTestingSupport.java
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.utilities.java.support.testing;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.sql.DataSource;
+
+import net.shibboleth.utilities.java.support.primitive.StringSupport;
+
+import org.hsqldb.jdbc.JDBCDataSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.io.CharStreams;
+
+/**
+ * Helper class for testing database-backed classes using custom DDL and SQL.
+ */
+public class DatabaseTestingSupport {
+
+    @Nonnull static Logger log = LoggerFactory.getLogger(DatabaseTestingSupport.class);
+    
+    public static void InitializeDataSource(@Nullable String initializingSQLFile, DataSource source) {
+
+        final String sql = ReadSqlFromFile(initializingSQLFile);
+        if (sql == null) {
+            return;
+        }
+        ExecuteUpdate(sql, source);
+    }
+
+    protected static String ReadSqlFromFile(@Nullable String initializingSQLFile) {
+
+        final String file = StringSupport.trimOrNull(initializingSQLFile);
+
+        if (null == file) {
+            return null;
+        }
+
+        final InputStream is = DatabaseTestingSupport.class.getResourceAsStream(file);
+
+        if (null == is) {
+            log.warn("Could not locate SQL file called {} ", file);
+            return null;
+        }
+        String sql;
+        try {
+            sql = StringSupport.trimOrNull(CharStreams.toString(new InputStreamReader(is)));
+        } catch (IOException e) {
+            log.warn("Could not read SQL file called {}.", file);
+            return null;
+        }
+
+        if (null == sql) {
+            log.warn("SQL file called {} was empty.", file);
+            return null;
+        }
+
+        return sql;
+    }
+
+    protected static void ExecuteUpdate(@Nullable String sql, DataSource source) {
+
+        log.debug("Applying SQL: \n {}", sql);
+
+        try {
+            Connection dbConn = source.getConnection();
+            Statement statement = dbConn.createStatement();
+
+            statement.executeUpdate(sql);
+        } catch (SQLException e) {
+            log.warn("Could not contact data source {} or execute commands", source, e);
+            return;
+        }
+    }
+
+    /**
+     * Summons up an in memory database with the provided identifier. The contents of the resource stream (if any) are
+     * then submitted to the database (so as to allow initializing to a known state.
+     * 
+     * @param initializingSQLFile a file in the classpath with SQL files
+     * @param identifier a name to uniquify this database.
+     * @return a DataSource which can then be used for testing.
+     */
+    public static DataSource GetMockDataSource(@Nullable String initializingSQLFile, @Nonnull String identifier) {
+
+        return GetDataSourceFromUrl(initializingSQLFile, "jdbc:hsqldb:mem:" + identifier);
+    }
+
+    /**
+     * Summons up a database connection to  an hsqldb server running somewhere.
+     * @param initializingSQLFile a file in the classpath with SQL files
+     * @param server the server name and database name.  For instance "//localhost/testdb"
+     * @return a DataSource which can then be used for testing
+     */
+    public static DataSource GetDataSourceFromHsqlServer(@Nullable String initializingSQLFile, @Nonnull String server) {
+
+        return GetDataSourceFromUrl(initializingSQLFile, "jdbc:hsqldb:hsql:" + server);
+    }
+
+    public static void InitializeDataSourceFromFile(String sqlFile, DataSource source) {
+        final String sql = ReadSqlFromFile(sqlFile);
+        final String[] statements = sql.split(";");
+        for (String statement : statements) {
+            ExecuteUpdate(statement.trim(), source);
+        }
+    }
+
+    protected static DataSource GetDataSourceFromUrl(String initializingSQLFile, String JdbcUri) {
+        JDBCDataSource jdbcSource = new JDBCDataSource();
+
+        jdbcSource.setUrl(JdbcUri);
+        jdbcSource.setUser("SA");
+        jdbcSource.setPassword("");
+
+        InitializeDataSource(initializingSQLFile, jdbcSource);
+
+        return jdbcSource;
+    }
+
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list