diff options
Diffstat (limited to 'src/test/java/org/javacs')
-rw-r--r-- | src/test/java/org/javacs/SourceFileManagerTest.java | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/test/java/org/javacs/SourceFileManagerTest.java b/src/test/java/org/javacs/SourceFileManagerTest.java new file mode 100644 index 0000000..ab40c5e --- /dev/null +++ b/src/test/java/org/javacs/SourceFileManagerTest.java @@ -0,0 +1,69 @@ +package org.javacs; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.util.*; +import java.util.logging.Logger; +import javax.tools.*; +import org.junit.Test; + +public class SourceFileManagerTest { + static final Path src = LanguageServerFixture.DEFAULT_WORKSPACE_ROOT.resolve("src"); + static final Path classes = LanguageServerFixture.DEFAULT_WORKSPACE_ROOT.resolve("target/classes"); + final SourceFileManager sourceFileManager = createSourceFileManager(); + final StandardJavaFileManager standardFileManager = createDelegateFileManager(); + + private static SourceFileManager createSourceFileManager() { + var fileManager = new SourceFileManager(Set.of(src)); + try { + fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, List.of(src)); + fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, List.of(classes)); + } catch (IOException e) { + throw new RuntimeException(e); + } + return fileManager; + } + + private static StandardJavaFileManager createDelegateFileManager() { + var compiler = ServiceLoader.load(JavaCompiler.class).iterator().next(); + var fileManager = + compiler.getStandardFileManager( + err -> LOG.severe(err.getMessage(null)), null, Charset.defaultCharset()); + try { + fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, List.of(src)); + fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, List.of(classes)); + } catch (IOException e) { + throw new RuntimeException(e); + } + return fileManager; + } + + @Test + public void binaryNameOfPackagePrivateClass() throws IOException { + var standardJava = + standardFileManager.getJavaFileForInput( + StandardLocation.SOURCE_PATH, "com.example.PackagePrivate", JavaFileObject.Kind.SOURCE); + var standardClass = + standardFileManager.getJavaFileForInput( + StandardLocation.CLASS_PATH, "com.example.PackagePrivate", JavaFileObject.Kind.CLASS); + var sourceJava = + sourceFileManager.getJavaFileForInput( + StandardLocation.SOURCE_PATH, "com.example.PackagePrivate", JavaFileObject.Kind.SOURCE); + var sourceClass = + sourceFileManager.getJavaFileForInput( + StandardLocation.CLASS_PATH, "com.example.PackagePrivate", JavaFileObject.Kind.CLASS); + var standardJavaName = standardFileManager.inferBinaryName(StandardLocation.SOURCE_PATH, standardJava); + var standardClassName = standardFileManager.inferBinaryName(StandardLocation.CLASS_PATH, standardClass); + var sourceJavaName = sourceFileManager.inferBinaryName(StandardLocation.SOURCE_PATH, sourceJava); + var sourceClassName = sourceFileManager.inferBinaryName(StandardLocation.CLASS_PATH, sourceClass); + assertThat(standardClassName, equalTo(standardJavaName)); + assertThat(sourceJavaName, equalTo(standardJavaName)); + assertThat(sourceClassName, equalTo(standardJavaName)); + } + + private static final Logger LOG = Logger.getLogger("main"); +} |