1 19 20 package org.netbeans.modules.java.builder; 21 22 import com.sun.source.tree.ClassTree; 23 import com.sun.source.util.TreePath; 24 import static org.netbeans.modules.java.source.builder.BufferRun.Kind.*; 25 import static org.netbeans.api.java.source.Comment.Style.*; 26 import static com.sun.tools.javac.parser.Token.*; 27 import java.beans.PropertyVetoException ; 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.io.OutputStream ; 31 import javax.xml.validation.Validator ; 32 import org.netbeans.api.java.source.CancellableTask; 33 import org.netbeans.api.java.source.CompilationController; 34 import org.netbeans.api.java.source.JavaSource; 35 import org.netbeans.api.java.source.JavaSource.Phase; 36 import org.netbeans.api.java.source.ModificationResult; 37 import org.netbeans.api.java.source.SourceUtils; 38 import org.netbeans.api.java.source.SourceUtilsTestUtil; 39 import org.netbeans.api.java.source.WorkingCopy; 40 import org.netbeans.junit.NbTestCase; 41 import org.openide.filesystems.FileLock; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.filesystems.LocalFileSystem; 45 import org.openide.filesystems.Repository; 46 47 51 public class CommentHandlerServiceTest extends NbTestCase { 52 53 public CommentHandlerServiceTest(String testName) { 54 super(testName); 55 } 56 57 protected void setUp() throws Exception { 58 SourceUtilsTestUtil.prepareTest(new String [0], new Object [0]); 59 } 60 61 public void testBrokenFile1() throws Exception { 62 performTest("package test;\npublic class Test extends "); 63 } 64 65 public void testBrokenFile2() throws Exception { 66 performTest("package test;\npublic class Test {private static test() {List<? extend test.test.}}"); 67 } 68 69 private void writeIntoFile(FileObject file, String what) throws Exception { 70 FileLock lock = file.lock(); 71 OutputStream out = file.getOutputStream(lock); 72 73 try { 74 out.write(what.getBytes()); 75 } finally { 76 out.close(); 77 lock.releaseLock(); 78 } 79 } 80 81 private void performTest(String sourceCode) throws Exception { 82 FileObject root = makeScratchDir(this); 83 84 FileObject sourceDir = root.createFolder("src"); 85 FileObject buildDir = root.createFolder("build"); 86 FileObject cacheDir = root.createFolder("cache"); 87 88 FileObject source = sourceDir.createFolder("test").createData("Test.java"); 89 90 writeIntoFile(source, sourceCode); 91 92 SourceUtilsTestUtil.prepareTest(sourceDir, buildDir, cacheDir, new FileObject[0]); 93 94 JavaSource js = JavaSource.forFileObject(source); 95 96 js.runUserActionTask(new CancellableTask<CompilationController>() { 97 public void cancel() { 98 } 99 public void run(CompilationController copy) throws Exception { 100 copy.toPhase(Phase.RESOLVED); 101 } 102 },true); 103 } 104 105 110 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 111 test.clearWorkDir(); 112 File root = test.getWorkDir(); 113 assert root.isDirectory() && root.list().length == 0; 114 FileObject fo = FileUtil.toFileObject(root); 115 if (fo != null) { 116 return fo; 118 } else { 119 LocalFileSystem lfs = new LocalFileSystem(); 121 try { 122 lfs.setRootDirectory(root); 123 } catch (PropertyVetoException e) { 124 assert false : e; 125 } 126 Repository.getDefault().addFileSystem(lfs); 127 return lfs.getRoot(); 128 } 129 } 130 131 } 132 | Popular Tags |