001: /*
002: * ElementHeadersTest.java
003: * JUnit based test
004: *
005: * Created on June 12, 2007, 6:13 PM
006: */
007:
008: package org.netbeans.api.java.source.ui;
009:
010: import com.sun.source.util.TreePath;
011: import java.io.File;
012: import javax.swing.text.Document;
013: import org.netbeans.api.java.lexer.JavaTokenId;
014: import org.netbeans.api.java.source.CompilationInfo;
015: import org.netbeans.api.java.source.JavaSource;
016: import org.netbeans.api.java.source.JavaSource.Phase;
017: import org.netbeans.api.java.source.SourceUtilsTestUtil;
018: import org.netbeans.api.java.source.TestUtilities;
019: import org.netbeans.api.lexer.Language;
020: import org.netbeans.junit.NbTestCase;
021: import org.netbeans.modules.java.JavaDataLoader;
022: import org.openide.cookies.EditorCookie;
023: import org.openide.filesystems.FileObject;
024: import org.openide.filesystems.FileUtil;
025: import org.openide.loaders.DataObject;
026:
027: /**
028: *
029: * @author phrebejk
030: */
031: public class ElementHeadersTest extends NbTestCase {
032:
033: public ElementHeadersTest(String testName) {
034: super (testName);
035: }
036:
037: @Override
038: protected void setUp() throws Exception {
039: super .setUp();
040: SourceUtilsTestUtil.prepareTest(new String[0],
041: new Object[] { JavaDataLoader.class });
042: }
043:
044: private void prepareTest(String fileName, String code)
045: throws Exception {
046: clearWorkDir();
047:
048: FileObject workFO = FileUtil.toFileObject(getWorkDir());
049:
050: assertNotNull(workFO);
051:
052: sourceRoot = workFO.createFolder("src");
053:
054: FileObject buildRoot = workFO.createFolder("build");
055: FileObject cache = workFO.createFolder("cache");
056:
057: FileObject data = FileUtil.createData(sourceRoot, fileName);
058: File dataFile = FileUtil.toFile(data);
059:
060: assertNotNull(dataFile);
061:
062: TestUtilities.copyStringToFile(dataFile, code);
063:
064: SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache);
065:
066: DataObject od = DataObject.find(data);
067: EditorCookie ec = od.getCookie(EditorCookie.class);
068:
069: assertNotNull(ec);
070:
071: doc = ec.openDocument();
072: doc.putProperty(Language.class, JavaTokenId.language());
073:
074: JavaSource js = JavaSource.forFileObject(data);
075:
076: assertNotNull(js);
077:
078: info = SourceUtilsTestUtil.getCompilationInfo(js,
079: Phase.RESOLVED);
080:
081: assertNotNull(info);
082: }
083:
084: private FileObject sourceRoot;
085: private CompilationInfo info;
086: private Document doc;
087:
088: protected void performTest(String fileName, String code, int pos,
089: String format, String golden) throws Exception {
090: prepareTest(fileName, code);
091:
092: TreePath path = info.getTreeUtilities().pathFor(pos);
093:
094: assertEquals(golden, ElementHeaders.getHeader(path, info,
095: format));
096: }
097:
098: public void testField() throws Exception {
099: performTest("test/Test.java",
100: "package test; public class Test { int aa; }", 39,
101: ElementHeaders.NAME, "aa");
102: performTest("test/Test.java",
103: "package test; public class Test { int aa; }", 39,
104: ElementHeaders.NAME, "aa");
105: performTest("test/Test.java",
106: "package test; public class Test { int aa; }", 39,
107: ElementHeaders.NAME, "aa");
108: performTest("test/Test.java",
109: "package test; public class Test { int aa; }", 39,
110: ElementHeaders.NAME, "aa");
111: performTest("test/Test.java",
112: "package test; public class Test { int aa; }", 39,
113: ElementHeaders.NAME, "aa");
114: performTest("test/Test.java",
115: "package test; public class Test { int aa; }", 39,
116: ElementHeaders.NAME, "aa");
117: }
118: }
|