Skip to content

Commit c23f5e1

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents e9c0d90 + 6c237aa commit c23f5e1

File tree

71 files changed

+1519
-321
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1519
-321
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,10 @@ jmeter/src/main/resources/*-JMeter.csv
6666
**/nb-configuration.xml
6767
core-scala/.cache-main
6868
core-scala/.cache-tests
69+
70+
71+
persistence-modules/hibernate5/transaction.log
72+
apache-avro/src/main/java/com/baeldung/avro/model/
73+
jta/transaction-logs/
74+
software-security/sql-injection-samples/derby.log
75+
spring-soap/src/main/java/com/baeldung/springsoap/gen/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.traits
2+
3+
trait AnimalTrait {
4+
5+
String basicBehavior() {
6+
return "Animalistic!!"
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.traits
2+
3+
class Dog implements WalkingTrait, SpeakingTrait {
4+
5+
String speakAndWalk() {
6+
WalkingTrait.super.speakAndWalk()
7+
}
8+
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.traits
2+
3+
class Employee implements UserTrait {
4+
5+
String name() {
6+
return 'Bob'
7+
}
8+
9+
String lastName() {
10+
return "Marley"
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.traits
2+
3+
interface Human {
4+
5+
String lastName()
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.traits
2+
3+
trait SpeakingTrait {
4+
5+
String basicAbility() {
6+
return "Speaking!!"
7+
}
8+
9+
String speakAndWalk() {
10+
return "Speak and walk!!"
11+
}
12+
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.traits
2+
3+
trait UserTrait implements Human {
4+
5+
String sayHello() {
6+
return "Hello!"
7+
}
8+
9+
abstract String name()
10+
11+
String showName() {
12+
return "Hello, ${name()}!"
13+
}
14+
15+
private String greetingMessage() {
16+
return 'Hello, from a private method!'
17+
}
18+
19+
String greet() {
20+
def msg = greetingMessage()
21+
println msg
22+
msg
23+
}
24+
25+
def whoAmI() {
26+
return this
27+
}
28+
29+
String showLastName() {
30+
return "Hello, ${lastName()}!"
31+
}
32+
33+
String email
34+
String address
35+
}
36+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.traits
2+
3+
trait WalkingTrait {
4+
5+
String basicAbility() {
6+
return "Walking!!"
7+
}
8+
9+
String speakAndWalk() {
10+
return "Walk and speak!!"
11+
}
12+
13+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.baeldung.traits
2+
3+
import spock.lang.Specification
4+
5+
class TraitsUnitTest extends Specification {
6+
7+
Employee employee
8+
Dog dog
9+
10+
void setup () {
11+
employee = new Employee()
12+
dog = new Dog()
13+
}
14+
15+
def 'Should return msg string when using Employee.sayHello method provided by UserTrait' () {
16+
when:
17+
def msg = employee.sayHello()
18+
then:
19+
msg
20+
msg instanceof String
21+
assert msg == "Hello!"
22+
}
23+
24+
def 'Should return displayMsg string when using Employee.showName method' () {
25+
when:
26+
def displayMsg = employee.showName()
27+
then:
28+
displayMsg
29+
displayMsg instanceof String
30+
assert displayMsg == "Hello, Bob!"
31+
}
32+
33+
def 'Should return greetMsg string when using Employee.greet method' () {
34+
when:
35+
def greetMsg = employee.greet()
36+
then:
37+
greetMsg
38+
greetMsg instanceof String
39+
assert greetMsg == "Hello, from a private method!"
40+
}
41+
42+
def 'Should return MissingMethodException when using Employee.greetingMessage method' () {
43+
when:
44+
def exception
45+
try {
46+
employee.greetingMessage()
47+
}catch(Exception e) {
48+
exception = e
49+
}
50+
51+
then:
52+
exception
53+
exception instanceof groovy.lang.MissingMethodException
54+
assert exception.message == "No signature of method: com.baeldung.traits.Employee.greetingMessage()"+
55+
" is applicable for argument types: () values: []"
56+
}
57+
58+
def 'Should return employee instance when using Employee.whoAmI method' () {
59+
when:
60+
def emp = employee.whoAmI()
61+
then:
62+
emp
63+
emp instanceof Employee
64+
assert emp.is(employee)
65+
}
66+
67+
def 'Should display lastName when using Employee.showLastName method' () {
68+
when:
69+
def lastNameMsg = employee.showLastName()
70+
then:
71+
lastNameMsg
72+
lastNameMsg instanceof String
73+
assert lastNameMsg == "Hello, Marley!"
74+
}
75+
76+
def 'Should be able to define properties of UserTrait in Employee instance' () {
77+
when:
78+
employee = new Employee(email: "a@e.com", address: "baeldung.com")
79+
then:
80+
employee
81+
employee instanceof Employee
82+
assert employee.email == "a@e.com"
83+
assert employee.address == "baeldung.com"
84+
}
85+
86+
def 'Should execute basicAbility method from SpeakingTrait and return msg string' () {
87+
when:
88+
def speakMsg = dog.basicAbility()
89+
then:
90+
speakMsg
91+
speakMsg instanceof String
92+
assert speakMsg == "Speaking!!"
93+
}
94+
95+
def 'Should verify multiple inheritance with traits and execute overridden traits method' () {
96+
when:
97+
def walkSpeakMsg = dog.speakAndWalk()
98+
println walkSpeakMsg
99+
then:
100+
walkSpeakMsg
101+
walkSpeakMsg instanceof String
102+
assert walkSpeakMsg == "Walk and speak!!"
103+
}
104+
105+
def 'Should implement AnimalTrait at runtime and access basicBehavior method' () {
106+
when:
107+
def dogInstance = new Dog() as AnimalTrait
108+
def basicBehaviorMsg = dogInstance.basicBehavior()
109+
then:
110+
basicBehaviorMsg
111+
basicBehaviorMsg instanceof String
112+
assert basicBehaviorMsg == "Animalistic!!"
113+
}
114+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.customannotations;
2+
3+
import static java.lang.annotation.ElementType.METHOD;
4+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
5+
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
9+
@Retention(RUNTIME)
10+
@Target(METHOD)
11+
public @interface Init {
12+
13+
}

0 commit comments

Comments
 (0)