Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 95b645e7500f382dae16963b5e5f9c9745f40284 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*******************************************************************************
 * Copyright (c) 2000, 2020 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.core.tests.model;

import junit.framework.Test;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

public class CreateMembersTests extends AbstractJavaModelTests {

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

	// Use this static initializer to specify subset for tests
	// All specified tests which do not belong to the class are skipped...
	static {
		// Names of tests to run: can be "testBugXXXX" or "BugXXXX")
//		TESTS_PREFIX = "testCombineAccessRestrictions";
//		TESTS_NAMES = new String[] {"test004"};
//		TESTS_NUMBERS = new int[] { 5, 6 };
//		TESTS_RANGE = new int[] { 21, 38 };
	}
	public static Test suite() {
		return buildModelTestSuite(CreateMembersTests.class, ALPHABETICAL_SORT);
	}
	@Override
	public void setUpSuite() throws Exception {
		super.setUpSuite();
		setUpJavaProject("CreateMembers", "14");
	}
	@Override
	public void tearDownSuite() throws Exception {
		deleteProject("CreateMembers");

		super.tearDownSuite();
	}

	public void test001() throws JavaModelException {
		ICompilationUnit compilationUnit = getCompilationUnit("CreateMembers", "src", "", "A.java");
		assertNotNull("No compilation unit", compilationUnit);
		IType[] types = compilationUnit.getTypes();
		assertNotNull("No types", types);
		assertEquals("Wrong size", 1, types.length);
		IType type = types[0];
		type.createMethod("\tpublic void foo() {\n\t\tSystem.out.println(\"Hello World\");\n\t}\n", null, true, new NullProgressMonitor());
		String expectedSource =
			"public class A {\n" +
			"\n" +
			"	public void foo() {\n" +
			"		System.out.println(\"Hello World\");\n" +
			"	}\n" +
			"}";
		assertSourceEquals("Unexpected source", expectedSource, type.getSource());
	}

	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=86906
	public void test002() throws JavaModelException {
		ICompilationUnit compilationUnit = getCompilationUnit("CreateMembers", "src", "", "E.java");
		assertNotNull("No compilation unit", compilationUnit);
		IType[] types = compilationUnit.getTypes();
		assertNotNull("No types", types);
		assertEquals("Wrong size", 1, types.length);
		IType type = types[0];
		IField sibling = type.getField("j");
		type.createField("int i;", sibling, true, null);
		String expectedSource =
			"public enum E {\n" +
			"	E1, E2;\n" +
			"	int i;\n" +
			"	int j;\n" +
			"}";
		assertSourceEquals("Unexpected source", expectedSource, type.getSource());
	}

	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=86906
	public void test003() throws JavaModelException {
		ICompilationUnit compilationUnit = getCompilationUnit("CreateMembers", "src", "", "Annot.java");
		assertNotNull("No compilation unit", compilationUnit);
		IType[] types = compilationUnit.getTypes();
		assertNotNull("No types", types);
		assertEquals("Wrong size", 1, types.length);
		IType type = types[0];
		IMethod sibling = type.getMethod("foo", new String[]{});
		type.createMethod("String bar();", sibling, true, null);
		String expectedSource =
			"public @interface Annot {\n" +
			"	String bar();\n" +
			"\n" +
			"	String foo();\n" +
			"}";
		assertSourceEquals("Unexpected source", expectedSource, type.getSource());
	}

