Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0e014b66b5e863ba30604fedd0992495263c2fe9 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************************
 * Copyright (c) 2006, 2007 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.core.tests.internal.jdtutility;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.MemberValuePair;
import org.eclipse.jdt.core.dom.NormalAnnotation;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jpt.core.internal.jdtutility.AnnotationAdapter;
import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationAdapter;
import org.eclipse.jpt.core.internal.jdtutility.Member;
import org.eclipse.jpt.core.internal.jdtutility.MemberAnnotationAdapter;
import org.eclipse.jpt.core.internal.jdtutility.ModifiedDeclaration;
import org.eclipse.jpt.core.internal.jdtutility.SimpleDeclarationAnnotationAdapter;

public class SimpleDeclarationAnnotationAdapterTests extends AnnotationTestCase {

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

	private void createAnnotation(String annotationName) throws Exception {
		this.javaProject.createType("annot", annotationName + ".java", "public @interface " + annotationName + " {}");
	}

	public void testGetAnnotation1() throws Exception {
		this.createAnnotation("Foo");
		this.createTestType("@annot.Foo");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
		AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
		Annotation annotation = aa.getAnnotation();
		assertNotNull(annotation);
		assertEquals("annot.Foo", annotation.getTypeName().getFullyQualifiedName());
		assertTrue(annotation.isMarkerAnnotation());
	}

	public void testGetAnnotation2() throws Exception {
		this.createAnnotation("Foo");
		this.createTestType("@annot.Foo(1) @annot.Foo(2)");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
		AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
		Annotation annotation = aa.getAnnotation();
		assertNotNull(annotation);
		assertEquals("annot.Foo", annotation.getTypeName().getFullyQualifiedName());
		assertTrue(annotation.isSingleMemberAnnotation());
		Expression value = ((SingleMemberAnnotation) annotation).getValue();
		assertEquals(ASTNode.NUMBER_LITERAL, value.getNodeType());
		assertEquals("1", ((NumberLiteral) value).getToken());
	}

	public void testGetAnnotation3() throws Exception {
		this.createAnnotation("Foo");
		this.createTestType("annot.Foo", "@Foo");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
		AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
		Annotation annotation = aa.getAnnotation();
		assertNotNull(annotation);
		assertEquals("Foo", annotation.getTypeName().getFullyQualifiedName());
		assertTrue(annotation.isMarkerAnnotation());
	}

	public void testGetAnnotationNull1() throws Exception {
		this.createAnnotation("Foo");
		this.createTestType();
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
		AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
		Annotation annotation = aa.getAnnotation();
		assertNull(annotation);
	}

	public void testGetAnnotationNull2() throws Exception {
		this.createAnnotation("Foo");
		this.createAnnotation("Fop");
		this.createTestType("@annot.Fop");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
		AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
		Annotation annotation = aa.getAnnotation();
		assertNull(annotation);
		this.assertSourceContains("@annot.Fop");
	}

	public void testGetAnnotationNull3() throws Exception {
		this.createAnnotation("Foo");
		this.createTestType("@annot.Foo");
		// un-qualified name
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("Foo");
		AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
		Annotation annotation = aa.getAnnotation();
		assertNull(annotation);
		this.assertSourceContains("@annot.Foo");
	}

	public void testRemoveAnnotation1() throws Exception {
		this.createAnnotation("Foo");
		this.createTestType("@annot.Foo");
		this.assertSourceContains("@annot.Foo");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
		AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
		Annotation annotation = aa.getAnnotation();
		assertNotNull(annotation);

		aa.removeAnnotation();
		this.assertSourceDoesNotContain("@annot.Foo");
	}

	public void testRemoveAnnotation2() throws Exception {
		this.createAnnotation("Foo");
		this.createTestType("@annot.Foo(1) @annot.Foo(2)");
		this.assertSourceContains("@annot.Foo(1) @annot.Foo(2)");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
		AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
		Annotation annotation = aa.getAnnotation();
		assertNotNull(annotation);

		aa.removeAnnotation();
		this.assertSourceDoesNotContain("@annot.Foo(1)");
		this.assertSourceContains("@annot.Foo(2)");
	}

	public void testNewMarkerAnnotation1() throws Exception {
		this.createAnnotation("Foo");
		this.createTestType();
		this.assertSourceDoesNotContain("@annot.Foo");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
		AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
		Annotation annotation = aa.getAnnotation();
		assertNull(annotation);

		aa.newMarkerAnnotation();
		this.assertSourceContains("import annot.Foo;");
		this.assertSourceContains("@Foo");
	}

	public void testNewMarkerAnnotation2() throws Exception {
		this.createAnnotation("Foo");
		this.createTestType("@annot.Foo(88)");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
		AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
		Annotation annotation = aa.getAnnotation();
		assertNotNull(annotation);

		aa.newMarkerAnnotation();
		this.assertSourceContains("import annot.Foo;");
		this.assertSourceContains("@Foo");
		this.assertSourceDoesNotContain("@annot.Foo(88)");
	}

	public void testNewSingleMemberAnnotation() throws Exception {
		this.createAnnotation("Foo");
		this.createTestType();
		this.assertSourceDoesNotContain("@Foo");
		this.idField().edit(new Member.Editor() {
			public void edit(ModifiedDeclaration declaration) {
				SimpleDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation(declaration);
			}
		});
		this.assertSourceContains("import annot.Foo;");
		this.assertSourceContains("@Foo(\"test string literal\")");
	}

	void editNewSingleMemberAnnotation(ModifiedDeclaration declaration) {
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
		SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
		assertNull(annotation);
		annotation = daa.newSingleMemberAnnotation(declaration);
		StringLiteral stringLiteral = annotation.getAST().newStringLiteral();
		stringLiteral.setLiteralValue("test string literal");
		annotation.setValue(stringLiteral);
	}

	public void testNewNormalAnnotation() throws Exception {
		this.createAnnotation("Foo");
		this.createTestType();
		this.assertSourceDoesNotContain("@Foo");
		this.idField().edit(new Member.Editor() {
			public void edit(ModifiedDeclaration declaration) {
				SimpleDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation(declaration);
			}
		});
		this.assertSourceContains("import annot.Foo;");
		this.assertSourceContains("@Foo(bar=\"test string literal\")");
	}

	void editNewNormalAnnotation(ModifiedDeclaration declaration) {
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
		NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
		assertNull(annotation);
		annotation = daa.newNormalAnnotation(declaration);
		MemberValuePair mvp = this.newMemberValuePair(annotation.getAST(), "bar", "test string literal");
		this.values(annotation).add(mvp);
	}

}

Back to the top