Skip to main content
summaryrefslogtreecommitdiffstats
blob: e27aa9a0ecc3f10279313344871a3b604ce2edbe (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
/*******************************************************************************
 * Copyright (c) 2008, 2011 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.eclipselink.core.tests.internal.resource.java;

import java.util.Iterator;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jpt.common.core.resource.java.JavaResourceField;
import org.eclipse.jpt.common.core.resource.java.JavaResourceType;
import org.eclipse.jpt.common.utility.internal.CollectionTools;
import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator;
import org.eclipse.jpt.jpa.eclipselink.core.resource.java.EclipseLink;
import org.eclipse.jpt.jpa.eclipselink.core.resource.java.EclipseLinkStructConverterAnnotation;

@SuppressWarnings("nls")
public class StructConverterAnnotationTests extends EclipseLinkJavaResourceModelTestCase {
	
	public StructConverterAnnotationTests(String name) {
		super(name);
	}

	private ICompilationUnit createTestStructConverter() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(EclipseLink.STRUCT_CONVERTER);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@StructConverter");
			}
		});
	}
	
	private ICompilationUnit createTestStructConverterWithConverter() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(EclipseLink.STRUCT_CONVERTER);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@StructConverter(converter=\"Foo\")");
			}
		});
	}
	
	private ICompilationUnit createTestStructConverterWithName() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(EclipseLink.STRUCT_CONVERTER);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@StructConverter(name=\"bar\")");
			}
		});
	}

	public void testStructConverterAnnotation() throws Exception {
		ICompilationUnit cu = this.createTestStructConverter();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
		
		assertNotNull(resourceField.getAnnotation(0, EclipseLink.STRUCT_CONVERTER));
		
		resourceField.removeAnnotation(0, EclipseLink.STRUCT_CONVERTER);
		assertNull(resourceField.getAnnotation(0, EclipseLink.STRUCT_CONVERTER));
		
		resourceField.addAnnotation(0, EclipseLink.STRUCT_CONVERTER);
		assertNotNull(resourceField.getAnnotation(0, EclipseLink.STRUCT_CONVERTER));
	}

	public void testGetConverter() throws Exception {
		ICompilationUnit cu = this.createTestStructConverterWithConverter();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
		
		EclipseLinkStructConverterAnnotation converter = (EclipseLinkStructConverterAnnotation) resourceField.getAnnotation(0, EclipseLink.STRUCT_CONVERTER);
		assertEquals("Foo", converter.getConverter());
	}

	public void testSetConverter() throws Exception {
		ICompilationUnit cu = this.createTestStructConverterWithConverter();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
		
		EclipseLinkStructConverterAnnotation converter = (EclipseLinkStructConverterAnnotation) resourceField.getAnnotation(0, EclipseLink.STRUCT_CONVERTER);
		assertEquals("Foo", converter.getConverter());
		
		converter.setConverter("Bar");
		assertEquals("Bar", converter.getConverter());
		
		assertSourceContains("@StructConverter(converter=\"Bar\")", cu);
	}
	
	public void testSetConverterNull() throws Exception {
		ICompilationUnit cu = this.createTestStructConverterWithConverter();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
		
		EclipseLinkStructConverterAnnotation converter = (EclipseLinkStructConverterAnnotation) resourceField.getAnnotation(0, EclipseLink.STRUCT_CONVERTER);
		assertEquals("Foo", converter.getConverter());
		
		converter.setConverter(null);
		assertNull(converter.getConverter());
		
		assertSourceContains("@StructConverter", cu);
		assertSourceDoesNotContain("converter", cu);
	}
	
	public void testGetName() throws Exception {
		ICompilationUnit cu = this.createTestStructConverterWithName();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
		
		EclipseLinkStructConverterAnnotation converter = (EclipseLinkStructConverterAnnotation) resourceField.getAnnotation(0, EclipseLink.STRUCT_CONVERTER);
		assertEquals("bar", converter.getName());
	}

	public void testSetName() throws Exception {
		ICompilationUnit cu = this.createTestStructConverterWithName();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
		
		EclipseLinkStructConverterAnnotation converter = (EclipseLinkStructConverterAnnotation) resourceField.getAnnotation(0, EclipseLink.STRUCT_CONVERTER);
		assertEquals("bar", converter.getName());
		
		converter.setName("foo");
		assertEquals("foo", converter.getName());
		
		assertSourceContains("@StructConverter(name=\"foo\")", cu);
	}
	
	public void testSetNameNull() throws Exception {
		ICompilationUnit cu = this.createTestStructConverterWithName();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
		
		EclipseLinkStructConverterAnnotation converter = (EclipseLinkStructConverterAnnotation) resourceField.getAnnotation(0, EclipseLink.STRUCT_CONVERTER);
		assertEquals("bar", converter.getName());
		
		converter.setName(null);
		assertNull(converter.getName());
		
		assertSourceContains("@StructConverter", cu);
		assertSourceDoesNotContain("name=", cu);
	}
}

Back to the top