summaryrefslogtreecommitdiff
path: root/src/test/java/org/javacs/ClassesTest.java
blob: 95212fe7ba2fb292e82114cdd0eca0543c585628 (plain)
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
package org.javacs;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.Collections;
import org.javacs.guava.ClassPath;
import org.junit.Ignore;
import org.junit.Test;

public class ClassesTest {

    @Test
    public void list() {
        var jdk = Classes.jdkTopLevelClasses();
        assertThat(jdk.classes(), hasItem("java.util.List"));
        assertThat(jdk.load("java.util.List"), not(nullValue()));

        var empty = Classes.classPathTopLevelClasses(Collections.emptySet());
        assertThat(empty.classes(), not(hasItem("java.util.List")));
    }

    @Test
    public void arrayList() {
        var jdk = Classes.jdkTopLevelClasses();
        assertThat(jdk.classes(), hasItem("java.util.ArrayList"));
        assertThat(jdk.load("java.util.ArrayList"), not(nullValue()));
    }

    @Test
    @Ignore
    public void platformClassPath() throws Exception {
        var fs = FileSystems.getFileSystem(URI.create("jrt:/"));
        var path = fs.getPath("/");
        Files.walk(path).forEach(p -> System.out.println(p));
    }

    @Test
    public void loadMain() throws Exception {
        var classes = ClassPath.from(this.getClass().getClassLoader());
        var found = classes.getTopLevelClasses("org.javacs");
        assertThat(found, hasItem(hasToString("org.javacs.Main")));

        var main = found.stream().filter(c -> c.getName().equals("org.javacs.Main")).findFirst();
        assertTrue(main.isPresent());

        var load = main.get().load();
        assertNotNull(load);
    }

    void ancestors(ClassLoader classLoader) {
        while (classLoader != null) {
            System.out.println(classLoader.toString());
            classLoader = classLoader.getParent();
        }
    }

    @Test
    @Ignore
    public void printAncestors() throws Exception {
        System.out.println("This:");
        ancestors(this.getClass().getClassLoader());
        System.out.println("List:");
        ancestors(java.util.List.class.getClassLoader());
        System.out.println("System:");
        ancestors(ClassLoader.getSystemClassLoader());
        System.out.println("Platform:");
        ancestors(ClassLoader.getPlatformClassLoader());
    }
}