blob: e8515473ebb371be4371dd981c2d051f7974d432 [file] [log] [blame]
Alex Volanis7004a612018-01-24 10:30:13 -05001/*
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// Generated code locations for Unit tests
23ext.genSrc = file("$buildDir/gen-java")
24ext.genBeanSrc = file("$buildDir/gen-javabean")
25ext.genReuseSrc = file("$buildDir/gen-javareuse")
26ext.genFullCamelSrc = file("$buildDir/gen-fullcamel")
Jiayu Liu1eb361a2022-02-21 13:36:44 +010027ext.genOptionTypeJdk8Src = file("$buildDir/gen-option-type-jdk8")
Ben Podgursky50bfc562018-04-16 23:21:46 -070028ext.genUnsafeSrc = file("$buildDir/gen-unsafe")
Alex Volanis7004a612018-01-24 10:30:13 -050029
30// Add the generated code directories to the test source set
31sourceSets {
Jiayu Liu1eb361a2022-02-21 13:36:44 +010032 test.java.srcDirs genSrc, genBeanSrc, genReuseSrc, genFullCamelSrc, genUnsafeSrc, genOptionTypeJdk8Src
Alex Volanis7004a612018-01-24 10:30:13 -050033}
34
35// ----------------------------------------------------------------------------
36// Code generation for Unit Testing
37
38// A callable closure to make this easier
39ext.thriftCompile = { Task task, String thriftFileName, String generator = 'java', File outputDir = genSrc ->
40 def thriftFile = file("$thriftRoot/test/$thriftFileName")
Jiayu Liu1eb361a2022-02-21 13:36:44 +010041 if (!thriftFile.exists()) {
Jiayu Liueac51032022-03-11 04:55:13 +010042 thriftFile = file("$projectDir/src/test/resources/$thriftFileName")
Jiayu Liu1eb361a2022-02-21 13:36:44 +010043 assert thriftFile.exists(), "can't find $thriftFile"
44 }
Alex Volanis7004a612018-01-24 10:30:13 -050045
46 task.inputs.file thriftFile
47 task.outputs.dir outputDir
48
49 task.doLast {
50 outputDir.mkdirs()
51 def result = exec {
52 executable file(thriftCompiler)
53 args '--gen', generator
54 args '-out', outputDir
55 args thriftFile
56 standardOutput = task.outputBuffer
57 errorOutput = task.outputBuffer
58 ignoreExitValue = true
59 }
60 if (result.exitValue != 0) {
61 // Only show the Thrift compiler output on failures, cuts down on noise!
62 println task.outputBuffer.toString()
63 result.rethrowFailure()
64 }
65 }
66}
67
68task generate(group: 'Build') {
69 description = 'Generate all unit test Thrift sources'
70 compileTestJava.dependsOn it
71}
72
73task generateJava(group: 'Build') {
74 description = 'Generate the thrift gen-java source'
75 generate.dependsOn it
76
77 ext.outputBuffer = new ByteArrayOutputStream()
78
79 thriftCompile(it, 'ThriftTest.thrift')
80 thriftCompile(it, 'JavaTypes.thrift')
81 thriftCompile(it, 'DebugProtoTest.thrift')
Ozan Can Altioke46419b2018-03-20 15:02:28 +030082 thriftCompile(it, 'DoubleConstantsTest.thrift')
Alex Volanis7004a612018-01-24 10:30:13 -050083 thriftCompile(it, 'OptionalRequiredTest.thrift')
84 thriftCompile(it, 'ManyOptionals.thrift')
85 thriftCompile(it, 'JavaDeepCopyTest.thrift')
86 thriftCompile(it, 'EnumContainersTest.thrift')
Ewan Higgsb3745ee2019-09-20 17:15:04 +020087 thriftCompile(it, 'JavaBinaryDefault.thrift')
Alex Kormukhinc9b7bd72022-02-18 21:04:14 +030088 thriftCompile(it, 'VoidMethExceptionsTest.thrift')
Jiayu Liuada08652022-05-06 03:19:57 +080089 thriftCompile(it, 'AnnotationTest.thrift')
kpandit5a9d1392021-11-20 00:56:17 +010090 thriftCompile(it, 'partial/thrift_test_schema.thrift')
Alex Volanis7004a612018-01-24 10:30:13 -050091}
92
Jiayu Liu1eb361a2022-02-21 13:36:44 +010093task generateOptionalTypeJava(group: 'Build') {
94 description = 'Generate the thrift gen-option-type-jdk8 source'
95 generate.dependsOn it
96
97 ext.outputBuffer = new ByteArrayOutputStream()
98
99 thriftCompile(it, 'JavaOptionTypeJdk8Test.thrift', 'java:option_type=jdk8', genOptionTypeJdk8Src)
100}
101
Alex Volanis7004a612018-01-24 10:30:13 -0500102task generateBeanJava(group: 'Build') {
103 description = 'Generate the thrift gen-javabean source'
104 generate.dependsOn it
105
106 ext.outputBuffer = new ByteArrayOutputStream()
107
Jiayu Liuaa822142022-04-12 22:42:10 +0800108 thriftCompile(it, 'JavaBeansTest.thrift', 'java:beans,nocamel,future_iface', genBeanSrc)
Alex Volanis7004a612018-01-24 10:30:13 -0500109}
110
111task generateReuseJava(group: 'Build') {
112 description = 'Generate the thrift gen-javareuse source'
113 generate.dependsOn it
114
115 ext.outputBuffer = new ByteArrayOutputStream()
116
Jiayu Liuaa822142022-04-12 22:42:10 +0800117 thriftCompile(it, 'FullCamelTest.thrift', 'java:fullcamel,future_iface', genFullCamelSrc)
Alex Volanis7004a612018-01-24 10:30:13 -0500118}
119
120task generateFullCamelJava(group: 'Build') {
121 description = 'Generate the thrift gen-fullcamel source'
122 generate.dependsOn it
123
124 ext.outputBuffer = new ByteArrayOutputStream()
125
Jiayu Liu5a44db82022-02-22 23:17:06 +0800126 thriftCompile(it, 'ReuseObjects.thrift', 'java:reuse_objects', genReuseSrc)
Alex Volanis7004a612018-01-24 10:30:13 -0500127}
Ben Podgursky50bfc562018-04-16 23:21:46 -0700128
129task generateUnsafeBinariesJava(group: 'Build') {
130 description = 'Generate the thrift gen-unsafebinaries source'
131 generate.dependsOn it
132
133 ext.outputBuffer = new ByteArrayOutputStream()
134
135 thriftCompile(it, 'UnsafeTypes.thrift', 'java:unsafe_binaries', genUnsafeSrc)
136}
Jiayu Liuada08652022-05-06 03:19:57 +0800137
138task generateWithAnnotationMetadata(group: 'Build') {
139 description = 'Generate with annotation enabled and add to the default source'
140 generate.dependsOn it
141
142 ext.outputBuffer = new ByteArrayOutputStream()
143
144 thriftCompile(it, 'JavaAnnotationTest.thrift', 'java:annotations_as_metadata', genSrc)
145}