Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7c563f7dc8eb5bbbb6de02e470149de194784e7f (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
/*******************************************************************************
 * Copyright (c) 2010 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.core.resource.java.JavaResourcePersistentAttribute;
import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType;
import org.eclipse.jpt.jaxb.core.resource.java.JAXB;
import org.eclipse.jpt.jaxb.core.resource.java.XmlElementRefAnnotation;
import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;

@SuppressWarnings("nls")
public class XmlElementRefAnnotationTests 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 XmlElementRefAnnotationTests(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);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@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);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@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);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@XmlElementRef(type = " + XML_ELEMENT_REF_TYPE  + ".class)");
			}
		});
	}

	public void testGetName() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRefWithName();
		JavaResourcePersistentType typeResource = buildJavaTypeResource(cu); 
		JavaResourcePersistentAttribute attributeResource = typeResource.fields().next();

		XmlElementRefAnnotation xmlElementRefAnnotation = (XmlElementRefAnnotation) attributeResource.getAnnotation(JAXB.XML_ELEMENT_REF);
		assertTrue(xmlElementRefAnnotation != null);
		assertEquals(XML_ELEMENT_REF_NAME, xmlElementRefAnnotation.getName());
	}

	public void testGetNull() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRef();
		JavaResourcePersistentType typeResource = buildJavaTypeResource(cu); 
		JavaResourcePersistentAttribute attributeResource = typeResource.fields().next();

		XmlElementRefAnnotation xmlElementRefAnnotation = (XmlElementRefAnnotation) attributeResource.getAnnotation(JAXB.XML_ELEMENT_REF);
		assertTrue(xmlElementRefAnnotation != null);
		assertNull(xmlElementRefAnnotation.getName());
		assertNull(xmlElementRefAnnotation.getNamespace());
		assertNull(xmlElementRefAnnotation.getType());
	}

	public void testSetName() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRef();
		JavaResourcePersistentType typeResource = buildJavaTypeResource(cu); 
		JavaResourcePersistentAttribute attributeResource = typeResource.fields().next();

		XmlElementRefAnnotation xmlElementRefAnnotation = (XmlElementRefAnnotation) attributeResource.getAnnotation(JAXB.XML_ELEMENT_REF);
		assertNull(xmlElementRefAnnotation.getName());
		xmlElementRefAnnotation.setName(XML_ELEMENT_REF_NAME);
		assertEquals(XML_ELEMENT_REF_NAME, xmlElementRefAnnotation.getName());

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

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

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

	public void testGetNamespace() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRefWithNamespace();
		JavaResourcePersistentType typeResource = buildJavaTypeResource(cu); 
		JavaResourcePersistentAttribute attributeResource = typeResource.fields().next();

		XmlElementRefAnnotation xmlElementRefAnnotation = (XmlElementRefAnnotation) attributeResource.getAnnotation(JAXB.XML_ELEMENT_REF);
		assertTrue(xmlElementRefAnnotation != null);
		assertEquals(XML_ELEMENT_REF_NAMESPACE, xmlElementRefAnnotation.getNamespace());
	}

	public void testSetNamespace() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRef();
		JavaResourcePersistentType typeResource = buildJavaTypeResource(cu); 
		JavaResourcePersistentAttribute attributeResource = typeResource.fields().next();

		XmlElementRefAnnotation xmlElementRefAnnotation = (XmlElementRefAnnotation) attributeResource.getAnnotation(JAXB.XML_ELEMENT_REF);
		assertNull(xmlElementRefAnnotation.getNamespace());
		xmlElementRefAnnotation.setNamespace(XML_ELEMENT_REF_NAMESPACE);
		assertEquals(XML_ELEMENT_REF_NAMESPACE, xmlElementRefAnnotation.getNamespace());

		assertSourceContains("@XmlElementRef(namespace = \"" + XML_ELEMENT_REF_NAMESPACE + "\")", cu);

		xmlElementRefAnnotation.setNamespace(null);
		assertNull(xmlElementRefAnnotation.getNamespace());

		assertSourceContains("@XmlElementRef", cu);
		assertSourceDoesNotContain("@XmlElementRef(namespace = \"" + XML_ELEMENT_REF_NAMESPACE + "\")", cu);
	}

	public void testGetType() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRefWithType();
		JavaResourcePersistentType typeResource = buildJavaTypeResource(cu); 
		JavaResourcePersistentAttribute attributeResource = typeResource.fields().next();

		XmlElementRefAnnotation xmlElementRefAnnotation = (XmlElementRefAnnotation) attributeResource.getAnnotation(JAXB.XML_ELEMENT_REF);
		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();
		JavaResourcePersistentType typeResource = buildJavaTypeResource(cu); 
		JavaResourcePersistentAttribute attributeResource = typeResource.fields().next();

		XmlElementRefAnnotation xmlElementRefAnnotation = (XmlElementRefAnnotation) attributeResource.getAnnotation(JAXB.XML_ELEMENT_REF);
		assertNull(xmlElementRefAnnotation.getType());
		xmlElementRefAnnotation.setType(XML_ELEMENT_REF_TYPE);
		assertEquals(XML_ELEMENT_REF_TYPE, xmlElementRefAnnotation.getType());

		assertSourceContains("@XmlElementRef(type = " + XML_ELEMENT_REF_TYPE  + ".class", cu);

		xmlElementRefAnnotation.setType(null);
		assertNull(xmlElementRefAnnotation.getType());

		assertSourceContains("@XmlElementRef", cu);
		assertSourceDoesNotContain("@XmlElementRef(type = " + XML_ELEMENT_REF_TYPE  + ".class", cu);
	}

	public void testAddXmlElementRefAnnotation() throws Exception {
		ICompilationUnit cu = this.createTestXmlElementRefWithName();
		JavaResourcePersistentType typeResource = buildJavaTypeResource(cu); 
		JavaResourcePersistentAttribute attributeResource = typeResource.fields().next();

		XmlElementRefAnnotation xmlElementRefAnnotation = (XmlElementRefAnnotation) attributeResource.getAnnotation(JAXB.XML_ELEMENT_REF);
		assertTrue(xmlElementRefAnnotation != null);
		assertEquals(XML_ELEMENT_REF_NAME, xmlElementRefAnnotation.getName());

		XmlElementRefAnnotation xmlElementRefAnnotation2 = (XmlElementRefAnnotation) attributeResource.addAnnotation(1, JAXB.XML_ELEMENT_REF, JAXB.XML_ELEMENT_REFS);
		xmlElementRefAnnotation2.setName("Foo");
		assertSourceContains("@XmlElementRefs({@XmlElementRef(name = \"" + XML_ELEMENT_REF_NAME + "\"),@XmlElementRef(name = \"Foo\")})", cu);
	}
}

Back to the top