1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbunit.database;
25
26 import java.sql.DatabaseMetaData;
27 import java.sql.PreparedStatement;
28 import java.util.Locale;
29
30 import org.dbunit.DatabaseEnvironment;
31 import org.dbunit.DatabaseUnitException;
32 import org.dbunit.dataset.ITable;
33
34
35
36
37
38
39
40 public class DatabaseConnectionIT extends AbstractDatabaseConnectionIT
41 {
42 public DatabaseConnectionIT(String s)
43 {
44 super(s);
45 }
46
47 protected String convertString(String str) throws Exception
48 {
49 return getEnvironment().convertString(str);
50 }
51
52
53 public void testCreateNullConnection() throws Exception
54 {
55 try
56 {
57 new DatabaseConnection(null);
58 fail("Should not be able to create a database connection without a JDBC connection");
59 }
60 catch(NullPointerException expected)
61 {
62
63 }
64 }
65
66 public void testCreateConnectionWithNonExistingSchemaAndStrictValidation() throws Exception
67 {
68 DatabaseEnvironment environment = getEnvironment();
69 String schema = environment.convertString("XYZ_INVALID_SCHEMA_1642344539");
70 IDatabaseConnection validConnection = super.getConnection();
71
72 try
73 {
74 boolean validate = true;
75 new DatabaseConnection(validConnection.getConnection(), schema, validate);
76 fail("Should not be able to create a database connection object with an unknown schema.");
77 }
78 catch(DatabaseUnitException expected)
79 {
80 String expectedMsg = "The given schema '" + convertString(schema) + "' does not exist.";
81 assertEquals(expectedMsg, expected.getMessage());
82 }
83 }
84
85 public void testCreateConnectionWithNonExistingSchemaAndLenientValidation() throws Exception
86 {
87 DatabaseEnvironment environment = getEnvironment();
88 String schema = environment.convertString("XYZ_INVALID_SCHEMA_1642344539");
89 IDatabaseConnection validConnection = super.getConnection();
90
91 boolean validate = false;
92 DatabaseConnection dbConnection = new DatabaseConnection(validConnection.getConnection(), schema, validate);
93 assertNotNull(dbConnection);
94 }
95
96
97 public void testCreateConnectionWithSchemaDbStoresUpperCaseIdentifiers() throws Exception
98 {
99 IDatabaseConnection validConnection = super.getConnection();
100 String schema = validConnection.getSchema();
101 assertNotNull("Precondition: schema of connection must not be null", schema);
102
103
104 DatabaseMetaData metaData = validConnection.getConnection().getMetaData();
105 if(metaData.storesUpperCaseIdentifiers())
106 {
107 boolean validate = true;
108 DatabaseConnection dbConnection = new DatabaseConnection(validConnection.getConnection(), schema.toLowerCase(Locale.ENGLISH), validate);
109 assertNotNull(dbConnection);
110 assertEquals(schema.toUpperCase(Locale.ENGLISH), dbConnection.getSchema());
111 }
112 else
113 {
114
115 assertTrue(true);
116 }
117 }
118
119
120 public void testCreateConnectionWithSchemaDbStoresLowerCaseIdentifiers() throws Exception
121 {
122 IDatabaseConnection validConnection = super.getConnection();
123 String schema = validConnection.getSchema();
124 assertNotNull("Precondition: schema of connection must not be null", schema);
125
126
127 DatabaseMetaData metaData = validConnection.getConnection().getMetaData();
128 if(metaData.storesLowerCaseIdentifiers())
129 {
130 boolean validate = true;
131 DatabaseConnection dbConnection = new DatabaseConnection(validConnection.getConnection(), schema.toUpperCase(Locale.ENGLISH), validate);
132 assertNotNull(dbConnection);
133 assertEquals(schema.toLowerCase(Locale.ENGLISH), dbConnection.getSchema());
134 }
135 else
136 {
137
138 assertTrue(true);
139 }
140 }
141
142 public void testCreateQueryWithPreparedStatement() throws Exception
143 {
144 IDatabaseConnection connection = super.getConnection();
145 PreparedStatement pstmt = connection.getConnection().prepareStatement("select * from TEST_TABLE where COLUMN0=?");
146
147 try{
148 pstmt.setString(1, "row 1 col 0");
149 ITable table = connection.createTable("MY_TABLE", pstmt);
150 assertEquals(1, table.getRowCount());
151 assertEquals(4, table.getTableMetaData().getColumns().length);
152 assertEquals("row 1 col 1", table.getValue(0, "COLUMN1"));
153
154
155 pstmt.setString(1, "row 2 col 0");
156 ITable table2 = connection.createTable("MY_TABLE", pstmt);
157 assertEquals(1, table2.getRowCount());
158 assertEquals(4, table2.getTableMetaData().getColumns().length);
159 assertEquals("row 2 col 1", table2.getValue(0, "COLUMN1"));
160 }
161 finally{
162 pstmt.close();
163 }
164 }
165
166 }
167
168