Alex Volanis | 7004a61 | 2018-01-24 10:30:13 -0500 | [diff] [blame] | 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 | // Generated code locations for Unit tests |
| 23 | ext.genSrc = file("$buildDir/gen-java") |
| 24 | ext.genBeanSrc = file("$buildDir/gen-javabean") |
| 25 | ext.genReuseSrc = file("$buildDir/gen-javareuse") |
| 26 | ext.genFullCamelSrc = file("$buildDir/gen-fullcamel") |
Jiayu Liu | 1eb361a | 2022-02-21 13:36:44 +0100 | [diff] [blame] | 27 | ext.genOptionTypeJdk8Src = file("$buildDir/gen-option-type-jdk8") |
Ben Podgursky | 50bfc56 | 2018-04-16 23:21:46 -0700 | [diff] [blame] | 28 | ext.genUnsafeSrc = file("$buildDir/gen-unsafe") |
Hernan Silberman | d5c6697 | 2022-06-08 11:29:43 -0700 | [diff] [blame^] | 29 | ext.genStructOrderTestASrc = file("$buildDir/resources/test/struct-order-test/a") |
| 30 | ext.genStructOrderTestBSrc = file("$buildDir/resources/test/struct-order-test/b") |
Alex Volanis | 7004a61 | 2018-01-24 10:30:13 -0500 | [diff] [blame] | 31 | |
| 32 | // Add the generated code directories to the test source set |
| 33 | sourceSets { |
Jiayu Liu | 1eb361a | 2022-02-21 13:36:44 +0100 | [diff] [blame] | 34 | test.java.srcDirs genSrc, genBeanSrc, genReuseSrc, genFullCamelSrc, genUnsafeSrc, genOptionTypeJdk8Src |
Alex Volanis | 7004a61 | 2018-01-24 10:30:13 -0500 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | // Code generation for Unit Testing |
| 39 | |
| 40 | // A callable closure to make this easier |
| 41 | ext.thriftCompile = { Task task, String thriftFileName, String generator = 'java', File outputDir = genSrc -> |
| 42 | def thriftFile = file("$thriftRoot/test/$thriftFileName") |
Jiayu Liu | 1eb361a | 2022-02-21 13:36:44 +0100 | [diff] [blame] | 43 | if (!thriftFile.exists()) { |
Jiayu Liu | eac5103 | 2022-03-11 04:55:13 +0100 | [diff] [blame] | 44 | thriftFile = file("$projectDir/src/test/resources/$thriftFileName") |
Jiayu Liu | 1eb361a | 2022-02-21 13:36:44 +0100 | [diff] [blame] | 45 | assert thriftFile.exists(), "can't find $thriftFile" |
| 46 | } |
Alex Volanis | 7004a61 | 2018-01-24 10:30:13 -0500 | [diff] [blame] | 47 | |
| 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 | |
| 70 | task generate(group: 'Build') { |
| 71 | description = 'Generate all unit test Thrift sources' |
| 72 | compileTestJava.dependsOn it |
| 73 | } |
| 74 | |
| 75 | task 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 Altiok | e46419b | 2018-03-20 15:02:28 +0300 | [diff] [blame] | 84 | thriftCompile(it, 'DoubleConstantsTest.thrift') |
Alex Volanis | 7004a61 | 2018-01-24 10:30:13 -0500 | [diff] [blame] | 85 | thriftCompile(it, 'OptionalRequiredTest.thrift') |
| 86 | thriftCompile(it, 'ManyOptionals.thrift') |
| 87 | thriftCompile(it, 'JavaDeepCopyTest.thrift') |
| 88 | thriftCompile(it, 'EnumContainersTest.thrift') |
Ewan Higgs | b3745ee | 2019-09-20 17:15:04 +0200 | [diff] [blame] | 89 | thriftCompile(it, 'JavaBinaryDefault.thrift') |
Alex Kormukhin | c9b7bd7 | 2022-02-18 21:04:14 +0300 | [diff] [blame] | 90 | thriftCompile(it, 'VoidMethExceptionsTest.thrift') |
Jiayu Liu | ada0865 | 2022-05-06 03:19:57 +0800 | [diff] [blame] | 91 | thriftCompile(it, 'AnnotationTest.thrift') |
kpandit | 5a9d139 | 2021-11-20 00:56:17 +0100 | [diff] [blame] | 92 | thriftCompile(it, 'partial/thrift_test_schema.thrift') |
Alex Volanis | 7004a61 | 2018-01-24 10:30:13 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Jiayu Liu | 1eb361a | 2022-02-21 13:36:44 +0100 | [diff] [blame] | 95 | task generateOptionalTypeJava(group: 'Build') { |
| 96 | description = 'Generate the thrift gen-option-type-jdk8 source' |
| 97 | generate.dependsOn it |
| 98 | |
| 99 | ext.outputBuffer = new ByteArrayOutputStream() |
| 100 | |
| 101 | thriftCompile(it, 'JavaOptionTypeJdk8Test.thrift', 'java:option_type=jdk8', genOptionTypeJdk8Src) |
| 102 | } |
| 103 | |
Alex Volanis | 7004a61 | 2018-01-24 10:30:13 -0500 | [diff] [blame] | 104 | task generateBeanJava(group: 'Build') { |
| 105 | description = 'Generate the thrift gen-javabean source' |
| 106 | generate.dependsOn it |
| 107 | |
| 108 | ext.outputBuffer = new ByteArrayOutputStream() |
| 109 | |
Jiayu Liu | aa82214 | 2022-04-12 22:42:10 +0800 | [diff] [blame] | 110 | thriftCompile(it, 'JavaBeansTest.thrift', 'java:beans,nocamel,future_iface', genBeanSrc) |
Alex Volanis | 7004a61 | 2018-01-24 10:30:13 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | task generateReuseJava(group: 'Build') { |
| 114 | description = 'Generate the thrift gen-javareuse source' |
| 115 | generate.dependsOn it |
| 116 | |
| 117 | ext.outputBuffer = new ByteArrayOutputStream() |
| 118 | |
Jiayu Liu | aa82214 | 2022-04-12 22:42:10 +0800 | [diff] [blame] | 119 | thriftCompile(it, 'FullCamelTest.thrift', 'java:fullcamel,future_iface', genFullCamelSrc) |
Alex Volanis | 7004a61 | 2018-01-24 10:30:13 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | task generateFullCamelJava(group: 'Build') { |
| 123 | description = 'Generate the thrift gen-fullcamel source' |
| 124 | generate.dependsOn it |
| 125 | |
| 126 | ext.outputBuffer = new ByteArrayOutputStream() |
| 127 | |
Jiayu Liu | 5a44db8 | 2022-02-22 23:17:06 +0800 | [diff] [blame] | 128 | thriftCompile(it, 'ReuseObjects.thrift', 'java:reuse_objects', genReuseSrc) |
Alex Volanis | 7004a61 | 2018-01-24 10:30:13 -0500 | [diff] [blame] | 129 | } |
Ben Podgursky | 50bfc56 | 2018-04-16 23:21:46 -0700 | [diff] [blame] | 130 | |
| 131 | task generateUnsafeBinariesJava(group: 'Build') { |
| 132 | description = 'Generate the thrift gen-unsafebinaries source' |
| 133 | generate.dependsOn it |
| 134 | |
| 135 | ext.outputBuffer = new ByteArrayOutputStream() |
| 136 | |
| 137 | thriftCompile(it, 'UnsafeTypes.thrift', 'java:unsafe_binaries', genUnsafeSrc) |
| 138 | } |
Jiayu Liu | ada0865 | 2022-05-06 03:19:57 +0800 | [diff] [blame] | 139 | |
| 140 | task generateWithAnnotationMetadata(group: 'Build') { |
| 141 | description = 'Generate with annotation enabled and add to the default source' |
| 142 | generate.dependsOn it |
| 143 | |
| 144 | ext.outputBuffer = new ByteArrayOutputStream() |
| 145 | |
| 146 | thriftCompile(it, 'JavaAnnotationTest.thrift', 'java:annotations_as_metadata', genSrc) |
| 147 | } |
Hernan Silberman | d5c6697 | 2022-06-08 11:29:43 -0700 | [diff] [blame^] | 148 | |
| 149 | task generateJavaStructOrderTestJava(group: 'Build') { |
| 150 | description = 'Generate structs defined in different order and add to build dir so we can compare them' |
| 151 | generate.dependsOn it |
| 152 | |
| 153 | ext.outputBuffer = new ByteArrayOutputStream() |
| 154 | |
| 155 | thriftCompile(it, 'JavaStructOrderA.thrift', 'java', genStructOrderTestASrc) |
| 156 | thriftCompile(it, 'JavaStructOrderB.thrift', 'java', genStructOrderTestBSrc) |
| 157 | } |