Lines 46-52
Link Here
|
46 |
import com.sun.source.tree.Tree; |
46 |
import com.sun.source.tree.Tree; |
47 |
import com.sun.source.util.TreePath; |
47 |
import com.sun.source.util.TreePath; |
48 |
import com.sun.source.util.TreePathScanner; |
48 |
import com.sun.source.util.TreePathScanner; |
|
|
49 |
import com.sun.tools.javac.code.Source; |
49 |
import java.io.File; |
50 |
import java.io.File; |
|
|
51 |
import java.io.FileOutputStream; |
52 |
import java.io.IOException; |
53 |
import java.io.InputStream; |
54 |
import java.net.URL; |
50 |
import java.util.Arrays; |
55 |
import java.util.Arrays; |
51 |
import java.util.HashSet; |
56 |
import java.util.HashSet; |
52 |
import java.util.Set; |
57 |
import java.util.Set; |
Lines 54-59
Link Here
|
54 |
import javax.lang.model.element.TypeElement; |
59 |
import javax.lang.model.element.TypeElement; |
55 |
import javax.swing.text.Document; |
60 |
import javax.swing.text.Document; |
56 |
import javax.tools.Diagnostic; |
61 |
import javax.tools.Diagnostic; |
|
|
62 |
import static junit.framework.Assert.assertEquals; |
63 |
import org.netbeans.api.java.classpath.ClassPath; |
57 |
import org.netbeans.api.java.lexer.JavaTokenId; |
64 |
import org.netbeans.api.java.lexer.JavaTokenId; |
58 |
import org.netbeans.api.java.source.ClasspathInfo; |
65 |
import org.netbeans.api.java.source.ClasspathInfo; |
59 |
import org.netbeans.api.java.source.CompilationController; |
66 |
import org.netbeans.api.java.source.CompilationController; |
Lines 67-72
Link Here
|
67 |
import org.netbeans.api.lexer.TokenSequence; |
74 |
import org.netbeans.api.lexer.TokenSequence; |
68 |
import org.netbeans.junit.NbTestCase; |
75 |
import org.netbeans.junit.NbTestCase; |
69 |
import org.netbeans.modules.java.source.tasklist.CompilerSettings; |
76 |
import org.netbeans.modules.java.source.tasklist.CompilerSettings; |
|
|
77 |
import org.netbeans.spi.java.classpath.support.ClassPathSupport; |
70 |
import org.openide.cookies.EditorCookie; |
78 |
import org.openide.cookies.EditorCookie; |
71 |
import org.openide.filesystems.FileObject; |
79 |
import org.openide.filesystems.FileObject; |
72 |
import org.openide.filesystems.FileUtil; |
80 |
import org.openide.filesystems.FileUtil; |
Lines 284-289
Link Here
|
284 |
}, true); |
292 |
}, true); |
285 |
} |
293 |
} |
286 |
|
294 |
|
|
|
295 |
public void testIfMissingObjectOnBootCPUseCPToGuessSourceLevel() throws Exception { |
296 |
Source ret = guessSourceLevel(false, false); |
297 |
assertEquals("Downgraded to 1.4", Source.JDK1_4, ret); |
298 |
} |
299 |
|
300 |
public void testIfObjectPresentOnBootDontUseCPToGuessSourceLevel() throws Exception { |
301 |
Source ret = guessSourceLevel(true, false); |
302 |
assertEquals("Downgraded to 1.4, as Object on bootCP, but no AssertError", Source.JDK1_3, ret); |
303 |
} |
304 |
|
305 |
public void testIfMissingObjectOnBootCPUseCPToGuessSourceLevelWithStringBuilder() throws Exception { |
306 |
Source ret = guessSourceLevel(false, true); |
307 |
assertEquals("Kept to 1.7", Source.JDK1_7, ret); |
308 |
} |
309 |
|
310 |
private Source guessSourceLevel(boolean objectOnBCP, boolean sbOnCP) throws Exception { |
311 |
clearWorkDir(); |
312 |
File bcp = new File(getWorkDir(), "bootcp"); |
313 |
bcp.mkdirs(); |
314 |
|
315 |
File cp = new File(getWorkDir(), "cp"); |
316 |
cp.mkdirs(); |
317 |
|
318 |
File src = new File(getWorkDir(), "src"); |
319 |
src.mkdirs(); |
320 |
|
321 |
if (objectOnBCP) { |
322 |
copyResource( |
323 |
JavacParserTest.class.getResource("/java/lang/Object.class"), |
324 |
new File(new File(new File(bcp, "java"), "lang"), "Object.class") |
325 |
); |
326 |
} |
327 |
|
328 |
copyResource( |
329 |
JavacParserTest.class.getResource("/java/lang/AssertionError.class"), |
330 |
new File(new File(new File(cp, "java"), "lang"), "AssertionError.class") |
331 |
); |
332 |
|
333 |
if (sbOnCP) { |
334 |
copyResource( |
335 |
JavacParserTest.class.getResource("/java/lang/StringBuilder.class"), |
336 |
new File(new File(new File(cp, "java"), "lang"), "StringBuilder.class") |
337 |
); |
338 |
} |
339 |
|
340 |
ClasspathInfo info = ClasspathInfo.create( |
341 |
ClassPathSupport.createClassPath(bcp.toURI().toURL()), |
342 |
ClassPathSupport.createClassPath(cp.toURI().toURL()), |
343 |
ClassPathSupport.createClassPath(src.toURI().toURL()) |
344 |
); |
345 |
|
346 |
return JavacParser.validateSourceLevel("1.7", info); |
347 |
} |
348 |
|
287 |
private FileObject createFile(String path, String content) throws Exception { |
349 |
private FileObject createFile(String path, String content) throws Exception { |
288 |
FileObject file = FileUtil.createData(sourceRoot, path); |
350 |
FileObject file = FileUtil.createData(sourceRoot, path); |
289 |
TestUtilities.copyStringToFile(file, content); |
351 |
TestUtilities.copyStringToFile(file, content); |
Lines 305-308
Link Here
|
305 |
SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache); |
367 |
SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache); |
306 |
} |
368 |
} |
307 |
|
369 |
|
|
|
370 |
private static void copyResource(URL resource, File file) throws IOException { |
371 |
assertNotNull("Resource found", resource); |
372 |
file.getParentFile().mkdirs(); |
373 |
assertTrue("New file " + file + " created", file.createNewFile()); |
374 |
FileOutputStream os = new FileOutputStream(file); |
375 |
InputStream is = resource.openStream(); |
376 |
FileUtil.copy(is, os); |
377 |
is.close(); |
378 |
os.close(); |
379 |
} |
380 |
|
308 |
} |
381 |
} |