1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
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.Before;
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();
try {
fileManager.setLocation(StandardLocation.SOURCE_PATH, List.of(src.toFile()));
fileManager.setLocation(StandardLocation.CLASS_PATH, List.of(classes.toFile()));
} 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;
}
@Before
public void setWorkspaceRoot() {
FileStore.setWorkspaceRoots(Set.of(LanguageServerFixture.DEFAULT_WORKSPACE_ROOT));
}
@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));
}
@Test
public void javaUtilList() throws IOException {
var file =
sourceFileManager.getJavaFileForInput(
StandardLocation.PLATFORM_CLASS_PATH, "java.util.List", JavaFileObject.Kind.CLASS);
assertThat("Found java.util.List in platform classpath", file, notNullValue());
var header = ClassHeader.of(file.openInputStream());
assertTrue(header.isInterface);
assertTrue(header.isAbstract);
assertTrue(header.isPublic);
}
private static final Logger LOG = Logger.getLogger("main");
}
|