Skip to content

Commit d36f75b

Browse files
committed
add integration tests
1 parent 55f65ca commit d36f75b

File tree

77 files changed

+1406
-373
lines changed

Some content is hidden

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

77 files changed

+1406
-373
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ description = 'Coditory Quark Configuration Library'
1414

1515
dependencies {
1616
api 'org.slf4j:slf4j-api:1.7.30'
17+
api 'org.jetbrains:annotations:16.0.1'
1718
testImplementation 'ch.qos.logback:logback-classic:1.2.3'
1819
testImplementation 'org.spockframework:spock-core:2.0-M4-groovy-3.0'
1920
testImplementation 'org.skyscreamer:jsonassert:1.5.0'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package annotated
2+
3+
import annotated.samples.beans_lifecycle.ConfigLifecycle
4+
import com.coditory.quark.context.Context
5+
import spock.lang.Specification
6+
7+
class BeanLifecycleSpec extends Specification {
8+
def "should register beans and initialize them"() {
9+
given:
10+
Context context = Context.scanPackage(ConfigLifecycle)
11+
12+
when:
13+
ConfigLifecycle config = context.get(ConfigLifecycle)
14+
then:
15+
config.initialized
16+
config.initialized2
17+
and:
18+
!config.finalized
19+
!config.finalized2
20+
21+
when:
22+
annotated.samples.beans_lifecycle.Bar bar = context.get(annotated.samples.beans_lifecycle.Bar)
23+
then:
24+
bar.initialized
25+
!bar.finalized
26+
27+
when:
28+
annotated.samples.beans_lifecycle.Baz baz = context.get(annotated.samples.beans_lifecycle.Baz)
29+
then:
30+
baz.initialized
31+
!baz.finalized
32+
}
33+
34+
def "should finalize beans when closing the context"() {
35+
given:
36+
Context context = Context.scanPackage(ConfigLifecycle)
37+
and:
38+
ConfigLifecycle config = context.get(ConfigLifecycle)
39+
annotated.samples.beans_lifecycle.Bar bar = context.get(annotated.samples.beans_lifecycle.Bar)
40+
annotated.samples.beans_lifecycle.Baz baz = context.get(annotated.samples.beans_lifecycle.Baz)
41+
when:
42+
context.close()
43+
then:
44+
config.finalized
45+
config.finalized2
46+
and:
47+
bar.finalized
48+
and:
49+
baz.finalized
50+
}
51+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package annotated
2+
3+
import com.coditory.quark.context.Context
4+
import com.coditory.quark.context.ContextException
5+
import spock.lang.Specification
6+
7+
class BeanRegistrationSpec extends Specification {
8+
def "should load annotated beans"() {
9+
when:
10+
Context context = Context.scanPackage(annotated.samples.beans.Foo.class)
11+
then:
12+
context.getOrNull(annotated.samples.beans.Foo) != null
13+
context.getOrNull(annotated.samples.beans.bar.Bar) != null
14+
context.getOrNull(annotated.samples.beans.bar.Foo) != null
15+
and:
16+
context.getOrNull(annotated.samples.beans.bar.Baz) == null
17+
}
18+
19+
def "should load annotated beans with dependencies"() {
20+
when:
21+
Context context = Context.scanPackage(annotated.samples.beans_with_deps.Foo)
22+
then:
23+
context.get(annotated.samples.beans_with_deps.Foo)
24+
context.get(annotated.samples.beans_with_deps.Bar)
25+
context.get(annotated.samples.beans_with_deps.Baz)
26+
}
27+
28+
def "should throw error when creating beans with circular dependency"() {
29+
given:
30+
Context context = Context.scanPackage(annotated.samples.beans_circular_deps.Foo)
31+
when:
32+
context.get(annotated.samples.beans_circular_deps.Bar)
33+
34+
then:
35+
ContextException e = thrown(ContextException)
36+
e.message == "Could not create bean of type: ${annotated.samples.beans_circular_deps.Bar.class.canonicalName}"
37+
e.cause.message == "Detected circular dependency: Bar -> Baz -> Foo -> Bar"
38+
}
39+
40+
def "should throw error when creating bean with a self dependency"() {
41+
given:
42+
Context context = Context.scanPackage(annotated.samples.beans_circular_deps.BarBar)
43+
when:
44+
context.get(annotated.samples.beans_circular_deps.BarBar)
45+
46+
then:
47+
ContextException e = thrown(ContextException)
48+
e.message == "Could not create bean of type: ${annotated.samples.beans_circular_deps.BarBar.class.canonicalName}"
49+
e.cause.message == "Detected circular dependency: BarBar -> BarBar"
50+
}
51+
52+
def "should inject named beans"() {
53+
given:
54+
Context context = Context.scanPackage(annotated.samples.named_bean.Bar)
55+
when:
56+
annotated.samples.named_bean.Bar bar = context.get(annotated.samples.named_bean.Bar)
57+
58+
then:
59+
bar.foo != null
60+
bar.baz == null
61+
}
62+
63+
def "should inject optional beans"() {
64+
given:
65+
Context context = Context.scanPackage(annotated.samples.optional_bean.Bar)
66+
when:
67+
annotated.samples.optional_bean.Bar bar = context.get(annotated.samples.optional_bean.Bar)
68+
69+
then:
70+
bar.foo == null
71+
bar.baz != null
72+
}
73+
74+
def "should inject dependencies by interface"() {
75+
given:
76+
Context context = Context.scanPackage(annotated.samples.optional_bean.Bar)
77+
when:
78+
annotated.samples.optional_bean.Bar bar = context.get(annotated.samples.optional_bean.Bar)
79+
80+
then:
81+
bar.foo == null
82+
bar.baz != null
83+
}
84+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package annotated
2+
3+
4+
import annotated.samples.config_inject.ConfigInject
5+
import com.coditory.quark.context.Context
6+
import spock.lang.Specification
7+
8+
class ConfigurationRegistrationSpec extends Specification {
9+
def "should register beans form a configuration"() {
10+
given:
11+
Context context = Context.scanPackage(annotated.samples.config.SampleConfig)
12+
13+
when:
14+
annotated.samples.config.Foo foo = context.get(annotated.samples.config.Foo)
15+
then:
16+
foo.name == "no-name"
17+
foo.bar != null
18+
19+
when:
20+
annotated.samples.config.Foo foooo = context.get(annotated.samples.config.Foo, "foooo")
21+
then:
22+
foooo.name == "foooo"
23+
foooo.bar != null
24+
25+
when:
26+
annotated.samples.config.Foo foo2 = context.get(annotated.samples.config.Foo, "foo2")
27+
then:
28+
foo2.name == "foo2"
29+
foo2.bar != null
30+
}
31+
32+
def "should inject bean into configuration constructor"() {
33+
given:
34+
Context context = Context.scanPackage(ConfigInject)
35+
36+
when:
37+
annotated.samples.config_inject.Baz baz = context.get(annotated.samples.config_inject.Baz)
38+
then:
39+
baz.bar != null
40+
}
41+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package annotated
2+
3+
import com.coditory.quark.context.Context
4+
import com.coditory.quark.context.ContextException
5+
import spock.lang.Specification
6+
7+
class InjectMultipleDependenciesSpec extends Specification {
8+
def "should inject list of beans"() {
9+
given:
10+
Context context = Context.scanPackage(annotated.samples.multiple_deps.Bar)
11+
when:
12+
annotated.samples.multiple_deps.Bar bar = context.get(annotated.samples.multiple_deps.Bar)
13+
14+
then:
15+
bar.foo != null
16+
bar.foo.size() == 2
17+
}
18+
19+
def "should inject empty list for no beans and optional dependency"() {
20+
given:
21+
Context context = Context.scanPackage(annotated.samples.optional_multiple_deps.Bar)
22+
when:
23+
annotated.samples.optional_multiple_deps.Bar bar = context.get(annotated.samples.optional_multiple_deps.Bar)
24+
25+
then:
26+
bar.foo != null
27+
bar.foo.isEmpty()
28+
}
29+
30+
def "should fail injecting required list of no beans"() {
31+
given:
32+
Context context = Context.scanPackage(annotated.samples.optional_multiple_deps.Baz)
33+
when:
34+
context.get(annotated.samples.optional_multiple_deps.Baz)
35+
36+
then:
37+
ContextException e = thrown(ContextException)
38+
e.message == "Could not create bean of type: ${annotated.samples.optional_multiple_deps.Baz.class.canonicalName}"
39+
e.cause.message == "Beans not found for type: ${annotated.samples.optional_multiple_deps.Foo.class.canonicalName}"
40+
}
41+
42+
def "should fail injecting named list of no beans"() {
43+
given:
44+
Context context = Context.scanPackage(annotated.samples.named_multiple_deps.Bar)
45+
when:
46+
context.get(annotated.samples.named_multiple_deps.Bar)
47+
48+
then:
49+
ContextException e = thrown(ContextException)
50+
e.message == "Could not create bean of type: ${annotated.samples.named_multiple_deps.Bar.class.canonicalName}"
51+
e.cause.message.startsWith("Detected named @Dependency for a list of dependencies")
52+
}
53+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.coditory.quark.context.annotated.samples.beans
1+
package annotated.samples.beans
22

33
import com.coditory.quark.context.Bean
44

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package annotated.samples.beans.bar
2+
3+
import com.coditory.quark.context.Bean
4+
5+
@Bean
6+
class Bar {
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package annotated.samples.beans.bar
2+
3+
class Baz {
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package annotated.samples.beans.bar
2+
3+
import com.coditory.quark.context.Bean
4+
5+
@Bean
6+
class Foo {
7+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package com.coditory.quark.context.annotated.samples.beans_circular_deps
1+
package annotated.samples.beans_circular_deps
22

33
import com.coditory.quark.context.Bean
44

55
import static java.util.Objects.requireNonNull
66

77
@Bean
88
class Bar {
9-
private final Baz baz
9+
final Baz baz
1010

1111
Bar(Baz baz) {
1212
this.baz = requireNonNull(baz)

0 commit comments

Comments
 (0)