Skip to content

Commit 6d8f335

Browse files
committed
Use Hamcrest assertions instead of JUnit in integrations/cdi/common-cdi/reference-counted-context - backport 2.x (helidon-io#1749)
1 parent 250742d commit 6d8f335

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

integrations/cdi/common-cdi/reference-counted-context/src/test/java/io/helidon/integrations/cdi/referencecountedcontext/TestReferenceCountedContext.java

+26-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
2+
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,9 +32,10 @@
3232
import org.junit.jupiter.api.BeforeEach;
3333
import org.junit.jupiter.api.Test;
3434

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;
3839

3940
@ApplicationScoped
4041
final class TestReferenceCountedContext {
@@ -53,7 +54,7 @@ private TestReferenceCountedContext() {
5354
private final void startContainer() {
5455
stopContainer();
5556
final SeContainerInitializer initializer = SeContainerInitializer.newInstance();
56-
assertNotNull(initializer);
57+
assertThat(initializer, notNullValue());
5758
initializer.disableDiscovery();
5859
initializer.addBeanClasses(this.getClass(), HousingBean.class, Gorp.class);
5960
initializer.addExtensions(ReferenceCountedExtension.class);
@@ -72,46 +73,46 @@ private final void onStartup(@Observes @Initialized(ApplicationScoped.class) fin
7273
final RequestContextController controller,
7374
final BeanManager beanManager,
7475
final Gorp gorpAsParameter) {
75-
assertNotNull(controller);
76-
assertNotNull(beanManager);
77-
assertNotNull(gorpAsParameter);
76+
assertThat(controller, notNullValue());
77+
assertThat(beanManager, notNullValue());
78+
assertThat(gorpAsParameter, notNullValue());
7879

7980
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));
8283
@SuppressWarnings("unchecked")
8384
final Bean<Gorp> gorpBean = (Bean<Gorp>)gorpBeans.iterator().next();
84-
assertNotNull(gorpBean);
85+
assertThat(gorpBean, notNullValue());
8586

8687
final ReferenceCountedContext context = ReferenceCountedContext.getInstanceFrom(beanManager);
87-
assertNotNull(context);
88-
assertTrue(context.isActive());
88+
assertThat(context, notNullValue());
89+
assertThat(context.isActive(), is(true));
8990

9091
// Gorp is @ReferenceCounted. It is proxied. Note that we do
9192
// not dereference the gorpAsParameter proxy yet, so no
9293
// underlying instance is created.
93-
assertEquals(0, context.getReferenceCount(gorpBean));
94+
assertThat(context.getReferenceCount(gorpBean), is(0));
9495

9596
Gorp gorp = null;
9697
try {
9798
controller.activate();
9899

99100
// this.bean is @RequestScoped. It houses an instance of
100101
// Gorp.
101-
assertNotNull(this.bean);
102-
assertEquals(0, context.getReferenceCount(gorpBean));
102+
assertThat(this.bean, notNullValue());
103+
assertThat(context.getReferenceCount(gorpBean), is(0));
103104

104105
// Here, the gorp acquired is a client proxy. No
105106
// contextual instance has been created yet.
106107
gorp = this.bean.getGorp();
107-
assertNotNull(gorp);
108-
assertEquals(0, context.getReferenceCount(gorpBean));
108+
assertThat(gorp, notNullValue());
109+
assertThat(context.getReferenceCount(gorpBean), is(0));
109110

110111
// This will cause the underlying instance to be created.
111112
// It will be the first Gorp instance created anywhere.
112113
// 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));
115116

116117
} finally {
117118

@@ -121,13 +122,13 @@ private final void onStartup(@Observes @Initialized(ApplicationScoped.class) fin
121122
// reference count of 0. This Gorp reference is therefore
122123
// destroyed.
123124
controller.deactivate();
124-
assertEquals(0, context.getReferenceCount(gorpBean));
125+
assertThat(context.getReferenceCount(gorpBean), is(0));
125126
}
126127

127128
// Now we kick the proxy. This will cause another instance to
128129
// 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));
131132

132133
// Next we refer to the Gorp instance that was referred to by
133134
// the request-scoped HousingBean. If Gorp had been
@@ -136,8 +137,8 @@ private final void onStartup(@Observes @Initialized(ApplicationScoped.class) fin
136137
// because the gorpParameter dereference above caused the
137138
// reference count to jump, THIS Gorp instance will now be the
138139
// 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));
141142

142143
}
143144

0 commit comments

Comments
 (0)