Skip to content

Commit 2b6586a

Browse files
committed
Add configuration cache compatibility and config cache test
1 parent 4c933e7 commit 2b6586a

File tree

2 files changed

+103
-69
lines changed

2 files changed

+103
-69
lines changed

typescript-generator-gradle-plugin/src/test/java/cz/habarta/typescript/generator/gradle/BuildLogicFunctionalTest.java

Lines changed: 33 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,60 @@
44
import org.gradle.testkit.runner.BuildResult;
55
import org.gradle.testkit.runner.GradleRunner;
66
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
78
import org.junit.jupiter.api.BeforeEach;
89
import org.junit.jupiter.api.Test;
910
import org.junit.jupiter.api.io.TempDir;
10-
import sun.misc.Unsafe;
1111

1212
import java.io.BufferedWriter;
1313
import java.io.File;
1414
import java.io.FileWriter;
1515
import java.io.IOException;
16-
import java.lang.reflect.Field;
1716
import java.net.URL;
18-
import java.net.URLClassLoader;
19-
import java.util.ArrayList;
20-
import java.util.Arrays;
21-
import java.util.List;
22-
import java.util.stream.Collectors;
17+
import java.nio.charset.StandardCharsets;
2318

19+
import static cz.habarta.typescript.generator.gradle.GradlePluginClasspathProvider.getClasspath;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
2421
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.CoreMatchers.containsString;
2524

2625

