44import org .gradle .testkit .runner .BuildResult ;
55import org .gradle .testkit .runner .GradleRunner ;
66import org .jetbrains .annotations .NotNull ;
7+ import org .jetbrains .annotations .Nullable ;
78import org .junit .jupiter .api .BeforeEach ;
89import org .junit .jupiter .api .Test ;
910import org .junit .jupiter .api .io .TempDir ;
10- import sun .misc .Unsafe ;
1111
1212import java .io .BufferedWriter ;
1313import java .io .File ;
1414import java .io .FileWriter ;
1515import java .io .IOException ;
16- import java .lang .reflect .Field ;
1716import 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 ;
2421import static org .junit .jupiter .api .Assertions .assertTrue ;
22+ import static org .hamcrest .MatcherAssert .assertThat ;
23+ import static org .hamcrest .CoreMatchers .containsString ;
2524
2625
2726public 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
0 commit comments