1 /*******************************************************************************
2 * Copyright (c) 2000, 2015 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.jdt.core.tests.model;
15
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.util.Hashtable;
20
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.JavaCore;
23 import org.eclipse.jdt.internal.codeassist.RelevanceConstants;
24
25 import junit.framework.*;
26
27 @SuppressWarnings({"rawtypes", "unchecked"})
28 public class CompletionWithMissingTypesTests2 extends ModifyingResourceTests implements RelevanceConstants {
29
CompletionWithMissingTypesTests2(String name)30 public CompletionWithMissingTypesTests2(String name) {
31 super(name);
32 }
33 @Override
setUpSuite()34 public void setUpSuite() throws Exception {
35 if (AbstractJavaModelCompletionTests.COMPLETION_PROJECT == null) {
36 AbstractJavaModelCompletionTests.COMPLETION_PROJECT = setUpJavaProject("Completion");
37 } else {
38 setUpProjectCompliance(AbstractJavaModelCompletionTests.COMPLETION_PROJECT, "1.4");
39 this.currentProject = AbstractJavaModelCompletionTests.COMPLETION_PROJECT;
40 }
41 super.setUpSuite();
42 }
43 @Override
tearDownSuite()44 public void tearDownSuite() throws Exception {
45 if (AbstractJavaModelCompletionTests.COMPLETION_SUITES == null) {
46 deleteProject("Completion");
47 } else {
48 AbstractJavaModelCompletionTests.COMPLETION_SUITES.remove(getClass());
49 if (AbstractJavaModelCompletionTests.COMPLETION_SUITES.size() == 0) {
50 deleteProject("Completion");
51 AbstractJavaModelCompletionTests.COMPLETION_SUITES = null;
52 }
53 }
54 if (AbstractJavaModelCompletionTests.COMPLETION_SUITES == null) {
55 AbstractJavaModelCompletionTests.COMPLETION_PROJECT = null;
56 }
57 super.tearDownSuite();
58 }
59
assertResults(String expected, String actual)60 protected static void assertResults(String expected, String actual) {
61 try {
62 assertEquals(expected, actual);
63 } catch(ComparisonFailure c) {
64 System.out.println(actual);
65 System.out.println();
66 throw c;
67 }
68 }
69 static {
70 // TESTS_NAMES = new String[] { "testBug96950" };
71 }
suite()72 public static Test suite() {
73 return buildModelTestSuite(CompletionWithMissingTypesTests2.class);
74 }
75
createFile(File parent, String name, String content)76 File createFile(File parent, String name, String content) throws IOException {
77 File file = new File(parent, name);
78 FileOutputStream out = new FileOutputStream(file);
79 try {
80 out.write(content.getBytes());
81 } finally {
82 out.close();
83 }
84 return file;
85 }
createDirectory(File parent, String name)86 File createDirectory(File parent, String name) {
87 File dir = new File(parent, name);
88 dir.mkdirs();
89 return dir;
90 }
91
92 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=44984
test0001()93 public void test0001() throws Exception {
94 Hashtable oldOptions = JavaCore.getOptions();
95 try {
96 Hashtable options = new Hashtable(oldOptions);
97 options.put(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, JavaCore.ERROR);
98 options.put(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, JavaCore.WARNING);
99 options.put(JavaCore.CODEASSIST_FORBIDDEN_REFERENCE_CHECK, JavaCore.DISABLED);
100 options.put(JavaCore.CODEASSIST_DISCOURAGED_REFERENCE_CHECK, JavaCore.DISABLED);
101 JavaCore.setOptions(options);
102
103 // create variable
104 // JavaCore.setClasspathVariables(
105 // new String[] {"JCL_LIB", "JCL_SRC", "JCL_SRCROOT"},
106 // new IPath[] {getExternalJCLPath(), getExternalJCLSourcePath(), getExternalJCLRootSourcePath()},
107 // null);
108
109 // create P1
110 this.createJavaProject(
111 "P1",
112 new String[]{"src"},
113 new String[]{"JCL_LIB"},
114 "bin");
115
116 this.createFolder("/P1/src/a");
117 this.createFile(
118 "/P1/src/a/XX.java",
119 "package a;\n"+
120 "public class XX {\n"+
121 " void foo() {}\n"+
122 "}");
123
124 this.createFolder("/P1/src/b");
125 this.createFile(
126 "/P1/src/b/XX.java",
127 "package b;\n"+
128 "public class XX {\n"+
129 " void foo() {}\n"+
130 "}");
131
132 // create P2
133 this.createJavaProject(
134 "P2",
135 new String[]{"src"},
136 new String[]{"JCL_LIB"},
137 null,
138 null,
139 new String[]{"/P1"},
140 new String[][]{{}},
141 new String[][]{{"a/*"}},
142 new boolean[]{false},
143 "bin",
144 null,
145 null,
146 null,
147 "1.4");
148 this.createFile(
149 "/P2/src/YY.java",
150 "public class YY {\n"+
151 " void foo() {\n"+
152 " XX x = null;\n"+
153 " x.fo\n"+
154 " }\n"+
155 "}");
156
157 waitUntilIndexesReady();
158
159 // do completion
160 CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, false, true);
161 requestor.allowAllRequiredProposals();
162 ICompilationUnit cu= getCompilationUnit("P2", "src", "", "YY.java");
163
164 String str = cu.getSource();
165 String completeBehind = "x.fo";
166 int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
167 cu.codeComplete(cursorLocation, requestor);
168
169 int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED + R_NO_PROBLEMS;
170 int start1 = str.lastIndexOf("x.fo") + "x.".length();
171 int end1 = start1 + "fo".length();
172 int start2 = str.lastIndexOf("XX");
173 int end2 = start2 + "XX".length();
174 assertResults(
175 "foo[METHOD_REF]{foo(), La.XX;, ()V, foo, null, ["+start1+", "+end1+"], " + (relevance1) + "}\n" +
176 " XX[TYPE_REF]{a.XX, a, La.XX;, null, null, ["+start2+", "+end2+"], " + (relevance1) + "}\n" +
177 "foo[METHOD_REF]{foo(), Lb.XX;, ()V, foo, null, ["+start1+", "+end1+"], " + (relevance1) + "}\n" +
178 " XX[TYPE_REF]{b.XX, b, Lb.XX;, null, null, ["+start2+", "+end2+"], " + (relevance1) + "}",
179 requestor.getResults());
180 } finally {
181 this.deleteProject("P1");
182 this.deleteProject("P2");
183 JavaCore.setOptions(oldOptions);
184 }
185 }
186 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=44984
test0002()187 public void test0002() throws Exception {
188 Hashtable oldOptions = JavaCore.getOptions();
189 try {
190 Hashtable options = new Hashtable(oldOptions);
191 options.put(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, JavaCore.ERROR);
192 options.put(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, JavaCore.WARNING);
193 options.put(JavaCore.CODEASSIST_FORBIDDEN_REFERENCE_CHECK, JavaCore.ENABLED);
194 options.put(JavaCore.CODEASSIST_DISCOURAGED_REFERENCE_CHECK, JavaCore.DISABLED);
195 JavaCore.setOptions(options);
196
197 // create variable
198 // JavaCore.setClasspathVariables(
199 // new String[] {"JCL_LIB", "JCL_SRC", "JCL_SRCROOT"},
200 // new IPath[] {getExternalJCLPath(), getExternalJCLSourcePath(), getExternalJCLRootSourcePath()},
201 // null);
202
203 // create P1
204 this.createJavaProject(
205 "P1",
206 new String[]{"src"},
207 new String[]{"JCL_LIB"},
208 "bin");
209
210 this.createFolder("/P1/src/a");
211 this.createFile(
212 "/P1/src/a/XX.java",
213 "package a;\n"+
214 "public class XX {\n"+
215 " void foo() {}\n"+
216 "}");
217
218 this.createFolder("/P1/src/b");
219 this.createFile(
220 "/P1/src/b/XX.java",
221 "package b;\n"+
222 "public class XX {\n"+
223 " void foo() {}\n"+
224 "}");
225
226 // create P2
227 this.createJavaProject(
228 "P2",
229 new String[]{"src"},
230 new String[]{"JCL_LIB"},
231 null,
232 null,
233 new String[]{"/P1"},
234 new String[][]{{}},
235 new String[][]{{"a/*"}},
236 new boolean[]{false},
237 "bin",
238 null,
239 null,
240 null,
241 "1.4");
242 this.createFile(
243 "/P2/src/YY.java",
244 "public class YY {\n"+
245 " void foo() {\n"+
246 " XX x = null;\n"+
247 " x.fo\n"+
248 " }\n"+
249 "}");
250
251 waitUntilIndexesReady();
252
253 // do completion
254 CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, false, true);
255 requestor.allowAllRequiredProposals();
256 ICompilationUnit cu= getCompilationUnit("P2", "src", "", "YY.java");
257
258 String str = cu.getSource();
259 String completeBehind = "x.fo";
260 int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
261 cu.codeComplete(cursorLocation, requestor);
262
263 int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED + R_NO_PROBLEMS;
264 int start1 = str.lastIndexOf("x.fo") + "x.".length();
265 int end1 = start1 + "fo".length();
266 int start2 = str.lastIndexOf("XX");
267 int end2 = start2 + "XX".length();
268 assertResults(
269 "foo[METHOD_REF]{foo(), Lb.XX;, ()V, foo, null, ["+start1+", "+end1+"], " + (relevance1) + "}\n" +
270 " XX[TYPE_REF]{b.XX, b, Lb.XX;, null, null, ["+start2+", "+end2+"], " + (relevance1) + "}",
271 requestor.getResults());
272 } finally {
273 this.deleteProject("P1");
274 this.deleteProject("P2");
275 JavaCore.setOptions(oldOptions);
276 }
277 }
278 }
279