Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2006 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.core.tests.model; |
12 |
|
13 |
import java.util.Map; |
14 |
|
15 |
import junit.framework.Test; |
16 |
|
17 |
import org.eclipse.jdt.core.Flags; |
18 |
import org.eclipse.jdt.core.ICompilationUnit; |
19 |
import org.eclipse.jdt.core.IJavaElement; |
20 |
import org.eclipse.jdt.core.JavaModelException; |
21 |
import org.eclipse.jdt.core.ToolFactory; |
22 |
import org.eclipse.jdt.core.formatter.CodeFormatter; |
23 |
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; |
24 |
import org.eclipse.jface.text.BadLocationException; |
25 |
import org.eclipse.jface.text.Document; |
26 |
import org.eclipse.text.edits.MalformedTreeException; |
27 |
import org.eclipse.text.edits.TextEdit; |
28 |
|
29 |
public class CreateImportsTests extends AbstractJavaModelTests { |
30 |
|
31 |
public CreateImportsTests(String name) { |
32 |
super(name); |
33 |
} |
34 |
public static Test suite() { |
35 |
return buildModelTestSuite(CreateImportsTests.class); |
36 |
} |
37 |
protected void setUp() throws Exception { |
38 |
super.setUp(); |
39 |
ICompilationUnit workingCopy = getCompilationUnit("P/X.java"); |
40 |
workingCopy.becomeWorkingCopy(null/*no problem requested*/, null/*no progress*/); |
41 |
this.workingCopies = new ICompilationUnit[] {workingCopy}; |
42 |
setContents( |
43 |
"public class X {\n" + |
44 |
"}" |
45 |
); |
46 |
} |
47 |
public void setUpSuite() throws Exception { |
48 |
super.setUpSuite(); |
49 |
createJavaProject("P", new String[] {""}, new String[] {"JCL15_LIB"}, "", "1.5"); |
50 |
} |
51 |
public void tearDownSuite() throws Exception { |
52 |
deleteProject("P"); |
53 |
super.tearDownSuite(); |
54 |
} |
55 |
|
56 |
private String createImport(String importName, int flags) throws JavaModelException { |
57 |
return createImport(importName, flags, null/*no sibling*/); |
58 |
} |
59 |
private String createImport(String importName, int flags, IJavaElement sibling) throws JavaModelException { |
60 |
ICompilationUnit workingCopy = this.workingCopies[0]; |
61 |
workingCopy.createImport(importName, sibling, flags, null/*no progress*/); |
62 |
Map options = getJavaProject("P").getOptions(true); |
63 |
options.put(DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE, "0"); |
64 |
CodeFormatter formatter = ToolFactory.createCodeFormatter(options); |
65 |
String source = workingCopy.getSource(); |
66 |
Document document = new Document(source); |
67 |
TextEdit edit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, source, 0, source.length() - 1, 0, "\n"); |
68 |
try { |
69 |
edit.apply(document, TextEdit.NONE); |
70 |
} catch(MalformedTreeException e) { |
71 |
// ignore |
72 |
} catch(BadLocationException e) { |
73 |
// ignore |
74 |
} |
75 |
return document.get(); |
76 |
} |
77 |
|
78 |
private void setContents(String contents) throws JavaModelException { |
79 |
this.workingCopies[0].getBuffer().setContents(contents); |
80 |
this.workingCopies[0].makeConsistent(null/*no progress*/); |
81 |
} |
82 |
|
83 |
/* |
84 |
* Ensures that adding a static import is reflected in the source. |
85 |
*/ |
86 |
public void test001() throws JavaModelException { |
87 |
String actualSource = createImport("java.lang.Math.*", Flags.AccStatic); |
88 |
assertSourceEquals( |
89 |
"Unexpected source", |
90 |
"import static java.lang.Math.*;\n" + |
91 |
"\n" + |
92 |
"public class X {\n" + |
93 |
"}", |
94 |
actualSource); |
95 |
} |
96 |
|
97 |
/* |
98 |
* Ensures that adding a non-static import is reflected in the source. |
99 |
*/ |
100 |
public void test002() throws JavaModelException { |
101 |
String actualSource = createImport("java.util.ZipFile", Flags.AccDefault); |
102 |
assertSourceEquals( |
103 |
"Unexpected source", |
104 |
"import java.util.ZipFile;\n" + |
105 |
"\n" + |
106 |
"public class X {\n" + |
107 |
"}", |
108 |
actualSource); |
109 |
} |
110 |
|
111 |
/* |
112 |
* Ensures that adding the same static import doesn't change the source. |
113 |
*/ |
114 |
public void test003() throws JavaModelException { |
115 |
setContents( |
116 |
"import static java.lang.Math.*;\n" + |
117 |
"\n" + |
118 |
"public class X {\n" + |
119 |
"}" |
120 |
); |
121 |
String actualSource = createImport("java.lang.Math.*", Flags.AccStatic); |
122 |
assertSourceEquals( |
123 |
"Unexpected source", |
124 |
"import static java.lang.Math.*;\n" + |
125 |
"\n" + |
126 |
"public class X {\n" + |
127 |
"}", |
128 |
actualSource); |
129 |
} |
130 |
|
131 |
/* |
132 |
* Ensures that adding the same non-static import doesn't change the source. |
133 |
*/ |
134 |
public void test004() throws JavaModelException { |
135 |
setContents( |
136 |
"import java.util.ZipFile;\n" + |
137 |
"\n" + |
138 |
"public class X {\n" + |
139 |
"}" |
140 |
); |
141 |
String actualSource = createImport("java.util.ZipFile", Flags.AccDefault); |
142 |
assertSourceEquals( |
143 |
"Unexpected source", |
144 |
"import java.util.ZipFile;\n" + |
145 |
"\n" + |
146 |
"public class X {\n" + |
147 |
"}", |
148 |
actualSource); |
149 |
} |
150 |
|
151 |
/* |
152 |
* Ensures that adding an onDemand static import starting with an existing non-onDemand import |
153 |
* is reflected in the source. |
154 |
*/ |
155 |
public void test005() throws JavaModelException { |
156 |
setContents( |
157 |
"import java.util.ZipFile;\n" + |
158 |
"\n" + |
159 |
"public class X {\n" + |
160 |
"}" |
161 |
); |
162 |
String actualSource = createImport("java.util.ZipFile.*", Flags.AccStatic); |
163 |
assertSourceEquals( |
164 |
"Unexpected source", |
165 |
"import java.util.ZipFile;\n" + |
166 |
"import static java.util.ZipFile.*;\n" + |
167 |
"\n" + |
168 |
"public class X {\n" + |
169 |
"}", |
170 |
actualSource); |
171 |
} |
172 |
|
173 |
/* |
174 |
* Ensures that adding an onDemand non-static import starting with an existing non-onDemand import |
175 |
* is reflected in the source. |
176 |
*/ |
177 |
public void test006() throws JavaModelException { |
178 |
setContents( |
179 |
"import java.util.ZipFile;\n" + |
180 |
"import static java.util.ZipFile.*;\n" + |
181 |
"\n" + |
182 |
"public class X {\n" + |
183 |
"}" |
184 |
); |
185 |
String actualSource = createImport("java.util.ZipFile.*", Flags.AccDefault); |
186 |
assertSourceEquals( |
187 |
"Unexpected source", |
188 |
"import java.util.ZipFile;\n" + |
189 |
"import static java.util.ZipFile.*;\n" + |
190 |
"import java.util.ZipFile.*;\n" + |
191 |
"\n" + |
192 |
"public class X {\n" + |
193 |
"}", |
194 |
actualSource); |
195 |
} |
196 |
|
197 |
/* |
198 |
* Ensures that adding an import triggers the correct delta. |
199 |
*/ |
200 |
public void test007() throws JavaModelException { |
201 |
try { |
202 |
startDeltas(); |
203 |
createImport("java.util.ZipFile", Flags.AccDefault); |
204 |
assertDeltas( |
205 |
"Unexpected delta", |
206 |
"<import container>[+]: {}" |
207 |
); |
208 |
} finally { |
209 |
stopDeltas(); |
210 |
} |
211 |
} |
212 |
|
213 |
/* |
214 |
* Ensures that adding an import triggers the correct delta. |
215 |
*/ |
216 |
public void test008() throws JavaModelException { |
217 |
setContents( |
218 |
"import static java.lang.Math.*;\n" + |
219 |
"\n" + |
220 |
"public class X {\n" + |
221 |
"}" |
222 |
); |
223 |
try { |
224 |
startDeltas(); |
225 |
createImport("java.util.*", Flags.AccDefault); |
226 |
assertDeltas( |
227 |
"Unexpected delta", |
228 |
"<import container>[*]: {CHILDREN | FINE GRAINED}\n" + |
229 |
" import java.util.*[+]: {}" |
230 |
); |
231 |
} finally { |
232 |
stopDeltas(); |
233 |
} |
234 |
} |
235 |
|
236 |
/* |
237 |
* Ensures that adding an import before a sibling is correctly reflected in the source. |
238 |
*/ |
239 |
public void test009() throws JavaModelException { |
240 |
setContents( |
241 |
"import static java.lang.Math.*;\n" + |
242 |
"\n" + |
243 |
"public class X {\n" + |
244 |
"}" |
245 |
); |
246 |
IJavaElement sibling = this.workingCopies[0].getImport("java.lang.Math.*"); |
247 |
String actualSource = createImport("java.util.ZipFile", Flags.AccDefault, sibling); |
248 |
assertSourceEquals( |
249 |
"Unexpected source", |
250 |
"import java.util.ZipFile;\n" + |
251 |
"import static java.lang.Math.*;\n" + |
252 |
"\n" + |
253 |
"public class X {\n" + |
254 |
"}", |
255 |
actualSource); |
256 |
} |
257 |
|
258 |
/* |
259 |
* Ensures that adding a null import throws the correct exception. |
260 |
*/ |
261 |
public void test010() throws JavaModelException { |
262 |
JavaModelException exception = null; |
263 |
try { |
264 |
createImport(null, Flags.AccStatic); |
265 |
} catch (JavaModelException e) { |
266 |
exception = e; |
267 |
} |
268 |
assertExceptionEquals( |
269 |
"Unexpected exception", |
270 |
"Invalid name specified: null", |
271 |
exception); |
272 |
} |
273 |
|
274 |
/* |
275 |
* Ensures that adding an import with an invalid name throws the correct exception. |
276 |
*/ |
277 |
public void test011() throws JavaModelException { |
278 |
JavaModelException exception = null; |
279 |
try { |
280 |
createImport("java.,.", Flags.AccStatic); |
281 |
} catch (JavaModelException e) { |
282 |
exception = e; |
283 |
} |
284 |
assertExceptionEquals( |
285 |
"Unexpected exception", |
286 |
"Invalid name specified: java.,.", |
287 |
exception); |
288 |
} |
289 |
} |