-
Notifications
You must be signed in to change notification settings - Fork 83
/
JooqFuncTest.groovy
1302 lines (1072 loc) · 37.2 KB
/
JooqFuncTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package nu.studer.gradle.jooq
import groovy.sql.Sql
import org.gradle.testkit.runner.TaskOutcome
import org.gradle.util.GradleVersion
import org.jooq.Constants
import spock.lang.AutoCleanup
import spock.lang.Requires
import spock.lang.Shared
import spock.lang.Unroll
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.sql.DriverManager
@Unroll
class JooqFuncTest extends BaseFuncTest {
private static final ALL_INPUTS_DECLARED_JOOQ_TASK = """
tasks.named('generateJooq').configure { allInputsDeclared = true }
"""
private static final ALL_INPUTS_NOT_DECLARED_JOOQ_TASK = """
tasks.named('generateJooq').configure { allInputsDeclared = false }
"""
@AutoCleanup
@Shared
Sql sql
void setupSpec() {
sql = new Sql(DriverManager.getConnection('jdbc:h2:~/test;AUTO_SERVER=TRUE', 'sa', ''))
sql.execute('CREATE SCHEMA IF NOT EXISTS jooq_test;')
sql.execute('CREATE TABLE IF NOT EXISTS jooq_test.foo (a INT);')
}
@SuppressWarnings(['SqlNoDataSourceInspection', 'SqlResolve'])
void cleanupSpec() {
sql.execute('DROP TABLE jooq_test.foo')
sql.execute('DROP SCHEMA jooq_test')
}
void "can invoke jOOQ task from configuration DSL with configuration name omitted from task name for 'main' configuration"() {
given:
buildFile << buildWithJooqPluginDSL()
when:
def result = runWithArguments('generateJooq')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
and:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
@Requires({ (determineGradleVersion().baseVersion >= GradleVersion.version('7.0')) })
void "can invoke jOOQ task from configuration DSL with Gradle configuration cache enabled"() {
given:
buildFile << buildWithJooqPluginDSL()
when:
def result = runWithArguments('generateJooq', '--configuration-cache')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.output.contains("Calculating task graph as no configuration cache is available for tasks: generateJooq")
|| result.output.contains("Calculating task graph as no cached configuration is available for tasks: generateJooq")
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
when:
new File(workspaceDir, 'build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java').delete()
result = runWithArguments('generateJooq', '--configuration-cache')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.output.contains("Reusing configuration cache.")
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "can invoke jOOQ task derived from configuration DSL with multiple items"() {
given:
buildFile << buildWithMultipleItemsJooqPluginDSL()
when:
def result = runWithArguments('generateSampleJooq')
then:
fileExists('build/generated-src/jooq/sample/nu/studer/sample_pkg/jooq_test/tables/Foo.java')
!fileExists('build/generated-src/jooq/main/nu/studer/main_pkg/jooq_test/tables/Foo.java')
and:
result.task(':generateSampleJooq').outcome == TaskOutcome.SUCCESS
}
void "can invoke jOOQ task derived from configuration DSL using Kotlin DSL"() {
given:
file('build.gradle.kts') << """
import org.jooq.meta.jaxb.Logging
import org.jooq.meta.jaxb.Jdbc
import org.jooq.meta.jaxb.Generator
import org.jooq.meta.jaxb.Database
import org.jooq.meta.jaxb.Target
plugins {
id("nu.studer.jooq")
java
}
repositories {
mavenCentral()
}
dependencies {
jooqGenerator("com.h2database:h2:2.1.214")
}
jooq {
version.set("3.19.1")
configurations {
create("main") {
jooqConfiguration.apply {
logging = Logging.WARN
jdbc.apply {
driver = "org.h2.Driver"
url = "jdbc:h2:~/test;AUTO_SERVER=TRUE"
user = "sa"
password = ""
}
generator.apply {
database.apply {
name = "org.jooq.meta.h2.H2Database"
includes = ".*"
excludes = ""
}
target.packageName = "nu.studer.sample"
}
}
}
}
}
"""
when:
def result = runWithArguments('generateJooq')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
and:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "can compile Java source files generated by jOOQ as part of invoking Java compile task with the matching source set"() {
given:
buildFile << buildWithJooqPluginDSL(null, null, Boolean.TRUE)
when:
def result = runWithArguments('classes')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
fileExists('build/classes/java/main/nu/studer/sample/jooq_test/tables/Foo.class')
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
result.task(':classes').outcome == TaskOutcome.SUCCESS
}
void "can disable auto-generation of schema source by jOOQ as part of invoking Java compile task with the matching source set"() {
given:
buildFile << buildWithJooqPluginDSL(null, null, Boolean.FALSE)
when:
def result = runWithArguments('classes')
then:
!fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
!fileExists('build/classes/java/main/nu/studer/sample/jooq_test/tables/Foo.class')
!result.task(':generateJooq')
result.task(':classes').outcome == TaskOutcome.UP_TO_DATE
}
void "can reconfigure the output dir"() {
given:
buildFile << buildWithJooqPluginDSL(null, 'src/generated/jooq')
buildFile << """
def newTargetDir = file('src/generated/jooq/other')
jooq {
configurations {
main {
generationTool {
generator {
target {
directory = newTargetDir
}
}
}
}
}
}
jooq.configurations.main.jooqConfiguration.generator.target.directory = file('src/generated/jooq/yet/another')
afterEvaluate {
SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets()
SourceSet sourceSet = sourceSets.findByName('main')
Set<File> dirs = sourceSet.getJava().getSrcDirs()
dirs.eachWithIndex { dir, index ->
println "\$dir---"
}
}
"""
when:
def result = runWithArguments('classes')
then:
!fileExists('src/generated/jooq/nu/studer/sample/jooq_test/tables/Foo.java')
!fileExists('src/generated/jooq/other/nu/studer/sample/jooq_test/tables/Foo.java')
fileExists('src/generated/jooq/yet/another/nu/studer/sample/jooq_test/tables/Foo.java')
fileExists('build/classes/java/main/nu/studer/sample/jooq_test/tables/Foo.class')
!result.output.contains('/src/generated/jooq---')
!result.output.contains('/src/generated/jooq/other---')
result.output.contains('/src/generated/jooq/yet/another---')
result.output.contains('/src/main/java---')
and:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
result.task(':classes').outcome == TaskOutcome.SUCCESS
}
void "can use default jOOQ version and edition"() {
given:
buildFile << buildWithJooqPluginDSL(null, null, null, null)
when:
def result = runWithArguments('dependencies', '--configuration', 'jooqGenerator')
then:
result.output.contains('org.jooq:jooq-codegen:3.19.1')
result.output.contains('org.jooq:jooq:3.19.1')
when:
result = runWithArguments('dependencies', '--configuration', 'compileClasspath')
then:
!result.output.contains('org.jooq:jooq-codegen:3.19.1')
result.output.contains('org.jooq:jooq:3.19.1')
}
void "can set custom jOOQ version"() {
given:
buildFile << buildWithJooqPluginDSL(null, null, null, '3.16.1')
when:
def result = runWithArguments('dependencies', '--configuration', 'jooqGenerator')
then:
result.output.contains('org.jooq:jooq-codegen:3.16.1')
result.output.contains('org.jooq:jooq:3.16.1')
when:
result = runWithArguments('dependencies', '--configuration', 'compileClasspath')
then:
!result.output.contains('org.jooq:jooq-codegen:3.16.1')
result.output.contains('org.jooq:jooq:3.16.1')
}
void "can set custom jOOQ version after initial configuration declaration"() {
given:
buildFile << buildWithJooqPluginDSL(null, null, null, '3.16.1')
buildFile << """
jooq.version = '3.17.1'
"""
when:
def result = runWithArguments('dependencies', '--configuration', 'jooqGenerator')
then:
result.output.contains('org.jooq:jooq-codegen:3.17.1')
result.output.contains('org.jooq:jooq:3.17.1')
when:
result = runWithArguments('dependencies', '--configuration', 'compileClasspath')
then:
!result.output.contains('org.jooq:jooq-codegen:3.17.1')
result.output.contains('org.jooq:jooq:3.17.1')
}
void "can set custom jOOQ edition"() {
given:
buildFile << buildWithJooqPluginDSL(null, null, null, null, JooqEdition.TRIAL)
when:
def result = runWithArguments('dependencies', '--configuration', 'jooqGenerator')
then: // resolution (incl. transitive dependencies) fails since the trial artifact is not in a public repository
result.output.contains('org.jooq.trial:jooq-codegen:3.19.1 FAILED')
!result.output.contains('org.jooq.trial:jooq:3.19.1')
when:
result = runWithArguments('dependencies', '--configuration', 'compileClasspath')
then:
!result.output.contains('org.jooq.trial:jooq-codegen:3.19.1')
result.output.contains('org.jooq.trial:jooq:3.19.1 FAILED')
}
void "can set custom jOOQ edition after initial configuration declaration"() {
given:
buildFile << buildWithJooqPluginDSL(null, null, null, null, JooqEdition.TRIAL)
buildFile << """
jooq.edition = nu.studer.gradle.jooq.JooqEdition.PRO
"""
when:
def result = runWithArguments('dependencies', '--configuration', 'jooqGenerator')
then: // resolution (incl. transitive dependencies) fails since the pro artifact is not in a public repository
result.output.contains('org.jooq.pro:jooq-codegen:3.19.1 FAILED')
!result.output.contains('org.jooq.pro:jooq:3.19.1')
when:
result = runWithArguments('dependencies', '--configuration', 'compileClasspath')
then:
!result.output.contains('org.jooq.pro:jooq-codegen:3.19.1')
result.output.contains('org.jooq.pro:jooq:3.19.1 FAILED')
}
void "supports task avoidance"() {
given:
buildFile << buildWithJooqPluginDSL()
buildFile << """
tasks.configureEach {
println("\${Thread.currentThread().id} Configuring \${it.path}")
}
task dummy {}
"""
when:
def result = runWithArguments(task)
then:
result.output.contains('Configuring :generateJooq') == expectGenerateJooq
result.output.contains('Configuring :compileJava') == expectCompileJava
where:
task | expectGenerateJooq | expectCompileJava
'dummy' | false | false
// 'generateJooq' | true | false
'compileJava' | true | true
}
void "does not participate in incremental build by default"() {
given:
buildFile << buildWithJooqPluginDSL()
when:
def result = runWithArguments('generateJooq')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
when:
result = runWithArguments('generateJooq')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "does not participate in incremental build if explicitly disabled"() {
given:
buildFile << buildWithJooqPluginDSL()
buildFile << ALL_INPUTS_NOT_DECLARED_JOOQ_TASK
when:
def result = runWithArguments('generateJooq')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
when:
result = runWithArguments('generateJooq')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "participates in incremental build if explicitly configured"() {
given:
buildFile << buildWithJooqPluginDSL()
buildFile << ALL_INPUTS_DECLARED_JOOQ_TASK
when:
def result = runWithArguments('generateJooq')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
when:
result = runWithArguments('generateJooq')
then:
result.task(':generateJooq').outcome == TaskOutcome.UP_TO_DATE
when:
buildFile.delete()
buildFile << buildWithJooqPluginDSL('different.target.package.name')
result = runWithArguments('generateJooq')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "detects when jOOQ configuration is different"() {
given:
buildFile << buildWithJooqPluginDSL()
when:
def result = runWithArguments('generateJooq')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
when:
buildFile.delete()
buildFile << buildWithJooqPluginDSL('different.target.package.name')
result = runWithArguments('build')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "detects when parts of the outputs are removed"() {
given:
buildFile << buildWithJooqPluginDSL()
when:
def result = runWithArguments('generateJooq')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
when:
new File(workspaceDir, 'build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java').delete()
result = runWithArguments('generateJooq')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "cleans output before each run"() {
given:
buildFile << buildWithJooqPluginDSL()
when:
def result = runWithArguments('generateJooq')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
when:
buildFile.delete()
buildFile << buildWithJooqPluginDSL('different.target.pkg.name')
result = runWithArguments('build')
then:
fileExists('build/generated-src/jooq/main/different/target/pkg/name/jooq_test/tables/Foo.java')
!fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "throws error when cleaning of output is set to false in the jOOQ configuration"() {
given:
buildFile << buildWithJooqPluginDSL()
buildFile << """
jooq.configurations.main.jooqConfiguration.generator.target.clean = false
"""
when:
def result = runAndFailWithArguments('generateJooq')
then:
result.output.contains "generator.target.clean must not be set to false. Disabling the cleaning of the output directory can lead to unexpected behavior in a Gradle build."
}
void "task output is cacheable if jooq generate task is marked as all inputs declared"() {
given:
buildFile << buildWithJooqPluginDSL()
buildFile << ALL_INPUTS_DECLARED_JOOQ_TASK
when:
def result = runWithArguments('generateJooq', '--build-cache')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
when:
result = runWithArguments('cleanGenerateJooq', 'generateJooq', '--build-cache')
then:
result.task(':generateJooq').outcome == TaskOutcome.FROM_CACHE
}
void "task inputs are relocatable if jooq generate task is marked as all inputs declared"() {
given:
buildFile << buildWithJooqPluginDSL(null, new File(workspaceDir, 'src/generated/jooq').absolutePath)
buildFile << ALL_INPUTS_DECLARED_JOOQ_TASK
and:
def otherProject = 'other'
def otherSettingsFile = file("$otherProject/settings.gradle")
Files.copy(settingsFile.toPath(), otherSettingsFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
def otherBuildFile = file("$otherProject/build.gradle")
otherBuildFile << buildWithJooqPluginDSL(null, new File(workspaceDir, "$otherProject/src/generated/jooq").absolutePath)
otherBuildFile << ALL_INPUTS_DECLARED_JOOQ_TASK
when:
def result = gradleRunner('generateJooq', '--build-cache')
.withProjectDir(workspaceDir)
.build()
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
when:
result = gradleRunner('generateJooq', '--build-cache')
.withProjectDir(new File(workspaceDir, otherProject))
.build()
then:
result.task(':generateJooq').outcome == TaskOutcome.FROM_CACHE
}
void "does not clean sources generated by jooq as part of Gradle's clean life-cycle task"() {
given:
buildFile << buildWithJooqPluginDSL(null, 'src/generated/jooq')
when:
runWithArguments('generateJooq')
then:
fileExists('src/generated/jooq/nu/studer/sample/jooq_test/tables/Foo.java')
when:
def result = runWithArguments('clean')
then:
fileExists('src/generated/jooq/nu/studer/sample/jooq_test/tables/Foo.java')
!result.task(':cleanGenerateJooq')
}
void "cleans sources by calling clean task rule"() {
given:
buildFile << buildWithJooqPluginDSL('nu.studer.sample', 'src/generated/jooq/main')
when:
runWithArguments('build')
then:
fileExists('src/generated/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
when:
def result = runWithArguments('cleanGenerateJooq')
then:
!fileExists('src/generated/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.task(':cleanGenerateJooq').outcome == TaskOutcome.SUCCESS
}
void "can normalize jooq configuration input property"() {
given:
def packageIgnoringConfigNormalization = """
generateJooq {
generationToolNormalization = { org.jooq.meta.jaxb.Configuration c ->
c.generator.target.packageName = ''
}
}
"""
when: // apply normalization
buildFile << buildWithJooqPluginDSL('some.place')
buildFile << ALL_INPUTS_DECLARED_JOOQ_TASK
buildFile << packageIgnoringConfigNormalization
def result = runWithArguments('generateJooq')
then:
fileExists('build/generated-src/jooq/main/some/place/jooq_test/tables/Foo.java')
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
when: // apply normalization
buildFile.delete()
buildFile << buildWithJooqPluginDSL('some.other.place')
buildFile << ALL_INPUTS_DECLARED_JOOQ_TASK
buildFile << packageIgnoringConfigNormalization
result = runWithArguments('generateJooq')
then:
result.task(':generateJooq').outcome == TaskOutcome.UP_TO_DATE
when: // do not apply normalization
buildFile.delete()
buildFile << buildWithJooqPluginDSL('yet.another.place')
buildFile << ALL_INPUTS_DECLARED_JOOQ_TASK
result = runWithArguments('generateJooq')
then:
fileExists('build/generated-src/jooq/main/yet/another/place/jooq_test/tables/Foo.java')
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "can customize java execution and handle execution result"() {
given:
buildFile << buildWithJooqPluginDSL()
buildFile << """
generateJooq {
def out = new ByteArrayOutputStream()
javaExecSpec = { JavaExecSpec s ->
s.standardOutput = out
s.errorOutput = out
s.ignoreExitValue = true
}
execResultHandler = { ExecResult r ->
if (r.exitValue == 0) {
println('Jooq source code generation succeeded')
}
}
}
"""
when:
def result = runWithArguments('generateJooq')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.output.contains('Jooq source code generation succeeded')
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "shows an error message with a link to the current XSD when a property is missing"() {
given:
buildFile << buildWithMissingProperty()
when:
def result = runAndFailWithArguments('generateJooq')
then:
result.output.contains "Invalid property: 'missing' on extension 'jooq.main.generationTool.generator.generate', value: true. Please check the current XSD: https://www.jooq.org/xsd/${Constants.XSD_CODEGEN}"
}
void "shows an error message with a link to the current XSD when a configuration container element is missing"() {
given:
buildFile << buildWithMissingConfigurationContainerElement()
when:
def result = runAndFailWithArguments('generateJooq')
then:
result.output.contains "Invalid configuration container element: 'missing' on extension 'jooq.main.generationTool'. Please check the current XSD: https://www.jooq.org/xsd/${Constants.XSD_CODEGEN}"
}
void "correctly writes out boolean default values"() {
given:
buildFile << buildWithJooqPluginDSL()
when:
def result = runWithArguments('generateJooq')
then:
def configXml = new File(workspaceDir, 'build/tmp/generateJooq/config.xml')
configXml.exists()
def rootNode = new XmlSlurper().parse(configXml)
rootNode.generator.generate.globalTableReferences == true
rootNode.generator.generate.emptySchemas == false
and:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "successfully applies custom strategies when a sub project is added to the jooqGenerator configuration"() {
given:
buildFile << buildWithCustomStrategiesOnSubProject()
settingsFile << "include 'custom-generator'"
file('custom-generator/build.gradle') << customGeneratorBuildFile()
file('custom-generator/src/main/java/nu/studer/sample/SampleGeneratorStrategy.java') << customGeneratorStrategyClass()
when:
def result = runWithArguments('build')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
and:
def foo = new File(workspaceDir, 'build/generated-src/jooq/main/org/jooq/generated/jooq_test/tables/records/FooRecord.java')
foo.exists()
foo.text.contains("public Integer A() {") // instead of getA, as the sample generator strategy removed the get prefix
}
void "accepts matcher strategies even when name is not explicitly set to null"() {
given:
buildFile << buildWithMatcherStrategies()
when:
def result = runWithArguments('build')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "accepts empty JDBC configuration even when not explicitly set to null"() {
given:
def sqlSchemaFile = file('src/main/resources/schema.sql')
sqlSchemaFile << 'CREATE TABLE jooq_test.foo (a INT);'
buildFile << buildWithNoJdbc(sqlSchemaFile)
when:
def result = runWithArguments('build')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
void "parses DSL with variables and methods references"() {
given:
buildFile << buildWithVariablesAndMethods()
when:
def result = runWithArguments('build')
then:
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
}
@Requires({ (determineGradleVersion().baseVersion < GradleVersion.version("6.7")) })
void "invoke jOOQ task and use JVM Toolchains fails at configuration time"() {
given:
buildFile << buildWithJooqPluginDSL()
buildFile << """
java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
}
"""
when:
def result = runAndFailWithArguments('generateJooq')
then:
!fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.task(':generateJooq') == null
result.output.contains('Could not find method toolchain()')
}
@Requires({ (determineGradleVersion().baseVersion >= GradleVersion.version('6.7')) })
void "can invoke jOOQ task and use default JVM toolchain"() {
given:
buildFile << buildWithJooqPluginDSL()
buildFile << """
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
generateJooq {
doFirst {
println("Running jOOQ task with JDK \${it.launcher.get().metadata.languageVersion.asInt()}")
}
}
"""
when:
def result = runWithArguments('generateJooq')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
result.output.contains('Running jOOQ task with JDK 17')
}
@Requires({ (determineGradleVersion().baseVersion >= GradleVersion.version('7.3')) })
void "can invoke jOOQ task and override default JVM toolchain"() {
given:
buildFile << buildWithJooqPluginDSL()
buildFile << """
java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
}
generateJooq {
launcher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(17)
}
doFirst {
println("Running jOOQ task with JDK \${it.launcher.get().metadata.languageVersion.asInt()}")
}
}
"""
when:
def result = runWithArguments('generateJooq')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
result.output.contains('Running jOOQ task with JDK 17')
}
@Requires({ (determineGradleVersion().baseVersion >= GradleVersion.version('7.3')) })
void "can invoke jOOQ task from configuration DSL with Gradle configuration cache enabled and using toolchain"() {
given:
buildFile << buildWithJooqPluginDSL()
buildFile << """
java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
}
generateJooq {
launcher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(17)
}
doFirst {
println("Running jOOQ task with JDK \${it.launcher.get().metadata.languageVersion.asInt()}")
}
}
"""
when:
def result = runWithArguments('generateJooq', '--configuration-cache')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.output.contains("Calculating task graph as no configuration cache is available for tasks: generateJooq")
|| result.output.contains("Calculating task graph as no cached configuration is available for tasks: generateJooq")
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
result.output.contains('Running jOOQ task with JDK 17')
when:
new File(workspaceDir, 'build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java').delete()
result = runWithArguments('generateJooq', '--configuration-cache')
then:
fileExists('build/generated-src/jooq/main/nu/studer/sample/jooq_test/tables/Foo.java')
result.output.contains("Reusing configuration cache.")
result.task(':generateJooq').outcome == TaskOutcome.SUCCESS
result.output.contains('Running jOOQ task with JDK 17')
}
private static String buildWithJooqPluginDSL(String targetPackageName = null,
String targetDirectory = null,
Boolean generateSchemaSourceOnCompilation = null,
String jooqVersion = null,
JooqEdition jooqEdition = null) {
"""
plugins {
id 'nu.studer.jooq'
}
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
jooqGenerator 'com.h2database:h2:2.1.214'
}
jooq {
${jooqVersion != null ? "version = '$jooqVersion'" : ""}
${jooqEdition != null ? "edition = nu.studer.gradle.jooq.JooqEdition.${jooqEdition.name()}" : ""}
configurations {
main {
${generateSchemaSourceOnCompilation != null ? "generateSchemaSourceOnCompilation = $generateSchemaSourceOnCompilation" : ""}
generationTool {
logging = org.jooq.meta.jaxb.Logging.WARN
jdbc {
driver = 'org.h2.Driver'
url = 'jdbc:h2:~/test;AUTO_SERVER=TRUE'
user = 'sa'
password = ''
}
generator {
name = 'org.jooq.codegen.DefaultGenerator'
strategy {
name = 'org.jooq.codegen.DefaultGeneratorStrategy'
}
database {
name = 'org.jooq.meta.h2.H2Database'
includes = '.*'
excludes = ''
forcedTypes {
forcedType {
name = 'varchar'
expression = '.*'
types = 'JSONB?'
}
forcedType {
name = 'varchar'
expression = '.*'
types = 'INET'
}
}
}
generate {
javaTimeTypes = true
}
target {
${targetPackageName != null ? "packageName = '$targetPackageName'" : "packageName = 'nu.studer.sample'"}
${targetDirectory != null ? "directory = '$targetDirectory'" : ""}
}
}
}
}
}
}
"""
}
private static String buildWithMultipleItemsJooqPluginDSL() {
"""
plugins {
id 'nu.studer.jooq'
}
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
jooqGenerator 'com.h2database:h2:2.1.214'
}
jooq {
edition = nu.studer.gradle.jooq.JooqEdition.OSS
configurations {
main {
generationTool {
logging = org.jooq.meta.jaxb.Logging.WARN
jdbc {
driver = 'org.h2.Driver'
url = 'jdbc:h2:~/test;AUTO_SERVER=TRUE'
user = 'sa'
password = ''
}
generator {
database {
name = 'org.jooq.meta.h2.H2Database'
}
target {
packageName = 'nu.studer.main-pkg'
}
}
}
}
sample {
generationTool {
logging = org.jooq.meta.jaxb.Logging.WARN
jdbc {
driver = 'org.h2.Driver'
url = 'jdbc:h2:~/test;AUTO_SERVER=TRUE'
user = 'sa'
password = ''
}
generator {
database {
name = 'org.jooq.meta.h2.H2Database'
}
target {
packageName = 'nu.studer.sample-pkg'
}
}
}
}
}
}
"""
}
private static String buildWithMissingProperty() {
"""
plugins {
id 'nu.studer.jooq'
}
apply plugin: 'java'