| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package ch.grengine.code.groovy; |
| 18 |
|
| 19 |
import ch.grengine.code.DefaultCode; |
| 20 |
import ch.grengine.source.Source; |
| 21 |
import ch.grengine.source.SourceUtil; |
| 22 |
import ch.grengine.TestUtil; |
| 23 |
import ch.grengine.code.Code; |
| 24 |
import ch.grengine.code.CompilerFactory; |
| 25 |
import ch.grengine.code.DefaultSingleSourceCode; |
| 26 |
import ch.grengine.except.CompileException; |
| 27 |
import ch.grengine.load.BytecodeClassLoader; |
| 28 |
import ch.grengine.load.LoadMode; |
| 29 |
import ch.grengine.source.DefaultSourceFactory; |
| 30 |
import ch.grengine.source.MockSource; |
| 31 |
import ch.grengine.source.SourceFactory; |
| 32 |
import ch.grengine.sources.Sources; |
| 33 |
import ch.grengine.sources.SourcesUtil; |
| 34 |
|
| 35 |
import java.io.File; |
| 36 |
import java.lang.reflect.Method; |
| 37 |
import java.net.URI; |
| 38 |
import java.util.HashMap; |
| 39 |
import java.util.List; |
| 40 |
import java.util.Map; |
| 41 |
import java.util.Set; |
| 42 |
import java.util.stream.IntStream; |
| 43 |
|
| 44 |
import groovy.grape.Grape; |
| 45 |
import groovy.grape.GrapeEngine; |
| 46 |
import groovy.lang.GroovyClassLoader; |
| 47 |
import org.codehaus.groovy.control.CompilerConfiguration; |
| 48 |
import org.codehaus.groovy.control.customizers.ImportCustomizer; |
| 49 |
import org.hamcrest.CoreMatchers; |
| •50 |
import org.junit.jupiter.api.Disabled; |
| 51 |
import org.junit.jupiter.api.Test; |
| 52 |
|
| 53 |
import static ch.grengine.TestUtil.assertThrowsMessageIs; |
| 54 |
import static ch.grengine.TestUtil.createTestDir; |
| 55 |
import static ch.grengine.TestUtil.toRuntimeException; |
| 56 |
import static java.util.stream.Collectors.toList; |
| 57 |
import static org.hamcrest.CoreMatchers.containsString; |
| 58 |
import static org.hamcrest.CoreMatchers.instanceOf; |
| 59 |
import static org.hamcrest.CoreMatchers.is; |
| 60 |
import static org.hamcrest.CoreMatchers.notNullValue; |
| 61 |
import static org.hamcrest.CoreMatchers.nullValue; |
| 62 |
import static org.hamcrest.CoreMatchers.sameInstance; |
| 63 |
import static org.hamcrest.MatcherAssert.assertThat; |
| 64 |
import static org.junit.jupiter.api.Assertions.fail; |
| 65 |
|
| 66 |
|
| 67 |
class DefaultGroovyCompilerTest { |
| 68 |
|
| 69 |
|
| 70 |
private static final int GRAPE_INSTANCE_DEFAULT_CALLEE_DEPTH = 3; |
| 71 |
|
| 72 |
@Test |
| 73 |
void testConstructDefaults() { |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
final DefaultGroovyCompiler.Builder builder = new DefaultGroovyCompiler.Builder(); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
final DefaultGroovyCompiler c = builder.build(); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
assertThat(c.getBuilder(), CoreMatchers.is(builder)); |
| 86 |
assertThat(c.getParent(), is(Thread.currentThread().getContextClassLoader())); |
| 87 |
assertThat(c.getParent(), is(c.getBuilder().getParent())); |
| 88 |
assertThat(c.getCompilerConfiguration(), is(notNullValue())); |
| 89 |
assertThat(c.getCompilerConfiguration(), is(c.getBuilder().getCompilerConfiguration())); |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
void testConstructAllDefined() { |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
final DefaultGroovyCompiler.Builder builder = new DefaultGroovyCompiler.Builder(); |
| 98 |
final ClassLoader parent = Thread.currentThread().getContextClassLoader().getParent(); |
| 99 |
final CompilerConfiguration config = new CompilerConfiguration(); |
| 100 |
builder.setParent(parent); |
| 101 |
builder.setCompilerConfiguration(config); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
final DefaultGroovyCompiler c = builder.build(); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
assertThat(c.getBuilder(), CoreMatchers.is(builder)); |
| 110 |
assertThat(c.getParent(), is(parent)); |
| 111 |
assertThat(c.getParent(), is(c.getBuilder().getParent())); |
| 112 |
assertThat(c.getCompilerConfiguration(), is(config)); |
| 113 |
assertThat(c.getCompilerConfiguration(), is(c.getBuilder().getCompilerConfiguration())); |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
void testModifyBuilderAfterUse() { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
final DefaultGroovyCompiler.Builder builder = new DefaultGroovyCompiler.Builder(); |
| 122 |
builder.build(); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
assertThrowsMessageIs(IllegalStateException.class, |
| 127 |
() -> builder.setParent(Thread.currentThread().getContextClassLoader()), |
| 128 |
"Builder already used."); |
| 129 |
} |
| 130 |
|
| 131 |
@Test |
| 132 |
void testConstructorNoArgs() { |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
final DefaultGroovyCompiler c = new DefaultGroovyCompiler(); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
assertThat(c.getParent(), is(Thread.currentThread().getContextClassLoader())); |
| 141 |
assertThat(c.getCompilerConfiguration(), is(notNullValue())); |
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
void testConstructorFromParent() { |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
final ClassLoader parent = Thread.currentThread().getContextClassLoader().getParent(); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
final DefaultGroovyCompiler c = new DefaultGroovyCompiler(parent); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
assertThat(c.getParent(), is(parent)); |
| 158 |
assertThat(c.getCompilerConfiguration(), is(notNullValue())); |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
void testConstructorFromParentAndConfig() { |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
final ClassLoader parent = Thread.currentThread().getContextClassLoader().getParent(); |
| 167 |
final CompilerConfiguration config = new CompilerConfiguration(); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
final DefaultGroovyCompiler c = new DefaultGroovyCompiler(parent, config); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
assertThat(c.getParent(), is(parent)); |
| 176 |
assertThat(c.getCompilerConfiguration(), is(config)); |
| 177 |
} |
| 178 |
|
| 179 |
@Test |
| 180 |
void testConstructFromParentNull() { |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
assertThrowsMessageIs(NullPointerException.class, |
| 185 |
() -> new DefaultGroovyCompiler((ClassLoader) null), |
| 186 |
"Parent class loader is null."); |
| 187 |
} |
| 188 |
|
| 189 |
@Test |
| 190 |
void testConstructFromParentAndConfigParentNull() { |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
assertThrowsMessageIs(NullPointerException.class, |
| 195 |
() -> new DefaultGroovyCompiler(null, new CompilerConfiguration()), |
| 196 |
"Parent class loader is null."); |
| 197 |
} |
| 198 |
|
| 199 |
@Test |
| 200 |
void testConstructFromParentAndConfigConfigNull() { |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
assertThrowsMessageIs(NullPointerException.class, |
| 205 |
() -> new DefaultGroovyCompiler(Thread.currentThread().getContextClassLoader(), null), |
| 206 |
"Compiler configuration is null."); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
@Test |
| 211 |
void testCompileBasic() throws Exception { |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
final DefaultGroovyCompiler c = new DefaultGroovyCompiler(); |
| 216 |
|
| 217 |
final SourceFactory f = new DefaultSourceFactory(); |
| 218 |
final Source textSource = f.fromText("println 'text source'"); |
| 219 |
final String expectedTextSourceMainClassName = "Script" + SourceUtil.md5("println 'text source'"); |
| 220 |
final Source textSourceWithName = f.fromText("println 'text source'", "MyTextScript"); |
| 221 |
final File dir = createTestDir(); |
| 222 |
final File scriptFile = new File(dir, "MyFileScript.groovy"); |
| 223 |
TestUtil.setFileText(scriptFile, "println 'file source'"); |
| 224 |
final Source fileSource = f.fromFile(scriptFile); |
| 225 |
final File urlScriptFile = new File(dir, "MyUrlScript.groovy"); |
| 226 |
TestUtil.setFileText(urlScriptFile, "println 'url source'"); |
| 227 |
final Source urlSource = f.fromUrl(urlScriptFile.toURI().toURL()); |
| 228 |
final Set<Source> sourceSet = SourceUtil.sourceArrayToSourceSet( |
| 229 |
textSource, textSourceWithName, fileSource, urlSource); |
| 230 |
final Sources sources = SourcesUtil.sourceSetToSources(sourceSet, "basic"); |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
final DefaultCode code = (DefaultCode) c.compile(sources); |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
assertThat(code.toString(), is("DefaultCode[sourcesName='basic', sources:4, classes:4]")); |
| 240 |
|
| 241 |
assertThat(code.getSourcesName(), is("basic")); |
| 242 |
|
| 243 |
assertThat(code.getSourceSet().size(), is(4)); |
| 244 |
assertThat(code.isForSource(textSource), is(true)); |
| 245 |
assertThat(code.isForSource(textSourceWithName), is(true)); |
| 246 |
assertThat(code.isForSource(fileSource), is(true)); |
| 247 |
assertThat(code.isForSource(urlSource), is(true)); |
| 248 |
|
| 249 |
assertThat(code.getMainClassName(textSource), is(expectedTextSourceMainClassName)); |
| 250 |
assertThat(code.getMainClassName(textSourceWithName), is("MyTextScript")); |
| 251 |
assertThat(code.getMainClassName(fileSource), is("MyFileScript")); |
| 252 |
assertThat(code.getMainClassName(urlSource), is("MyUrlScript")); |
| 253 |
|
| 254 |
assertThat(code.getLastModifiedAtCompileTime(fileSource), is(scriptFile.lastModified())); |
| 255 |
|
| 256 |
assertThat(code.getClassNameSet().size(), is(4)); |
| 257 |
assertThat(code.getBytecode(expectedTextSourceMainClassName), is(notNullValue())); |
| 258 |
assertThat(code.getBytecode("MyTextScript"), is(notNullValue())); |
| 259 |
assertThat(code.getBytecode("MyFileScript"), is(notNullValue())); |
| 260 |
assertThat(code.getBytecode("MyUrlScript"), is(notNullValue())); |
| 261 |
} |
| 262 |
|
| 263 |
@Test |
| 264 |
void testCompileBasicWithTargetDir() throws Exception { |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
final File dir = createTestDir(); |
| 269 |
final File targetDir = new File(dir, "target"); |
| 270 |
assertThat(targetDir.mkdir(), is(true)); |
| 271 |
assertThat(targetDir.exists(), is(true)); |
| 272 |
final CompilerConfiguration config = new CompilerConfiguration(); |
| 273 |
config.setTargetDirectory(targetDir); |
| 274 |
|
| 275 |
final CompilerFactory compilerFactory = new DefaultGroovyCompilerFactory(config); |
| 276 |
|
| 277 |
final ClassLoader parent = Thread.currentThread().getContextClassLoader(); |
| 278 |
|
| 279 |
final DefaultGroovyCompiler c = (DefaultGroovyCompiler) compilerFactory.newCompiler(parent); |
| 280 |
|
| 281 |
final SourceFactory f = new DefaultSourceFactory(); |
| 282 |
final Source textSource = f.fromText("println 'text source'"); |
| 283 |
final String expectedTextSourceMainClassName = "Script" + SourceUtil.md5("println 'text source'"); |
| 284 |
final Source textSourceWithName = f.fromText("println 'text source'", "MyTextScript"); |
| 285 |
final File scriptFile = new File(dir, "MyFileScript.groovy"); |
| 286 |
TestUtil.setFileText(scriptFile, "println 'file source'"); |
| 287 |
final Source fileSource = f.fromFile(scriptFile); |
| 288 |
final File urlScriptFile = new File(dir, "MyUrlScript.groovy"); |
| 289 |
TestUtil.setFileText(urlScriptFile, "println 'url source'"); |
| 290 |
final Source urlSource = f.fromUrl(urlScriptFile.toURI().toURL()); |
| 291 |
final Set<Source> sourceSet = SourceUtil.sourceArrayToSourceSet( |
| 292 |
textSource, textSourceWithName, fileSource, urlSource); |
| 293 |
final Sources sources = SourcesUtil.sourceSetToSources(sourceSet, "basic", compilerFactory); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
final DefaultCode code = (DefaultCode) c.compile(sources); |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
assertThat(code.toString(), is("DefaultCode[sourcesName='basic', sources:4, classes:4]")); |
| 302 |
|
| 303 |
assertThat(code.getSourcesName(), is("basic")); |
| 304 |
|
| 305 |
assertThat(code.getSourceSet().size(), is(4)); |
| 306 |
assertThat(code.isForSource(textSource), is(true)); |
| 307 |
assertThat(code.isForSource(textSourceWithName), is(true)); |
| 308 |
assertThat(code.isForSource(fileSource), is(true)); |
| 309 |
assertThat(code.isForSource(urlSource), is(true)); |
| 310 |
|
| 311 |
assertThat(code.getMainClassName(textSource), is(expectedTextSourceMainClassName)); |
| 312 |
assertThat(code.getMainClassName(textSourceWithName), is("MyTextScript")); |
| 313 |
assertThat(code.getMainClassName(fileSource), is("MyFileScript")); |
| 314 |
assertThat(code.getMainClassName(urlSource), is("MyUrlScript")); |
| 315 |
|
| 316 |
assertThat(code.getLastModifiedAtCompileTime(fileSource), is(scriptFile.lastModified())); |
| 317 |
|
| 318 |
assertThat(code.getClassNameSet().size(), is(4)); |
| 319 |
assertThat(code.getBytecode(expectedTextSourceMainClassName), is(notNullValue())); |
| 320 |
assertThat(code.getBytecode("MyTextScript"), is(notNullValue())); |
| 321 |
assertThat(code.getBytecode("MyFileScript"), is(notNullValue())); |
| 322 |
assertThat(code.getBytecode("MyUrlScript"), is(notNullValue())); |
| 323 |
|
| 324 |
assertThat(new File(targetDir, expectedTextSourceMainClassName + ".class").exists(), is(true)); |
| 325 |
assertThat(new File(targetDir, "MyTextScript.class").exists(), is(true)); |
| 326 |
assertThat(new File(targetDir, "MyFileScript.class").exists(), is(true)); |
| 327 |
assertThat(new File(targetDir, "MyUrlScript.class").exists(), is(true)); |
| 328 |
} |
| 329 |
|
| 330 |
@Test |
| 331 |
void testCompileBasicSingleSource() { |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
final DefaultGroovyCompiler c = new DefaultGroovyCompiler(); |
| 336 |
|
| 337 |
final SourceFactory f = new DefaultSourceFactory(); |
| 338 |
final Source textSource = f.fromText("println 'text source'"); |
| 339 |
final String expectedTextSourceMainClassName = "Script" + SourceUtil.md5("println 'text source'"); |
| 340 |
final Set<Source> sourceSet = SourceUtil.sourceArrayToSourceSet(textSource); |
| 341 |
final Sources sources = SourcesUtil.sourceSetToSources(sourceSet, "basicSingle"); |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
final DefaultSingleSourceCode code = (DefaultSingleSourceCode) c.compile(sources); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
assertThat(code.toString(), is("DefaultSingleSourceCode[sourcesName='basicSingle', " + |
| 350 |
"mainClassName=" + expectedTextSourceMainClassName + ", classes:[" + expectedTextSourceMainClassName + |
| 351 |
"]]")); |
| 352 |
|
| 353 |
assertThat(code.getSourcesName(), is("basicSingle")); |
| 354 |
|
| 355 |
assertThat(code.getSourceSet().size(), is(1)); |
| 356 |
assertThat(code.isForSource(textSource), is(true)); |
| 357 |
|
| 358 |
assertThat(code.getMainClassName(textSource), is(expectedTextSourceMainClassName)); |
| 359 |
|
| 360 |
assertThat(code.getClassNameSet().size(), is(1)); |
| 361 |
assertThat(code.getBytecode(expectedTextSourceMainClassName), is(notNullValue())); |
| 362 |
|
| 363 |
assertThat(code.getSource(), is(textSource)); |
| 364 |
assertThat(code.getMainClassName(), is(expectedTextSourceMainClassName)); |
| 365 |
} |
| 366 |
|
| 367 |
@Test |
| 368 |
void testCompileSourcesNull() { |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
assertThrowsMessageIs(NullPointerException.class, |
| 373 |
() -> new DefaultGroovyCompiler().compile(null), |
| 374 |
"Sources are null."); |
| 375 |
} |
| 376 |
|
| 377 |
@Test |
| 378 |
void testCompileFailsSyntaxWrong() throws Exception { |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
final DefaultGroovyCompiler c = new DefaultGroovyCompiler(); |
| 383 |
|
| 384 |
final SourceFactory f = new DefaultSourceFactory(); |
| 385 |
final Source textSource = f.fromText("%%)("); |
| 386 |
final Set<Source> sourceSet = SourceUtil.sourceArrayToSourceSet(textSource); |
| 387 |
final Sources sources = SourcesUtil.sourceSetToSources(sourceSet, "syntaxWrong"); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
try { |
| 392 |
c.compile(sources); |
| 393 |
fail("did not throw"); |
| 394 |
} catch (CompileException e) { |
| 395 |
|
| 396 |
assertThat(e.getMessage().startsWith("Compile failed for sources FixedSetSources[name='syntaxWrong']. " + |
| 397 |
"Cause: org.codehaus.groovy.control.MultipleCompilationErrorsException:"), is(true)); |
| 398 |
assertThat(e.getSources(), is(sources)); |
| 399 |
Thread.sleep(30); |
| 400 |
assertThat(e.getDateThrown().getTime() < System.currentTimeMillis(), is(true)); |
| 401 |
assertThat(e.getDateThrown().getTime() > System.currentTimeMillis() - 5000, is(true)); |
| 402 |
|
| 403 |
assertThat(e.getCause().toString().startsWith( |
| 404 |
"org.codehaus.groovy.control.MultipleCompilationErrorsException:"), is(true)); |
| 405 |
|
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
@Test |
| 410 |
void testCompileFailsUnknownSource() throws Exception { |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
final DefaultGroovyCompiler c = new DefaultGroovyCompiler(); |
| 415 |
|
| 416 |
final Source mockSource = new MockSource("id1"); |
| 417 |
final Set<Source> sourceSet = SourceUtil.sourceArrayToSourceSet(mockSource); |
| 418 |
final Sources sources = SourcesUtil.sourceSetToSources(sourceSet, "unknownSource"); |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
try { |
| 423 |
c.compile(sources); |
| 424 |
fail("did not throw"); |
| 425 |
} catch (CompileException e) { |
| 426 |
|
| 427 |
assertThat(e.getMessage(), is("Don't know how to compile source MockSource[ID='id1', lastModified=0].")); |
| 428 |
assertThat(e.getSources(), is(sources)); |
| 429 |
Thread.sleep(30); |
| 430 |
assertThat(e.getDateThrown().getTime() < System.currentTimeMillis(), is(true)); |
| 431 |
assertThat(e.getDateThrown().getTime() > System.currentTimeMillis() - 5000, is(true)); |
| 432 |
assertThat(e.getCause(), is(nullValue())); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
@Test |
| 437 |
void testCompileFailsSameClassNameTwice() throws Exception { |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
final DefaultGroovyCompiler c = new DefaultGroovyCompiler(); |
| 442 |
|
| 443 |
final SourceFactory f = new DefaultSourceFactory(); |
| 444 |
final Source s1 = f.fromText("public class Twice { public def x() {} }"); |
| 445 |
final Source s2 = f.fromText("public class Twice { public def y() {} }"); |
| 446 |
final Set<Source> sourceSet = SourceUtil.sourceArrayToSourceSet(s1, s2); |
| 447 |
final Sources sources = SourcesUtil.sourceSetToSources(sourceSet, "twice"); |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
try { |
| 452 |
c.compile(sources); |
| 453 |
fail("did not throw"); |
| 454 |
} catch (CompileException e) { |
| 455 |
|
| 456 |
assertThat(e.getMessage().startsWith("Compile failed for sources FixedSetSources[name='twice']. Cause: "), is(true)); |
| 457 |
assertThat(e.getMessage().contains("Invalid duplicate class definition of class Twice"), is(true)); |
| 458 |
assertThat(e.getSources(), is(sources)); |
| 459 |
Thread.sleep(30); |
| 460 |
assertThat(e.getDateThrown().getTime() < System.currentTimeMillis(), is(true)); |
| 461 |
assertThat(e.getDateThrown().getTime() > System.currentTimeMillis() - 5000, is(true)); |
| 462 |
|
| 463 |
assertThat(e.getCause().getMessage().contains("Invalid duplicate class definition of class Twice"), is(true)); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
@Test |
| 468 |
void testCompileSameClassNameTwiceDifferentLoaders() throws Exception { |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
final SourceFactory f = new DefaultSourceFactory(); |
| 473 |
final Source s1 = f.fromText("public class Twice { public def get() { return Inner1.get() }\n" + |
| 474 |
"public class Inner1 { static def get() { return 1 } } }"); |
| 475 |
final Source s2 = f.fromText("public class Twice { public def get() { return Inner2.get() }\n" + |
| 476 |
"public class Inner2 { static def get() { return 2 } } }"); |
| 477 |
final Set<Source> sourceSet1 = SourceUtil.sourceArrayToSourceSet(s1); |
| 478 |
final Set<Source> sourceSet2 = SourceUtil.sourceArrayToSourceSet(s2); |
| 479 |
final Sources sources1 = SourcesUtil.sourceSetToSources(sourceSet1, "sources1"); |
| 480 |
final Sources sources2 = SourcesUtil.sourceSetToSources(sourceSet2, "sources2"); |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
final ClassLoader parent = Thread.currentThread().getContextClassLoader(); |
| 485 |
final DefaultGroovyCompiler c1 = new DefaultGroovyCompiler(parent); |
| 486 |
final Code code1 = c1.compile(sources1); |
| 487 |
final ClassLoader loader1 = new BytecodeClassLoader(parent, LoadMode.PARENT_FIRST, code1); |
| 488 |
final Class<?> clazz1 = loader1.loadClass("Twice"); |
| 489 |
final Object obj1 = clazz1.getConstructor().newInstance(); |
| 490 |
final Method method1 = clazz1.getDeclaredMethod("get"); |
| 491 |
assertThat(method1.invoke(obj1), is(1)); |
| 492 |
loader1.loadClass("Twice$Inner1"); |
| 493 |
assertThrowsMessageIs(ClassNotFoundException.class, |
| 494 |
() -> loader1.loadClass("Twice$Inner2"), |
| 495 |
"Twice$Inner2"); |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
final DefaultGroovyCompiler c2 = new DefaultGroovyCompiler(loader1); |
| 500 |
final Code code2 = c2.compile(sources2); |
| 501 |
final ClassLoader loader2 = new BytecodeClassLoader(loader1, LoadMode.CURRENT_FIRST, code2); |
| 502 |
final Class<?> clazz2 = loader2.loadClass("Twice"); |
| 503 |
final Object obj2 = clazz2.getConstructor().newInstance(); |
| 504 |
final Method method2 = clazz2.getDeclaredMethod("get"); |
| 505 |
assertThat(method2.invoke(obj2), is(2)); |
| 506 |
loader2.loadClass("Twice$Inner1"); |
| 507 |
loader2.loadClass("Twice$Inner2"); |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
final ClassLoader loader22 = new BytecodeClassLoader(loader1, LoadMode.PARENT_FIRST, code2); |
| 512 |
final Class<?> clazz22 = loader22.loadClass("Twice"); |
| 513 |
final Object obj22 = clazz22.getConstructor().newInstance(); |
| 514 |
final Method method22 = clazz22.getDeclaredMethod("get"); |
| 515 |
assertThat(method22.invoke(obj22), is(1)); |
| 516 |
loader22.loadClass("Twice$Inner1"); |
| 517 |
loader22.loadClass("Twice$Inner2"); |
| 518 |
} |
| 519 |
|
| 520 |
@Test |
| 521 |
void testWithGrape() { |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
final CompilerConfiguration config = new CompilerConfiguration(); |
| 526 |
final GroovyClassLoader loader = new GroovyClassLoader(); |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
final CompilerConfiguration config2 = DefaultGroovyCompiler.withGrape(config, loader); |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
assertThat(config2, sameInstance(config)); |
| 535 |
assertThat(config.getCompilationCustomizers().size(), is(1)); |
| 536 |
assertThat(config.getCompilationCustomizers().get(0), |
| 537 |
instanceOf(DefaultGroovyCompiler.GrapeCompilationCustomizer.class)); |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
final DefaultGroovyCompiler.GrapeCompilationCustomizer customizer = |
| 542 |
(DefaultGroovyCompiler.GrapeCompilationCustomizer) config.getCompilationCustomizers().get(0); |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
assertThat(customizer.runtimeLoader, sameInstance(loader)); |
| 547 |
|
| 548 |
customizer.call(null, null, null); |
| 549 |
} |
| 550 |
|
| 551 |
@Test |
| 552 |
void testWithGrape_configNull() { |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
final GroovyClassLoader loader = new GroovyClassLoader(); |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
assertThrowsMessageIs(NullPointerException.class, |
| 561 |
() -> DefaultGroovyCompiler.withGrape(null, loader), |
| 562 |
"Compiler configuration is null."); |
| 563 |
} |
| 564 |
|
| 565 |
@Test |
| 566 |
void testEnableDisableGrapeSupportDefault() { |
| 567 |
try { |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
DefaultGroovyCompiler.enableGrapeSupport(); |
| 572 |
GrapeEngine engine = Grape.getInstance(); |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
assertThat(engine, instanceOf(DefaultGroovyCompiler.GrengineGrapeEngine.class)); |
| 577 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(Grape.class)); |
| 578 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, |
| 579 |
is(GRAPE_INSTANCE_DEFAULT_CALLEE_DEPTH)); |
| 580 |
assertThat(((DefaultGroovyCompiler.GrengineGrapeEngine) engine).innerEngine.getClass().getName(), |
| 581 |
is("groovy.grape.GrapeIvy")); |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
DefaultGroovyCompiler.enableGrapeSupport(); |
| 586 |
engine = Grape.getInstance(); |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
assertThat(engine, instanceOf(DefaultGroovyCompiler.GrengineGrapeEngine.class)); |
| 591 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(Grape.class)); |
| 592 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, |
| 593 |
is(GRAPE_INSTANCE_DEFAULT_CALLEE_DEPTH)); |
| 594 |
assertThat(((DefaultGroovyCompiler.GrengineGrapeEngine) engine).innerEngine.getClass().getName(), |
| 595 |
is("groovy.grape.GrapeIvy")); |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 600 |
engine = Grape.getInstance(); |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
assertThat(engine.getClass().getName(), is("groovy.grape.GrapeIvy")); |
| 605 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(nullValue())); |
| 606 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, is(0)); |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 611 |
engine = Grape.getInstance(); |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
assertThat(engine.getClass().getName(), is("groovy.grape.GrapeIvy")); |
| 616 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(nullValue())); |
| 617 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, is(0)); |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
DefaultGroovyCompiler.enableGrapeSupport(); |
| 622 |
engine = Grape.getInstance(); |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
assertThat(engine, instanceOf(DefaultGroovyCompiler.GrengineGrapeEngine.class)); |
| 627 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(Grape.class)); |
| 628 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, |
| 629 |
is(GRAPE_INSTANCE_DEFAULT_CALLEE_DEPTH)); |
| 630 |
assertThat(((DefaultGroovyCompiler.GrengineGrapeEngine) engine).innerEngine.getClass().getName(), |
| 631 |
is("groovy.grape.GrapeIvy")); |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 636 |
engine = Grape.getInstance(); |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
assertThat(engine.getClass().getName(), is("groovy.grape.GrapeIvy")); |
| 641 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(nullValue())); |
| 642 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, is(0)); |
| 643 |
|
| 644 |
} finally { |
| 645 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
@Test |
| 650 |
void testEnableDisableGrapeSupportSpecificLock() { |
| 651 |
try { |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
final Object lock = String.class; |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
DefaultGroovyCompiler.enableGrapeSupport(lock); |
| 660 |
GrapeEngine engine = Grape.getInstance(); |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
assertThat(engine, instanceOf(DefaultGroovyCompiler.GrengineGrapeEngine.class)); |
| 665 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(lock)); |
| 666 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, |
| 667 |
is(GRAPE_INSTANCE_DEFAULT_CALLEE_DEPTH)); |
| 668 |
assertThat(((DefaultGroovyCompiler.GrengineGrapeEngine) engine).innerEngine.getClass().getName(), |
| 669 |
is("groovy.grape.GrapeIvy")); |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
DefaultGroovyCompiler.enableGrapeSupport(lock); |
| 674 |
engine = Grape.getInstance(); |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
assertThat(engine, instanceOf(DefaultGroovyCompiler.GrengineGrapeEngine.class)); |
| 679 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(lock)); |
| 680 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, |
| 681 |
is(GRAPE_INSTANCE_DEFAULT_CALLEE_DEPTH)); |
| 682 |
assertThat(((DefaultGroovyCompiler.GrengineGrapeEngine) engine).innerEngine.getClass().getName(), |
| 683 |
is("groovy.grape.GrapeIvy")); |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 688 |
engine = Grape.getInstance(); |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
assertThat(engine.getClass().getName(), is("groovy.grape.GrapeIvy")); |
| 693 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(nullValue())); |
| 694 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, is(0)); |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 699 |
engine = Grape.getInstance(); |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
assertThat(engine.getClass().getName(), is("groovy.grape.GrapeIvy")); |
| 704 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(nullValue())); |
| 705 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, is(0)); |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
DefaultGroovyCompiler.enableGrapeSupport(lock); |
| 710 |
engine = Grape.getInstance(); |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
assertThat(engine, instanceOf(DefaultGroovyCompiler.GrengineGrapeEngine.class)); |
| 715 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(lock)); |
| 716 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, |
| 717 |
is(GRAPE_INSTANCE_DEFAULT_CALLEE_DEPTH)); |
| 718 |
assertThat(((DefaultGroovyCompiler.GrengineGrapeEngine) engine).innerEngine.getClass().getName(), |
| 719 |
is("groovy.grape.GrapeIvy")); |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 724 |
engine = Grape.getInstance(); |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
assertThat(engine.getClass().getName(), is("groovy.grape.GrapeIvy")); |
| 729 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.lock, is(nullValue())); |
| 730 |
assertThat(DefaultGroovyCompiler.GrengineGrapeEngine.grapeInstanceDefaultCalleeDepth, is(0)); |
| 731 |
|
| 732 |
} finally { |
| 733 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
@Test |
| 738 |
void testEnableGrapeSupport_differentLock() { |
| 739 |
try { |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
DefaultGroovyCompiler.enableGrapeSupport(); |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
assertThrowsMessageIs(IllegalStateException.class, |
| 748 |
() -> DefaultGroovyCompiler.enableGrapeSupport(new Object()), |
| 749 |
"Attempt to change lock for wrapped Grape class (unwrap first)."); |
| 750 |
} finally { |
| 751 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
@Test |
| 756 |
void testEnableGrapeSupport_lockNull() { |
| 757 |
try { |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
assertThrowsMessageIs(NullPointerException.class, |
| 762 |
() -> DefaultGroovyCompiler.enableGrapeSupport(null), |
| 763 |
"Lock is null."); |
| 764 |
} finally { |
| 765 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
@Test |
| 770 |
void testGetLoaderIfConfigured() { |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
final GroovyClassLoader parent = new GroovyClassLoader(); |
| 775 |
final CompilerConfiguration config = new CompilerConfiguration(); |
| 776 |
config.addCompilationCustomizers(new ImportCustomizer()); |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
GroovyClassLoader loader = DefaultGroovyCompiler.GrapeCompilationCustomizer |
| 781 |
.getLoaderIfConfigured(parent, config); |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
assertThat(loader, is(nullValue())); |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
DefaultGroovyCompiler.withGrape(config, parent); |
| 790 |
loader = DefaultGroovyCompiler.GrapeCompilationCustomizer |
| 791 |
.getLoaderIfConfigured(parent, config); |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
assertThat(loader, instanceOf(DefaultGroovyCompiler.CompileTimeGroovyClassLoader.class)); |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
DefaultGroovyCompiler.CompileTimeGroovyClassLoader compileTimeLoader = |
| 800 |
(DefaultGroovyCompiler.CompileTimeGroovyClassLoader) loader; |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
assertThat(compileTimeLoader.runtimeLoader, sameInstance(parent)); |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
private volatile boolean failed; |
| 809 |
|
| 810 |
private static Map<String, Object> getDefaultArgs() { |
| 811 |
Map<String, Object> args = new HashMap<>(); |
| 812 |
args.put("disableChecksums", false); |
| 813 |
args.put("autoDownload", true); |
| 814 |
return args; |
| 815 |
} |
| 816 |
|
| 817 |
private static Map<String, Object> getGuavaDependency() { |
| 818 |
Map<String, Object> dependency = new HashMap<>(); |
| 819 |
dependency.put("module", "guava"); |
| 820 |
dependency.put("version", "18.0"); |
| 821 |
dependency.put("group", "com.google.guava"); |
| 822 |
return dependency; |
| 823 |
} |
| 824 |
|
| 825 |
private static Map<String, Object> getDefaultMerged() { |
| 826 |
Map<String, Object> merged = new HashMap<>(); |
| 827 |
merged.putAll(getDefaultArgs()); |
| 828 |
merged.putAll(getGuavaDependency()); |
| 829 |
return merged; |
| 830 |
} |
| 831 |
|
| •832 |
@Disabled("touches $HOME") |
| 833 |
@Test |
| 834 |
void testGrabs() { |
| 835 |
try { |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
DefaultGroovyCompiler.enableGrapeSupport(); |
| 840 |
|
| 841 |
final GroovyClassLoader runtimeLoader = new GroovyClassLoader(); |
| 842 |
final CompilerConfiguration config1 = new CompilerConfiguration(); |
| 843 |
DefaultGroovyCompiler.withGrape(config1, runtimeLoader); |
| 844 |
final DefaultGroovyCompiler.CompileTimeGroovyClassLoader compileTimeLoader = |
| 845 |
(DefaultGroovyCompiler.CompileTimeGroovyClassLoader) |
| 846 |
DefaultGroovyCompiler.GrapeCompilationCustomizer |
| 847 |
.getLoaderIfConfigured(runtimeLoader, config1); |
| 848 |
|
| 849 |
final CompilerConfiguration config2 = new CompilerConfiguration(); |
| 850 |
DefaultGroovyCompiler.withGrape(config2, null); |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
DefaultGroovyCompiler.CompileTimeGroovyClassLoader compileTimeLoaderWithNullRuntimeLoaderInside = |
| 855 |
(DefaultGroovyCompiler.CompileTimeGroovyClassLoader) |
| 856 |
DefaultGroovyCompiler.GrapeCompilationCustomizer |
| 857 |
.getLoaderIfConfigured(null, config2); |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
assertThat(compileTimeLoaderWithNullRuntimeLoaderInside.runtimeLoader, is(nullValue())); |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
final GrapeEngine engine = Grape.getInstance(); |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
assertThat(engine, instanceOf(DefaultGroovyCompiler.GrengineGrapeEngine.class)); |
| 870 |
|
| 871 |
Map<String, Object> args; |
| 872 |
Map<String, Object> dependency; |
| 873 |
Map<String, Object> merged; |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
args = getDefaultArgs(); |
| 878 |
dependency = getGuavaDependency(); |
| 879 |
args.put("classLoader", compileTimeLoader); |
| 880 |
engine.grab(args, dependency); |
| 881 |
|
| 882 |
args = getDefaultArgs(); |
| 883 |
dependency = getGuavaDependency(); |
| 884 |
args.put("classLoader", runtimeLoader); |
| 885 |
engine.grab(args, dependency); |
| 886 |
|
| 887 |
args = getDefaultArgs(); |
| 888 |
dependency = getGuavaDependency(); |
| 889 |
args.put("classLoader", compileTimeLoaderWithNullRuntimeLoaderInside); |
| 890 |
engine.grab(args, dependency); |
| 891 |
|
| 892 |
args = getDefaultArgs(); |
| 893 |
dependency = getGuavaDependency(); |
| 894 |
args.put("classLoader", compileTimeLoader); |
| 895 |
args.put("calleeDepth", 4); |
| 896 |
engine.grab(args, dependency); |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
merged = getDefaultMerged(); |
| 901 |
merged.put("classLoader", compileTimeLoader); |
| 902 |
engine.grab(merged); |
| 903 |
|
| 904 |
merged = getDefaultMerged(); |
| 905 |
merged.put("classLoader", runtimeLoader); |
| 906 |
engine.grab(merged); |
| 907 |
|
| 908 |
merged = getDefaultMerged(); |
| 909 |
merged.put("classLoader", compileTimeLoaderWithNullRuntimeLoaderInside); |
| 910 |
engine.grab(merged); |
| 911 |
|
| 912 |
merged = getDefaultMerged(); |
| 913 |
merged.put("classLoader", compileTimeLoader); |
| 914 |
merged.put("calleeDepth", 5); |
| 915 |
engine.grab(merged); |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
assertThrowsMessageIs(RuntimeException.class, |
| 920 |
() -> engine.grab("endorsed"), |
| 921 |
"No suitable ClassLoader found for grab"); |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
engine.enumerateGrapes(); |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
args = getDefaultArgs(); |
| 931 |
args.put("classLoader", compileTimeLoader); |
| 932 |
URI[] uris = engine.resolve(args, dependency); |
| 933 |
assertThat(uris.length, is(1)); |
| 934 |
assertThat(uris[0].toString(), containsString("guava-18.0.jar")); |
| 935 |
|
| 936 |
args = getDefaultArgs(); |
| 937 |
args.put("classLoader", compileTimeLoader); |
| 938 |
args.put("calleeDepth", 4); |
| 939 |
uris = engine.resolve(args, dependency); |
| 940 |
assertThat(uris.length, is(1)); |
| 941 |
assertThat(uris[0].toString(), containsString("guava-18.0.jar")); |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
args = getDefaultArgs(); |
| 946 |
args.put("classLoader", compileTimeLoader); |
| 947 |
uris = engine.resolve(args, (List) null, dependency); |
| 948 |
assertThat(uris.length, is(1)); |
| 949 |
assertThat(uris[0].toString(), containsString("guava-18.0.jar")); |
| 950 |
|
| 951 |
args = getDefaultArgs(); |
| 952 |
args.put("classLoader", compileTimeLoader); |
| 953 |
args.put("calleeDepth", 4); |
| 954 |
uris = engine.resolve(args, (List) null, dependency); |
| 955 |
assertThat(uris.length, is(1)); |
| 956 |
assertThat(uris[0].toString(), containsString("guava-18.0.jar")); |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
engine.listDependencies(runtimeLoader); |
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
args = getDefaultArgs(); |
| 966 |
args.put("root", "dummy"); |
| 967 |
engine.addResolver(args); |
| 968 |
|
| 969 |
} finally { |
| 970 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 971 |
} |
| 972 |
} |
| 973 |
|
| •974 |
@Disabled("touches $HOME") |
| 975 |
@Test |
| 976 |
void testConcurrentGrabs() { |
| 977 |
try { |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
failed = false; |
| 982 |
|
| 983 |
DefaultGroovyCompiler.enableGrapeSupport(); |
| 984 |
|
| 985 |
final GroovyClassLoader runtimeLoader = new GroovyClassLoader(); |
| 986 |
final CompilerConfiguration config = new CompilerConfiguration(); |
| 987 |
DefaultGroovyCompiler.withGrape(config, runtimeLoader); |
| 988 |
final DefaultGroovyCompiler.CompileTimeGroovyClassLoader compileTimeLoader = |
| 989 |
(DefaultGroovyCompiler.CompileTimeGroovyClassLoader) |
| 990 |
DefaultGroovyCompiler.GrapeCompilationCustomizer |
| 991 |
.getLoaderIfConfigured(runtimeLoader, config); |
| 992 |
|
| 993 |
final GrapeEngine engine = Grape.getInstance(); |
| 994 |
|
| 995 |
final int n = 50; |
| 996 |
final List<Thread> threads = IntStream.range(0, n) |
| 997 |
.boxed() |
| 998 |
.map(i -> new Thread(() -> { |
| 999 |
try { |
| 1000 |
final Map<String, Object> args = getDefaultArgs(); |
| 1001 |
final Map<String, Object> dependency = getGuavaDependency(); |
| 1002 |
args.put("classLoader", compileTimeLoader); |
| 1003 |
engine.grab(args, dependency); |
| 1004 |
} catch (Throwable t) { |
| 1005 |
System.out.println("Thread failed: " + t); |
| 1006 |
failed = true; |
| 1007 |
} |
| 1008 |
})) |
| 1009 |
.collect(toList()); |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
threads.forEach(Thread::start); |
| 1014 |
threads.forEach(t -> toRuntimeException(t::join)); |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
assertThat(failed, is(false)); |
| 1019 |
|
| 1020 |
} finally { |
| 1021 |
DefaultGroovyCompiler.disableGrapeSupport(); |
| 1022 |
} |
| 1023 |
|
| 1024 |
} |
| 1025 |
|
| 1026 |
@Test |
| 1027 |
void testWrappingFails_instanceNotGrapeIvy() { |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
final GrapeEngine innerEngine = Grape.getInstance(); |
| 1032 |
try { |
| 1033 |
|
| 1034 |
|
| 1035 |
new Grape() { |
| 1036 |
void set() { |
| 1037 |
Grape.instance = new DefaultGroovyCompiler.GrengineGrapeEngine(Grape.getInstance()); |
| 1038 |
} |
| 1039 |
}.set(); |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
assertThrowsMessageIs(IllegalStateException.class, |
| 1044 |
DefaultGroovyCompiler::enableGrapeSupport, |
| 1045 |
"Unable to wrap GrapeEngine in Grape.class " + |
| 1046 |
"(current GrapeEngine is ch.grengine.code.groovy.DefaultGroovyCompiler$GrengineGrapeEngine, " + |
| 1047 |
"supported is groovy.grape.GrapeIvy)."); |
| 1048 |
} finally { |
| 1049 |
|
| 1050 |
|
| 1051 |
new Grape() { |
| 1052 |
void set() { |
| 1053 |
Grape.instance = innerEngine; |
| 1054 |
} |
| 1055 |
}.set(); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| •1059 |
} |