blob: ea19e54fb6cbb55bc1956d142a2af7b6112fe8cd [file] [log] [blame]
kmooreda90c112011-02-06 02:33:04 +00001/*******************************************************************************
kmoorec7a08672011-08-01 15:10:18 +00002 * Copyright (c) 2009, 2011 Oracle.
kmooreda90c112011-02-06 02:33:04 +00003 * All rights reserved. This program and the accompanying materials are
4 * made available under the terms of the Eclipse Public License v1.0 which
5 * accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Oracle - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jpt.jpa.core.tests.internal.jpa2.resource.java;
12
13import java.util.Iterator;
14import org.eclipse.jdt.core.ICompilationUnit;
kmoorec7a08672011-08-01 15:10:18 +000015import org.eclipse.jpt.common.core.resource.java.JavaResourceField;
16import org.eclipse.jpt.common.core.resource.java.JavaResourceType;
17import org.eclipse.jpt.common.utility.internal.CollectionTools;
kmooreda90c112011-02-06 02:33:04 +000018import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator;
19import org.eclipse.jpt.jpa.core.jpa2.resource.java.JPA2_0;
20import org.eclipse.jpt.jpa.core.jpa2.resource.java.MapsId2_0Annotation;
kmooreda90c112011-02-06 02:33:04 +000021
22@SuppressWarnings("nls")
23public class MapsId2_0AnnotationTests
24 extends JavaResourceModel2_0TestCase
25{
26 public MapsId2_0AnnotationTests(String name) {
27 super(name);
28 }
29
30
31 private ICompilationUnit createTestMapsId() throws Exception {
32 return this.createTestType(new DefaultAnnotationWriter() {
33 @Override
34 public Iterator<String> imports() {
35 return new ArrayIterator<String>(JPA2_0.MAPS_ID);
36 }
37 @Override
38 public void appendIdFieldAnnotationTo(StringBuilder sb) {
39 sb.append("@MapsId");
40 }
41 });
42 }
43
44 private ICompilationUnit createTestMapsIdWithValue() throws Exception {
45 return this.createTestType(new DefaultAnnotationWriter() {
46 @Override
47 public Iterator<String> imports() {
48 return new ArrayIterator<String>(JPA2_0.MAPS_ID);
49 }
50 @Override
51 public void appendIdFieldAnnotationTo(StringBuilder sb) {
52 sb.append("@MapsId(\"foo\")");
53 }
54 });
55 }
56
57 public void testMapsId() throws Exception {
58 ICompilationUnit cu = this.createTestMapsId();
kmoorec7a08672011-08-01 15:10:18 +000059 JavaResourceType resourceType = buildJavaResourceType(cu);
60 JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
kmooreda90c112011-02-06 02:33:04 +000061
kmoorec7a08672011-08-01 15:10:18 +000062 MapsId2_0Annotation annotation = (MapsId2_0Annotation) resourceField.getAnnotation(JPA2_0.MAPS_ID);
kmooreda90c112011-02-06 02:33:04 +000063 assertNotNull(annotation);
64 }
65
66 public void testGetValue() throws Exception {
67 ICompilationUnit cu = this.createTestMapsIdWithValue();
kmoorec7a08672011-08-01 15:10:18 +000068 JavaResourceType resourceType = buildJavaResourceType(cu);
69 JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
kmooreda90c112011-02-06 02:33:04 +000070
kmoorec7a08672011-08-01 15:10:18 +000071 MapsId2_0Annotation annotation = (MapsId2_0Annotation) resourceField.getAnnotation(JPA2_0.MAPS_ID);
kmooreda90c112011-02-06 02:33:04 +000072 assertEquals("foo", annotation.getValue());
73 }
74
75 public void testSetValue() throws Exception {
76 ICompilationUnit cu = this.createTestMapsId();
kmoorec7a08672011-08-01 15:10:18 +000077 JavaResourceType resourceType = buildJavaResourceType(cu);
78 JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
kmooreda90c112011-02-06 02:33:04 +000079
kmoorec7a08672011-08-01 15:10:18 +000080 MapsId2_0Annotation annotation = (MapsId2_0Annotation) resourceField.getAnnotation(JPA2_0.MAPS_ID);
kmooreda90c112011-02-06 02:33:04 +000081 annotation.setValue("foo");
82 assertSourceContains("@MapsId(\"foo\")", cu);
83
84 annotation.setValue(null);
85 assertSourceContains("@MapsId", cu);
86 }
87}