Skip to main content
summaryrefslogtreecommitdiffstats
blob: adebaa97b8388418e0fdd6d010ecfc1e9408c4bb (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) 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.Date;
import java.util.GregorianCalendar;
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.iterators.ArrayIterator;
import org.eclipse.jpt.jaxb.core.resource.java.JAXB;
import org.eclipse.jpt.jaxb.core.resource.java.XmlSchemaTypeAnnotation;

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

	private static final String TEST_NAME = "foo";
	private static final String TEST_NAME_2 = "bar";

	private static final String TEST_NAMESPACE = "http://www.eclipse.org/test/schema";
	private static final String TEST_NAMESPACE_2 = "http://www.eclipse.org/test/schema2";

	private static final String TEST_CLASS = GregorianCalendar.class.getSimpleName();
	private static final String TEST_CLASS_2 = Date.class.getSimpleName();
	private static final String FQ_TEST_CLASS = GregorianCalendar.class.getName();
	private static final String FQ_TEST_CLASS_2 = Date.class.getName();


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

	private ICompilationUnit createTestXmlAttributeWithSchemaTypeAndName() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_SCHEMA_TYPE);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@XmlSchemaType(name = \"" + TEST_NAME + "\")");
			}
		});
	}

	private ICompilationUnit createTestXmlAttributeWithSchemaTypeAndNamespace() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_SCHEMA_TYPE);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@XmlSchemaType(namespace = \"" + TEST_NAMESPACE + "\")");
			}
		});
	}

	private ICompilationUnit createTestXmlAttributeWithSchemaTypeAndType() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_SCHEMA_TYPE, FQ_TEST_CLASS, FQ_TEST_CLASS_2);
			}
			@Override
			public void appendIdFieldAnnotationTo(StringBuilder sb) {
				sb.append("@XmlSchemaType(type = " + TEST_CLASS + ".class)");
			}
		});
	}

	public void testName() throws Exception {
		ICompilationUnit cu = this.createTestXmlAttributeWithSchemaTypeAndName();
		JavaResourceType resourceType = this.buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = this.getField(resourceType, 0);

		XmlSchemaTypeAnnotation annotation = 
				(XmlSchemaTypeAnnotation) resourceAttribute.getAnnotation(0, JAXB.XML_SCHEMA_TYPE);
		assertTrue(annotation != null);
		assertEquals(TEST_NAME, annotation.getName());
		assertSourceContains("@XmlSchemaType(name = \"" + TEST_NAME + "\")", cu);

		annotation.setName(TEST_NAME_2);
		assertEquals(TEST_NAME_2, annotation.getName());
		assertSourceContains("@XmlSchemaType(name = \"" + TEST_NAME_2 + "\")", cu);

		annotation.setName(null);
		assertEquals(null, annotation.getName());
		assertSourceContains("@XmlSchemaType", cu);

		annotation.setName(TEST_NAME);
		assertEquals(TEST_NAME, annotation.getName());
		assertSourceContains("@XmlSchemaType(name = \"" + TEST_NAME + "\")", cu);
	}

	public void testNamespace() throws Exception {
		ICompilationUnit cu = this.createTestXmlAttributeWithSchemaTypeAndNamespace();
		JavaResourceType resourceType = this.buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = this.getField(resourceType, 0);

		XmlSchemaTypeAnnotation annotation = 
				(XmlSchemaTypeAnnotation) resourceAttribute.getAnnotation(0, JAXB.XML_SCHEMA_TYPE);
		assertTrue(annotation != null);
		assertEquals(TEST_NAMESPACE, annotation.getNamespace());
		assertSourceContains("@XmlSchemaType(namespace = \"" + TEST_NAMESPACE + "\")", cu);

		annotation.setNamespace(TEST_NAMESPACE_2);
		assertEquals(TEST_NAMESPACE_2, annotation.getNamespace());
		assertSourceContains("@XmlSchemaType(namespace = \"" + TEST_NAMESPACE_2 + "\")", cu);

		annotation.setNamespace(null);
		assertEquals(null, annotation.getNamespace());
		assertSourceContains("@XmlSchemaType", cu);

		annotation.setNamespace(TEST_NAMESPACE);
		assertEquals(TEST_NAMESPACE, annotation.getNamespace());
		assertSourceContains("@XmlSchemaType(namespace = \"" + TEST_NAMESPACE + "\")", cu);
	}

	public void testType() throws Exception {
		ICompilationUnit cu = this.createTestXmlAttributeWithSchemaTypeAndType();
		JavaResourceType resourceType = this.buildJavaResourceType(cu); 
		JavaResourceField resourceAttribute = this.getField(resourceType, 0);

		XmlSchemaTypeAnnotation annotation = 
				(XmlSchemaTypeAnnotation) resourceAttribute.getAnnotation(0, JAXB.XML_SCHEMA_TYPE);
		assertTrue(annotation != null);
		assertEquals(TEST_CLASS, annotation.getType());
		assertEquals(FQ_TEST_CLASS, annotation.getFullyQualifiedType());
		assertSourceContains("@XmlSchemaType(type = " + TEST_CLASS + ".class)", cu);

		annotation.setType(TEST_CLASS_2);
		assertEquals(TEST_CLASS_2, annotation.getType());
		assertEquals(FQ_TEST_CLASS_2, annotation.getFullyQualifiedType());
		assertSourceContains("@XmlSchemaType(type = " + TEST_CLASS_2 + ".class)", cu);

		annotation.setType(null);
		assertEquals(null, annotation.getType());
		assertEquals(null, annotation.getFullyQualifiedType());
		assertSourceContains("@XmlSchemaType", cu);

		annotation.setType(TEST_CLASS);
		assertEquals(TEST_CLASS, annotation.getType());
		assertEquals(FQ_TEST_CLASS, annotation.getFullyQualifiedType());
		assertSourceContains("@XmlSchemaType(type = " + TEST_CLASS + ".class)", cu);
	}
}

Back to the top