Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0f40fb55710ebae8a4f89e7744257ee961cdd51a (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*******************************************************************************
 * Copyright (c) 2007, 2012 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.jpa.core.tests.internal.context.java;

import java.util.Iterator;
import java.util.ListIterator;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jpt.common.core.resource.java.JavaResourceType;
import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement.AstNodeType;
import org.eclipse.jpt.common.utility.internal.iterator.ArrayIterator;
import org.eclipse.jpt.jpa.core.context.Entity;
import org.eclipse.jpt.jpa.core.context.NamedNativeQuery;
import org.eclipse.jpt.jpa.core.context.QueryHint;
import org.eclipse.jpt.jpa.core.resource.java.JPA;
import org.eclipse.jpt.jpa.core.resource.java.NamedNativeQueryAnnotation;
import org.eclipse.jpt.jpa.core.tests.internal.context.ContextModelTestCase;

@SuppressWarnings("nls")
public class JavaNamedNativeQueryTests extends ContextModelTestCase
{
	private static final String QUERY_NAME = "QUERY_NAME";
	private static final String QUERY_QUERY = "MY_QUERY";
		
	private ICompilationUnit createTestEntityWithNamedNativeQuery() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JPA.ENTITY, JPA.NAMED_NATIVE_QUERY);
			}
			@Override
			public void appendTypeAnnotationTo(StringBuilder sb) {
				sb.append("@Entity").append(CR);
				sb.append("@NamedNativeQuery(name=\"" + QUERY_NAME + "\", query=\"" + QUERY_QUERY + "\")");
			}
		});
	}


		
	public JavaNamedNativeQueryTests(String name) {
		super(name);
	}
	
	public void testUpdateName() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();

		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation javaNamedNativeQuery = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);

		assertEquals(QUERY_NAME, javaNamedNativeQuery.getName());
		assertEquals(QUERY_NAME, namedNativeQuery.getName());

		//set name to null in the resource model
		javaNamedNativeQuery.setName(null);
		getJpaProject().synchronizeContextModel();
		assertNull(javaNamedNativeQuery.getName());
		assertNull(namedNativeQuery.getName());

		//set name in the resource model, verify context model updated
		javaNamedNativeQuery.setName("foo");
		getJpaProject().synchronizeContextModel();
		assertEquals("foo", javaNamedNativeQuery.getName());
		assertEquals("foo", namedNativeQuery.getName());
	}
	
	public void testModifyName() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
				
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation javaNamedNativeQuery = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);

		assertEquals(QUERY_NAME, javaNamedNativeQuery.getName());
		assertEquals(QUERY_NAME, namedNativeQuery.getName());
				
		//set name to null in the context model
		namedNativeQuery.setName(null);
		assertNull(javaNamedNativeQuery.getName());
		assertNull(namedNativeQuery.getName());

		//set name in the context model, verify resource model updated
		namedNativeQuery.setName("foo");
		assertEquals("foo", javaNamedNativeQuery.getName());
		assertEquals("foo", namedNativeQuery.getName());
	}
	
	public void testUpdateQuery() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
				
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation javaNamedNativeQuery = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);

		assertEquals(QUERY_QUERY, javaNamedNativeQuery.getQuery());
		assertEquals(QUERY_QUERY, namedNativeQuery.getQuery());
				
		//set name to null in the resource model
		javaNamedNativeQuery.setQuery(null);
		getJpaProject().synchronizeContextModel();
		assertNull(javaNamedNativeQuery.getQuery());
		assertNull(namedNativeQuery.getQuery());

		//set name in the resource model, verify context model updated
		javaNamedNativeQuery.setQuery("foo");
		getJpaProject().synchronizeContextModel();
		assertEquals("foo", javaNamedNativeQuery.getQuery());
		assertEquals("foo", namedNativeQuery.getQuery());
	}
	
	public void testModifyQuery() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
				
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation javaNamedNativeQuery = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);
	
		assertEquals(QUERY_QUERY, javaNamedNativeQuery.getQuery());
		assertEquals(QUERY_QUERY, namedNativeQuery.getQuery());
				
		//set name to null in the context model
		namedNativeQuery.setQuery(null);
		assertNull(javaNamedNativeQuery.getQuery());
		assertNull(namedNativeQuery.getQuery());

		//set name in the context model, verify resource model updated
		namedNativeQuery.setQuery("foo");
		assertEquals("foo", javaNamedNativeQuery.getQuery());
		assertEquals("foo", namedNativeQuery.getQuery());
	}
	
	
	public void testAddHint() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
				
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation javaNamedNativeQuery = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);
	
		
		QueryHint queryHint = namedNativeQuery.addHint(0);
		queryHint.setName("FOO");

		assertEquals("FOO", javaNamedNativeQuery.hintAt(0).getName());
		
		QueryHint queryHint2 = namedNativeQuery.addHint(0);
		queryHint2.setName("BAR");
		
		assertEquals("BAR", javaNamedNativeQuery.hintAt(0).getName());
		assertEquals("FOO", javaNamedNativeQuery.hintAt(1).getName());
		
		QueryHint queryHint3 = namedNativeQuery.addHint(1);
		queryHint3.setName("BAZ");
		
		assertEquals("BAR", javaNamedNativeQuery.hintAt(0).getName());
		assertEquals("BAZ", javaNamedNativeQuery.hintAt(1).getName());
		assertEquals("FOO", javaNamedNativeQuery.hintAt(2).getName());
		
		ListIterator<? extends QueryHint> hints = namedNativeQuery.getHints().iterator();
		assertEquals(queryHint2, hints.next());
		assertEquals(queryHint3, hints.next());
		assertEquals(queryHint, hints.next());
		
		hints = namedNativeQuery.getHints().iterator();
		assertEquals("BAR", hints.next().getName());
		assertEquals("BAZ", hints.next().getName());
		assertEquals("FOO", hints.next().getName());
	}
	
	public void testRemoveHint() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
				
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation javaNamedNativeQuery = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);

		namedNativeQuery.addHint(0).setName("FOO");
		namedNativeQuery.addHint(1).setName("BAR");
		namedNativeQuery.addHint(2).setName("BAZ");
		
		assertEquals(3, javaNamedNativeQuery.getHintsSize());
		
		namedNativeQuery.removeHint(0);
		assertEquals(2, javaNamedNativeQuery.getHintsSize());
		assertEquals("BAR", javaNamedNativeQuery.hintAt(0).getName());
		assertEquals("BAZ", javaNamedNativeQuery.hintAt(1).getName());

		namedNativeQuery.removeHint(0);
		assertEquals(1, javaNamedNativeQuery.getHintsSize());
		assertEquals("BAZ", javaNamedNativeQuery.hintAt(0).getName());
		
		namedNativeQuery.removeHint(0);
		assertEquals(0, javaNamedNativeQuery.getHintsSize());
	}
	
	public void testMoveHint() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
				
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation javaNamedNativeQuery = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);

		namedNativeQuery.addHint(0).setName("FOO");
		namedNativeQuery.addHint(1).setName("BAR");
		namedNativeQuery.addHint(2).setName("BAZ");
		
		assertEquals(3, javaNamedNativeQuery.getHintsSize());
		
		
		namedNativeQuery.moveHint(2, 0);
		ListIterator<? extends QueryHint> hints = namedNativeQuery.getHints().iterator();
		assertEquals("BAR", hints.next().getName());
		assertEquals("BAZ", hints.next().getName());
		assertEquals("FOO", hints.next().getName());

		assertEquals("BAR", javaNamedNativeQuery.hintAt(0).getName());
		assertEquals("BAZ", javaNamedNativeQuery.hintAt(1).getName());
		assertEquals("FOO", javaNamedNativeQuery.hintAt(2).getName());


		namedNativeQuery.moveHint(0, 1);
		hints = namedNativeQuery.getHints().iterator();
		assertEquals("BAZ", hints.next().getName());
		assertEquals("BAR", hints.next().getName());
		assertEquals("FOO", hints.next().getName());

		assertEquals("BAZ", javaNamedNativeQuery.hintAt(0).getName());
		assertEquals("BAR", javaNamedNativeQuery.hintAt(1).getName());
		assertEquals("FOO", javaNamedNativeQuery.hintAt(2).getName());
	}
	
	public void testUpdateHints() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
				
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation queryAnnotation = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);
		
		queryAnnotation.addHint(0).setName("FOO");
		queryAnnotation.addHint(1).setName("BAR");
		queryAnnotation.addHint(2).setName("BAZ");
		getJpaProject().synchronizeContextModel();
	
		ListIterator<? extends QueryHint> hints = namedNativeQuery.getHints().iterator();
		assertEquals("FOO", hints.next().getName());
		assertEquals("BAR", hints.next().getName());
		assertEquals("BAZ", hints.next().getName());
		assertFalse(hints.hasNext());
		
		queryAnnotation.moveHint(2, 0);
		getJpaProject().synchronizeContextModel();
		hints = namedNativeQuery.getHints().iterator();
		assertEquals("BAR", hints.next().getName());
		assertEquals("BAZ", hints.next().getName());
		assertEquals("FOO", hints.next().getName());
		assertFalse(hints.hasNext());
	
		queryAnnotation.moveHint(0, 1);
		getJpaProject().synchronizeContextModel();
		hints = namedNativeQuery.getHints().iterator();
		assertEquals("BAZ", hints.next().getName());
		assertEquals("BAR", hints.next().getName());
		assertEquals("FOO", hints.next().getName());
		assertFalse(hints.hasNext());
	
		queryAnnotation.removeHint(1);
		getJpaProject().synchronizeContextModel();
		hints = namedNativeQuery.getHints().iterator();
		assertEquals("BAZ", hints.next().getName());
		assertEquals("FOO", hints.next().getName());
		assertFalse(hints.hasNext());
	
		queryAnnotation.removeHint(1);
		getJpaProject().synchronizeContextModel();
		hints = namedNativeQuery.getHints().iterator();
		assertEquals("BAZ", hints.next().getName());
		assertFalse(hints.hasNext());
		
		queryAnnotation.removeHint(0);
		getJpaProject().synchronizeContextModel();
		assertFalse(namedNativeQuery.getHints().iterator().hasNext());
	}

	public void testHintsSize() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		Entity entity = getJavaEntity();
		
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
		assertEquals(0, namedNativeQuery.getHintsSize());
		
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation javaNamedNativeQuery = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);
		
		
		javaNamedNativeQuery.addHint(0);
		getJpaProject().synchronizeContextModel();
		assertEquals(1, namedNativeQuery.getHintsSize());
		
		javaNamedNativeQuery.addHint(0);
		getJpaProject().synchronizeContextModel();
		assertEquals(2, namedNativeQuery.getHintsSize());
		
		javaNamedNativeQuery.removeHint(0);
		javaNamedNativeQuery.removeHint(0);
		getJpaProject().synchronizeContextModel();
		assertEquals(0, namedNativeQuery.getHintsSize());
	}
	
	public void testUpdateResultClass() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
				
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation queryAnnotation = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);
		
		assertEquals(null, queryAnnotation.getResultClass());
		assertEquals(null, namedNativeQuery.getResultClass());

		//set name in the resource model, verify context model updated
		queryAnnotation.setResultClass("foo");
		this.getJpaProject().synchronizeContextModel();
		assertEquals("foo", queryAnnotation.getResultClass());
		assertEquals("foo", namedNativeQuery.getResultClass());
		
		//set name to null in the resource model
		queryAnnotation.setResultClass(null);
		this.getJpaProject().synchronizeContextModel();
		assertNull(queryAnnotation.getResultClass());
		assertNull(namedNativeQuery.getResultClass());
	}
	
	public void testModifyResultClass() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
				
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation javaNamedNativeQuery = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);
		
		assertEquals(null, javaNamedNativeQuery.getResultClass());
		assertEquals(null, namedNativeQuery.getResultClass());
				
		//set name in the context model, verify resource model updated
		namedNativeQuery.setResultClass("foo");
		assertEquals("foo", javaNamedNativeQuery.getResultClass());
		assertEquals("foo", namedNativeQuery.getResultClass());

		//set name to null in the context model
		namedNativeQuery.setResultClass(null);
		assertNull(javaNamedNativeQuery.getResultClass());
		assertNull(namedNativeQuery.getResultClass());
	}
	
	public void testUpdateResultSetMapping() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
				
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation javaNamedNativeQuery = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);
		
		assertEquals(null, javaNamedNativeQuery.getResultSetMapping());
		assertEquals(null, namedNativeQuery.getResultSetMapping());

		//set name in the resource model, verify context model updated
		javaNamedNativeQuery.setResultSetMapping("foo");
		getJpaProject().synchronizeContextModel();
		assertEquals("foo", javaNamedNativeQuery.getResultSetMapping());
		assertEquals("foo", namedNativeQuery.getResultSetMapping());
		
		//set name to null in the resource model
		javaNamedNativeQuery.setResultSetMapping(null);
		getJpaProject().synchronizeContextModel();
		assertNull(javaNamedNativeQuery.getResultSetMapping());
		assertNull(namedNativeQuery.getResultSetMapping());
	}
	
	public void testModifyResultSetMapping() throws Exception {
		createTestEntityWithNamedNativeQuery();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		Entity entity = getJavaEntity();
		NamedNativeQuery namedNativeQuery = entity.getQueryContainer().getNamedNativeQueries().iterator().next();
				
		JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, AstNodeType.TYPE);
		NamedNativeQueryAnnotation javaNamedNativeQuery = (NamedNativeQueryAnnotation) resourceType.getAnnotation(0, NamedNativeQueryAnnotation.ANNOTATION_NAME);
		
		assertEquals(null, javaNamedNativeQuery.getResultSetMapping());
		assertEquals(null, namedNativeQuery.getResultSetMapping());

		//set name in the context model, verify resource model updated
		namedNativeQuery.setResultSetMapping("foo");
		assertEquals("foo", javaNamedNativeQuery.getResultSetMapping());
		assertEquals("foo", namedNativeQuery.getResultSetMapping());
		
		//set name to null in the context model
		namedNativeQuery.setResultSetMapping(null);
		assertNull(javaNamedNativeQuery.getResultSetMapping());
		assertNull(namedNativeQuery.getResultSetMapping());
	}

}

Back to the top