Skip to main content
summaryrefslogtreecommitdiffstats
blob: dd5e46b91af86d399f6596ff059f7b9f206bf869 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*******************************************************************************
 * Copyright (c) 2006, 2008 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.db.tests.internal.platforms;

import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObject;
import org.eclipse.jpt.db.Catalog;
import org.eclipse.jpt.db.Column;
import org.eclipse.jpt.db.ForeignKey;
import org.eclipse.jpt.db.Schema;
import org.eclipse.jpt.db.Table;

/**
 *  Derby 10.1 Embedded Driver Test
 */
@SuppressWarnings("nls")
public class DerbyTests extends DTPPlatformTests {

	public DerbyTests(String name) {
		super(name);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	@Override
	protected String getPlatformPropertiesFileName() {
		return "derby.properties";
	}

	@Override
	protected String getDriverName() {
		return "Derby Embedded JDBC Driver";
	}

	@Override
	protected String getDriverDefinitionID() {
		return "DriverDefn.Derby Embedded JDBC Driver";
	}

	@Override
	protected String getDriverDefinitionType() {
		return "org.eclipse.datatools.connectivity.db.derby101.genericDriverTemplate";
	}

	@Override
	protected String getDatabaseVendor() {
		return "Derby";
	}

	@Override
	protected String getDatabaseVersion() {
		return "10.1";
	}

	@Override
	protected String getDriverClass() {
		return "org.apache.derby.jdbc.EmbeddedDriver";
	}

	@Override
	protected String getProfileName() {
		return "Derby_10.1_Embedded";
	}

	@Override
	protected String getProfileDescription() {
		return "Derby 10.1 Embedded JDBC Profile [Test]";
	}

	@Override
	protected String getProviderID() {
		return "org.eclipse.datatools.connectivity.db.derby.embedded.connectionProfile";
	}

	public void testSchema() throws Exception {
		this.connectionProfile.connect();
		TestConnectionListener listener = new TestConnectionListener();
		this.connectionProfile.addConnectionListener(listener);

		this.dropSchema("TEST1");
		this.dropSchema("TEST2");

		this.executeUpdate("CREATE SCHEMA TEST1");
		((ICatalogObject) this.getDTPDatabase()).refresh();

		Schema schema1 = this.getDatabase().getSchemaNamed("TEST1");
		assertNotNull(schema1);
		Catalog catalog = this.getDatabase().getCatalogNamed("");  // Derby's only catalog
		Schema schema1a = catalog.getSchemaNamed("TEST1");
		assertNotNull(schema1a);
		assertSame(schema1, schema1a);  // same schema should be returned by both database and catalog

		this.executeUpdate("CREATE SCHEMA TEST2");
		Schema schema2 = this.getDatabase().getSchemaNamed("TEST2");
		assertNull(schema2);  // should be null until refresh

		((ICatalogObject) getDTPCatalog(catalog)).refresh();
		assertSame(catalog, listener.changedCatalog);
		assertSame(catalog, this.getDatabase().getCatalogNamed(""));

		schema2 = this.getDatabase().getSchemaNamed("TEST2");
		assertNotNull(schema2);
		Schema schema2a = catalog.getSchemaNamed("TEST2");
		assertNotNull(schema2a);
		assertSame(schema2, schema2a);
		assertNotSame(schema1, catalog.getSchemaNamed("TEST1"));  // we should have a new schema after the refresh

		this.dropSchema("TEST2");
		this.dropSchema("TEST1");
		this.connectionProfile.removeConnectionListener(listener);
		this.connectionProfile.disconnect();
	}

	public void testSchemaLookup() throws Exception {
		this.connectionProfile.connect();
		TestConnectionListener listener = new TestConnectionListener();
		this.connectionProfile.addConnectionListener(listener);

		this.dropSchema("LOOKUP_TEST");
		this.dropSchema("\"lookup_TEST\"");

		this.executeUpdate("CREATE SCHEMA LOOKUP_TEST");
		((ICatalogObject) this.getDTPDatabase()).refresh();

		assertNotNull(this.getDatabase().getSchemaNamed("LOOKUP_TEST"));
		assertNotNull(this.getDatabase().getSchemaNamed("lookup_test"));
		assertNotNull(this.getDatabase().getSchemaNamed("lookup_TEST"));
		assertNotNull(this.getDatabase().getSchemaNamed("\"LOOKUP_TEST\""));
		assertNull(this.getDatabase().getSchemaNamed("\"lookup_TEST\""));

		this.dropSchema("LOOKUP_TEST");

		this.executeUpdate("CREATE SCHEMA \"lookup_TEST\"");
		((ICatalogObject) this.getDTPDatabase()).refresh();

		assertNull(this.getDatabase().getSchemaNamed("LOOKUP_TEST"));
		assertNull(this.getDatabase().getSchemaNamed("lookup_test"));
		assertNull(this.getDatabase().getSchemaNamed("lookup_TEST"));
		assertNull(this.getDatabase().getSchemaNamed("\"LOOKUP_TEST\""));
		assertNotNull(this.getDatabase().getSchemaNamed("\"lookup_TEST\""));

		this.dropSchema("\"lookup_TEST\"");

		this.connectionProfile.removeConnectionListener(listener);
		this.connectionProfile.disconnect();
	}

	public void testSchemaAnnotationIdentifier() throws Exception {
		this.connectionProfile.connect();
		TestConnectionListener listener = new TestConnectionListener();
		this.connectionProfile.addConnectionListener(listener);

		this.dropSchema("LOOKUP_TEST");
		this.dropSchema("\"lookup_TEST\"");

		this.executeUpdate("CREATE SCHEMA lookup_test");  // this gets folded to uppercase
		this.executeUpdate("CREATE SCHEMA \"lookup_TEST\"");
		((ICatalogObject) this.getDTPDatabase()).refresh();

		Schema schema = this.getDatabase().getSchemaNamed("LOOKUP_TEST");
		assertEquals("LOOKUP_TEST", schema.getAnnotationIdentifier());
		assertEquals("LOOKUP_TEST", schema.getAnnotationIdentifier("LookupTest"));
		assertNull(schema.getAnnotationIdentifier("Lookup_Test"));

		schema = this.getDatabase().getSchemaNamed("lookup_test");
		assertEquals("LOOKUP_TEST", schema.getAnnotationIdentifier());

		schema = this.getDatabase().getSchemaNamed("\"lookup_TEST\"");
		assertEquals("\"lookup_TEST\"", schema.getAnnotationIdentifier());
		assertEquals("\"lookup_TEST\"", schema.getAnnotationIdentifier("lookup_TEST"));

		this.dropSchema("\"lookup_TEST\"");
		this.dropSchema("LOOKUP_TEST");

		this.connectionProfile.removeConnectionListener(listener);
		this.connectionProfile.disconnect();
	}

	public void testTable() throws Exception {
		this.connectionProfile.connect();
		TestConnectionListener listener = new TestConnectionListener();
		this.connectionProfile.addConnectionListener(listener);

		this.dropTable("TABLE_TEST", "FOO_BAZ");
		this.dropTable("TABLE_TEST", "BAZ");
		this.dropTable("TABLE_TEST", "FOO");
		this.dropTable("TABLE_TEST", "BAR");
		this.dropSchema("TABLE_TEST");

		this.executeUpdate("CREATE SCHEMA TABLE_TEST");
		this.executeUpdate("SET SCHEMA = TABLE_TEST");

		this.executeUpdate(this.buildBarDDL());
		this.executeUpdate(this.buildFooDDL());
		this.executeUpdate(this.buildBazDDL());
		this.executeUpdate(this.buildFooBazDDL());
		((ICatalogObject) this.getDTPDatabase()).refresh();

		Schema schema = this.getDatabase().getSchemaNamed("TABLE_TEST");

		// FOO
		Table fooTable = schema.getTableNamed("FOO");
		assertEquals(3, fooTable.columnsSize());
		assertEquals(1, fooTable.primaryKeyColumnsSize());
		assertEquals(1, fooTable.foreignKeysSize());

		Column pkColumn = fooTable.getPrimaryKeyColumn();
		assertEquals("ID", pkColumn.getName());
		Column idColumn = fooTable.getColumnNamed("ID");
		assertSame(pkColumn, idColumn);
		assertEquals("INTEGER", idColumn.getDataTypeName());
		assertSame(fooTable, idColumn.getTable());
		assertTrue(idColumn.isPrimaryKeyColumn());
		assertFalse(idColumn.isForeignKeyColumn());
		assertEquals("int", idColumn.getJavaTypeDeclaration());

		Column nameColumn = fooTable.getColumnNamed("NAME");
		assertEquals("VARCHAR", nameColumn.getDataTypeName());
		assertEquals("java.lang.String", nameColumn.getJavaTypeDeclaration());
		assertFalse(nameColumn.isPrimaryKeyColumn());

		Column barColumn = fooTable.getColumnNamed("BAR_ID");
		assertEquals("INTEGER", barColumn.getDataTypeName());
		assertTrue(barColumn.isForeignKeyColumn());
		assertFalse(barColumn.isPrimaryKeyColumn());

		ForeignKey barFK = fooTable.foreignKeys().next();  // there should only be 1 foreign key
		assertEquals(1, barFK.columnPairsSize());
		assertEquals("BAR", barFK.getAttributeName());
		assertNull(barFK.getJoinColumnAnnotationIdentifier("bar"));
		assertEquals("BAR_ID", barFK.getJoinColumnAnnotationIdentifier("primaryBar"));
		assertSame(fooTable, barFK.getBaseTable());

		assertFalse(fooTable.isPossibleJoinTable());
		assertSame(schema, fooTable.getSchema());

		// BAR
		Table barTable = schema.getTableNamed("BAR");
		assertEquals(2, barTable.columnsSize());
		assertEquals(1, barTable.primaryKeyColumnsSize());
		assertEquals(0, barTable.foreignKeysSize());
		assertEquals("ID", barTable.getPrimaryKeyColumn().getName());
		assertFalse(barTable.isPossibleJoinTable());
		assertEquals("BLOB", barTable.getColumnNamed("CHUNK").getDataTypeName());
		assertEquals("byte[]", barTable.getColumnNamed("CHUNK").getJavaTypeDeclaration());
		assertTrue(barTable.getColumnNamed("CHUNK").dataTypeIsLOB());
		assertSame(barTable, barFK.getReferencedTable());

		// FOO_BAZ
		Table foo_bazTable = schema.getTableNamed("FOO_BAZ");
		assertEquals(2, foo_bazTable.columnsSize());
		assertEquals(0, foo_bazTable.primaryKeyColumnsSize());
		assertEquals(2, foo_bazTable.foreignKeysSize());
		assertTrue(foo_bazTable.isPossibleJoinTable());
		assertTrue(foo_bazTable.joinTableNameIsDefault());
		assertTrue(foo_bazTable.getColumnNamed("FOO_ID").isForeignKeyColumn());

		this.dropTable("TABLE_TEST", "FOO_BAZ");
		this.dropTable("TABLE_TEST", "BAZ");
		this.dropTable("TABLE_TEST", "FOO");
		this.dropTable("TABLE_TEST", "BAR");
		this.dropSchema("TABLE_TEST");

		this.connectionProfile.removeConnectionListener(listener);
		this.connectionProfile.disconnect();
	}

	private static final String CR = System.getProperty("line.separator");  //$NON-NLS-1$

	private String buildBarDDL() {
		StringBuilder sb = new StringBuilder(200);
		sb.append("CREATE TABLE BAR (").append(CR);
		sb.append("    ID INT PRIMARY KEY,").append(CR);
		sb.append("    CHUNK BLOB(100K)").append(CR);
		sb.append(")").append(CR);
		return sb.toString();
	}

	private String buildFooDDL() {
		StringBuilder sb = new StringBuilder(200);
		sb.append("CREATE TABLE FOO (").append(CR);
		sb.append("    ID INT PRIMARY KEY,").append(CR);
		sb.append("    NAME VARCHAR(20),").append(CR);
		sb.append("    BAR_ID INT REFERENCES BAR(ID)").append(CR);
		sb.append(")").append(CR);
		return sb.toString();
	}

	private String buildBazDDL() {
		StringBuilder sb = new StringBuilder(200);
		sb.append("CREATE TABLE BAZ (").append(CR);
		sb.append("    ID INT PRIMARY KEY,").append(CR);
		sb.append("    NAME VARCHAR(20)").append(CR);
		sb.append(")").append(CR);
		return sb.toString();
	}

	private String buildFooBazDDL() {
		StringBuilder sb = new StringBuilder(200);
		sb.append("CREATE TABLE FOO_BAZ (").append(CR);
		sb.append("    FOO_ID INT REFERENCES FOO(ID),").append(CR);
		sb.append("    BAZ_ID INT REFERENCES BAZ(ID)").append(CR);
		sb.append(")").append(CR);
		return sb.toString();
	}

	public void testColumnLookup() throws Exception {
		this.connectionProfile.connect();
		TestConnectionListener listener = new TestConnectionListener();
		this.connectionProfile.addConnectionListener(listener);

		this.dropTable("TABLE_TEST", "test");
		this.dropSchema("TABLE_TEST");

		this.executeUpdate("CREATE SCHEMA TABLE_TEST");
		this.executeUpdate("SET SCHEMA = TABLE_TEST");

		// lowercase
		this.executeUpdate("CREATE TABLE test (id INTEGER, name VARCHAR(20))");
		((ICatalogObject) this.getDTPDatabase()).refresh();

		Table table = this.getDatabase().getSchemaNamed("TABLE_TEST").getTableNamed("test");
		assertNotNull(table.getColumnNamed("id"));
		assertNotNull(table.getColumnNamed("name"));

		this.dropTable("TABLE_TEST", "test");

		// uppercase
		this.executeUpdate("CREATE TABLE test (ID INTEGER, NAME VARCHAR(20))");
		((ICatalogObject) this.getDTPDatabase()).refresh();

		table = this.getDatabase().getSchemaNamed("TABLE_TEST").getTableNamed("test");
		assertNotNull(table.getColumnNamed("ID"));
		assertNotNull(table.getColumnNamed("NAME"));

		this.dropTable("TABLE_TEST", "test");

		// mixed case
		this.executeUpdate("CREATE TABLE test (Id INTEGER, Name VARCHAR(20))");
		((ICatalogObject) this.getDTPDatabase()).refresh();

		table = this.getDatabase().getSchemaNamed("TABLE_TEST").getTableNamed("test");
		assertNotNull(table.getColumnNamed("Id"));
		assertNotNull(table.getColumnNamed("Name"));

		this.dropTable("TABLE_TEST", "test");

		// delimited
		this.executeUpdate("CREATE TABLE test (\"Id\" INTEGER, \"Name\" VARCHAR(20))");
		((ICatalogObject) this.getDTPDatabase()).refresh();

		table = this.getDatabase().getSchemaNamed("TABLE_TEST").getTableNamed("test");
		assertNotNull(table.getColumnNamed("\"Id\""));
		assertNotNull(table.getColumnNamed("\"Name\""));

		this.dropTable("TABLE_TEST", "test");
		this.dropSchema("TABLE_TEST");

		this.connectionProfile.removeConnectionListener(listener);
		this.connectionProfile.disconnect();
	}

	private void dropTable(String schemaName, String tableName) throws Exception {
		Schema schema= this.getSchemaNamed(schemaName);
		if (schema != null) {
			if (schema.getTableNamed(tableName) != null) {
				this.executeUpdate("DROP TABLE " + schemaName + '.' + tableName);
			}
		}
	}

	/**
	 * NB: A Derby schema must be empty before it can be dropped.
	 */
	private void dropSchema(String name) throws Exception {
		if (this.getSchemaNamed(name) != null) {
			this.executeUpdate("DROP SCHEMA " + name + " RESTRICT");
		}
	}

}

Back to the top