Lines 11-36
Link Here
|
11 |
package org.eclipse.jdt.core.tests.model; |
11 |
package org.eclipse.jdt.core.tests.model; |
12 |
|
12 |
|
13 |
import junit.framework.Test; |
13 |
import junit.framework.Test; |
14 |
import junit.framework.TestSuite; |
|
|
15 |
|
14 |
|
16 |
import org.eclipse.core.resources.IFile; |
15 |
import org.eclipse.core.resources.IFile; |
17 |
import org.eclipse.core.runtime.CoreException; |
16 |
import org.eclipse.core.runtime.CoreException; |
18 |
import org.eclipse.core.runtime.NullProgressMonitor; |
17 |
import org.eclipse.core.runtime.NullProgressMonitor; |
19 |
import org.eclipse.jdt.core.IClasspathEntry; |
18 |
import org.eclipse.jdt.core.*; |
20 |
import org.eclipse.jdt.core.IJavaProject; |
19 |
import org.eclipse.jdt.core.dom.AST; |
21 |
import org.eclipse.jdt.core.IType; |
20 |
import org.eclipse.jdt.internal.core.SourceType; |
22 |
import org.eclipse.jdt.core.JavaCore; |
21 |
|
23 |
import org.eclipse.jdt.core.JavaModelException; |
22 |
|
24 |
/** |
23 |
/** |
25 |
* Test retrieving types by their name. |
24 |
* Test retrieving types by their name. |
26 |
*/ |
25 |
*/ |
27 |
public class ClassNameTests extends ModifyingResourceTests { |
26 |
public class ClassNameTests extends ModifyingResourceTests { |
28 |
|
27 |
|
|
|
28 |
static IJavaProject TEST_PROJECT; |
29 |
final static int SF_LENGTH = 5; |
30 |
static int TESTS_COUNT; |
29 |
|
31 |
|
30 |
public ClassNameTests(String name) { |
32 |
public ClassNameTests(String name) { |
31 |
super(name); |
33 |
super(name); |
32 |
} |
34 |
} |
33 |
|
35 |
|
|
|
36 |
static { |
37 |
// org.eclipse.jdt.internal.core.NameLookup.VERBOSE = true; |
38 |
// TESTS_NAMES = new String[] { "testReconcileMultipleProject" }; |
39 |
// TESTS_PREFIX = "testReconcile"; |
40 |
} |
41 |
public static Test suite() { |
42 |
Test suite = buildTestSuite(ClassNameTests.class); |
43 |
TESTS_COUNT = suite.countTestCases(); |
44 |
return suite; |
45 |
} |
46 |
|
47 |
/* (non-Javadoc) |
48 |
* @see org.eclipse.jdt.core.tests.model.AbstractJavaModelTests#setUp() |
49 |
*/ |
50 |
protected void setUp() throws Exception { |
51 |
if (org.eclipse.jdt.internal.core.NameLookup.VERBOSE || org.eclipse.jdt.internal.core.search.BasicSearchEngine.VERBOSE) { |
52 |
System.out.println("--------------------------------------------------------------------------------"); |
53 |
System.out.println("Running test "+getName()+"..."); |
54 |
} |
55 |
super.setUp(); |
56 |
if (TEST_PROJECT == null) { |
57 |
String[] sourceFolders = new String[SF_LENGTH]; |
58 |
for (int i=0; i<SF_LENGTH; i++) { |
59 |
sourceFolders[i] = "src" + i; |
60 |
} |
61 |
TEST_PROJECT = createJavaProject("TestProject", sourceFolders, new String[] {"JCL_LIB"}, "bin"); |
62 |
createFolder("/TestProject/src0/org/eclipse/jdt/core/test0"); |
63 |
createFile( |
64 |
"/TestProject/src0/org/eclipse/jdt/core/test0/Foo.java", |
65 |
"package org.eclipse.jdt.core.test0;\n" + |
66 |
"public class Foo {\n" + |
67 |
" class InFoo {}\n" + |
68 |
"}\n" + |
69 |
"class Secondary {\n" + |
70 |
" class InSecondary {}\n" + |
71 |
"}\n" |
72 |
); |
73 |
createFile( |
74 |
"/TestProject/src1/Foo.java", |
75 |
"public class Foo {\n" + |
76 |
" class InFoo {}\n" + |
77 |
"}\n" + |
78 |
"class Secondary {\n" + |
79 |
" class InSecondary {}\n" + |
80 |
"}\n" |
81 |
); |
82 |
int length = SF_LENGTH - 1; |
83 |
createFolder("/TestProject/src"+length+"/org/eclipse/jdt/core/test"+length); |
84 |
createFile( |
85 |
"/TestProject/src"+length+"/org/eclipse/jdt/core/test"+length+"/Foo.java", |
86 |
"package org.eclipse.jdt.core.test"+length+";\n" + |
87 |
"public class Foo {\n" + |
88 |
"}\n" + |
89 |
"class Secondary {\n" + |
90 |
"}\n" |
91 |
); |
92 |
createFile( |
93 |
"/TestProject/src"+length+"/org/eclipse/jdt/core/test"+length+"/Test.java", |
94 |
"package org.eclipse.jdt.core.test"+length+";\n" + |
95 |
"public class Test {\n" + |
96 |
" public static void main(String[] args) {\n" + |
97 |
" Secondary s = new Secondary();\n" + |
98 |
" }\n" + |
99 |
"}\n" |
100 |
); |
101 |
} |
102 |
} |
103 |
|
104 |
/* (non-Javadoc) |
105 |
* @see org.eclipse.jdt.core.tests.model.AbstractJavaModelTests#tearDown() |
106 |
*/ |
107 |
protected void tearDown() throws Exception { |
108 |
TESTS_COUNT--; |
109 |
if (TEST_PROJECT != null && TESTS_COUNT == 0) { |
110 |
deleteResource(TEST_PROJECT.getProject()); |
111 |
} |
112 |
super.tearDown(); |
113 |
} |
114 |
|
115 |
protected void assertTypeFound(String typeName, String expectedResult) throws JavaModelException { |
116 |
assertNotNull("TEST_PROJECT should not be null!!!", TEST_PROJECT); |
117 |
IType type = TEST_PROJECT.findType(typeName); |
118 |
assertTrue("type "+typeName+" should exist!", type != null && type.exists()); |
119 |
assertEquals("Expected type "+typeName+" NOT found!", |
120 |
expectedResult, |
121 |
((SourceType)type).toStringWithAncestors() |
122 |
); |
123 |
} |
124 |
protected void assertTypeFound(String packageName, String typeName, String expectedResult) throws JavaModelException { |
125 |
assertNotNull("TEST_PROJECT should not be null!!!", TEST_PROJECT); |
126 |
IType type = TEST_PROJECT.findType(packageName, typeName); |
127 |
assertTrue("type "+typeName+" should exist!", type != null && type.exists()); |
128 |
assertEquals("Expected type "+typeName+" NOT found!", |
129 |
expectedResult, |
130 |
((SourceType)type).toStringWithAncestors() |
131 |
); |
132 |
} |
133 |
|
134 |
protected void assertTypeNotFound(String typeName) throws JavaModelException { |
135 |
assertNotNull("TEST_PROJECT should not be null!!!", TEST_PROJECT); |
136 |
IType type = TEST_PROJECT.findType(typeName); |
137 |
assertNotNull("type "+typeName+" should NOT be null!", type); |
138 |
assertFalse("type "+typeName+" should NOT exist!", type.exists()); |
139 |
} |
140 |
protected void assertTypeNotFound(String packageName, String typeName) throws JavaModelException { |
141 |
assertNotNull("TEST_PROJECT should not be null!!!", TEST_PROJECT); |
142 |
IType type = TEST_PROJECT.findType(packageName, typeName); |
143 |
assertNotNull("type "+typeName+" should NOT be null!", type); |
144 |
assertFalse("type "+typeName+" should NOT exist!", type.exists()); |
145 |
} |
146 |
|
147 |
protected void assertTypeUnknown(String typeName) throws JavaModelException { |
148 |
assertNotNull("TEST_PROJECT should not be null!!!", TEST_PROJECT); |
149 |
IType type = TEST_PROJECT.findType(typeName); |
150 |
assertNull("type "+typeName+" should NOT be found!", type); |
151 |
} |
152 |
protected void assertTypeUnknown(String packageName, String typeName) throws JavaModelException { |
153 |
assertNotNull("TEST_PROJECT should not be null!!!", TEST_PROJECT); |
154 |
IType type = TEST_PROJECT.findType(packageName, typeName); |
155 |
assertNull("type "+typeName+" should NOT be found!", type); |
156 |
} |
157 |
|
34 |
/** |
158 |
/** |
35 |
* Tests that a type in a jar with a name ending with $ can be retrieved. |
159 |
* Tests that a type in a jar with a name ending with $ can be retrieved. |
36 |
*/ |
160 |
*/ |
Lines 945-957
Link Here
|
945 |
} |
1069 |
} |
946 |
} |
1070 |
} |
947 |
|
1071 |
|
948 |
public static Test suite() { |
1072 |
/** |
949 |
TestSuite suite = new Suite(ClassNameTests.class.getName()); |
1073 |
* Bug 36032: JavaProject.findType() fails to find second type in source file |
950 |
suite.addTest(new ClassNameTests("testClassNameWithDollar")); |
1074 |
* @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=36032" |
951 |
suite.addTest(new ClassNameTests("testFindTypeWithDot")); |
1075 |
*/ |
952 |
suite.addTest(new ClassNameTests("testSearchTypeNameInJars")); |
1076 |
public void testFindSecondaryType_Exist01() throws JavaModelException, CoreException { |
953 |
return suite; |
1077 |
int length = SF_LENGTH - 1; |
|
|
1078 |
assertTypeFound( |
1079 |
"org.eclipse.jdt.core.test"+length+".Foo", |
1080 |
"Foo [in Foo.java [in org.eclipse.jdt.core.test"+length+" [in src"+length+" [in TestProject]]]]" |
1081 |
); |
1082 |
} |
1083 |
public void testFindSecondaryType_Exist02() throws JavaModelException, CoreException { |
1084 |
int length = SF_LENGTH - 1; |
1085 |
assertTypeFound( |
1086 |
"org.eclipse.jdt.core.test"+length+".Secondary", |
1087 |
"Secondary [in Foo.java [in org.eclipse.jdt.core.test"+length+" [in src"+length+" [in TestProject]]]]" |
1088 |
); |
1089 |
} |
1090 |
public void testFindSecondaryType_Exist03() throws JavaModelException, CoreException { |
1091 |
assertTypeFound( |
1092 |
"org.eclipse.jdt.core.test0.Foo.InFoo", |
1093 |
"InFoo [in Foo [in Foo.java [in org.eclipse.jdt.core.test0 [in src0 [in TestProject]]]]]" |
1094 |
); |
1095 |
} |
1096 |
public void testFindSecondaryType_Exist04() throws JavaModelException, CoreException { |
1097 |
assertTypeFound( |
1098 |
"org.eclipse.jdt.core.test0.Secondary.InSecondary", |
1099 |
"InSecondary [in Secondary [in Foo.java [in org.eclipse.jdt.core.test0 [in src0 [in TestProject]]]]]" |
1100 |
); |
1101 |
} |
1102 |
public void testFindSecondaryType_Exist05() throws JavaModelException, CoreException { |
1103 |
assertTypeFound( |
1104 |
"Foo", |
1105 |
"Foo [in Foo.java [in <default> [in src1 [in TestProject]]]]" |
1106 |
); |
1107 |
} |
1108 |
public void testFindSecondaryType_Exist06() throws JavaModelException, CoreException { |
1109 |
assertTypeFound( |
1110 |
"Secondary", |
1111 |
"Secondary [in Foo.java [in <default> [in src1 [in TestProject]]]]" |
1112 |
); |
954 |
} |
1113 |
} |
|
|
1114 |
// duplicate bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=72179 |
1115 |
public void testFindSecondaryType_Bug72179() throws JavaModelException, CoreException { |
1116 |
try { |
1117 |
IJavaProject javaProject = createJavaProject("P", new String[] {""}, ""); |
1118 |
createFolder("/P/p1"); |
1119 |
createFile( |
1120 |
"/P/p1/jc.java", |
1121 |
"package p1;\n" + |
1122 |
"class jc008{}\n" + |
1123 |
"class jc009{}\n" + |
1124 |
"class jc010 extends jc009 {\n" + |
1125 |
" jc008 a;\n" + |
1126 |
"}\n" |
1127 |
); |
1128 |
IType type = javaProject.findType("p1", "jc008"); |
1129 |
assertTrue("type 'jc008' should exist!", type != null && type.exists()); |
1130 |
assertEquals("Expected type 'jc008' NOT found!", |
1131 |
"class jc008 [in jc.java [in p1 [in <project root> [in P]]]]", |
1132 |
type.toString() |
1133 |
); |
1134 |
type = javaProject.findType("p1", "jc009"); |
1135 |
assertTrue("type 'jc009' should exist!", type != null && type.exists()); |
1136 |
assertEquals("Expected type 'jc009' NOT found!", |
1137 |
"class jc009 [in jc.java [in p1 [in <project root> [in P]]]]", |
1138 |
type.toString() |
1139 |
); |
1140 |
type = javaProject.findType("p1", "jc010"); |
1141 |
assertTrue("type 'jc010' should exist!", type != null && type.exists()); |
1142 |
assertEquals("Expected type 'jc010' NOT found!", |
1143 |
"class jc010 [in jc.java [in p1 [in <project root> [in P]]]]\n" + |
1144 |
" jc008 a", |
1145 |
type.toString() |
1146 |
); |
1147 |
} finally { |
1148 |
deleteProject("P"); |
1149 |
} |
1150 |
} |
1151 |
public void testFindSecondaryType_NotFound01() throws JavaModelException, CoreException { |
1152 |
assertTypeUnknown("test.Foo"); |
1153 |
} |
1154 |
public void testFindSecondaryType_NotFound02() throws JavaModelException, CoreException { |
1155 |
assertTypeUnknown("InFoo"); |
1156 |
} |
1157 |
public void testFindSecondaryType_NotFound03() throws JavaModelException, CoreException { |
1158 |
assertTypeUnknown("InSecondary"); |
1159 |
} |
1160 |
public void testFindSecondaryType_NotFound04() throws JavaModelException, CoreException { |
1161 |
assertTypeUnknown("Foo.inFoo"); |
1162 |
} |
1163 |
public void testFindSecondaryType_NotFound05() throws JavaModelException, CoreException { |
1164 |
assertTypeUnknown("Secondary.inBar"); |
1165 |
} |
1166 |
public void testFindSecondaryType_Unknown01() throws JavaModelException, CoreException { |
1167 |
assertTypeUnknown("Unknown"); |
1168 |
} |
1169 |
public void testFindSecondaryType_Unknown02() throws JavaModelException, CoreException { |
1170 |
assertTypeUnknown("Foo.Unknown"); |
1171 |
} |
1172 |
public void testFindSecondaryType_Unknown03() throws JavaModelException, CoreException { |
1173 |
assertTypeUnknown("org.eclipse.jdt.core.test.Unknown"); |
1174 |
} |
1175 |
public void testReconcileSingleProject01() throws CoreException { |
1176 |
ICompilationUnit workingCopy = null; |
1177 |
try { |
1178 |
createJavaProject("P", new String[] {""}, new String[] {"JCL_LIB"}, "bin"); |
1179 |
String source = |
1180 |
"public class Test {\n" + |
1181 |
" public static void main(String[] args) {\n" + |
1182 |
" new SFoo().foo();\n" + |
1183 |
" }\n" + |
1184 |
"}\n"; |
1185 |
this.createFile( |
1186 |
"/P/Foo.java", |
1187 |
"class SFoo { void foo() {} }\n" |
1188 |
); |
1189 |
this.createFile( |
1190 |
"/P/Test.java", |
1191 |
source |
1192 |
); |
1193 |
ProblemRequestor problemRequestor = new ProblemRequestor(); |
1194 |
workingCopy = getCompilationUnit("/P/Test.java").getWorkingCopy(new WorkingCopyOwner() {}, problemRequestor, null); |
1195 |
problemRequestor.initialize(source.toCharArray()); |
1196 |
workingCopy.getBuffer().setContents(source); |
1197 |
workingCopy.reconcile(AST.JLS3, true, null, null); |
1198 |
if (problemRequestor.problemCount > 0) { |
1199 |
assertEquals("Working copy should NOT have any problem!", "", problemRequestor.problems.toString()); |
1200 |
} |
955 |
|
1201 |
|
|
|
1202 |
// Add new secondary type |
1203 |
this.createFile( |
1204 |
"/P/Bar.java", |
1205 |
"class SBar{ void bar() {} }\n" |
1206 |
); |
1207 |
waitUntilIndexesReady(); |
1208 |
source = |
1209 |
"public class Test {\n" + |
1210 |
" public static void main(String[] args) {\n" + |
1211 |
" new SFoo().foo();\n" + |
1212 |
" new SBar().bar();\n" + |
1213 |
" }\n" + |
1214 |
"}\n"; |
1215 |
problemRequestor.initialize(source.toCharArray()); |
1216 |
workingCopy.getBuffer().setContents(source); |
1217 |
workingCopy.reconcile(AST.JLS3, true, null, null); |
1218 |
if (problemRequestor.problemCount > 0) { |
1219 |
assertEquals("Working copy should NOT have any problem!", "", problemRequestor.problems.toString()); |
1220 |
} |
1221 |
} finally { |
1222 |
if (workingCopy != null) workingCopy.discardWorkingCopy(); |
1223 |
deleteProject("P"); |
1224 |
} |
1225 |
} |
1226 |
public void testReconcileSingleProject02() throws CoreException { |
1227 |
ICompilationUnit workingCopy = null; |
1228 |
try { |
1229 |
createJavaProject("P", new String[] {""}, new String[] {"JCL_LIB"}, "bin"); |
1230 |
String source = |
1231 |
"public class Test {\n" + |
1232 |
" public static void main(String[] args) {\n" + |
1233 |
" new SFoo().foo();\n" + |
1234 |
" new SBar().bar();\n" + |
1235 |
" }\n" + |
1236 |
"}\n"; |
1237 |
createFile( |
1238 |
"/P/Foo.java", |
1239 |
"class SFoo { void foo() {} }\n" |
1240 |
); |
1241 |
createFile( |
1242 |
"/P/Test.java", |
1243 |
source |
1244 |
); |
1245 |
createFile( |
1246 |
"/P/Bar.java", |
1247 |
"class SBar{ void bar() {} }\n" |
1248 |
); |
1249 |
ProblemRequestor problemRequestor = new ProblemRequestor(); |
1250 |
workingCopy = getCompilationUnit("/P/Test.java").getWorkingCopy(new WorkingCopyOwner() {}, problemRequestor, null); |
1251 |
problemRequestor.initialize(source.toCharArray()); |
1252 |
workingCopy.getBuffer().setContents(source); |
1253 |
workingCopy.reconcile(AST.JLS3, true, null, null); |
1254 |
if (problemRequestor.problemCount > 0) { |
1255 |
assertEquals("Working copy should NOT have any problem!", "", problemRequestor.problems.toString()); |
1256 |
} |
956 |
|
1257 |
|
|
|
1258 |
// Delete secondary type => should get a problem |
1259 |
waitUntilIndexesReady(); |
1260 |
deleteFile("/P/Bar.java"); |
1261 |
workingCopy.reconcile(AST.JLS3, true, null, null); |
1262 |
assertEquals("Working copy should not find secondary type 'Bar'!", 1, problemRequestor.problemCount); |
1263 |
assertEquals("Working copy should have problem!", |
1264 |
"----------\n" + |
1265 |
"----------\n" + |
1266 |
"----------\n" + |
1267 |
"1. ERROR in /P/Test.java (at line 4)\r\n" + |
1268 |
" new SBar().bar();\r\n" + |
1269 |
" ^^^^\n" + |
1270 |
"SBar cannot be resolved to a type\n" + |
1271 |
"----------\n", |
1272 |
problemRequestor.problems.toString() |
1273 |
); |
1274 |
|
1275 |
// Fix the problem |
1276 |
source = |
1277 |
"public class Test {\n" + |
1278 |
" public static void main(String[] args) {\n" + |
1279 |
" new SFoo().foo();\n" + |
1280 |
" }\n" + |
1281 |
"}\n"; |
1282 |
problemRequestor.initialize(source.toCharArray()); |
1283 |
workingCopy.getBuffer().setContents(source); |
1284 |
workingCopy.reconcile(AST.JLS3, true, null, null); |
1285 |
if (problemRequestor.problemCount > 0) { |
1286 |
assertEquals("Working copy should NOT have any problem!", "", problemRequestor.problems.toString()); |
1287 |
} |
1288 |
} finally { |
1289 |
if (workingCopy != null) workingCopy.discardWorkingCopy(); |
1290 |
deleteProject("P"); |
1291 |
} |
1292 |
} |
1293 |
public void testReconcileMultipleProject() throws CoreException { |
1294 |
ICompilationUnit workingCopy = null; |
1295 |
try { |
1296 |
// Create first project |
1297 |
createJavaProject("P1", new String[] {""}, new String[] {"JCL_LIB"}, "bin"); |
1298 |
createFolder("/P1/test"); |
1299 |
createFile( |
1300 |
"/P1/test/Foo.java", |
1301 |
"package test;\n" + |
1302 |
"class Secondary{ void foo() {} }\n" |
1303 |
); |
1304 |
createFile( |
1305 |
"/P1/test/Test1.java", |
1306 |
"package test;\n" + |
1307 |
"public class Test1 {\n" + |
1308 |
" public static void main(String[] args) {\n" + |
1309 |
" new Secondary().foo();\n" + |
1310 |
" }\n" + |
1311 |
"}\n" |
1312 |
); |
1313 |
|
1314 |
// Create second project |
1315 |
createJavaProject("P2", new String[] {""}, new String[] {"JCL_LIB"}, new String[] { "/P1" }, "bin"); |
1316 |
String source2 = |
1317 |
"package test;\n" + |
1318 |
"public class Test2 {\n" + |
1319 |
" public static void main(String[] args) {\n" + |
1320 |
" new Secondary().foo();\n" + |
1321 |
" }\n" + |
1322 |
"}\n"; |
1323 |
createFolder("/P2/test"); |
1324 |
createFile( |
1325 |
"/P2/test/Test2.java", |
1326 |
source2 |
1327 |
); |
1328 |
ProblemRequestor problemRequestor = new ProblemRequestor(); |
1329 |
workingCopy = getCompilationUnit("/P2/test/Test2.java").getWorkingCopy(new WorkingCopyOwner() {}, problemRequestor, null); |
1330 |
problemRequestor.initialize(source2.toCharArray()); |
1331 |
workingCopy.getBuffer().setContents(source2); |
1332 |
workingCopy.reconcile(AST.JLS3, true, null, null); |
1333 |
if (problemRequestor.problemCount > 0) { |
1334 |
assertEquals("Working copy should NOT have any problem!", "", problemRequestor.problems.toString()); |
1335 |
} |
1336 |
} finally { |
1337 |
if (workingCopy != null) workingCopy.discardWorkingCopy(); |
1338 |
deleteProject("P1"); |
1339 |
deleteProject("P2"); |
1340 |
} |
1341 |
} |
957 |
} |
1342 |
} |