kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2009, 2011 Oracle. |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 3 | * 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 | *******************************************************************************/ |
| 11 | package org.eclipse.jpt.jpa.core.tests.internal.jpa2.resource.java; |
| 12 | |
| 13 | import java.util.Iterator; |
| 14 | import org.eclipse.jdt.core.ICompilationUnit; |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 15 | import org.eclipse.jpt.common.core.resource.java.JavaResourceField; |
| 16 | import org.eclipse.jpt.common.core.resource.java.JavaResourceType; |
| 17 | import org.eclipse.jpt.common.utility.internal.CollectionTools; |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 18 | import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator; |
| 19 | import org.eclipse.jpt.jpa.core.jpa2.resource.java.JPA2_0; |
| 20 | import org.eclipse.jpt.jpa.core.jpa2.resource.java.MapsId2_0Annotation; |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 21 | |
| 22 | @SuppressWarnings("nls") |
| 23 | public 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(); |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 59 | JavaResourceType resourceType = buildJavaResourceType(cu); |
| 60 | JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 61 | |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 62 | MapsId2_0Annotation annotation = (MapsId2_0Annotation) resourceField.getAnnotation(JPA2_0.MAPS_ID); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 63 | assertNotNull(annotation); |
| 64 | } |
| 65 | |
| 66 | public void testGetValue() throws Exception { |
| 67 | ICompilationUnit cu = this.createTestMapsIdWithValue(); |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 68 | JavaResourceType resourceType = buildJavaResourceType(cu); |
| 69 | JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 70 | |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 71 | MapsId2_0Annotation annotation = (MapsId2_0Annotation) resourceField.getAnnotation(JPA2_0.MAPS_ID); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 72 | assertEquals("foo", annotation.getValue()); |
| 73 | } |
| 74 | |
| 75 | public void testSetValue() throws Exception { |
| 76 | ICompilationUnit cu = this.createTestMapsId(); |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 77 | JavaResourceType resourceType = buildJavaResourceType(cu); |
| 78 | JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 79 | |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 80 | MapsId2_0Annotation annotation = (MapsId2_0Annotation) resourceField.getAnnotation(JPA2_0.MAPS_ID); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 81 | annotation.setValue("foo"); |
| 82 | assertSourceContains("@MapsId(\"foo\")", cu); |
| 83 | |
| 84 | annotation.setValue(null); |
| 85 | assertSourceContains("@MapsId", cu); |
| 86 | } |
| 87 | } |