All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.integtests.samples.SamplesApplicationIntegrationTest.groovy Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.integtests.samples

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.Sample
import org.gradle.integtests.fixtures.ScriptExecuter
import org.gradle.test.fixtures.file.TestFile
import org.gradle.util.Requires
import org.junit.Rule
import spock.lang.Unroll

import static org.gradle.util.TestPrecondition.KOTLIN_SCRIPT

@Requires(KOTLIN_SCRIPT)
class SamplesApplicationIntegrationTest extends AbstractIntegrationSpec {

    @Rule Sample sample = new Sample(temporaryFolder, 'application')

    def setup() {
        executer.withRepositoryMirrors()
    }

    @Unroll
    def "can run the application using run task with #dsl dsl"() {
        when:
        executer.inDirectory(sample.dir.file(dsl))
        succeeds('run')

        then:
        outputContains('Greetings from the sample application.')

        where:
        dsl << ['groovy', 'kotlin']
    }

    @Unroll
    def "can build and run the installed application with #dsl dsl"() {
        when:
        def dslDir = sample.dir.file(dsl)
        appendExecutableDir(dslDir, executableDir)
        executer.inDirectory(dslDir)
        succeeds('installDist')

        then:
        def installDir = dslDir.file('build/install/my-app')
        installDir.assertIsDir()

        checkApplicationImage(installDir, executableDir)

        where:
        executableDir << ['bin', 'customBin']
        dsl << ['groovy', 'kotlin']
    }

    @Unroll
    def "can build and run the zipped distribution with #dsl dsl"() {
        when:
        def dslDir = sample.dir.file(dsl)
        appendExecutableDir(dslDir, executableDir)
        executer.inDirectory(dslDir)
        succeeds('distZip')

        then:
        def distFile = dslDir.file('build/distributions/my-app-1.0.2.zip')
        distFile.assertIsFile()

        def installDir = dslDir.file('unzip')
        distFile.usingNativeTools().unzipTo(installDir)

        checkApplicationImage(installDir.file('my-app-1.0.2'), executableDir)

        where:
        dsl << ['groovy', 'kotlin']
        executableDir << ['bin', 'customBin']
    }

    private void appendExecutableDir(TestFile dslDir, String executableDir) {
        def extension = dslDir.name == 'groovy' ? 'gradle' : 'gradle.kts'
        dslDir.file("build.$extension") << """
application {
    executableDir = "${executableDir}" 
}
"""
    }

    private void checkApplicationImage(TestFile installDir, String executableDir) {
        installDir.file("${executableDir}/my-app").assertIsFile()
        installDir.file("${executableDir}/my-app.bat").assertIsFile()
        installDir.file('lib/application-1.0.2.jar').assertIsFile()
        installDir.file('lib/commons-collections-3.2.2.jar').assertIsFile()

        installDir.file('LICENSE').assertIsFile()
        installDir.file('docs/readme.txt').assertIsFile()

        def builder = new ScriptExecuter()
        builder.workingDir installDir.file(executableDir)
        builder.executable 'my-app'
        builder.standardOutput = new ByteArrayOutputStream()
        builder.errorOutput = new ByteArrayOutputStream()

        def result = builder.run()
        result.assertNormalExitValue()

        assert builder.standardOutput.toString().contains('Greetings from the sample application.')
        assert builder.errorOutput.toString() == ''
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy