Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4b5d3ecc84707e019cdb72a08d704c4c608d734e (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
/*******************************************************************************
 *  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.context.java;

import java.util.Iterator;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.NormalAnnotation;
import org.eclipse.jpt.common.core.resource.java.JavaResourceType;
import org.eclipse.jpt.common.core.utility.jdt.AnnotatedElement;
import org.eclipse.jpt.common.core.utility.jdt.Member;
import org.eclipse.jpt.common.core.utility.jdt.ModifiedDeclaration;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.common.utility.internal.iterator.ArrayIterator;
import org.eclipse.jpt.jaxb.core.context.JaxbClassMapping;
import org.eclipse.jpt.jaxb.core.context.XmlSeeAlso;
import org.eclipse.jpt.jaxb.core.context.java.JavaClass;
import org.eclipse.jpt.jaxb.core.resource.java.JAXB;
import org.eclipse.jpt.jaxb.core.resource.java.XmlSeeAlsoAnnotation;
import org.eclipse.jpt.jaxb.core.tests.internal.context.JaxbContextModelTestCase;


@SuppressWarnings("nls")
public class GenericJavaXmlSeeAlsoTests
		extends JaxbContextModelTestCase {
	
	public GenericJavaXmlSeeAlsoTests(String name) {
		super(name);
	}
	
	
	private ICompilationUnit createAnnotatedPersistentClassWithXmlSeeAlso() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JAXB.XML_TYPE, JAXB.XML_SEE_ALSO);
			}
			@Override
			public void appendTypeAnnotationTo(StringBuilder sb) {
				sb.append("@XmlType" + CR);
				sb.append("@XmlSeeAlso");
			}
		});
	}
	
	public void testModifyClasses() throws Exception {
		createAnnotatedPersistentClassWithXmlSeeAlso();
		JavaClass jaxbClass = (JavaClass) IterableTools.get(getContextRoot().getJavaTypes(), 0);
		JaxbClassMapping classMapping = jaxbClass.getMapping();
		XmlSeeAlso contextXmlSeeAlso = classMapping.getXmlSeeAlso();
		JavaResourceType resourceType = jaxbClass.getJavaResourceType();
		
		assertEquals(0, contextXmlSeeAlso.getClassesSize());
		
		// add a class
		contextXmlSeeAlso.addClass(0, "foo");
		XmlSeeAlsoAnnotation annotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertEquals(1, annotation.getClassesSize());
		assertTrue(IterableTools.contains(annotation.getClasses(), "foo"));
		assertFalse(IterableTools.contains(annotation.getClasses(), "bar"));
		
		// add another
		contextXmlSeeAlso.addClass(0, "bar");
		annotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertEquals(2, annotation.getClassesSize());
		assertTrue(IterableTools.contains(annotation.getClasses(), "foo"));
		assertTrue(IterableTools.contains(annotation.getClasses(), "bar"));
		
		 // remove one
		contextXmlSeeAlso.removeClass(1);
		annotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertEquals(1, annotation.getClassesSize());
		assertFalse(IterableTools.contains(annotation.getClasses(), "foo"));
		assertTrue(IterableTools.contains(annotation.getClasses(), "bar"));
		
		// remove the other
		contextXmlSeeAlso.removeClass(0);
		annotation = (XmlSeeAlsoAnnotation) resourceType.getAnnotation(JAXB.XML_SEE_ALSO);
		assertEquals(0, annotation.getClassesSize());
		assertFalse(IterableTools.contains(annotation.getClasses(), "bar"));
		assertFalse(IterableTools.contains(annotation.getClasses(), "foo"));
	}
	
	public void testUpdateClasses() throws Exception {
		createAnnotatedPersistentClassWithXmlSeeAlso();
		JavaClass jaxbClass = (JavaClass) IterableTools.get(getContextRoot().getJavaTypes(), 0);
		JaxbClassMapping classMapping = jaxbClass.getMapping();
		XmlSeeAlso contextXmlSeeAlso = classMapping.getXmlSeeAlso();
		JavaResourceType resourceType = jaxbClass.getJavaResourceType();
		AnnotatedElement annotatedElement = annotatedElement(resourceType);
		
		assertEquals(0, contextXmlSeeAlso.getClassesSize());
		
		// add a class
		annotatedElement.edit(new Member.Editor() {
			public void edit(ModifiedDeclaration declaration) {
				GenericJavaXmlSeeAlsoTests.this.addClass(declaration, 0, "foo");
			}
		});
		assertEquals(1, contextXmlSeeAlso.getClassesSize());
		assertTrue(IterableTools.contains(contextXmlSeeAlso.getClasses(), "foo"));
		assertFalse(IterableTools.contains(contextXmlSeeAlso.getClasses(), "bar"));
		
		// add another
		annotatedElement.edit(new Member.Editor() {
			public void edit(ModifiedDeclaration declaration) {
				GenericJavaXmlSeeAlsoTests.this.addClass(declaration, 0, "bar");
			}
		});
		assertEquals(2, contextXmlSeeAlso.getClassesSize());
		assertTrue(IterableTools.contains(contextXmlSeeAlso.getClasses(), "foo"));
		assertTrue(IterableTools.contains(contextXmlSeeAlso.getClasses(), "bar"));
		
		 // remove one
		annotatedElement.edit(new Member.Editor() {
			public void edit(ModifiedDeclaration declaration) {
				GenericJavaXmlSeeAlsoTests.this.removeClass(declaration, 1);
			}
		});
		assertEquals(1, contextXmlSeeAlso.getClassesSize());
		assertFalse(IterableTools.contains(contextXmlSeeAlso.getClasses(), "foo"));
		assertTrue(IterableTools.contains(contextXmlSeeAlso.getClasses(), "bar"));
		
		// remove the other
		annotatedElement.edit(new Member.Editor() {
			public void edit(ModifiedDeclaration declaration) {
				GenericJavaXmlSeeAlsoTests.this.removeClass(declaration, 0);
			}
		});
		assertEquals(0, contextXmlSeeAlso.getClassesSize());
		assertFalse(IterableTools.contains(contextXmlSeeAlso.getClasses(), "bar"));
		assertFalse(IterableTools.contains(contextXmlSeeAlso.getClasses(), "foo"));
	}
	
	protected void addClass(ModifiedDeclaration declaration, int index, String clazz) {
		addArrayElement(declaration, JAXB.XML_SEE_ALSO, index, JAXB.XML_SEE_ALSO__VALUE, newTypeLiteral(declaration.getAst(), clazz));		
	}
	
	protected void removeClass(ModifiedDeclaration declaration, int index) {
		removeArrayElement((NormalAnnotation) getXmlSeeAlsoAnnotation(declaration), JAXB.XML_SEE_ALSO__VALUE, index);
	}
	
	protected Annotation getXmlSeeAlsoAnnotation(ModifiedDeclaration declaration) {
		return declaration.getAnnotationNamed(JAXB.XML_SEE_ALSO);
	}
}

Back to the top