Skip to main content
summaryrefslogtreecommitdiffstats
blob: 079f2f59f62df3f5771fbc86f602afcfd8898109 (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
/*******************************************************************************
 * 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.Signature;
import org.eclipse.jpt.core.internal.jdtutility.ConversionDeclarationAnnotationElementAdapter;
import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationAdapter;
import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationElementAdapter;
import org.eclipse.jpt.core.internal.jdtutility.FieldAttribute;
import org.eclipse.jpt.core.internal.jdtutility.JDTTools;
import org.eclipse.jpt.core.internal.jdtutility.SimpleDeclarationAnnotationAdapter;

public class JDTToolsTests extends AnnotationTestCase {

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

	public void testResolveSignature1() throws Exception {
		this.verifyResolveSignature("String", "java.lang.String");
	}

	public void testResolveSignature2() throws Exception {
		this.verifyResolveSignature("List", null);
	}

	public void testResolveSignature3() throws Exception {
		this.verifyResolveSignature("int", "int");
	}

	public void testResolveSignature4() throws Exception {
		this.verifyResolveSignature("void", "void");
	}

	public void testResolveSignature5() throws Exception {
		this.verifyResolveSignature("int[]", "int[]");
	}

	public void testResolveSignature6() throws Exception {
		this.verifyResolveSignature("String[]", "java.lang.String[]");
	}

	public void testResolveSignature7() throws Exception {
		this.verifyResolveSignature("java.util.List[][][]", "java.util.List[][][]");
	}

	public void testResolveSignature8a() throws Exception {
		// inner class
		this.verifyResolveSignature("java.util.Map.Entry", "java.util.Map.Entry");
	}

	public void testResolveSignature8b() throws Exception {
		// inner class
		this.createTestType("java.util.Map", "");
		this.verifyResolveSignature2("Map.Entry", "java.util.Map.Entry");
	}

	public void testResolveSignature8c() throws Exception {
		// inner class
		this.createTestType("java.util.Map.Entry", "");
		this.verifyResolveSignature2("Entry", "java.util.Map.Entry");
	}

	public void testResolveSignature9() throws Exception {
		// inner class
		this.verifyResolveSignature("Character.Subset", "java.lang.Character.Subset");
	}

	public void testResolveSignature10() throws Exception {
		// generic type
		this.verifyResolveSignature("java.util.List<java.lang.String>", "java.util.List");  // ????
	}

	public void testResolveSignature11() throws Exception {
		// annotation
		this.createTestType("java.lang.annotation.Target", "");
		this.verifyResolveSignature2("Target", "java.lang.annotation.Target");
	}

	public void testResolveSignature12() throws Exception {
		this.createTestType("java.lang.annotation.ElementType", "");
		this.verifyResolveSignature2("ElementType", "java.lang.annotation.ElementType");
	}

	private void verifyResolveSignature(String unresolvedTypeName, String expected) throws Exception {
		this.createTestType();
		this.verifyResolveSignature2(unresolvedTypeName, expected);
	}

	private void verifyResolveSignature2(String unresolvedTypeName, String expected) throws Exception {
		String signature = Signature.createTypeSignature(unresolvedTypeName, false);
		String actual = JDTTools.resolveSignature(signature, this.jdtType());
		assertEquals(expected, actual);
	}


	private void createEnumAndMembers(String enumName, String enumBody) throws Exception {
		this.javaProject.createType("enums", enumName + ".java", "public enum " + enumName + " { " + enumBody + " }");
	}

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

	public void testResolveEnum1() throws Exception {
		this.createEnumAndMembers("TestEnum", "FOO, BAR, BAZ");
		this.createAnnotationAndMembers("TestAnnotation", "TestEnum foo();");

		this.createTestType("@annot.TestAnnotation(foo=enums.TestEnum.BAZ)");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.TestAnnotation");
		DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "foo");
		FieldAttribute field = this.idField();

		String actual = JDTTools.resolveEnum(this.jdtType().getCompilationUnit(), field.annotationElementExpression(daea));
		assertEquals("enums.TestEnum.BAZ", actual);
	}

	public void testResolveEnum2() throws Exception {
		this.createEnumAndMembers("TestEnum", "FOO, BAR, BAZ");
		this.createAnnotationAndMembers("TestAnnotation", "TestEnum foo();");

		this.createTestType("static enums.TestEnum.BAZ", "@annot.TestAnnotation(foo=BAZ)");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.TestAnnotation");
		DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "foo");
		FieldAttribute field = this.idField();

		String actual = JDTTools.resolveEnum(this.jdtType().getCompilationUnit(), field.annotationElementExpression(daea));
		assertEquals("enums.TestEnum.BAZ", actual);
	}

	public void testResolveEnum3() throws Exception {
		this.createEnumAndMembers("TestEnum", "FOO, BAR, BAZ");
		this.createAnnotationAndMembers("TestAnnotation", "TestEnum foo();");

		this.createTestType("static enums.TestEnum.*", "@annot.TestAnnotation(foo=BAZ)");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.TestAnnotation");
		DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "foo");
		FieldAttribute field = this.idField();

		String actual = JDTTools.resolveEnum(this.jdtType().getCompilationUnit(), field.annotationElementExpression(daea));
		assertEquals("enums.TestEnum.BAZ", actual);
	}

	public void testResolveEnum4() throws Exception {
		this.createEnumAndMembers("TestEnum", "FOO, BAR, BAZ");
		this.createAnnotationAndMembers("TestAnnotation", "TestEnum foo();");

		this.createTestType("enums.TestEnum", "@annot.TestAnnotation(foo=TestEnum.BAZ)");
		DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.TestAnnotation");
		DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "foo");
		FieldAttribute field = this.idField();

		String actual = JDTTools.resolveEnum(this.jdtType().getCompilationUnit(), field.annotationElementExpression(daea));
		assertEquals("enums.TestEnum.BAZ", actual);
	}

}

Back to the top