1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20// Following Gradle best practices to keep build logic organized
21
22// ----------------------------------------------------------------------------
23// Functional testing harness creation. This helps run the cross-check tests.
24// The Makefile precross target invokes the shadowJar task and the tests.json
25// code is changed to call runclient or runserver as needed.
26
27// ----------------------------------------------------------------------------
28// Cross Test sources are separated in their own sourceSet
29//
30sourceSets {
31    crossTest {
32        java {
33            srcDir 'test'
34            include '**/test/TestClient.java'
35            include '**/test/TestServer.java'
36            include '**/test/TestNonblockingServer.java'
37            include '**/test/TestTServletServer.java'
38        }
39    }
40}
41
42configurations {
43    crossTestCompile { extendsFrom testCompile }
44    crossTestRuntime { extendsFrom crossTestCompile, testRuntime }
45}
46
47dependencies {
48    crossTestCompile sourceSets.main.output
49    crossTestCompile sourceSets.test.output
50}
51
52// I am using shadow plugin to make a self contained functional test Uber JAR that
53// eliminates startup problems with wrapping the cross-check harness in Gradle.
54// This is used by the runner scripts as the single classpath entry which
55// allows the process to be as lightweight as it can.
56shadowJar {
57    description = 'Assemble a test JAR file for cross-check execution'
58    // make sure the runners are created when this runs
59    dependsOn 'generateRunnerScriptForClient', 'generateRunnerScriptForServer', 'generateRunnerScriptForNonblockingServer', 'generateRunnerScriptForTServletServer'
60
61    baseName = 'functionalTest'
62    destinationDir = file("$buildDir/functionalTestJar")
63    classifier = null
64
65    // We do not need a version number for this internal jar
66    version = null
67
68    // Bundle the complete set of unit test classes including generated code
69    // and the runtime dependencies in one JAR to expedite execution.
70    from sourceSets.test.output
71    from sourceSets.crossTest.output
72    configurations = [project.configurations.testRuntime]
73}
74
75// Common script runner configuration elements
76def scriptExt = ''
77def execExt = ''
78def scriptHead = '#!/bin/bash'
79def args = '$*'
80
81// Although this is marked internal it is an available and stable interface
82if (org.gradle.internal.os.OperatingSystem.current().windows) {
83    scriptExt = '.bat'
84    execExt = '.exe'
85    scriptHead = '@echo off'
86    args = '%*'
87}
88
89// The Java executable to use with the runner scripts
90def javaExe = file("${System.getProperty('java.home')}/bin/java${execExt}").canonicalPath
91// The common Uber jar path
92def jarPath = shadowJar.archivePath.canonicalPath
93def trustStore = file('test/.truststore').canonicalPath
94def keyStore = file('test/.keystore').canonicalPath
95
96task generateRunnerScriptForClient(group: 'Build') {
97    description = 'Generate a runner script for cross-check tests with TestClient'
98
99    def clientFile = file("$buildDir/runclient${scriptExt}")
100
101    def runClientText = """\
102${scriptHead}
103
104"${javaExe}" -cp "$jarPath" "-Djavax.net.ssl.trustStore=$trustStore" -Djavax.net.ssl.trustStorePassword=thrift org.apache.thrift.test.TestClient $args
105"""
106    inputs.property 'runClientText', runClientText
107    outputs.file clientFile
108
109    doLast {
110        clientFile.parentFile.mkdirs()
111        clientFile.text = runClientText
112        clientFile.setExecutable(true, false)
113    }
114}
115
116task generateRunnerScriptForServer(group: 'Build') {
117    description = 'Generate a runner script for cross-check tests with TestServer'
118
119    def serverFile = file("$buildDir/runserver${scriptExt}")
120
121    def runServerText = """\
122${scriptHead}
123
124"${javaExe}" -cp "$jarPath" "-Djavax.net.ssl.keyStore=$keyStore" -Djavax.net.ssl.keyStorePassword=thrift org.apache.thrift.test.TestServer $args
125"""
126
127    inputs.property 'runServerText', runServerText
128    outputs.file serverFile
129
130    doLast {
131        serverFile.parentFile.mkdirs()
132        serverFile.text = runServerText
133        serverFile.setExecutable(true, false)
134    }
135}
136
137task generateRunnerScriptForNonblockingServer(group: 'Build') {
138    description = 'Generate a runner script for cross-check tests with TestNonblockingServer'
139
140    def serverFile = file("$buildDir/runnonblockingserver${scriptExt}")
141
142    def runServerText = """\
143${scriptHead}
144
145"${javaExe}" -cp "$jarPath" "-Djavax.net.ssl.keyStore=$keyStore" -Djavax.net.ssl.keyStorePassword=thrift org.apache.thrift.test.TestNonblockingServer $args
146"""
147
148    inputs.property 'runServerText', runServerText
149    outputs.file serverFile
150
151    doLast {
152        serverFile.parentFile.mkdirs()
153        serverFile.text = runServerText
154        serverFile.setExecutable(true, false)
155    }
156}
157
158task generateRunnerScriptForTServletServer(group: 'Build') {
159    description = 'Generate a runner script for cross-check tests with TestTServletServer'
160
161    def serverFile = file("$buildDir/runservletserver${scriptExt}")
162
163    def runServerText = """\
164${scriptHead}
165
166"${javaExe}" -cp "$jarPath" "-Djavax.net.ssl.keyStore=$keyStore" -Djavax.net.ssl.keyStorePassword=thrift org.apache.thrift.test.TestTServletServer $args
167"""
168
169    inputs.property 'runServerText', runServerText
170    outputs.file serverFile
171
172    doLast {
173        serverFile.parentFile.mkdirs()
174        serverFile.text = runServerText
175        serverFile.setExecutable(true, false)
176    }
177}
178