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

import java.util.Iterator;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator;
import org.eclipse.jpt.jaxb.core.resource.java.JAXB;
import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceAttribute;
import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceField;
import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceType;
import org.eclipse.jpt.jaxb.core.resource.java.XmlElementRefAnnotation;
import org.eclipse.jpt.jaxb.core.resource.java.XmlElementRefsAnnotation;


public class XmlElementRefsAnnotationTests
		extends JaxbJavaResourceModelTestCase {
	
	private static final String XML_ELEMENT_REF_NAME = "elementName";
	private static final String XML_ELEMENT_REF_NAMESPACE = "XmlElementRefNamespace";
	private static final String XML_ELEMENT_REF_TYPE = "String";
	
	
	public XmlElementRefsAnnotationTests(String name) {
		super(name);
	}
	
	
	private ICompilationUnit createTestXmlElementRef() throws Exception {
		
		return this.createTestType(new DefaultAnnotationWriter() {
			
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_ELEMENT_REF, JAXB.XML_ELEMENT_REFS);
			}
			
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@XmlElementRefs(@XmlElementRef)");
			}
		});
	}
	
	private ICompilationUnit createTestXmlElementRefWithName() throws Exception {
		return this.createTestXmlElementRefWithStringElement("name", XML_ELEMENT_REF_NAME);
	}
	
	private ICompilationUnit createTestXmlElementRefWithNamespace() throws Exception {
		return this.createTestXmlElementRefWithStringElement("namespace", XML_ELEMENT_REF_NAMESPACE);
	}
	
	private ICompilationUnit createTestXmlElementRefWithStringElement(final String element, final String value) throws Exception {
		
		return this.createTestType(new DefaultAnnotationWriter() {
			
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_ELEMENT_REF, JAXB.XML_ELEMENT_REFS);
			}
			
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@XmlElementRefs(@XmlElementRef(" + element + " = \"" + value + "\"))");
			}
		});
	}
	
	private ICompilationUnit createTestXmlElementRefWithType() throws Exception {
		
		return this.createTestType(new DefaultAnnotationWriter() {
			
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_ELEMENT_REF, JAXB.XML_ELEMENT_REFS);
			}
			
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@XmlElementRefs(@XmlElementRef(type = " + XML_ELEMENT_REF_TYPE  + ".class))");
			}
		});
	}
	
	private ICompilationUnit createTestXmlElementRefWithAll() throws Exception {
		
		return this.createTestType(new DefaultAnnotationWriter() {
			
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_ELEMENT_REF, JAXB.XML_ELEMENT_REFS);
			}
			
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@XmlElementRefs(@XmlElementRef(name = \"" + XML_ELEMENT_REF_NAME + "\", namespace = \"" + XML_ELEMENT_REF_NAMESPACE + "\", type = " + XML_ELEMENT_REF_TYPE  + ".class))");
			}
		});
	}
	
	
	private XmlElementRefAnnotation getXmlElementRefAnnotation(JavaResourceAttribute resourceAttribute) {
		XmlElementRefsAnnotation refsAnnotation 
				= (XmlElementRefsAnnotation) resourceAttribute.getAnnotation(JAXB.XML_ELEMENT_REFS);
		return refsAnnotation.xmlElementRefAt(0);
	}
	
	private XmlElementRefAnnotation addXmlElementRefAnnotation(int index, JavaResourceAttribute resourceAttribute) {
		XmlElementRefsAnnotation refsAnnotation 
				= (XmlElementRefsAnnotation) resourceAttribute.getAnnotation(JAXB.XML_ELEMENT_REFS);
		return refsAnnotation.addXmlElementRef(index);
	}
	
	private void removeXmlElementRefAnnotation(int index, JavaResourceAttribute resourceAttribute) {
		XmlElementRefsAnnotation refsAnnotation 
				= (XmlElementRefsAnnotation) resourceAttribute.getAnnotation(JAXB.XML_ELEMENT_REFS);
		refsAnnotation.removeXmlElementRef(index);
	}
	
	public void testGetNull() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRef();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = getField(resourceType, 0);
		XmlElementRefAnnotation xmlElementRefAnnotation = getXmlElementRefAnnotation(resourceAttribute);
		
		assertTrue(xmlElementRefAnnotation != null);
		assertNull(xmlElementRefAnnotation.getName());
		assertNull(xmlElementRefAnnotation.getNamespace());
		assertNull(xmlElementRefAnnotation.getType());
	}

	public void testGetName() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRefWithName();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = getField(resourceType, 0);
		XmlElementRefAnnotation xmlElementRefAnnotation = getXmlElementRefAnnotation(resourceAttribute);
		
		assertTrue(xmlElementRefAnnotation != null);
		assertEquals(XML_ELEMENT_REF_NAME, xmlElementRefAnnotation.getName());
	}

	public void testSetName() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRef();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = getField(resourceType, 0);
		XmlElementRefAnnotation xmlElementRefAnnotation = getXmlElementRefAnnotation(resourceAttribute);
		
		assertNull(xmlElementRefAnnotation.getName());
		
		xmlElementRefAnnotation.setName(XML_ELEMENT_REF_NAME);
		assertEquals(XML_ELEMENT_REF_NAME, xmlElementRefAnnotation.getName());

		assertSourceContains("@XmlElementRefs(@XmlElementRef(name = \"" + XML_ELEMENT_REF_NAME + "\"))", cu);

		xmlElementRefAnnotation.setName(null);
		assertNull(xmlElementRefAnnotation.getName());

		assertSourceContains("@XmlElementRefs(@XmlElementRef)", cu);
	}
	
	public void testGetNamespace() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRefWithNamespace();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = getField(resourceType, 0);
		XmlElementRefAnnotation xmlElementRefAnnotation = getXmlElementRefAnnotation(resourceAttribute);
		
		assertTrue(xmlElementRefAnnotation != null);
		assertEquals(XML_ELEMENT_REF_NAMESPACE, xmlElementRefAnnotation.getNamespace());
	}
	
	public void testSetNamespace() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRef();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = getField(resourceType, 0);
		XmlElementRefAnnotation xmlElementRefAnnotation = getXmlElementRefAnnotation(resourceAttribute);
		
		assertNull(xmlElementRefAnnotation.getNamespace());
		
		xmlElementRefAnnotation.setNamespace(XML_ELEMENT_REF_NAMESPACE);
		
		assertEquals(XML_ELEMENT_REF_NAMESPACE, xmlElementRefAnnotation.getNamespace());
		assertSourceContains("@XmlElementRefs(@XmlElementRef(namespace = \"" + XML_ELEMENT_REF_NAMESPACE + "\"))", cu);
		
		xmlElementRefAnnotation.setNamespace(null);
		
		assertNull(xmlElementRefAnnotation.getNamespace());
		assertSourceContains("@XmlElementRefs(@XmlElementRef)", cu);
	}
	
	public void testGetType() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRefWithType();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = getField(resourceType, 0);
		XmlElementRefAnnotation xmlElementRefAnnotation = getXmlElementRefAnnotation(resourceAttribute);
		
		assertTrue(xmlElementRefAnnotation != null);
		assertEquals(XML_ELEMENT_REF_TYPE, xmlElementRefAnnotation.getType());
		assertEquals("java.lang." + XML_ELEMENT_REF_TYPE, xmlElementRefAnnotation.getFullyQualifiedTypeName());
	}
	
	public void testSetType() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRef();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = getField(resourceType, 0);
		XmlElementRefAnnotation xmlElementRefAnnotation = getXmlElementRefAnnotation(resourceAttribute);
		
		assertNull(xmlElementRefAnnotation.getType());
		
		xmlElementRefAnnotation.setType(XML_ELEMENT_REF_TYPE);
		
		assertEquals(XML_ELEMENT_REF_TYPE, xmlElementRefAnnotation.getType());
		assertSourceContains("@XmlElementRefs(@XmlElementRef(type = " + XML_ELEMENT_REF_TYPE  + ".class))", cu);
		
		xmlElementRefAnnotation.setType(null);
		
		assertNull(xmlElementRefAnnotation.getType());
		assertSourceContains("@XmlElementRefs(@XmlElementRef)", cu);
	}
	
	public void testAddXmlElementRef() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRefWithAll();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = getField(resourceType, 0);
		addXmlElementRefAnnotation(1, resourceAttribute);
		XmlElementRefsAnnotation refsAnnotation = (XmlElementRefsAnnotation) resourceAttribute.getAnnotation(JAXB.XML_ELEMENT_REFS);
		
		assertSourceContains(
				"@XmlElementRefs({@XmlElementRef(name = \"" + XML_ELEMENT_REF_NAME 
						+ "\", namespace = \"" + XML_ELEMENT_REF_NAMESPACE
						+ "\", type = " + XML_ELEMENT_REF_TYPE 
						+ ".class),@XmlElementRef})", cu);
		assertNull(resourceAttribute.getAnnotation(JAXB.XML_ELEMENT_REF));
		assertNotNull(refsAnnotation);
		assertEquals(2, refsAnnotation.getXmlElementRefsSize());
	}
	
	public void testAddXmlElementRefToBeginningOfList() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRefWithAll();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = getField(resourceType, 0);
		addXmlElementRefAnnotation(1, resourceAttribute);
		XmlElementRefsAnnotation refsAnnotation = (XmlElementRefsAnnotation) resourceAttribute.getAnnotation(JAXB.XML_ELEMENT_REFS);
		
		assertSourceContains(
				"@XmlElementRefs({@XmlElementRef(name = \"" + XML_ELEMENT_REF_NAME 
						+ "\", namespace = \"" + XML_ELEMENT_REF_NAMESPACE
						+ "\", type = " + XML_ELEMENT_REF_TYPE 
						+ ".class),@XmlElementRef})", cu);
				
		addXmlElementRefAnnotation(0, resourceAttribute);
		
		assertSourceContains(
				"@XmlElementRefs({@XmlElementRef,@XmlElementRef(name = \"" + XML_ELEMENT_REF_NAME 
						+ "\", namespace = \"" + XML_ELEMENT_REF_NAMESPACE
						+ "\", type = " + XML_ELEMENT_REF_TYPE 
						+ ".class), @XmlElementRef})", cu);
		
		assertNull(resourceAttribute.getAnnotation(JAXB.XML_ELEMENT_REF));
		assertNotNull(refsAnnotation);
		assertEquals(3, refsAnnotation.getXmlElementRefsSize());
	}
	
	public void testRemoveXmlElementRef() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRefWithAll();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = getField(resourceType, 0);
		addXmlElementRefAnnotation(1, resourceAttribute);
		
		assertSourceContains(
				"@XmlElementRefs({@XmlElementRef(name = \"" + XML_ELEMENT_REF_NAME 
						+ "\", namespace = \"" + XML_ELEMENT_REF_NAMESPACE
						+ "\", type = " + XML_ELEMENT_REF_TYPE 
						+ ".class),@XmlElementRef})", cu);
		
		removeXmlElementRefAnnotation(1, resourceAttribute);
		
		assertSourceContains(
				"@XmlElementRef(name = \"" + XML_ELEMENT_REF_NAME 
						+ "\", namespace = \"" + XML_ELEMENT_REF_NAMESPACE
						+ "\", type = " + XML_ELEMENT_REF_TYPE 
						+ ".class)", cu);
	}
}

Back to the top