1
1
/*
2
- * Copyright (c) 2019, 2021 Oracle and/or its affiliates.
2
+ * Copyright (c) 2019, 2022 Oracle and/or its affiliates.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
32
32
import org .junit .jupiter .api .BeforeEach ;
33
33
import org .junit .jupiter .api .Test ;
34
34
35
- import static org .junit .jupiter .api .Assertions .assertEquals ;
36
- import static org .junit .jupiter .api .Assertions .assertNotNull ;
37
- import static org .junit .jupiter .api .Assertions .assertTrue ;
35
+ import static org .hamcrest .CoreMatchers .is ;
36
+ import static org .hamcrest .CoreMatchers .notNullValue ;
37
+ import static org .hamcrest .MatcherAssert .assertThat ;
38
+ import static org .hamcrest .collection .IsCollectionWithSize .hasSize ;
38
39
39
40
@ ApplicationScoped
40
41
final class TestReferenceCountedContext {
@@ -53,7 +54,7 @@ private TestReferenceCountedContext() {
53
54
private final void startContainer () {
54
55
stopContainer ();
55
56
final SeContainerInitializer initializer = SeContainerInitializer .newInstance ();
56
- assertNotNull (initializer );
57
+ assertThat (initializer , notNullValue () );
57
58
initializer .disableDiscovery ();
58
59
initializer .addBeanClasses (this .getClass (), HousingBean .class , Gorp .class );
59
60
initializer .addExtensions (ReferenceCountedExtension .class );
@@ -72,46 +73,46 @@ private final void onStartup(@Observes @Initialized(ApplicationScoped.class) fin
72
73
final RequestContextController controller ,
73
74
final BeanManager beanManager ,
74
75
final Gorp gorpAsParameter ) {
75
- assertNotNull (controller );
76
- assertNotNull (beanManager );
77
- assertNotNull (gorpAsParameter );
76
+ assertThat (controller , notNullValue () );
77
+ assertThat (beanManager , notNullValue () );
78
+ assertThat (gorpAsParameter , notNullValue () );
78
79
79
80
final Set <Bean <?>> gorpBeans = beanManager .getBeans (Gorp .class );
80
- assertNotNull (gorpBeans );
81
- assertEquals ( 1 , gorpBeans . size ( ));
81
+ assertThat (gorpBeans , notNullValue () );
82
+ assertThat ( gorpBeans , hasSize ( 1 ));
82
83
@ SuppressWarnings ("unchecked" )
83
84
final Bean <Gorp > gorpBean = (Bean <Gorp >)gorpBeans .iterator ().next ();
84
- assertNotNull (gorpBean );
85
+ assertThat (gorpBean , notNullValue () );
85
86
86
87
final ReferenceCountedContext context = ReferenceCountedContext .getInstanceFrom (beanManager );
87
- assertNotNull (context );
88
- assertTrue (context .isActive ());
88
+ assertThat (context , notNullValue () );
89
+ assertThat (context .isActive (), is ( true ));
89
90
90
91
// Gorp is @ReferenceCounted. It is proxied. Note that we do
91
92
// not dereference the gorpAsParameter proxy yet, so no
92
93
// underlying instance is created.
93
- assertEquals ( 0 , context .getReferenceCount (gorpBean ));
94
+ assertThat ( context .getReferenceCount (gorpBean ), is ( 0 ));
94
95
95
96
Gorp gorp = null ;
96
97
try {
97
98
controller .activate ();
98
99
99
100
// this.bean is @RequestScoped. It houses an instance of
100
101
// Gorp.
101
- assertNotNull (this .bean );
102
- assertEquals ( 0 , context .getReferenceCount (gorpBean ));
102
+ assertThat (this .bean , notNullValue () );
103
+ assertThat ( context .getReferenceCount (gorpBean ), is ( 0 ));
103
104
104
105
// Here, the gorp acquired is a client proxy. No
105
106
// contextual instance has been created yet.
106
107
gorp = this .bean .getGorp ();
107
- assertNotNull (gorp );
108
- assertEquals ( 0 , context .getReferenceCount (gorpBean ));
108
+ assertThat (gorp , notNullValue () );
109
+ assertThat ( context .getReferenceCount (gorpBean ), is ( 0 ));
109
110
110
111
// This will cause the underlying instance to be created.
111
112
// It will be the first Gorp instance created anywhere.
112
113
// The reference count is 1.
113
- assertEquals ( 1 , gorp .getId ());
114
- assertEquals ( 1 , context .getReferenceCount (gorpBean ));
114
+ assertThat ( gorp .getId (), is ( 1 ));
115
+ assertThat ( context .getReferenceCount (gorpBean ), is ( 1 ));
115
116
116
117
} finally {
117
118
@@ -121,13 +122,13 @@ private final void onStartup(@Observes @Initialized(ApplicationScoped.class) fin
121
122
// reference count of 0. This Gorp reference is therefore
122
123
// destroyed.
123
124
controller .deactivate ();
124
- assertEquals ( 0 , context .getReferenceCount (gorpBean ));
125
+ assertThat ( context .getReferenceCount (gorpBean ), is ( 0 ));
125
126
}
126
127
127
128
// Now we kick the proxy. This will cause another instance to
128
129
// be created. The reference count will bump to 1.
129
- assertEquals ( 2 , gorpAsParameter .getId ());
130
- assertEquals ( 1 , context .getReferenceCount (gorpBean ));
130
+ assertThat ( gorpAsParameter .getId (), is ( 2 ));
131
+ assertThat ( context .getReferenceCount (gorpBean ), is ( 1 ));
131
132
132
133
// Next we refer to the Gorp instance that was referred to by
133
134
// the request-scoped HousingBean. If Gorp had been
@@ -136,8 +137,8 @@ private final void onStartup(@Observes @Initialized(ApplicationScoped.class) fin
136
137
// because the gorpParameter dereference above caused the
137
138
// reference count to jump, THIS Gorp instance will now be the
138
139
// same one as gorpAsParameter.
139
- assertEquals ( 2 , gorp .getId ());
140
- assertEquals ( 2 , context .getReferenceCount (gorpBean ));
140
+ assertThat ( gorp .getId (), is ( 2 ));
141
+ assertThat ( context .getReferenceCount (gorpBean ), is ( 2 ));
141
142
142
143
}
143
144
0 commit comments