001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.dialect;
022:
023: import java.sql.DatabaseMetaData;
024: import java.sql.SQLException;
025: import java.sql.Types;
026: import java.util.List;
027: import java.util.regex.Pattern;
028:
029: import net.sf.hajdbc.ColumnProperties;
030: import net.sf.hajdbc.Dialect;
031: import net.sf.hajdbc.SequenceProperties;
032: import net.sf.hajdbc.TableProperties;
033:
034: import org.easymock.EasyMock;
035: import org.testng.annotations.DataProvider;
036: import org.testng.annotations.Test;
037:
038: /**
039: * @author Paul Ferraro
040: *
041: */
042: @SuppressWarnings("nls")
043: public class TestPostgreSQLDialect extends TestStandardDialect {
044: @Override
045: protected Dialect createDialect() {
046: return new PostgreSQLDialect();
047: }
048:
049: @Override
050: @Test(dataProvider="column")
051: public int getColumnType(ColumnProperties properties)
052: throws SQLException {
053: EasyMock.expect(properties.getNativeType()).andReturn("oid");
054:
055: this .replay();
056:
057: int type = this .dialect.getColumnType(properties);
058:
059: this .verify();
060:
061: assert type == Types.BLOB : type;
062:
063: this .reset();
064:
065: EasyMock.expect(properties.getNativeType()).andReturn("int");
066: EasyMock.expect(properties.getType()).andReturn(Types.INTEGER);
067:
068: this .replay();
069:
070: type = this .dialect.getColumnType(properties);
071:
072: this .verify();
073:
074: assert type == Types.INTEGER : type;
075:
076: return type;
077: }
078:
079: /*
080: @Override
081: @Test(dataProvider = "table")
082: public String getLockTableSQL(TableProperties properties) throws SQLException
083: {
084: EasyMock.expect(properties.getName()).andReturn("table");
085:
086: this.replay();
087:
088: String sql = this.dialect.getLockTableSQL(properties);
089:
090: this.verify();
091:
092: assert sql.equals("LOCK TABLE table IN EXCLUSIVE MODE") : sql;
093:
094: return sql;
095: }
096: */
097: @Override
098: @Test(dataProvider="table")
099: public String getTruncateTableSQL(TableProperties properties)
100: throws SQLException {
101: EasyMock.expect(properties.getName()).andReturn("table");
102:
103: this .replay();
104:
105: String sql = this .dialect.getTruncateTableSQL(properties);
106:
107: this .verify();
108:
109: assert sql.equals("TRUNCATE TABLE table");
110:
111: return sql;
112: }
113:
114: @Override
115: @Test(dataProvider="sequence")
116: public String getNextSequenceValueSQL(SequenceProperties sequence)
117: throws SQLException {
118: EasyMock.expect(sequence.getName()).andReturn("sequence");
119:
120: this .replay();
121:
122: String sql = this .dialect.getNextSequenceValueSQL(sequence);
123:
124: this .verify();
125:
126: assert sql.equals("SELECT NEXTVAL('sequence')") : sql;
127:
128: return sql;
129: }
130:
131: @Override
132: @Test(dataProvider="null")
133: public String parseSequence(String sql) throws SQLException {
134: this .replay();
135:
136: String sequence = this .dialect
137: .parseSequence("SELECT CURRVAL('sequence')");
138:
139: this .verify();
140:
141: assert sequence.equals("sequence") : sequence;
142:
143: this .reset();
144: this .replay();
145:
146: sequence = this .dialect
147: .parseSequence("SELECT nextval('sequence')");
148:
149: this .verify();
150:
151: assert sequence.equals("sequence") : sequence;
152:
153: this .reset();
154: this .replay();
155:
156: sequence = this .dialect.parseSequence("SELECT * FROM table");
157:
158: this .verify();
159:
160: assert sequence == null : sequence;
161:
162: return sequence;
163: }
164:
165: @Override
166: @Test(dataProvider="meta-data")
167: public List<String> getDefaultSchemas(DatabaseMetaData metaData)
168: throws SQLException {
169: EasyMock.expect(metaData.getConnection()).andReturn(
170: this .connection);
171: EasyMock.expect(this .connection.createStatement()).andReturn(
172: this .statement);
173:
174: EasyMock
175: .expect(this .statement.executeQuery("SHOW search_path"))
176: .andReturn(this .resultSet);
177: EasyMock.expect(this .resultSet.next()).andReturn(false);
178: EasyMock.expect(this .resultSet.getString(1)).andReturn(
179: "$user,public");
180:
181: this .resultSet.close();
182: this .statement.close();
183:
184: EasyMock.expect(metaData.getUserName()).andReturn("user");
185:
186: this .replay();
187:
188: List<String> schemaList = this .dialect
189: .getDefaultSchemas(metaData);
190:
191: this .verify();
192:
193: assert schemaList.size() == 2 : schemaList.size();
194:
195: assert schemaList.get(0).equals("user") : schemaList.get(0);
196: assert schemaList.get(1).equals("public") : schemaList.get(1);
197:
198: return schemaList;
199: }
200:
201: @Override
202: @Test(dataProvider="column")
203: public boolean isIdentity(ColumnProperties properties)
204: throws SQLException {
205: EasyMock.expect(properties.getNativeType()).andReturn("serial");
206:
207: this .replay();
208:
209: boolean identity = this .dialect.isIdentity(properties);
210:
211: this .verify();
212:
213: assert identity;
214:
215: this .reset();
216:
217: EasyMock.expect(properties.getNativeType()).andReturn(
218: "bigserial");
219:
220: this .replay();
221:
222: identity = this .dialect.isIdentity(properties);
223:
224: this .verify();
225:
226: assert identity;
227:
228: this .reset();
229:
230: EasyMock.expect(this .columnProperties.getNativeType())
231: .andReturn("int");
232:
233: this .replay();
234:
235: identity = this .dialect.isIdentity(properties);
236:
237: this .verify();
238:
239: assert !identity;
240:
241: return identity;
242: }
243:
244: @Override
245: @Test(dataProvider="table-column-long")
246: public String getAlterIdentityColumnSQL(TableProperties table,
247: ColumnProperties column, long value) throws SQLException {
248: EasyMock.expect(table.getName()).andReturn("table");
249: EasyMock.expect(column.getName()).andReturn("column");
250:
251: this .replay();
252:
253: String sql = this .dialect.getAlterIdentityColumnSQL(table,
254: column, value);
255:
256: this .verify();
257:
258: assert sql
259: .equals("ALTER SEQUENCE table_column_seq RESTART WITH 1000") : sql;
260:
261: return sql;
262: }
263:
264: @Override
265: @Test(dataProvider="meta-data")
266: public Pattern getIdentifierPattern(DatabaseMetaData metaData)
267: throws SQLException {
268: EasyMock.expect(metaData.getDriverMajorVersion()).andReturn(8);
269: EasyMock.expect(metaData.getDriverMinorVersion()).andReturn(0);
270: EasyMock.expect(metaData.getExtraNameCharacters())
271: .andReturn("");
272:
273: this .replay();
274:
275: Pattern pattern = this .dialect.getIdentifierPattern(metaData);
276:
277: this .verify();
278:
279: assert pattern.pattern().equals("[\\w\\Q\\E]+") : pattern
280: .pattern();
281:
282: this .reset();
283:
284: EasyMock.expect(metaData.getDriverMajorVersion()).andReturn(8);
285: EasyMock.expect(metaData.getDriverMinorVersion()).andReturn(1);
286:
287: this .replay();
288:
289: pattern = this .dialect.getIdentifierPattern(metaData);
290:
291: this .verify();
292:
293: assert pattern.pattern().equals(
294: "[A-Za-z\\0200-\\0377_][A-Za-z\\0200-\\0377_0-9\\$]*") : pattern
295: .pattern();
296:
297: return pattern;
298: }
299:
300: @Override
301: @DataProvider(name="random")
302: Object[][] randomProvider() {
303: return new Object[][] {
304: new Object[] { "SELECT RANDOM() FROM success" },
305: new Object[] { "SELECT RANDOM ( ) FROM success" },
306: new Object[] { "SELECT OPERANDOM() FROM failure" },
307: new Object[] { "SELECT 1 FROM failure" }, };
308: }
309: }
|