2726
public class BuildLogicFunctionalTest {
2827

29-
30-
String sampleGradle = "/Users/igor/typescript-generator/sample-gradle";
28+
String sampleGradle = "../../typescript-generator/sample-gradle";
3129
File sourceDir = new File(sampleGradle + "/src");
3230
@TempDir
3331
File testProjectDir;
3432
private File buildFile;
3533
private File classpathFile;
3634

3735
@BeforeEach
38-
public void setup() throws IOException {
39-
// settingsFile = new File(testProjectDir, "settings.gradle");
40-
36+
public void setup() {
4137
buildFile = new File(testProjectDir, "build.gradle");
42-
classpathFile = new File(new File(BuildLogicFunctionalTest.class.getResource("/build.gradle.template").getPath()).getParent(), "plugin-under-test-metadata.properties");
38+
classpathFile = new File(buildGradleTemplate().getParent(), "plugin-under-test-metadata.properties");
4339
}
4440

45-
4641
@Test
47-
public void testConfigurationCache() throws IOException {
48-
writeFile(classpathFile, "implementation-classpath=" + getClasspath(testProjectDir).stream().collect(Collectors.joining(File.pathSeparator)));
49-
FileUtils.copyToFile(getClass().getResourceAsStream("/build.gradle.template"), buildFile);
42+
public void shouldWorkWithConfigurationCache() throws IOException, NoSuchFieldException, IllegalAccessException {
43+
String classpath = "implementation-classpath=" + String.join(File.pathSeparator, getClasspath(testProjectDir));
44+
System.out.println("Classpath: " + classpath);
45+
writeFile(classpathFile, classpath);
46+
FileUtils.copyToFile(buildGradleTemplateUrl().openStream(), buildFile);
5047
FileUtils.copyDirectory(sourceDir, new File(testProjectDir, "src"));
48+
5149
assertTrue(runGradle("assemble").getOutput().contains("BUILD SUCCESSFUL"));
5250
BuildResult generateTypeScript = runGradle("generateTypeScript");
5351
assertTrue(generateTypeScript.getOutput().contains("BUILD SUCCESSFUL"));
52+
53+
String testFileName = testProjectDir.getName() + ".d.ts";
54+
String testFilePath = testProjectDir.toString() + "/build/typescript-generator/" + testFileName;
55+
String schema = FileUtils.readFileToString(new File(testFilePath) , StandardCharsets.UTF_8);
56+
assertThat(schema, containsString("export interface Person {\n"));
57+
assertThat(schema, containsString("export interface PersonGroovy {\n"));
58+
assertThat(schema, containsString("export interface PersonKt {\n"));
59+
assertThat(schema, containsString("export interface PersonScala {\n"));
60+
5461
}
5562

5663
private BuildResult runGradle(String task) {
@@ -59,71 +66,28 @@ private BuildResult runGradle(String task) {
5966
.withGradleVersion("8.2.1")
6067
.withPluginClasspath()
6168
.withArguments(
62-
"--stacktrace",
63-
"--info",
69+
"--stacktrace",
70+
"--info",
6471
"--configuration-cache",
6572
task
6673
)
6774
.build();
6875
}
6976

70-
private static List<String> getClasspath(File projectDir) {
71-
List<String> list = new ArrayList<>(Arrays.asList(getUrls(ClassLoader.getSystemClassLoader())).stream().map(URL::getFile).collect(Collectors.toList()));
72-
list.addAll(buildDirs(projectDir.toString()));
73-
return list;
77+
@NotNull
78+
private static File buildGradleTemplate() {
79+
return new File(buildGradleTemplateUrl().getPath());
7480
}
7581

76-
@NotNull
77-
private static List<String> buildDirs(String sampleGradle) {
78-
List<String> projectBuildDirs = new ArrayList<>();
79-
projectBuildDirs.add(sampleGradle + "/build/classes/java/main/");
80-
projectBuildDirs.add(sampleGradle + "/build/classes/groovy/main/");
81-
projectBuildDirs.add(sampleGradle + "/build/classes/scala/main/");
82-
projectBuildDirs.add(sampleGradle + "/build/classes/kotlin/main/");
83-
return projectBuildDirs;
82+
@Nullable
83+
private static URL buildGradleTemplateUrl() {
84+
return BuildLogicFunctionalTest.class.getResource("/build.gradle.template");
8485
}
8586

8687
private void writeFile(File destination, String content) throws IOException {
87-
BufferedWriter output = null;
88-
try {
89-
output = new BufferedWriter(new FileWriter(destination));
88+
try (BufferedWriter output = new BufferedWriter(new FileWriter(destination))) {
9089
output.write(content);
91-
} finally {
92-
if (output != null) {
93-
output.close();
94-
}
95-
}
96-
}
97-
98-
public static URL[] getUrls(ClassLoader classLoader) {
99-
if (classLoader instanceof URLClassLoader) {
100-
return ((URLClassLoader) classLoader).getURLs();
101-
}
102-
103-
// jdk9
104-
if (classLoader.getClass().getName().startsWith("jdk.internal.loader.ClassLoaders$")) {
105-
try {
106-
Field field = Unsafe.class.getDeclaredField("theUnsafe");
107-
field.setAccessible(true);
108-
Unsafe unsafe = (Unsafe) field.get(null);
109-
110-
// jdk.internal.loader.ClassLoaders.AppClassLoader.ucp
111-
Field ucpField = classLoader.getClass().getSuperclass().getDeclaredField("ucp");
112-
long ucpFieldOffset = unsafe.objectFieldOffset(ucpField);
113-
Object ucpObject = unsafe.getObject(classLoader, ucpFieldOffset);
114-
115-
// jdk.internal.loader.URLClassPath.path
116-
Field pathField = ucpField.getType().getDeclaredField("path");
117-
long pathFieldOffset = unsafe.objectFieldOffset(pathField);
118-
ArrayList<URL> path = (ArrayList<URL>) unsafe.getObject(ucpObject, pathFieldOffset);
119-
120-
return path.toArray(new URL[path.size()]);
121-
} catch (Exception e) {
122-
e.printStackTrace();
123-
return null;
124-
}
12590
}
126-
return null;
12791
}
12892
}
12993

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cz.habarta.typescript.generator.gradle;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import sun.misc.Unsafe;
5+
6+
import java.io.File;
7+
import java.lang.reflect.Field;
8+
import java.net.MalformedURLException;
9+
import java.net.URL;
10+
import java.net.URLClassLoader;
11+
import java.util.*;
12+
import java.util.stream.Collectors;
13+
14+
public class GradlePluginClasspathProvider {
15+
public static List<String> getClasspath(File projectDir) throws NoSuchFieldException, IllegalAccessException {
16+
List<String> list = new ArrayList<>(Arrays.asList(GradlePluginClasspathProvider.getUrls(ClassLoader.getSystemClassLoader())).stream().map(URL::getFile).collect(Collectors.toList()));
17+
list.addAll(buildDirs(projectDir.toString()));
18+
return list;
19+
}
20+
21+
public static URL[] getUrls(ClassLoader classLoader) throws NoSuchFieldException, IllegalAccessException {
22+
System.out.println(classLoader.getClass().getName());
23+
if (classLoader instanceof URLClassLoader) {
24+
return ((URLClassLoader) classLoader).getURLs();
25+
}
26+
27+
// jdk9
28+
if (classLoader.getClass().getName().startsWith("jdk.internal.loader.ClassLoaders$")) {
29+
Field field = Unsafe.class.getDeclaredField("theUnsafe");
30+
field.setAccessible(true);
31+
Unsafe unsafe = (Unsafe) field.get(null);
32+
33+
// jdk.internal.loader.ClassLoaders.AppClassLoader.ucp
34+
Field ucpField = classLoader.getClass().getSuperclass().getDeclaredField("ucp");
35+
long ucpFieldOffset = unsafe.objectFieldOffset(ucpField);
36+
Object ucpObject = unsafe.getObject(classLoader, ucpFieldOffset);
37+
38+
// jdk.internal.loader.URLClassPath.path
39+
Field pathField = ucpField.getType().getDeclaredField("path");
40+
long pathFieldOffset = unsafe.objectFieldOffset(pathField);
41+
ArrayList<URL> path = (ArrayList<URL>) unsafe.getObject(ucpObject, pathFieldOffset);
42+
43+
Field mapField = ucpField.getType().getDeclaredField("lmap");
44+
long mapFieldOffset = unsafe.objectFieldOffset(mapField);
45+
Map<String, Object> map = (Map<String, Object>) unsafe.getObject(ucpObject, mapFieldOffset);
46+
List<URL> all = new ArrayList<>();
47+
all.addAll(path);
48+
all.addAll(map.keySet().stream().map(url -> {
49+
try {
50+
return new URL(url);
51+
} catch (MalformedURLException e) {
52+
throw new RuntimeException(e);
53+
}
54+
}).collect(Collectors.toSet()));
55+
return all.toArray(new URL[0]);
56+
}
57+
return null;
58+
}
59+
60+
@NotNull
61+
private static List<String> buildDirs(String sampleGradleDir) {
62+
List<String> buildDirs = new ArrayList<>();
63+
buildDirs.add(sampleGradleDir + "/build/classes/java/main/");
64+
buildDirs.add(sampleGradleDir + "/build/classes/groovy/main/");
65+
buildDirs.add(sampleGradleDir + "/build/classes/scala/main/");
66+
buildDirs.add(sampleGradleDir + "/build/classes/kotlin/main/");
67+
return buildDirs;
68+
}
69+
70+
}

0 commit comments

Comments
 (0)