Skip to main content
summaryrefslogtreecommitdiffstats
blob: a3125626670f5d4cdb21a2a6e1014867c461d633 (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
/*******************************************************************************
 * 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 java.util.ListIterator;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jpt.jaxb.core.resource.java.JAXB;
import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceType;
import org.eclipse.jpt.jaxb.core.resource.java.XmlSeeAlsoAnnotation;
import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;

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


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

	private ICompilationUnit createTestXmlSeeAlso() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_SEE_ALSO);
			}
			@Override
			public void appendTypeAnnotationTo(StringBuilder sb) {
				sb.append("@XmlSeeAlso");
			}
		});
	}

	private ICompilationUnit createTestXmlSeeAlsoWithValue() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_SEE_ALSO);
			}
			@Override
			public void appendTypeAnnotationTo(StringBuilder sb) {
				sb.append("@XmlSeeAlso(value = {Foo.class, Bar.class})");
			}
		});
	}


	public void testGetNull() throws Exception {
		ICompilationUnit cu = this.createTestXmlSeeAlso();
		JavaResourceType resourceType = buildJavaResourceType(cu); 

		XmlSeeAlsoAnnotation xmlSeeAlsoAnnotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertTrue(xmlSeeAlsoAnnotation != null);
		assertEquals(0, xmlSeeAlsoAnnotation.getClassesSize());
	}

	public void testGetClasses() throws Exception {
		ICompilationUnit cu = this.createTestXmlSeeAlsoWithValue();
		JavaResourceType resourceType = buildJavaResourceType(cu); 

		XmlSeeAlsoAnnotation xmlSeeAlsoAnnotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertTrue(xmlSeeAlsoAnnotation != null);
		ListIterator<String> classes = xmlSeeAlsoAnnotation.getClasses().iterator();
		assertEquals("Foo", classes.next());
		assertEquals("Bar", classes.next());
	}

	public void testGetClassesSize() throws Exception {
		ICompilationUnit cu = this.createTestXmlSeeAlsoWithValue();
		JavaResourceType resourceType = buildJavaResourceType(cu); 

		XmlSeeAlsoAnnotation xmlSeeAlsoAnnotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertTrue(xmlSeeAlsoAnnotation != null);
		assertEquals(2, xmlSeeAlsoAnnotation.getClassesSize());
	}

	public void testAddClass() throws Exception {
		ICompilationUnit cu = this.createTestXmlSeeAlso();
		JavaResourceType resourceType = buildJavaResourceType(cu); 

		XmlSeeAlsoAnnotation xmlSeeAlsoAnnotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertTrue(xmlSeeAlsoAnnotation != null);

		xmlSeeAlsoAnnotation.addClass("Fooo");
		xmlSeeAlsoAnnotation.addClass("Barrr");

		assertSourceContains("@XmlSeeAlso({ Fooo.class, Barrr.class })", cu);
	}

	public void testAddClassIndex() throws Exception {
		ICompilationUnit cu = this.createTestXmlSeeAlso();
		JavaResourceType resourceType = buildJavaResourceType(cu); 

		XmlSeeAlsoAnnotation xmlSeeAlsoAnnotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertTrue(xmlSeeAlsoAnnotation != null);

		xmlSeeAlsoAnnotation.addClass(0, "Fooo");
		xmlSeeAlsoAnnotation.addClass(0, "Barr");
		xmlSeeAlsoAnnotation.addClass(1, "Blah");

		assertSourceContains("@XmlSeeAlso({ Barr.class, Blah.class, Fooo.class })", cu);
	}

	public void testRemoveClass() throws Exception {
		ICompilationUnit cu = this.createTestXmlSeeAlsoWithValue();
		JavaResourceType resourceType = buildJavaResourceType(cu); 

		XmlSeeAlsoAnnotation xmlSeeAlsoAnnotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertTrue(xmlSeeAlsoAnnotation != null);

		xmlSeeAlsoAnnotation.removeClass("Foo");
		assertSourceContains("@XmlSeeAlso(value = Bar.class)", cu);

		xmlSeeAlsoAnnotation.removeClass("Bar");
		assertSourceContains("@XmlSeeAlso", cu);
		assertSourceDoesNotContain("value", cu);
	}

	public void testRemoveClassIndex() throws Exception {
		ICompilationUnit cu = this.createTestXmlSeeAlsoWithValue();
		JavaResourceType resourceType = buildJavaResourceType(cu); 

		XmlSeeAlsoAnnotation xmlSeeAlsoAnnotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertTrue(xmlSeeAlsoAnnotation != null);

		xmlSeeAlsoAnnotation.removeClass(0);
		assertSourceContains("@XmlSeeAlso(value = Bar.class)", cu);

		xmlSeeAlsoAnnotation.removeClass(0);
		assertSourceContains("@XmlSeeAlso", cu);
		assertSourceDoesNotContain("value", cu);
	}

	public void testMoveClass() throws Exception {
		ICompilationUnit cu = this.createTestXmlSeeAlso();
		JavaResourceType resourceType = buildJavaResourceType(cu); 

		XmlSeeAlsoAnnotation xmlSeeAlsoAnnotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertTrue(xmlSeeAlsoAnnotation != null);

		xmlSeeAlsoAnnotation.addClass("Fooo");
		xmlSeeAlsoAnnotation.addClass("Barr");
		xmlSeeAlsoAnnotation.addClass("Blah");
		assertSourceContains("@XmlSeeAlso({ Fooo.class, Barr.class, Blah.class })", cu);

		xmlSeeAlsoAnnotation.moveClass(0, 1);
		assertSourceContains("@XmlSeeAlso({ Barr.class, Fooo.class, Blah.class })", cu);

		xmlSeeAlsoAnnotation.moveClass(2, 1);
		assertSourceContains("@XmlSeeAlso({ Barr.class, Blah.class, Fooo.class })", cu);
	}
}

Back to the top