-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassDesignASTTransformsTest.groovy
411 lines (372 loc) · 10.6 KB
/
ClassDesignASTTransformsTest.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
class ClassDesignASTTransformsTest extends GroovyTestCase {
void testDelegateTransformation() {
assertScript '''
// tag::delegating_class[]
class Event {
@Delegate Date when
String title
}
// end::delegating_class[]
/*
// tag::delegating_class_generated[]
class Event {
Date when
String title
boolean before(Date other) {
when.before(other)
}
// ...
}
// end::delegating_class_generated[]
*/
// tag::delegation_assert[]
def ev = new Event(title:'Groovy keynote', when: Date.parse('yyyy/MM/dd', '2013/09/10'))
def now = new Date()
assert ev.before(now)
// end::delegation_assert[]
'''
assertScript '''
// tag::delegate_example_interfaces[]
interface Greeter { void sayHello() }
class MyGreeter implements Greeter { void sayHello() { println 'Hello!'} }
class DelegatingGreeter { // no explicit interface
@Delegate MyGreeter greeter = new MyGreeter()
}
def greeter = new DelegatingGreeter()
assert greeter instanceof Greeter // interface was added transparently
// end::delegate_example_interfaces[]
'''
assertScript '''
// tag::delegate_deprecated_header[]
class WithDeprecation {
@Deprecated
void foo() {}
}
class WithoutDeprecation {
@Deprecated
void bar() {}
}
class Delegating {
@Delegate(deprecated=true) WithDeprecation with = new WithDeprecation()
@Delegate WithoutDeprecation without = new WithoutDeprecation()
}
def d = new Delegating()
d.foo() // passes thanks to deprecated=true
// end::delegate_deprecated_header[]
try {
// tag::delegate_deprecated_footer[]
d.bar() // fails because of @Deprecated
// end::delegate_deprecated_footer[]
} catch (e ) {}
'''
assertScript '''
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target([ElementType.METHOD])
@interface Transactional {}
// tag::delegate_example_annotations[]
class WithAnnotations {
@Transactional
void method() {
}
}
class DelegatingWithoutAnnotations {
@Delegate WithAnnotations delegate
}
class DelegatingWithAnnotations {
@Delegate(methodAnnotations = true) WithAnnotations delegate
}
def d1 = new DelegatingWithoutAnnotations()
def d2 = new DelegatingWithAnnotations()
assert d1.class.getDeclaredMethod('method').annotations.length==0
assert d2.class.getDeclaredMethod('method').annotations.length==1
// end::delegate_example_annotations[]
'''
assertScript '''
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target([ElementType.PARAMETER])
@interface NotNull {}
// tag::delegate_example_parameter_annotations[]
class WithAnnotations {
void method(@NotNull String str) {
}
}
class DelegatingWithoutAnnotations {
@Delegate WithAnnotations delegate
}
class DelegatingWithAnnotations {
@Delegate(parameterAnnotations = true) WithAnnotations delegate
}
def d1 = new DelegatingWithoutAnnotations()
def d2 = new DelegatingWithAnnotations()
assert d1.class.getDeclaredMethod('method',String).parameterAnnotations[0].length==0
assert d2.class.getDeclaredMethod('method',String).parameterAnnotations[0].length==1
// end::delegate_example_parameter_annotations[]
'''
assertScript '''
// tag::delegate_example_excludes_header[]
class Worker {
void task1() {}
void task2() {}
}
class Delegating {
@Delegate(excludes=['task2']) Worker worker = new Worker()
}
def d = new Delegating()
d.task1() // passes
// end::delegate_example_excludes_header[]
groovy.test.GroovyAssert.shouldFail {
// tag::delegate_example_excludes_footer[]
d.task2() // fails because method is excluded
// end::delegate_example_excludes_footer[]
}
'''
assertScript '''
// tag::delegate_example_includes_header[]
class Worker {
void task1() {}
void task2() {}
}
class Delegating {
@Delegate(includes=['task1']) Worker worker = new Worker()
}
def d = new Delegating()
d.task1() // passes
// end::delegate_example_includes_header[]
groovy.test.GroovyAssert.shouldFail {
// tag::delegate_example_includes_footer[]
d.task2() // fails because method is not included
// end::delegate_example_includes_footer[]
}
'''
assertScript '''
// tag::delegate_example_includeTypes_header[]
interface AppendBooleanSelector {
StringBuilder append(boolean b)
}
interface AppendFloatSelector {
StringBuilder append(float b)
}
class NumberBooleanBuilder {
@Delegate(includeTypes=AppendBooleanSelector, interfaces=false)
StringBuilder nums = new StringBuilder()
@Delegate(includeTypes=[AppendFloatSelector], interfaces=false)
StringBuilder bools = new StringBuilder()
String result() { "${nums.toString()} | ${bools.toString()}" }
}
def b = new NumberBooleanBuilder()
b.append(true)
b.append(3.14f)
b.append(false)
b.append(0.0f)
assert b.result() == "truefalse | 3.140.0"
// end::delegate_example_includeTypes_header[]
groovy.test.GroovyAssert.shouldFail {
// tag::delegate_example_includeTypes_footer[]
b.append(3.5d) // would fail because we didn't include append(double)
// end::delegate_example_includeTypes_footer[]
}
'''
assertScript '''
// tag::delegate_example_excludeTypes[]
interface AppendStringSelector {
StringBuilder append(String str)
}
class UpperStringBuilder {
@Delegate(excludeTypes=AppendStringSelector)
StringBuilder sb1 = new StringBuilder()
@Delegate(includeTypes=AppendStringSelector)
StringBuilder sb2 = new StringBuilder()
String toString() { sb1.toString() + sb2.toString().toUpperCase() }
}
def usb = new UpperStringBuilder()
usb.append(3.5d)
usb.append('hello')
usb.append(true)
assert usb.toString() == '3.5trueHELLO'
// end::delegate_example_excludeTypes[]
'''
}
void testImmutable() {
assertScript '''
// tag::immutable_simple[]
import groovy.transform.Immutable
@Immutable
class Point {
int x
int y
}
// end::immutable_simple[]
def p = new Point(x:1,y:2)
assert p.toString() == 'Point(1, 2)' // @ToString equivalent
try {
p.x = 2
} catch (ReadOnlyPropertyException rope) {
println 'ReadOnly property'
}
'''
assertScript '''
// tag::immutable_example_knownimmutableclasses[]
import groovy.transform.Immutable
import groovy.transform.TupleConstructor
@TupleConstructor
final class Point {
final int x
final int y
public String toString() { "($x,$y)" }
}
@Immutable(knownImmutableClasses=[Point])
class Triangle {
Point a,b,c
}
// end::immutable_example_knownimmutableclasses[]
def p = new Triangle(a:[47,22], b:[12,12],c:[88,17])
assert p.toString() == 'Triangle((47,22), (12,12), (88,17))' // @ToString equivalent
try {
p.a = [0,0]
} catch (ReadOnlyPropertyException rope) {
println 'ReadOnly property'
}
'''
assertScript '''
// tag::immutable_example_knownimmutables[]
import groovy.transform.Immutable
import groovy.transform.TupleConstructor
@TupleConstructor
final class Point {
final int x
final int y
public String toString() { "($x,$y)" }
}
@Immutable(knownImmutables=['a','b','c'])
class Triangle {
Point a,b,c
}
// end::immutable_example_knownimmutables[]
def p = new Triangle(a:[47,22], b:[12,12],c:[88,17])
assert p.toString() == 'Triangle((47,22), (12,12), (88,17))' // @ToString equivalent
try {
p.a = [0,0]
} catch (ReadOnlyPropertyException rope) {
println 'ReadOnly property'
}
'''
assertScript '''
// tag::immutable_example_copyWith[]
import groovy.transform.Immutable
@Immutable( copyWith=true )
class User {
String name
Integer age
}
def bob = new User( 'bob', 43 )
def alice = bob.copyWith( name:'alice' )
assert alice.name == 'alice'
assert alice.age == 43
// end::immutable_example_copyWith[]
try {
bob.name = 'alice'
} catch (ReadOnlyPropertyException rope) {
println 'ReadOnly property'
}
'''
}
void testMemoized() {
assertScript '''
import groovy.transform.Memoized
// tag::memoized_long_computation[]
long longComputation(int seed) {
// slow computation
Thread.sleep(100*seed)
System.nanoTime()
}
// end::memoized_long_computation[]
// tag::memoized_long_computation_asserts[]
def x = longComputation(1)
def y = longComputation(1)
assert x!=y
// end::memoized_long_computation_asserts[]
'''
assertScript '''
import groovy.transform.Memoized
// tag::memoized_long_computation_cached[]
@Memoized
long longComputation(int seed) {
// slow computation
Thread.sleep(100*seed)
System.nanoTime()
}
def x = longComputation(1) // returns after 100 milliseconds
def y = longComputation(1) // returns immediatly
def z = longComputation(2) // returns after 200 milliseconds
assert x==y
assert x!=z
// end::memoized_long_computation_cached[]
'''
}
void testSingleton() {
assertScript '''
// tag::singleton_simple[]
@Singleton
class GreetingService {
String greeting(String name) { "Hello, $name!" }
}
assert GreetingService.instance.greeting('Bob') == 'Hello, Bob!'
// end::singleton_simple[]
'''
assertScript '''
// tag::singleton_example_property[]
@Singleton(property='theOne')
class GreetingService {
String greeting(String name) { "Hello, $name!" }
}
assert GreetingService.theOne.greeting('Bob') == 'Hello, Bob!'
// end::singleton_example_property[]
'''
assertScript '''
// tag::singleton_example_lazy[]
class Collaborator {
public static boolean init = false
}
@Singleton(lazy=true,strict=false)
class GreetingService {
static void init() {}
GreetingService() {
Collaborator.init = true
}
String greeting(String name) { "Hello, $name!" }
}
GreetingService.init() // make sure class is initialized
assert Collaborator.init == false
GreetingService.instance
assert Collaborator.init == true
assert GreetingService.instance.greeting('Bob') == 'Hello, Bob!'
// end::singleton_example_lazy[]
'''
}
}