001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.builder;
043:
044: import static org.netbeans.api.java.source.Comment.Style.*;
045: import static com.sun.tools.javac.parser.Token.*;
046: import java.beans.PropertyVetoException;
047: import java.io.File;
048: import java.io.IOException;
049: import java.io.OutputStream;
050: import org.netbeans.api.java.source.Task;
051: import org.netbeans.api.java.source.CompilationController;
052: import org.netbeans.api.java.source.JavaSource;
053: import org.netbeans.api.java.source.JavaSource.Phase;
054: import org.netbeans.api.java.source.SourceUtilsTestUtil;
055: import org.netbeans.junit.NbTestCase;
056: import org.openide.filesystems.FileLock;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.FileUtil;
059: import org.openide.filesystems.LocalFileSystem;
060: import org.openide.filesystems.Repository;
061:
062: /**
063: *
064: * @author Jan Lahoda
065: */
066: public class CommentHandlerServiceTest extends NbTestCase {
067:
068: public CommentHandlerServiceTest(String testName) {
069: super (testName);
070: }
071:
072: @Override
073: protected void setUp() throws Exception {
074: SourceUtilsTestUtil.prepareTest(new String[0], new Object[0]);
075: }
076:
077: public void testBrokenFile1() throws Exception {
078: performTest("package test;\npublic class Test extends ");
079: }
080:
081: public void testBrokenFile2() throws Exception {
082: performTest("package test;\npublic class Test {private static test() {List<? extend test.test.}}");
083: }
084:
085: private void writeIntoFile(FileObject file, String what)
086: throws Exception {
087: FileLock lock = file.lock();
088: OutputStream out = file.getOutputStream(lock);
089:
090: try {
091: out.write(what.getBytes());
092: } finally {
093: out.close();
094: lock.releaseLock();
095: }
096: }
097:
098: private void performTest(String sourceCode) throws Exception {
099: FileObject root = makeScratchDir(this );
100:
101: FileObject sourceDir = root.createFolder("src");
102: FileObject buildDir = root.createFolder("build");
103: FileObject cacheDir = root.createFolder("cache");
104:
105: FileObject source = sourceDir.createFolder("test").createData(
106: "Test.java");
107:
108: writeIntoFile(source, sourceCode);
109:
110: SourceUtilsTestUtil.prepareTest(sourceDir, buildDir, cacheDir,
111: new FileObject[0]);
112:
113: JavaSource js = JavaSource.forFileObject(source);
114:
115: js.runUserActionTask(new Task<CompilationController>() {
116:
117: public void run(CompilationController copy)
118: throws Exception {
119: copy.toPhase(Phase.RESOLVED);
120: }
121: }, true);
122: }
123:
124: /**Copied from org.netbeans.api.project.
125: * Create a scratch directory for tests.
126: * Will be in /tmp or whatever, and will be empty.
127: * If you just need a java.io.File use clearWorkDir + getWorkDir.
128: */
129: @SuppressWarnings("deprecation")
130: public static FileObject makeScratchDir(NbTestCase test)
131: throws IOException {
132: test.clearWorkDir();
133: File root = test.getWorkDir();
134: assert root.isDirectory() && root.list().length == 0;
135: FileObject fo = FileUtil.toFileObject(root);
136: if (fo != null) {
137: // Presumably using masterfs.
138: return fo;
139: } else {
140: // For the benefit of those not using masterfs.
141: LocalFileSystem lfs = new LocalFileSystem();
142: try {
143: lfs.setRootDirectory(root);
144: } catch (PropertyVetoException e) {
145: assert false : e;
146: }
147: Repository.getDefault().addFileSystem(lfs);
148: return lfs.getRoot();
149: }
150: }
151:
152: }
|