openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r1090025 - in /openjpa/trunk/openjpa-jdbc/src: main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java test/java/org/apache/openjpa/jdbc/sql/TestDB2Dictionary.java
Date Thu, 07 Apr 2011 21:28:32 GMT
Author: mikedd
Date: Thu Apr  7 21:28:32 2011
New Revision: 1090025

URL: http://svn.apache.org/viewvc?rev=1090025&view=rev
Log:
OPENJPA-1975: Move default schema name code to its own method.

Added:
    openjpa/trunk/openjpa-jdbc/src/test/java/org/apache/openjpa/jdbc/sql/TestDB2Dictionary.java
  (with props)
Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java?rev=1090025&r1=1090024&r2=1090025&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
Thu Apr  7 21:28:32 2011
@@ -34,6 +34,8 @@ import java.util.Arrays;
 import java.util.Date;
 import java.util.StringTokenizer;
 
+import javax.sql.DataSource;
+
 import org.apache.openjpa.jdbc.identifier.DBIdentifier;
 import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
 import org.apache.openjpa.jdbc.kernel.JDBCStore;
@@ -268,26 +270,6 @@ public class DB2Dictionary
         super.connectedConfiguration(conn);
 
         DatabaseMetaData metaData = conn.getMetaData();
-        Statement stmnt = null;	
-        ResultSet rs =null;
-        try {
-            String str = "SELECT CURRENT SCHEMA FROM " + SYSDUMMY;
-            stmnt = conn.createStatement();
-            rs = stmnt.executeQuery(str);
-            if (rs.next()) {
-                String currSchema = rs.getString(1);
-                if (currSchema != null)
-                    setDefaultSchemaName(currSchema.trim());
-            }
-        } catch (SQLException e) {
-            if (log.isTraceEnabled())
-                log.trace(_loc.get("can_not_get_current_schema", e.getMessage()));
-        } finally {
-            if (rs != null)
-                rs.close();
-            if (stmnt != null)
-                stmnt.close();
-        }
 
         String driverName = metaData.getDriverName();
         if (driverName != null && driverName.startsWith("IBM DB2"))
@@ -1115,4 +1097,79 @@ public class DB2Dictionary
     public int getDB2MinorVersion() {
         return min;
     }
+    
+    public String getDefaultSchemaName()  {
+        if (defaultSchemaName == null) {
+            Connection conn = null;
+            Statement stmnt = null;
+            ResultSet rs = null;
+            try {
+                String str = "SELECT CURRENT SCHEMA FROM " + SYSDUMMY;
+                conn = getConnection(); 
+                stmnt = conn.createStatement();
+                rs = stmnt.executeQuery(str);
+                if (rs.next()) {
+                    String currSchema = rs.getString(1);
+                    if (currSchema != null) {
+                        setDefaultSchemaName(currSchema.trim());
+                    }
+                }
+            } catch (SQLException e) {
+                if (log.isTraceEnabled()) {
+                    log.trace(_loc.get("can_not_get_current_schema", e.getMessage()));
+                }
+            } finally {
+                if (rs != null) {
+                    try {
+                        rs.close();
+                    } catch (SQLException se) {
+                        // ignore
+                    }
+                }
+
+                if (stmnt != null) {
+                    try {
+                        stmnt.close();
+                    } catch (SQLException se) {
+                        // ignore
+                    }
+                }
+                if (conn != null) { 
+                    try { 
+                        conn.close(); 
+                    }
+                    catch(SQLException se) { 
+                        // ignore
+                    }
+                }
+            }
+        }
+        return defaultSchemaName;
+    }
+
+    /**
+     * Obtain a connection from the configuration. Tries to use the jta-data-source first
but falls back on the
+     * non-jta-data-source if no jta-data-source has been defined.
+     * 
+     * In practice this method is only called by getDefaultSchemaName which in turn is only
used by the schema tool.
+     * 
+     * @throws SQLException If neither datasource is available.
+     * @return A connection which may be used to obtain the default schema name. Callers
do not need to check for null. 
+     */
+    private Connection getConnection() throws SQLException {
+        // try to obtain a connection from the primary datasource 
+        DataSource ds = conf.getDataSource(null);
+        
+        if(ds == null) {
+            // use datasource 2 if available
+            ds = conf.getDataSource2(null);
+        }
+        
+        if (ds != null) {
+            return ds.getConnection();
+        }
+        
+        // throw
+        throw new SQLException("Unable to obtain a datasource");
+    }
 }

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=1090025&r1=1090024&r2=1090025&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
Thu Apr  7 21:28:32 2011
@@ -373,7 +373,7 @@ public class DBDictionary
     public String nameConcatenator = "_";
     public String delimitedCase = SCHEMA_CASE_PRESERVE;
     public String catalogSeparator = ".";
