[libpostgresql-jdbc-java] 14/24: Add tests for schema name containing special characters

Emmanuel Bourg ebourg-guest at moszumanska.debian.org
Mon Jan 9 21:17:40 UTC 2017


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

ebourg-guest pushed a commit to tag REL9_3_1103
in repository libpostgresql-jdbc-java.

commit 851342e153aba7b06c62247c64ff5a0bf24ab604
Author: Alexis Meneses <alexismeneses at users.noreply.github.com>
Date:   Thu Dec 4 21:27:36 2014 +0100

    Add tests for schema name containing special characters
    
    (cherry picked from commit 169c6dfe48e07234f9d083ae92697298e8d5b630)
    
    Conflicts:
    	org/postgresql/test/jdbc4/jdbc41/SchemaTest.java
---
 org/postgresql/test/jdbc4/jdbc41/SchemaTest.java | 194 +++++++++++++++++++++++
 1 file changed, 194 insertions(+)

diff --git a/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java b/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java
new file mode 100644
index 0000000..4a544ee
--- /dev/null
+++ b/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java
@@ -0,0 +1,194 @@
+/*-------------------------------------------------------------------------
+*
+* Copyright (c) 2010-2014, PostgreSQL Global Development Group
+*
+*
+*-------------------------------------------------------------------------
+*/
+package org.postgresql.test.jdbc4.jdbc41;
+
+import java.sql.*;
+
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+import org.postgresql.test.TestUtil;
+
+public class SchemaTest extends TestCase
+{
+
+    private Connection _conn;
+
+    public SchemaTest(String name)
+    {
+        super(name);
+    }
+
+    protected void setUp() throws Exception
+    {
+        _conn = TestUtil.openDB();
+        Statement stmt = _conn.createStatement();
+        stmt.execute("CREATE SCHEMA schema1");
+        stmt.execute("CREATE SCHEMA schema2");
+        stmt.execute("CREATE SCHEMA \"schema 3\"");
+        stmt.execute("CREATE SCHEMA \"schema \"\"4\"");
+        stmt.execute("CREATE SCHEMA \"schema '5\"");
+        stmt.execute("CREATE SCHEMA \"UpperCase\"");
+        TestUtil.createTable(_conn, "schema1.table1", "id integer");
+        TestUtil.createTable(_conn, "schema2.table2", "id integer");
+        TestUtil.createTable(_conn, "\"UpperCase\".table3", "id integer");
+    }
+
+    protected void tearDown() throws SQLException
+    {
+        _conn.setSchema(null);
+        Statement stmt = _conn.createStatement();
+        stmt.execute("DROP SCHEMA schema1 CASCADE");
+        stmt.execute("DROP SCHEMA schema2 CASCADE");
+        stmt.execute("DROP SCHEMA \"schema 3\" CASCADE");
+        stmt.execute("DROP SCHEMA \"schema \"\"4\" CASCADE");
+        stmt.execute("DROP SCHEMA \"schema '5\" CASCADE");
+        stmt.execute("DROP SCHEMA \"UpperCase\" CASCADE");
+        TestUtil.closeDB(_conn);
+    }
+
+    /**
+     * Test that what you set is what you get
+     */
+    public void testGetSetSchema() throws SQLException
+    {
+        _conn.setSchema("schema1");
+        assertEquals("schema1", _conn.getSchema());
+        _conn.setSchema("schema2");
+        assertEquals("schema2", _conn.getSchema());
+        _conn.setSchema("schema 3");
+        assertEquals("\"schema 3\"", _conn.getSchema());
+        _conn.setSchema("schema \"4");
+        assertEquals("\"schema \"\"4\"", _conn.getSchema());
+        _conn.setSchema("schema '5");
+        assertEquals("\"schema '5\"", _conn.getSchema());
+        _conn.setSchema("UpperCase");
+        assertEquals("\"UpperCase\"", _conn.getSchema());
+    }
+
+    /**
+     * Test that setting the schema allows to access objects of this schema
+     * without prefix, hide objects from other schemas but doesn't prevent
+     * to prefix-access to them.
+     */
+    public void testUsingSchema() throws SQLException
+    {
+        Statement stmt = _conn.createStatement();
+        try
+        {
+            try
+            {
+                _conn.setSchema("schema1");
+                stmt.executeQuery(TestUtil.selectSQL("table1", "*"));
+                stmt.executeQuery(TestUtil.selectSQL("schema2.table2", "*"));
+                try
+                {
+                    stmt.executeQuery(TestUtil.selectSQL("table2", "*"));
+                    fail("Objects of schema2 should not be visible without prefix");
+                }
+                catch (SQLException e)
+                {
+                    // expected
+                }
+
+                _conn.setSchema("schema2");
+                stmt.executeQuery(TestUtil.selectSQL("table2", "*"));
+                stmt.executeQuery(TestUtil.selectSQL("schema1.table1", "*"));
+                try
+                {
+                    stmt.executeQuery(TestUtil.selectSQL("table1", "*"));
+                    fail("Objects of schema1 should not be visible without prefix");
+                }
+                catch (SQLException e)
+                {
+                    // expected
+                }
+
+                _conn.setSchema("UpperCase");
+                stmt.executeQuery(TestUtil.selectSQL("table3", "*"));
+                stmt.executeQuery(TestUtil.selectSQL("schema1.table1", "*"));
+                try
+                {
+                    stmt.executeQuery(TestUtil.selectSQL("table1", "*"));
+                    fail("Objects of schema1 should not be visible without prefix");
+                }
+                catch (SQLException e)
+                {
+                    // expected
+                }
+            }
+            catch (SQLException e)
+            {
+                fail("Could not find expected schema elements: " + e.getMessage());
+            }
+        }
+        finally
+        {
+            try
+            {
+                stmt.close();
+            }
+            catch (SQLException e)
+            {
+            }
+        }
+    }
+
+    /**
+     * Test that get schema returns the schema with the highest priority
+     * in the search path
+     */
+    public void testMultipleSearchPath() throws SQLException
+    {
+        Statement stmt = _conn.createStatement();
+        try
+        {
+            stmt.execute("SET search_path TO schema1,schema2");
+        }
+        finally
+        {
+            try
+            {
+                stmt.close();
+            }
+            catch (SQLException e)
+            {
+            }
+        }
+        assertEquals("schema1", _conn.getSchema());
+    }
+
+    public void testSchemaInProperties() throws Exception
+    {
+        Properties properties = new Properties();
+        properties.setProperty("currentSchema", "schema1");
+        Connection conn = TestUtil.openDB(properties);
+        try
+        {
+            assertEquals("schema1", conn.getSchema());
+
+            Statement stmt = conn.createStatement();
+            stmt.executeQuery(TestUtil.selectSQL("table1", "*"));
+            stmt.executeQuery(TestUtil.selectSQL("schema2.table2", "*"));
+            try
+            {
+                stmt.executeQuery(TestUtil.selectSQL("table2", "*"));
+                fail("Objects of schema2 should not be visible without prefix");
+            }
+            catch (SQLException e)
+            {
+                // expected
+            }
+        }
+        finally
+        {
+            TestUtil.closeDB(conn);
+        }
+    }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git



More information about the pkg-java-commits mailing list