|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.beryx.runtime |
| 17 | + |
| 18 | +import org.gradle.testkit.runner.BuildResult |
| 19 | +import org.gradle.testkit.runner.GradleRunner |
| 20 | +import spock.lang.Specification |
| 21 | +import spock.lang.TempDir |
| 22 | +import spock.lang.Unroll |
| 23 | +import spock.util.environment.OperatingSystem |
| 24 | + |
| 25 | +import java.nio.file.Path |
| 26 | + |
| 27 | +import static org.gradle.testkit.runner.TaskOutcome.SUCCESS |
| 28 | + |
| 29 | +class RuntimePluginSpecGradle9 extends Specification { |
| 30 | + @TempDir Path testProjectDir |
| 31 | + |
| 32 | + def cleanup() { |
| 33 | + println "CLEANUP" |
| 34 | + } |
| 35 | + |
| 36 | + def setUpBuild(Collection<String> modules) { |
| 37 | + new AntBuilder().copy( todir: testProjectDir ) { |
| 38 | + fileset( dir: 'src/test/resources/hello-logback-gradle9' ) |
| 39 | + } |
| 40 | + |
| 41 | + File buildFile = new File(testProjectDir.toFile(), "build.gradle") |
| 42 | + buildFile << ''' |
| 43 | + runtime { |
| 44 | + options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages'] |
| 45 | + '''.stripIndent() |
| 46 | + if(modules != null) { |
| 47 | + buildFile << " modules = [${modules.collect{'\'' + it + '\''}.join(', ')}]\n" |
| 48 | + } |
| 49 | + buildFile << '}\n' |
| 50 | + println "Executing build script:\n${buildFile.text}" |
| 51 | + } |
| 52 | + |
| 53 | + @Unroll |
| 54 | + def "if modules=#modules, then buildSucceeds=#buildShouldSucceed and runSucceeds=#runShouldSucceed with Gradle #gradleVersion"() { |
| 55 | + when: |
| 56 | + setUpBuild(modules) |
| 57 | + BuildResult result |
| 58 | + try { |
| 59 | + result = GradleRunner.create() |
| 60 | + .withDebug(true) |
| 61 | + .withProjectDir(testProjectDir.toFile()) |
| 62 | + .withGradleVersion(gradleVersion) |
| 63 | + .withPluginClasspath() |
| 64 | + .withArguments(RuntimePlugin.TASK_NAME_RUNTIME, "-is") |
| 65 | + .build() |
| 66 | + } catch (Exception e) { |
| 67 | + if(buildShouldSucceed) { |
| 68 | + e.printStackTrace() |
| 69 | + } |
| 70 | + assert !buildShouldSucceed |
| 71 | + return |
| 72 | + } |
| 73 | + def imageBinDir = new File(testProjectDir.toFile(), 'build/image/bin') |
| 74 | + def launcherExt = OperatingSystem.current.windows ? '.bat' : '' |
| 75 | + def imageLauncher = new File(imageBinDir, "runtime-hello-gradle9$launcherExt") |
| 76 | + |
| 77 | + then: |
| 78 | + result.task(":$RuntimePlugin.TASK_NAME_RUNTIME").outcome == SUCCESS |
| 79 | + imageLauncher.exists() |
| 80 | + |
| 81 | + when: |
| 82 | + imageLauncher.setExecutable(true) |
| 83 | + def process = imageLauncher.absolutePath.execute([], imageBinDir) |
| 84 | + def out = new ByteArrayOutputStream(2048) |
| 85 | + def err = new ByteArrayOutputStream(2048) |
| 86 | + process.waitForProcessOutput(out, err) |
| 87 | + def outputText = out.toString() |
| 88 | + |
| 89 | + then: |
| 90 | + (outputText.trim() == 'LOG: Hello, runtime!') == runShouldSucceed |
| 91 | + |
| 92 | + where: |
| 93 | + modules | buildShouldSucceed | runShouldSucceed | gradleVersion |
| 94 | + null | true | true | '9.1.0' |
| 95 | + [] | true | true | '9.0.0' |
| 96 | + ['java.base'] | true | false | '9.0.0' |
| 97 | + ['foo.bar'] | false | false | '9.0.0' |
| 98 | + ['java.naming'] | true | false | '9.0.0' |
| 99 | + ['java.naming', 'java.xml'] | true | true | '9.0.0' |
| 100 | + ['java.naming', 'java.xml', 'java.logging'] | true | true | '9.0.0' |
| 101 | + ['java.naming', 'java.xml', 'foo.bar'] | false | false | '9.0.0' |
| 102 | + ['java.naming', 'java.xml', 'java.logging'] | true | true | '9.0.0' |
| 103 | + ['java.naming', 'java.xml', 'java.logging'] | true | true | '9.1.0' |
| 104 | + } |
| 105 | +} |
0 commit comments