blob: 7d1f24808e681f56ededa9cb3d7ad17dda50df64 [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")
Klemen Košir4f635732023-04-27 15:13:18 +090029ext.genDefinitionOrderTestASrc = file("$buildDir/resources/test/definition-order-test/a")
30ext.genDefinitionOrderTestBSrc = file("$buildDir/resources/test/definition-order-test/b")
Alex Volanis7004a612018-01-24 10:30:13 -050031
32// Add the generated code directories to the test source set
33sourceSets {
Jiayu Liu1eb361a2022-02-21 13:36:44 +010034 test.java.srcDirs genSrc, genBeanSrc, genReuseSrc, genFullCamelSrc, genUnsafeSrc, genOptionTypeJdk8Src
Alex Volanis7004a612018-01-24 10:30:13 -050035}
36
37// ----------------------------------------------------------------------------
38// Code generation for Unit Testing
39
40// A callable closure to make this easier
nicolasb29285e39c2023-05-25 23:59:11 +020041ext.thriftCompile = { Task task, String thriftFileName, String generator = 'java:jakarta_annotations', File outputDir = genSrc ->
Alex Volanis7004a612018-01-24 10:30:13 -050042 def thriftFile = file("$thriftRoot/test/$thriftFileName")
Jiayu Liu1eb361a2022-02-21 13:36:44 +010043 if (!thriftFile.exists()) {
Jiayu Liueac51032022-03-11 04:55:13 +010044 thriftFile = file("$projectDir/src/test/resources/$thriftFileName")
Jiayu Liu1eb361a2022-02-21 13:36:44 +010045 assert thriftFile.exists(), "can't find $thriftFile"
46 }
Alex Volanis7004a612018-01-24 10:30:13 -050047
48 task.inputs.file thriftFile
49 task.outputs.dir outputDir
50
51 task.doLast {
52 outputDir.mkdirs()
53 def result = exec {
54 executable file(thriftCompiler)
55 args '--gen', generator
56 args '-out', outputDir
57 args thriftFile
58 standardOutput = task.outputBuffer
59 errorOutput = task.outputBuffer
60 ignoreExitValue = true
61 }
62 if (result.exitValue != 0) {
63 // Only show the Thrift compiler output on failures, cuts down on noise!
64 println task.outputBuffer.toString()
65 result.rethrowFailure()
66 }
67 }
68}
69
70task generate(group: 'Build') {
71 description = 'Generate all unit test Thrift sources'
72 compileTestJava.dependsOn it
73}
74
75task generateJava(group: 'Build') {
76 description = 'Generate the thrift gen-java source'
77 generate.dependsOn it
78
79 ext.outputBuffer = new ByteArrayOutputStream()
80
81 thriftCompile(it, 'ThriftTest.thrift')
82 thriftCompile(it, 'JavaTypes.thrift')
83 thriftCompile(it, 'DebugProtoTest.thrift')
Ozan Can Altioke46419b2018-03-20 15:02:28 +030084 thriftCompile(it, 'DoubleConstantsTest.thrift')
Alex Volanis7004a612018-01-24 10:30:13 -050085 thriftCompile(it, 'OptionalRequiredTest.thrift')
86 thriftCompile(it, 'ManyOptionals.thrift')
87 thriftCompile(it, 'JavaDeepCopyTest.thrift')
88 thriftCompile(it, 'EnumContainersTest.thrift')
Ewan Higgsb3745ee2019-09-20 17:15:04 +020089 thriftCompile(it, 'JavaBinaryDefault.thrift')
Alex Kormukhinc9b7bd72022-02-18 21:04:14 +030090 thriftCompile(it, 'VoidMethExceptionsTest.thrift')
Jiayu Liu6fefbf42023-04-20 07:41:11 +080091 thriftCompile(it, 'JavaAnnotationTest.thrift')
kpandit5a9d1392021-11-20 00:56:17 +010092 thriftCompile(it, 'partial/thrift_test_schema.thrift')
Hasnain Lakhani4f877ca2025-08-25 13:24:26 -070093 thriftCompile(it, 'FuzzTest.thrift')
Alex Volanis7004a612018-01-24 10:30:13 -050094}
95
Jiayu Liu1eb361a2022-02-21 13:36:44 +010096task generateOptionalTypeJava(group: 'Build') {
97 description = 'Generate the thrift gen-option-type-jdk8 source'
98 generate.dependsOn it
99
100 ext.outputBuffer = new ByteArrayOutputStream()
101
nicolasb29285e39c2023-05-25 23:59:11 +0200102 thriftCompile(it, 'JavaOptionTypeJdk8Test.thrift', 'java:option_type=jdk8,jakarta_annotations', genOptionTypeJdk8Src)
Jiayu Liu1eb361a2022-02-21 13:36:44 +0100103}
104
Alex Volanis7004a612018-01-24 10:30:13 -0500105task generateBeanJava(group: 'Build') {
106 description = 'Generate the thrift gen-javabean source'
107 generate.dependsOn it
108
109 ext.outputBuffer = new ByteArrayOutputStream()
110
nicolasb29285e39c2023-05-25 23:59:11 +0200111 thriftCompile(it, 'JavaBeansTest.thrift', 'java:beans,nocamel,future_iface,jakarta_annotations', genBeanSrc)
Alex Volanis7004a612018-01-24 10:30:13 -0500112}
113
114task generateReuseJava(group: 'Build') {
115 description = 'Generate the thrift gen-javareuse source'
116 generate.dependsOn it
117
118 ext.outputBuffer = new ByteArrayOutputStream()
119
nicolasb29285e39c2023-05-25 23:59:11 +0200120 thriftCompile(it, 'FullCamelTest.thrift', 'java:fullcamel,future_iface,jakarta_annotations', genFullCamelSrc)
Alex Volanis7004a612018-01-24 10:30:13 -0500121}
122
123task generateFullCamelJava(group: 'Build') {
124 description = 'Generate the thrift gen-fullcamel source'
125 generate.dependsOn it
126
127 ext.outputBuffer = new ByteArrayOutputStream()
128
nicolasb29285e39c2023-05-25 23:59:11 +0200129 thriftCompile(it, 'ReuseObjects.thrift', 'java:reuse_objects,jakarta_annotations', genReuseSrc)
Alex Volanis7004a612018-01-24 10:30:13 -0500130}
Ben Podgursky50bfc562018-04-16 23:21:46 -0700131
132task generateUnsafeBinariesJava(group: 'Build') {
133 description = 'Generate the thrift gen-unsafebinaries source'
134 generate.dependsOn it
135
136 ext.outputBuffer = new ByteArrayOutputStream()
137
nicolasb29285e39c2023-05-25 23:59:11 +0200138 thriftCompile(it, 'UnsafeTypes.thrift', 'java:unsafe_binaries,jakarta_annotations', genUnsafeSrc)
Ben Podgursky50bfc562018-04-16 23:21:46 -0700139}
Jiayu Liuada08652022-05-06 03:19:57 +0800140
141task generateWithAnnotationMetadata(group: 'Build') {
142 description = 'Generate with annotation enabled and add to the default source'
143 generate.dependsOn it
144
145 ext.outputBuffer = new ByteArrayOutputStream()
146
nicolasb29285e39c2023-05-25 23:59:11 +0200147 thriftCompile(it, 'JavaAnnotationTest.thrift', 'java:annotations_as_metadata,jakarta_annotations', genSrc)
Jiayu Liuada08652022-05-06 03:19:57 +0800148}
Hernan Silbermand5c66972022-06-08 11:29:43 -0700149
Klemen Košir4f635732023-04-27 15:13:18 +0900150task generateJavaDefinitionOrderTestJava(group: 'Build') {
151 description = 'Generate fields defined in different order and add to build dir so we can compare them'
Hernan Silbermand5c66972022-06-08 11:29:43 -0700152 generate.dependsOn it
153
154 ext.outputBuffer = new ByteArrayOutputStream()
155
nicolasb29285e39c2023-05-25 23:59:11 +0200156 thriftCompile(it, 'JavaDefinitionOrderA.thrift', 'java:jakarta_annotations', genDefinitionOrderTestASrc)
157 thriftCompile(it, 'JavaDefinitionOrderB.thrift', 'java:jakarta_annotations', genDefinitionOrderTestBSrc)
Hernan Silbermand5c66972022-06-08 11:29:43 -0700158}