	/*
	 * Ensures that the handle for a created method that has varargs type arguments is correct.
	 * (regression test for bug 93487 IType#findMethods fails on vararg methods)
	 */
	public void test004() throws JavaModelException {
		IType type = getCompilationUnit("/CreateMembers/src/A.java").getType("A");
		IMethod method = type.createMethod(
			"void bar(String... args) {}",
			null, // no siblings
			false, // don't force
			null // no progress monitor
		);
		assertTrue("Method should exist", method.exists());
	}

	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=95580
	public void test005() throws JavaModelException {
		ICompilationUnit compilationUnit = getCompilationUnit("CreateMembers", "src", "", "E2.java");
		assertNotNull("No compilation unit", compilationUnit);
		IType[] types = compilationUnit.getTypes();
		assertNotNull("No types", types);
		assertEquals("Wrong size", 1, types.length);
		IType type = types[0];
		type.createField("int i;", null, true, null);
		String expectedSource =
			"public enum E2 {\n" +
			"	A, B, C;\n\n" +
			"	int i;\n" +
			"}";
		assertSourceEquals("Unexpected source", expectedSource, type.getSource());
	}

	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=95580
	public void test006() throws JavaModelException {
		ICompilationUnit compilationUnit = getCompilationUnit("CreateMembers", "src", "", "E3.java");
		assertNotNull("No compilation unit", compilationUnit);
		IType[] types = compilationUnit.getTypes();
		assertNotNull("No types", types);
		assertEquals("Wrong size", 1, types.length);
		IType type = types[0];
		type.createType("class DD {}", null, true, null);
		String expectedSource =
			"public enum E3 {\n" +
			"	A, B, C;\n\n" +
			"	class DD {}\n" +
			"}";
		assertSourceEquals("Unexpected source", expectedSource, type.getSource());
	}

	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=95480
	public void test007() throws Exception {
		JavaModelException expected = null;
		try {
			IType type = getCompilationUnit("CreateMembers/src/E.java").getType("E");
			type.createType("class Member {}", type.getField("E1"), false/*don't force*/, null/*no progress*/);
		} catch (JavaModelException e) {
			expected = e;
		}
		assertExceptionEquals(
			"Unexpected exception",
			"Invalid sibling: E1 [in E [in E.java [in <default> [in src [in CreateMembers]]]]]",
			expected);
	}
	public void testBug563622_1() throws JavaModelException {
		IJavaProject javaProject = getJavaProject("CreateMembers");
		String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true);
		javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
		try {
			ICompilationUnit compilationUnit = getCompilationUnit("CreateMembers", "src", "", "Outer.java");
			assertNotNull("No compilation unit", compilationUnit);
			IType[] types = compilationUnit.getTypes();
			assertNotNull("No types", types);
			assertEquals("Wrong size", 1, types.length);
			IType type = types[0];
			type.createType("record Point() {}", null, true, null);
			String expectedSource =
					"public class Outer {\n" +
					"\n" +
					"	record Point() {}\n" +
					"}";
			assertSourceEquals("Unexpected source", expectedSource, type.getSource());
		} finally {
			javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
		}
	}
	public void testBug563622_2() throws JavaModelException {
		IJavaProject javaProject = getJavaProject("CreateMembers");
		String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true);
		javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.DISABLED);
		JavaModelException expected = null;
		try {
			ICompilationUnit compilationUnit = getCompilationUnit("CreateMembers", "src", "", "Outer.java");
			assertNotNull("No compilation unit", compilationUnit);
			IType[] types = compilationUnit.getTypes();
			assertNotNull("No types", types);
			assertEquals("Wrong size", 1, types.length);
			IType type = types[0];
			try {
				type.createType("record Point() {}", null, true, null);
			} catch (JavaModelException e) {
				expected = e;
			}
			assertExceptionEquals(
					"Unexpected exception",
					"Invalid contents specified",
					expected);
		} finally {
			javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
		}
	}

	public void testBug565015_1() throws JavaModelException {
		IJavaProject javaProject = getJavaProject("CreateMembers");
		String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true);
		javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
		try {
			ICompilationUnit compilationUnit = getCompilationUnit("CreateMembers", "src", "", "OuterRecord.java");
			assertNotNull("No compilation unit", compilationUnit);
			IType[] types = compilationUnit.getTypes();
			assertNotNull("No types", types);
			assertEquals("Wrong size", 1, types.length);
			IType type = types[0];
			type.createType("record Point() {}", null, true, null);
			String expectedSource =
					"public record OuterRecord() {\n" +
					"\n" +
					"	record Point() {}\n" +
					"}";
			assertSourceEquals("Unexpected source", expectedSource, type.getSource());
		} finally {
			javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
		}
	}

	public void testBug565015_2() throws JavaModelException {
		IJavaProject javaProject = getJavaProject("CreateMembers");
		String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true);
		javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
		try {
			ICompilationUnit compilationUnit = getCompilationUnit("CreateMembers", "src", "testBug565015", "PkgRecord.java");
			assertNotNull("No compilation unit", compilationUnit);
			IType[] types = compilationUnit.getTypes();
			assertNotNull("No types", types);
			assertEquals("Wrong size", 1, types.length);
			IType type = types[0];
			type.createType("record Point() {}", null, true, null);
			String expectedSource =
					"package testBug565015;\n" +
					"\n" +
					"public record PkgRecord() {\n" +
					"\n" +
					"	record Point() {}\n" +
					"}";
			assertSourceEquals("Unexpected source", expectedSource, compilationUnit.getSource());
		} finally {
			javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
		}
	}
}

Back to the top