KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > builder > CommentHandlerServiceTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import javax.xml.validation.Validator JavaDoc;
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 /**
48  *
49  * @author Jan Lahoda
50  */

51 public class CommentHandlerServiceTest extends NbTestCase {
52     
53     public CommentHandlerServiceTest(String JavaDoc testName) {
54         super(testName);
55     }
56
57     protected void setUp() throws Exception JavaDoc {
58         SourceUtilsTestUtil.prepareTest(new String JavaDoc[0], new Object JavaDoc[0]);
59     }
60     
61     public void testBrokenFile1() throws Exception JavaDoc {
62         performTest("package test;\npublic class Test extends ");
63     }
64     
65     public void testBrokenFile2() throws Exception JavaDoc {
66         performTest("package test;\npublic class Test {private static test() {List<? extend test.test.}}");
67     }
68     
69     private void writeIntoFile(FileObject file, String JavaDoc what) throws Exception JavaDoc {
70         FileLock lock = file.lock();
71         OutputStream JavaDoc 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 JavaDoc sourceCode) throws Exception JavaDoc {
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 JavaDoc {
100                 copy.toPhase(Phase.RESOLVED);
101             }
102         },true);
103     }
104     
105     /**Copied from org.netbeans.api.project.
106      * Create a scratch directory for tests.
107      * Will be in /tmp or whatever, and will be empty.
108      * If you just need a java.io.File use clearWorkDir + getWorkDir.
109      */

110     public static FileObject makeScratchDir(NbTestCase test) throws IOException JavaDoc {
111         test.clearWorkDir();
112         File JavaDoc root = test.getWorkDir();
113         assert root.isDirectory() && root.list().length == 0;
114         FileObject fo = FileUtil.toFileObject(root);
115         if (fo != null) {
116             // Presumably using masterfs.
117
return fo;
118         } else {
119             // For the benefit of those not using masterfs.
120
LocalFileSystem lfs = new LocalFileSystem();
121             try {
122                 lfs.setRootDirectory(root);
123             } catch (PropertyVetoException JavaDoc e) {
124                 assert false : e;
125             }
126             Repository.getDefault().addFileSystem(lfs);
127             return lfs.getRoot();
128         }
129     }
130     
131 }
132
Popular Tags