-    private String defaultSchemaName = null;
+    protected String defaultSchemaName = null;
     private String conversionKey = null;
        
     // Naming utility and naming rules

Added: openjpa/trunk/openjpa-jdbc/src/test/java/org/apache/openjpa/jdbc/sql/TestDB2Dictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/test/java/org/apache/openjpa/jdbc/sql/TestDB2Dictionary.java?rev=1090025&view=auto
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/test/java/org/apache/openjpa/jdbc/sql/TestDB2Dictionary.java
(added)
+++ openjpa/trunk/openjpa-jdbc/src/test/java/org/apache/openjpa/jdbc/sql/TestDB2Dictionary.java
Thu Apr  7 21:28:32 2011
@@ -0,0 +1,206 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF 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 org.apache.openjpa.jdbc.sql;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+import javax.sql.DataSource;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.kernel.StoreContext;
+import org.jmock.Expectations;
+import org.jmock.integration.junit3.MockObjectTestCase;
+
+public class TestDB2Dictionary extends MockObjectTestCase {
+    final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class);
+    final Statement mockStatement = mock(Statement.class);
+    final Connection mockConnection = mock(Connection.class);
+    final ResultSet mockRS = mock(ResultSet.class);
+    final DataSource mockDS = mock(DataSource.class);
+
+    final StoreContext sc = null;
+    final String schema = "abcd";
+    
+    /*
+     * When DS1 is non null we should get a connection and use it to obtain the schema name.

+     */
+    public void testGetDefaultSchemaNameDS1() throws Exception {
+        // Expected method calls on the mock objects above. If any of these are
+        // do not occur, or if any other methods are invoked on the mock objects
+        // an exception will be thrown and the test will fail.
+        checking(new Expectations() {
+            {
+                // Wiring, make sure the appropriate mocks are created.                
+                oneOf(mockConfiguration).getDataSource(with(equal(sc)));
+                will(returnValue(mockDS));
+
+                oneOf(mockDS).getConnection();
+                will(returnValue(mockConnection));
+
+                oneOf(mockConnection).createStatement();
+                will(returnValue(mockStatement));
+
+                oneOf(mockStatement).executeQuery(with(any(String.class)));
+                will(returnValue(mockRS));
+
+                // expect one call to rs.next() - needs to return true.
+                oneOf(mockRS).next();
+                will(returnValue(true));
+
+                // return our schema name
+                oneOf(mockRS).getString(1);
+                will(returnValue(schema));
+
+                // cleanup
+                oneOf(mockConnection).close();
+                oneOf(mockRS).close();
+                oneOf(mockStatement).close();
+
+                allowing(mockConfiguration);
+            }
+        });
+
+        DBDictionary dict = new DB2Dictionary();
+        dict.setConfiguration(mockConfiguration);
+        assertNotNull(dict);
+        assertEquals(schema, dict.getDefaultSchemaName());
+    }
+    
+    /*
+     * When ds1 is null, fallback to ds2 
+     */
+    public void testGetDefaultSchemaNameDS2() throws Exception {
+        // Expected method calls on the mock objects above. If any of these are
+        // do not occur, or if any other methods are invoked on the mock objects
+        // an exception will be thrown and the test will fail.
+        checking(new Expectations() {
+            {
+                // Wiring, make sure the appropriate mocks are created.                
+                oneOf(mockConfiguration).getDataSource(with(equal(sc)));
+                will(returnValue(null));
+                
+                oneOf(mockConfiguration).getDataSource2(with(equal(sc)));
+                will(returnValue(mockDS));
+
+                oneOf(mockDS).getConnection();
+                will(returnValue(mockConnection));
+
+                oneOf(mockConnection).createStatement();
+                will(returnValue(mockStatement));
+
+                oneOf(mockStatement).executeQuery(with(any(String.class)));
+                will(returnValue(mockRS));
+
+                // expect one call to rs.next() - needs to return true.
+                oneOf(mockRS).next();
+                will(returnValue(true));
+
+                // return our schema name
+                oneOf(mockRS).getString(1);
+                will(returnValue(schema));
+
+                // cleanup
+                oneOf(mockConnection).close();
+                oneOf(mockRS).close();
+                oneOf(mockStatement).close();
+
+                allowing(mockConfiguration);
+            }
+        });
+
+        DBDictionary dict = new DB2Dictionary();
+        dict.setConfiguration(mockConfiguration);
+        assertNotNull(dict);
+        assertEquals(schema, dict.getDefaultSchemaName());
+    }    
+    
+    /*
+     * When ds1 is null, fallback to ds2 
+     */
+    public void testGetDefaultSchemaNameNoDS() throws Exception {
+        // Expected method calls on the mock objects above. If any of these are
+        // do not occur, or if any other methods are invoked on the mock objects
+        // an exception will be thrown and the test will fail.
+        checking(new Expectations() {
+            {
+                // both datasources are null for this test.
+                oneOf(mockConfiguration).getDataSource(with(equal(sc)));
+                will(returnValue(null));
+                
+                oneOf(mockConfiguration).getDataSource2(with(equal(sc)));
+                will(returnValue(null));
+
+                allowing(mockConfiguration);
+            }
+        });
+
+        DBDictionary dict = new DB2Dictionary();
+        dict.setConfiguration(mockConfiguration);
+        assertNotNull(dict);
+        assertEquals(null, dict.getDefaultSchemaName());
+    }
+    
+    /*
+     * TestWhitespace trim 
+     */
+    public void testGetDefaultSchemaNameTrimmed() throws Exception {
+        final String schema2 = "abcd     ";
+        // Expected method calls on the mock objects above. If any of these are
+        // do not occur, or if any other methods are invoked on the mock objects
+        // an exception will be thrown and the test will fail.
+        checking(new Expectations() {
+            {
+                // Wiring, make sure the appropriate mocks are created.                
+                oneOf(mockConfiguration).getDataSource(with(equal(sc)));
+                will(returnValue(mockDS));
+
+                oneOf(mockDS).getConnection();
+                will(returnValue(mockConnection));
+
+                oneOf(mockConnection).createStatement();
+                will(returnValue(mockStatement));
+
+                oneOf(mockStatement).executeQuery(with(any(String.class)));
+                will(returnValue(mockRS));
+
+                // expect one call to rs.next() - needs to return true.
+                oneOf(mockRS).next();
+                will(returnValue(true));
+
+                // return our schema name
+                oneOf(mockRS).getString(1);
+                will(returnValue(schema2));
+
+                // cleanup
+                oneOf(mockConnection).close();
+                oneOf(mockRS).close();
+                oneOf(mockStatement).close();
+
+                allowing(mockConfiguration);
+            }
+        });
+
+        DBDictionary dict = new DB2Dictionary();
+        dict.setConfiguration(mockConfiguration);
+        assertNotNull(dict);
+        assertEquals(schema2.trim(), dict.getDefaultSchemaName());
+    }
+}

Propchange: openjpa/trunk/openjpa-jdbc/src/test/java/org/apache/openjpa/jdbc/sql/TestDB2Dictionary.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message