Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2cbd003b11bed0ff8e69c070c70e17203d81aa32 (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
/*******************************************************************************
 * 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.jaxb.core.resource.java.JAXB;
import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceAttribute;
import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceType;
import org.eclipse.jpt.jaxb.core.resource.java.XmlAnyElementAnnotation;
import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;

@SuppressWarnings("nls")
public class XmlAnyElementAnnotationTests extends JaxbJavaResourceModelTestCase {

	private static final String XML_ANY_ELEMENT_VALUE = "String";

	public XmlAnyElementAnnotationTests(String name) {
		super(name);
	}

	private ICompilationUnit createTestXmlAnyElement() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_ANY_ELEMENT);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@XmlAnyElement");
			}
		});
	}

	private ICompilationUnit createTestXmlAnyElementWithBooleanElement(final String booleanElement) throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_ANY_ELEMENT);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@XmlAnyElement(" + booleanElement + " = true)");
			}
		});
	}

	private ICompilationUnit createTestXmlAnyElementWithValue() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_ANY_ELEMENT);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@XmlAnyElement(value = " + XML_ANY_ELEMENT_VALUE  + ".class)");
			}
		});
	}

	public void testGetNull() throws Exception {
		ICompilationUnit cu = this.createTestXmlAnyElement();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceAttribute resourceAttribute = getField(resourceType, 0);

		XmlAnyElementAnnotation xmlAnyElementAnnotation = (XmlAnyElementAnnotation) resourceAttribute.getAnnotation(JAXB.XML_ANY_ELEMENT);
		assertTrue(xmlAnyElementAnnotation != null);
		assertNull(xmlAnyElementAnnotation.getLax());
		assertNull(xmlAnyElementAnnotation.getValue());
	}

	public void testGetLax() throws Exception {
		ICompilationUnit cu = this.createTestXmlAnyElementWithBooleanElement("lax");
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceAttribute resourceAttribute = getField(resourceType, 0);
		XmlAnyElementAnnotation xmlAnyElementAnnotation = (XmlAnyElementAnnotation) resourceAttribute.getAnnotation(JAXB.XML_ANY_ELEMENT);

		assertEquals(Boolean.TRUE, xmlAnyElementAnnotation.getLax());
	}

	public void testSetLax() throws Exception {
		ICompilationUnit cu = this.createTestXmlAnyElement();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceAttribute resourceAttribute = getField(resourceType, 0);
		XmlAnyElementAnnotation xmlAnyElementAnnotation = (XmlAnyElementAnnotation) resourceAttribute.getAnnotation(JAXB.XML_ANY_ELEMENT);

		assertNotNull(xmlAnyElementAnnotation);
		assertNull(xmlAnyElementAnnotation.getLax());

		xmlAnyElementAnnotation.setLax(Boolean.FALSE);
		assertEquals(Boolean.FALSE, xmlAnyElementAnnotation.getLax());

		assertSourceContains("@XmlAnyElement(lax = false)", cu);

		xmlAnyElementAnnotation.setLax(null);
		assertSourceContains("@XmlAnyElement", cu);
		assertSourceDoesNotContain("lax", cu);
	}

	public void testGetValue() throws Exception {
		ICompilationUnit cu = this.createTestXmlAnyElementWithValue();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceAttribute resourceAttribute = getField(resourceType, 0);

		XmlAnyElementAnnotation xmlAnyElementAnnotation = (XmlAnyElementAnnotation) resourceAttribute.getAnnotation(JAXB.XML_ANY_ELEMENT);
		assertTrue(xmlAnyElementAnnotation != null);
		assertEquals(XML_ANY_ELEMENT_VALUE, xmlAnyElementAnnotation.getValue());
		assertEquals("java.lang." + XML_ANY_ELEMENT_VALUE, xmlAnyElementAnnotation.getFullyQualifiedValueClassName());
	}

	public void testSetValue() throws Exception {
		ICompilationUnit cu = this.createTestXmlAnyElement();
		JavaResourceType resourceType = buildJavaResourceType(cu); 
		JavaResourceAttribute resourceAttribute = getField(resourceType, 0);

		XmlAnyElementAnnotation xmlAnyElementAnnotation = (XmlAnyElementAnnotation) resourceAttribute.getAnnotation(JAXB.XML_ANY_ELEMENT);
		assertNull(xmlAnyElementAnnotation.getValue());
		xmlAnyElementAnnotation.setValue(XML_ANY_ELEMENT_VALUE);
		assertEquals(XML_ANY_ELEMENT_VALUE, xmlAnyElementAnnotation.getValue());

		assertSourceContains("@XmlAnyElement(" + XML_ANY_ELEMENT_VALUE  + ".class)", cu);

		xmlAnyElementAnnotation.setValue(null);
		assertNull(xmlAnyElementAnnotation.getValue());

		assertSourceContains("@XmlAnyElement", cu);
		assertSourceDoesNotContain("@XmlAnyElement(value = " + XML_ANY_ELEMENT_VALUE  + ".class)", cu);
	}
}

Back to the top