Last active
February 20, 2016 11:34
-
-
Save sherter/30906735f115a85b85df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'groovy' | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
testCompile gradleTestKit() | |
testCompile gradleApi() | |
testCompile localGroovy() | |
testCompile('org.spockframework:spock-core:1.0-groovy-2.4') { | |
exclude group: 'org.codehaus.groovy', module: 'groovy-all' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.gradle.testfixtures.ProjectBuilder | |
import org.gradle.testkit.runner.GradleRunner | |
import spock.lang.Specification | |
class ClassLoading extends Specification { | |
def "A class loaded by URLClassLoader can load all classes that the URLClassLoader can load"() { | |
def project = new ProjectBuilder().build() | |
project.repositories { jcenter() } | |
def config = project.configurations.create('c') | |
project.dependencies.add(config.name, 'com.google.googlejavaformat:google-java-format:0.1-alpha') | |
def urls = config.resolve().collect { it.toURI().toURL() } | |
def classLoader = new URLClassLoader(urls as URL[]) | |
def formatter = classLoader.loadClass('com.google.googlejavaformat.java.Formatter').newInstance() | |
when: | |
classLoader.loadClass('org.eclipse.core.runtime.CoreException') | |
then: | |
notThrown(NoClassDefFoundError) | |
when: | |
formatter.formatSource('public final class Test {}') | |
then: | |
notThrown(NoClassDefFoundError) | |
} | |
def "the same thing in gradle runner"() { | |
File tempDir = File.createTempDir() | |
File buildFile = new File(tempDir, 'build.gradle') | |
buildFile << """ | |
repositories { | |
jcenter() | |
} | |
configurations { | |
conf | |
} | |
dependencies { | |
conf 'com.google.googlejavaformat:google-java-format:0.1-alpha' | |
} | |
def urls = configurations.conf.resolve().collect { it.toURI().toURL() } | |
def classLoader = new URLClassLoader(urls as URL[]) | |
def formatter = classLoader.loadClass('com.google.googlejavaformat.java.Formatter').newInstance() | |
println formatter.formatSource('public final class Test {}') | |
""" | |
def runner = GradleRunner.create() | |
.withProjectDir(tempDir) | |
.withArguments('--stacktrace') | |
when: | |
def result = runner.build() | |
then: | |
result.output.contains('BUILD SUCCESSFUL') | |
cleanup: | |
tempDir.deleteDir() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment