33 importClassAsync ,
44 appendClasspath ,
55 appendClasspathAny ,
6- appendClasspathDir ,
76} from '../.' ;
87import { expect } from 'chai' ;
98import { ClassTool , shouldIncreaseTimeout } from './testUtil' ;
@@ -15,7 +14,7 @@ let classTool: ClassTool | null = null;
1514interface JarClassOpts {
1615 extraImports ?: string [ ] ;
1716 extraCode ?: string ;
18- extraCompilerOpts ?: string [ ] ;
17+ classpath ?: string [ ] ;
1918}
2019
2120function createJarWithBasicClass (
@@ -33,7 +32,7 @@ function createJarWithBasicClass(
3332 ${ opts ?. extraCode ?? '' }
3433 }` ,
3534 className ,
36- opts ?. extraCompilerOpts ?? [ ]
35+ opts ?. classpath ?? [ ]
3736 ) ;
3837
3938 const fileName = `${ pkgName . replaceAll ( '.' , '/' ) } /${ className } .class` ;
@@ -123,19 +122,28 @@ describe('ClassTest', () => {
123122 extraImports : [ 'external.ExternalClass' ] ,
124123 extraCode : `
125124 private final ExternalClass ext;
126-
125+
127126 public Class2() {
128127 this.ext = new ExternalClass();
129128 }
130-
129+
131130 public ExternalClass getExt() {
132131 return this.ext;
133132 }
134133 ` ,
135- extraCompilerOpts : [
136- '-classpath' ,
137- path . join ( classTool ! . outDir , 'thirteenth.jar' ) + ':.' ,
138- ] ,
134+ classpath : [ path . join ( classTool ! . outDir , 'twelfth.jar' ) ] ,
135+ } ) ;
136+ createJarWithBasicClass ( 'dyn.external' , 'Class1' , 'fourteenth.jar' ) ;
137+ createJarWithBasicClass ( 'dyn.importing' , 'Class2' , 'fifteenth.jar' , {
138+ extraImports : [ 'dyn.external.Class1' ] ,
139+ extraCode : `
140+ public final Class<?> ext;
141+
142+ public Class2() throws ClassNotFoundException {
143+ this.ext = Class.forName("dyn.external.Class1");
144+ }
145+ ` ,
146+ classpath : [ path . join ( classTool ! . outDir , 'fourteenth.jar' ) ] ,
139147 } ) ;
140148 } ) ;
141149
@@ -265,6 +273,9 @@ describe('ClassTest', () => {
265273 } ) . timeout ( timeout ) ;
266274
267275 it ( 'Classes from multiple jars' , ( ) => {
276+ expect ( ( ) => importClass ( 'test.ClassWithPackage' ) ) . to . throw ( ) ;
277+ expect ( ( ) => importClass ( 'test.ClassWithPackageAndImport' ) ) . to . throw ( ) ;
278+
268279 appendClasspath ( [
269280 path . join ( classTool ! . outDir , 'first.jar' ) ,
270281 path . join ( classTool ! . outDir , 'second.jar' ) ,
@@ -286,6 +297,9 @@ describe('ClassTest', () => {
286297 } ) . timeout ( timeout ) ;
287298
288299 it ( 'Classes from multiple jars (async)' , async ( ) => {
300+ expect ( ( ) => importClass ( 'async.Class1' ) ) . to . throw ( ) ;
301+ expect ( ( ) => importClass ( 'async.Class2' ) ) . to . throw ( ) ;
302+
289303 appendClasspath ( [
290304 path . join ( classTool ! . outDir , 'third.jar' ) ,
291305 path . join ( classTool ! . outDir , 'fourth.jar' ) ,
@@ -307,7 +321,10 @@ describe('ClassTest', () => {
307321 } ) . timeout ( timeout ) ;
308322
309323 it ( 'Classes from multiple jars with directory import' , ( ) => {
310- appendClasspath ( path . join ( classTool ! . outDir , 'dir' ) + '/' ) ;
324+ expect ( ( ) => importClass ( 'dir.Class1' ) ) . to . throw ( ) ;
325+ expect ( ( ) => importClass ( 'dir.Class2' ) ) . to . throw ( ) ;
326+
327+ appendClasspathAny ( path . join ( classTool ! . outDir , 'dir' ) , true ) ;
311328
312329 const Class1 = importClass ( 'dir.Class1' ) ;
313330 const Class2 = importClass ( 'dir.Class2' ) ;
@@ -323,6 +340,10 @@ describe('ClassTest', () => {
323340 } ) . timeout ( timeout ) ;
324341
325342 it ( 'Classes from multiple jars with any import' , ( ) => {
343+ expect ( ( ) => importClass ( 'any.Class1' ) ) . to . throw ( ) ;
344+ expect ( ( ) => importClass ( 'any.Class2' ) ) . to . throw ( ) ;
345+ expect ( ( ) => importClass ( 'other.Class1' ) ) . to . throw ( ) ;
346+
326347 appendClasspathAny ( [
327348 path . join ( classTool ! . outDir , 'any' ) ,
328349 path . join ( classTool ! . outDir , 'ninth.jar' ) ,
@@ -347,7 +368,10 @@ describe('ClassTest', () => {
347368 } ) . timeout ( timeout ) ;
348369
349370 it ( 'Classes from multiple jars with dir import' , ( ) => {
350- appendClasspathDir ( path . join ( classTool ! . outDir , 'dir1' ) ) ;
371+ expect ( ( ) => importClass ( 'dir1.Class1' ) ) . to . throw ( ) ;
372+ expect ( ( ) => importClass ( 'dir1.Class2' ) ) . to . throw ( ) ;
373+
374+ appendClasspathAny ( path . join ( classTool ! . outDir , 'dir1' ) ) ;
351375
352376 const Class1 = importClass ( 'dir1.Class1' ) ;
353377 const Class2 = importClass ( 'dir1.Class2' ) ;
@@ -363,6 +387,9 @@ describe('ClassTest', () => {
363387 } ) . timeout ( timeout ) ;
364388
365389 it ( 'Class with external dependency' , ( ) => {
390+ expect ( ( ) => importClass ( 'importing.Class2' ) ) . to . throw ( ) ;
391+ expect ( ( ) => importClass ( 'external.ExternalClass' ) ) . to . throw ( ) ;
392+
366393 appendClasspath ( [
367394 path . join ( classTool ! . outDir , 'twelfth.jar' ) ,
368395 path . join ( classTool ! . outDir , 'thirteenth.jar' ) ,
@@ -378,6 +405,28 @@ describe('ClassTest', () => {
378405 expect ( ext ) . to . be . an ( 'object' ) ;
379406 } ) ;
380407
408+ it ( 'Class with external dependency loaded dynamically' , ( ) => {
409+ expect ( ( ) => importClass ( 'dyn.importing.Class2' ) ) . to . throw ( ) ;
410+ expect ( ( ) => importClass ( 'dyn.external.Class1' ) ) . to . throw ( ) ;
411+
412+ appendClasspath ( [
413+ path . join ( classTool ! . outDir , 'fourteenth.jar' ) ,
414+ path . join ( classTool ! . outDir , 'fifteenth.jar' ) ,
415+ ] ) ;
416+
417+ const Class2 = importClass ( 'dyn.importing.Class2' ) ;
418+ expect ( Class2 ) . to . be . a ( 'function' ) ;
419+
420+ const instance = new Class2 ( ) ;
421+ expect ( instance ) . to . be . an ( 'object' ) ;
422+
423+ const ext = instance . ext ;
424+ expect ( ext ) . to . be . an ( 'object' ) ;
425+
426+ const instance2 = ext . newInstanceSync ( ) ;
427+ expect ( instance2 ) . to . be . an ( 'object' ) ;
428+ } ) ;
429+
381430 after ( ( ) => {
382431 classTool ?. dispose ( ) ;
383432 } ) ;
0 commit comments