Merge pull request #8464 from kwoyke/BAEL-3734
BAEL-3734: Fix Groovy Metaprogramming examples
This commit is contained in:
commit
f6b1f17484
@ -2,10 +2,13 @@ package com.baeldung.metaprogramming.extension
|
|||||||
|
|
||||||
import com.baeldung.metaprogramming.Employee
|
import com.baeldung.metaprogramming.Employee
|
||||||
|
|
||||||
|
import java.time.LocalDate
|
||||||
|
import java.time.Year
|
||||||
|
|
||||||
class BasicExtensions {
|
class BasicExtensions {
|
||||||
|
|
||||||
static int getYearOfBirth(Employee self) {
|
static int getYearOfBirth(Employee self) {
|
||||||
return (new Date().getYear() + 1900) - self.age;
|
return Year.now().value - self.age
|
||||||
}
|
}
|
||||||
|
|
||||||
static String capitalize(String self) {
|
static String capitalize(String self) {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package com.baeldung.metaprogramming
|
package com.baeldung.metaprogramming
|
||||||
|
|
||||||
import groovy.time.TimeCategory
|
|
||||||
|
import java.time.LocalDate
|
||||||
|
import java.time.Period
|
||||||
|
import java.time.Year
|
||||||
|
|
||||||
class MetaprogrammingUnitTest extends GroovyTestCase {
|
class MetaprogrammingUnitTest extends GroovyTestCase {
|
||||||
|
|
||||||
@ -51,14 +54,16 @@ class MetaprogrammingUnitTest extends GroovyTestCase {
|
|||||||
|
|
||||||
void testJavaMetaClass() {
|
void testJavaMetaClass() {
|
||||||
String.metaClass.capitalize = { String str ->
|
String.metaClass.capitalize = { String str ->
|
||||||
str.substring(0, 1).toUpperCase() + str.substring(1);
|
str.substring(0, 1).toUpperCase() + str.substring(1)
|
||||||
}
|
}
|
||||||
assert "norman".capitalize() == "Norman"
|
assert "norman".capitalize() == "Norman"
|
||||||
}
|
}
|
||||||
|
|
||||||
void testEmployeeExtension() {
|
void testEmployeeExtension() {
|
||||||
Employee emp = new Employee(age: 28)
|
def age = 28
|
||||||
assert emp.getYearOfBirth() == 1992
|
def expectedYearOfBirth = Year.now() - age
|
||||||
|
Employee emp = new Employee(age: age)
|
||||||
|
assert emp.getYearOfBirth() == expectedYearOfBirth.value
|
||||||
}
|
}
|
||||||
|
|
||||||
void testJavaClassesExtensions() {
|
void testJavaClassesExtensions() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user