Skip to content

Refactor rename BeanContext to BeanScope (to better align with ApplicationScope and RequestScope) #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Constants {
static final String META_INF_FACTORY = "META-INF/services/io.avaje.inject.spi.BeanContextFactory";

static final String REQUESTSCOPE = "io.avaje.inject.RequestScope";
static final String BEANCONTEXT = "io.avaje.inject.BeanContext";
static final String BEANCONTEXT = "io.avaje.inject.BeanScope";
static final String CONTEXTMODULE = "io.avaje.inject.ContextModule";

static final String GENERATED = "io.avaje.inject.spi.Generated";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SimpleFactoryWriter {

private static final String CODE_COMMENT_FACTORY =
"/**\n" +
" * Generated source - Creates the BeanContext for the %s module.\n" +
" * Generated source - Creates the BeanScope for the %s module.\n" +
" * \n" +
" * With JPMS Java module system this generated class should be explicitly\n" +
" * registered in module-info via a <code>provides</code> clause like:\n" +
Expand All @@ -33,16 +33,12 @@ class SimpleFactoryWriter {

private static final String CODE_COMMENT_CREATE_CONTEXT =
" /**\n" +
" * Create and return the BeanContext.\n" +
" * Create the beans.\n" +
" * <p>\n" +
" * Creates all the beans in order based on constuctor dependencies.\n" +
" * The beans are registered into the builder along with callbacks for\n" +
" * field injection, method injection and lifecycle support.\n" +
" * <p>\n" +
" * Ultimately the builder returns the BeanContext containing the beans.\n" +
" *\n" +
" * @param parent The parent context for multi-module wiring\n" +
" * @return The BeanContext containing the beans\n" +
" */";

private final MetaDataOrdering ordering;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

import static org.assertj.core.api.Assertions.assertThat;

public class BeanContextBuilderTest {
public class BeanScopeBuilderTest {

@Test
public void noDepends() {

DBeanContextBuilder.FactoryOrder factoryOrder = new DBeanContextBuilder.FactoryOrder(Collections.emptySet(), true, true);
DBeanScopeBuilder.FactoryOrder factoryOrder = new DBeanScopeBuilder.FactoryOrder(Collections.emptySet(), true, true);
factoryOrder.add(bc("1", null, null));
factoryOrder.add(bc("2", null, null));
factoryOrder.add(bc("3", null, null));
Expand All @@ -27,7 +27,7 @@ public void noDepends() {
@Test
public void name_depends() {

DBeanContextBuilder.FactoryOrder factoryOrder = new DBeanContextBuilder.FactoryOrder(Collections.emptySet(), true, true);
DBeanScopeBuilder.FactoryOrder factoryOrder = new DBeanScopeBuilder.FactoryOrder(Collections.emptySet(), true, true);
factoryOrder.add(bc("two", null, "one"));
factoryOrder.add(bc("one", null, null));
factoryOrder.orderFactories();
Expand All @@ -38,7 +38,7 @@ public void name_depends() {
@Test
public void name_depends4() {

DBeanContextBuilder.FactoryOrder factoryOrder = new DBeanContextBuilder.FactoryOrder(Collections.emptySet(), true, true);
DBeanScopeBuilder.FactoryOrder factoryOrder = new DBeanScopeBuilder.FactoryOrder(Collections.emptySet(), true, true);
factoryOrder.add(bc("1", null, "3"));
factoryOrder.add(bc("2", null, "4"));
factoryOrder.add(bc("3", null, "4"));
Expand All @@ -52,7 +52,7 @@ public void name_depends4() {
@Test
public void nameFeature_depends() {

DBeanContextBuilder.FactoryOrder factoryOrder = new DBeanContextBuilder.FactoryOrder(Collections.emptySet(), true, true);
DBeanScopeBuilder.FactoryOrder factoryOrder = new DBeanScopeBuilder.FactoryOrder(Collections.emptySet(), true, true);
factoryOrder.add(bc("1", "a", "3"));
factoryOrder.add(bc("2", null, "4,a"));
factoryOrder.add(bc("3", null, "4"));
Expand All @@ -66,7 +66,7 @@ public void nameFeature_depends() {
@Test
public void feature_depends() {

DBeanContextBuilder.FactoryOrder factoryOrder = new DBeanContextBuilder.FactoryOrder(Collections.emptySet(), true, true);
DBeanScopeBuilder.FactoryOrder factoryOrder = new DBeanScopeBuilder.FactoryOrder(Collections.emptySet(), true, true);
factoryOrder.add(bc("two", null, "myfeature"));
factoryOrder.add(bc("one", "myfeature", null));
factoryOrder.orderFactories();
Expand All @@ -77,7 +77,7 @@ public void feature_depends() {
@Test
public void feature_depends2() {

DBeanContextBuilder.FactoryOrder factoryOrder = new DBeanContextBuilder.FactoryOrder(Collections.emptySet(), true, true);
DBeanScopeBuilder.FactoryOrder factoryOrder = new DBeanScopeBuilder.FactoryOrder(Collections.emptySet(), true, true);
factoryOrder.add(bc("two", null, "myfeature"));
factoryOrder.add(bc("one", "myfeature", null));
factoryOrder.add(bc("three", "myfeature", null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void get_when_onePrimary() {
entry.add(DContextEntryBean.of("N", null, BeanEntry.NORMAL));
entry.add(DContextEntryBean.of("S", null, BeanEntry.SECONDARY));

assertEquals(entry.candidate(null).getBean(), "P");
assertEquals(entry.get(null), "P");
}

@Test
Expand All @@ -27,7 +27,7 @@ public void get_when_twoPrimary() {
entry.add(DContextEntryBean.of("N", null, BeanEntry.NORMAL));
entry.add(DContextEntryBean.of("S", null, BeanEntry.PRIMARY));

entry.candidate(null);
entry.get(null);
});
}

Expand All @@ -38,7 +38,7 @@ public void get_when_oneNormal() {
entry.add(DContextEntryBean.of("N", null, BeanEntry.NORMAL));
entry.add(DContextEntryBean.of("S", null, BeanEntry.SECONDARY));

assertEquals(entry.candidate(null).getBean(), "N");
assertEquals(entry.get(null), "N");
}


Expand All @@ -50,7 +50,7 @@ public void get_when_oneNormal2() {
entry.add(DContextEntryBean.of("S1", null, BeanEntry.SECONDARY));
entry.add(DContextEntryBean.of("S2", null, BeanEntry.SECONDARY));

assertEquals(entry.candidate(null).getBean(), "N");
assertEquals(entry.get(null), "N");
}

@Test
Expand All @@ -60,7 +60,7 @@ public void get_when_multiSecondaryOnly() {
entry.add(DContextEntryBean.of("S1", null, BeanEntry.SECONDARY));
entry.add(DContextEntryBean.of("S2", null, BeanEntry.SECONDARY));

entry.candidate(null);
entry.get(null);
});
}

Expand All @@ -72,7 +72,7 @@ public void get_when_multiSecondary_butNamed() {
entry.add(DContextEntryBean.of("S1", "a", BeanEntry.SECONDARY));
entry.add(DContextEntryBean.of("S2", "b", BeanEntry.SECONDARY));

assertEquals(entry.candidate("b").getBean(), "S2");
assertEquals(entry.get("b"), "S2");
}

@Test
Expand All @@ -82,7 +82,7 @@ public void get_when_multiSecondary_butNamed2() {
entry.add(DContextEntryBean.of("S1", null, BeanEntry.SECONDARY));
entry.add(DContextEntryBean.of("S2", "b", BeanEntry.SECONDARY));

assertEquals(entry.candidate("b").getBean(), "S2");
assertEquals(entry.get("b"), "S2");
}

@Test
Expand All @@ -92,6 +92,6 @@ public void get_when_secondary_butNamed() {
entry.add(DContextEntryBean.of("S1", null, BeanEntry.PRIMARY));
entry.add(DContextEntryBean.of("S2", "b", BeanEntry.SECONDARY));

assertEquals(entry.candidate("b").getBean(), "S2");
assertEquals(entry.get("b"), "S2");
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package org.example.coffee;

import io.avaje.inject.BeanContext;
import io.avaje.inject.BeanScope;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.verify;

public class BeanContextBuilderAddTest {
public class BeanScopeBuilderAddTest {

@Test
public void withModules_exludingThisOne() {
assertThrows(IllegalStateException.class, () -> {
TDPump testDoublePump = new TDPump();

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withBeans(testDoublePump)
// our module is "org.example.coffee"
// so this effectively includes no modules
Expand All @@ -35,7 +35,7 @@ public void withModules_includeThisOne() {

TDPump testDoublePump = new TDPump();

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withBeans(testDoublePump)
.withModules("org.example")
.build()) {
Expand All @@ -53,7 +53,7 @@ public void withBean_expect_testDoublePumpUsed() {

TDPump testDoublePump = new TDPump();

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withBeans(testDoublePump)
.build()) {

Expand All @@ -70,7 +70,7 @@ public void withMockitoMock_expect_mockUsed() {

Pump mock = Mockito.mock(Pump.class);

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withBean(Pump.class, mock)
.build()) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.example.coffee;

import io.avaje.inject.BeanContext;
import io.avaje.inject.BeanScope;
import org.example.coffee.factory.SomeImpl;
import org.example.coffee.factory.SomeImplBean;
import org.example.coffee.factory.Unused;
Expand All @@ -18,15 +18,15 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

public class BeanContext_Builder_mockitoSpyTest {
public class BeanScope_Builder_mockitoSpyTest {

@Test
public void withBeans_asMocks() {

Pump pump = mock(Pump.class);
Grinder grinder = mock(Grinder.class);

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withBeans(pump, grinder)
.build()) {

Expand All @@ -47,7 +47,7 @@ public void withBeans_asMocks() {
@Test
public void withMockitoSpy_noSetup_expect_spyUsed() {

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withSpy(Pump.class)
.build()) {

Expand All @@ -63,7 +63,7 @@ public void withMockitoSpy_noSetup_expect_spyUsed() {
@Test
public void withMockitoSpy_postLoadSetup_expect_spyUsed() {

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withSpy(Pump.class)
.withSpy(Grinder.class)
.build()) {
Expand All @@ -86,7 +86,7 @@ public void withMockitoSpy_postLoadSetup_expect_spyUsed() {
@Test
public void withMockitoSpy_expect_spyUsed() {

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withSpy(Pump.class, pump -> {
// setup the spy
doNothing().when(pump).pumpWater();
Expand All @@ -109,7 +109,7 @@ public void withMockitoSpy_expect_spyUsed() {
@Test
public void withMockitoSpy_whenPrimary_expect_spyUsed() {

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withSpy(PEmailer.class) // has a primary
.build()) {

Expand All @@ -124,7 +124,7 @@ public void withMockitoSpy_whenPrimary_expect_spyUsed() {
@Test
public void withMockitoSpy_whenOnlySecondary_expect_spyUsed() {

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withSpy(Widget.class) // only secondary
.build()) {

Expand All @@ -144,7 +144,7 @@ public void withMockitoSpy_whenOnlySecondary_expect_spyUsed() {
@Test
public void withMockitoSpy_whenSecondary_expect_spyUsed() {

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withSpy(Something.class) // has a secondary and a normal
.build()) {

Expand All @@ -168,7 +168,7 @@ public void withMockitoMock_expect_mockUsed() {

AtomicReference<Grinder> mock = new AtomicReference<>();

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withMock(Pump.class)
.withMock(Grinder.class, grinder -> {
// setup the mock
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.coffee;

import io.avaje.inject.ApplicationScope;
import io.avaje.inject.BeanContext;
import io.avaje.inject.BeanScope;
import io.avaje.inject.SystemContext;
import org.example.coffee.core.DuperPump;
import org.junit.jupiter.api.Test;
Expand All @@ -26,7 +26,7 @@ public void makeIt_via_SystemContext() {
@Test
public void makeIt_via_BootContext_withNoShutdownHook() {

try (BeanContext context = BeanContext.newBuilder()
try (BeanScope context = BeanScope.newBuilder()
.withNoShutdownHook()
.build()) {

Expand All @@ -38,7 +38,7 @@ public void makeIt_via_BootContext_withNoShutdownHook() {
@Test
public void makeIt_via_BootContext() {

try (BeanContext context = BeanContext.newBuilder().build()) {
try (BeanScope context = BeanScope.newBuilder().build()) {
String makeIt = context.getBean(CoffeeMaker.class).makeIt();
assertThat(makeIt).isEqualTo("done");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.coffee;

import io.avaje.inject.BeanContext;
import io.avaje.inject.BeanContextBuilder;
import io.avaje.inject.BeanScope;
import io.avaje.inject.BeanScopeBuilder;

import java.util.List;

Expand All @@ -15,8 +15,8 @@ class ExtensionExample {
this.withSpies = withSpies;
}

BeanContext build() {
BeanContextBuilder bootContext = BeanContext.newBuilder();
BeanScope build() {
BeanScopeBuilder bootContext = BeanScope.newBuilder();
withMocks.forEach(bootContext::withMock);
withSpies.forEach(bootContext::withSpy);
return bootContext.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.coffee;

import io.avaje.inject.BeanContext;
import io.avaje.inject.BeanContextBuilder;
import io.avaje.inject.BeanScope;
import io.avaje.inject.BeanScopeBuilder;
import org.example.coffee.qualifier.SomeStore;
import org.example.coffee.secondary.SEmailer;
import org.example.coffee.secondary.Widget;
Expand All @@ -16,12 +16,12 @@ public class ExtensionExampleTest {
public void checkForCompilerWarningsOnly_notATestThatRuns() {

ExtensionExample extensionExample = new ExtensionExample(asList(Widget.class, SEmailer.class), asList(SomeStore.class));
BeanContext context = extensionExample.build();
BeanScope context = extensionExample.build();

Class cls0 = Widget.class;
Class<?> cls1 = SEmailer.class;

BeanContextBuilder bootContext = BeanContext.newBuilder()
BeanScopeBuilder bootContext = BeanScope.newBuilder()
.withSpy(cls0)
.withSpy(cls1)
.withMock(cls0)
Expand Down
4 changes: 2 additions & 2 deletions inject-test/src/test/java/org/example/coffee/FactoryTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.example.coffee;

import io.avaje.inject.BeanContext;
import io.avaje.inject.BeanScope;
import org.example.coffee.factory.BFact;
import org.junit.jupiter.api.Test;

Expand All @@ -11,7 +11,7 @@ public class FactoryTest {
@Test
public void test() {

try (BeanContext context = BeanContext.newBuilder().build()) {
try (BeanScope context = BeanScope.newBuilder().build()) {
BFact bean = context.getBean(BFact.class);
String b = bean.b();
assertThat(b).isNotNull();
Expand Down
Loading