Skip to content
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

BeanScopeBuilder.profiles() for explicit profiles #380

Merged
merged 1 commit into from
Jul 23, 2023
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 @@ -31,9 +31,4 @@ public boolean equalTo(String property, String value) {
public boolean notEqualTo(String property, String value) {
return !value.equals(Config.getNullable(property));
}

@Override
public void set(String property, String value) {
Config.setProperty(property, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private Scope buildSet(BeanScope parent, Object testInstance) {
.orElse("");

if (!profiles.isBlank()) {
builder.propertyPlugin().set("avaje.profiles", profiles);
builder.profiles(profiles);
}
// register mocks and spies local to this test
reader.build(builder, testInstance);
Expand Down
7 changes: 7 additions & 0 deletions inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ public interface BeanScopeBuilder {
*/
<D> BeanScopeBuilder bean(Type type, D bean);

/**
* Set the explicit profiles to use when building the scope.
*
* <p>If profiles are not set explicitly here they are read from the properties plugin.
*/
BeanScopeBuilder profiles(String profiles);

/**
* Add a supplied bean provider that acts as a default fallback for a dependency.
* <p>
Expand Down
9 changes: 8 additions & 1 deletion inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class DBeanScopeBuilder implements BeanScopeBuilder.ForTesting {
private boolean shutdownHook;
private ClassLoader classLoader;
private PropertyRequiresPlugin propertyRequiresPlugin;
private String profiles;

/**
* Create a BeanScopeBuilder to ultimately load and return a new BeanScope.
Expand Down Expand Up @@ -103,6 +104,12 @@ public <D> BeanScopeBuilder bean(@Nullable String name, Type type, D bean) {
return this;
}

@Override
public BeanScopeBuilder profiles(String profiles) {
this.profiles = profiles;
return this;
}

@Override
public <D> BeanScopeBuilder provideDefault(@Nullable String name, Type type, Supplier<D> supplier) {
final Provider<D> provider = supplier::get;
Expand Down Expand Up @@ -241,7 +248,7 @@ public BeanScope build() {
final var level = propertyRequiresPlugin.contains("printModules") ? INFO : DEBUG;
log.log(level, "building with modules {0}", moduleNames);

final Builder builder = Builder.newBuilder(propertyRequiresPlugin, suppliedBeans, enrichBeans, parent, parentOverride);
final Builder builder = Builder.newBuilder(profiles, propertyRequiresPlugin, suppliedBeans, enrichBeans, parent, parentOverride);
for (final Module factory : factoryOrder.factories()) {
factory.build(builder);
}
Expand Down
9 changes: 3 additions & 6 deletions inject/src/main/java/io/avaje/inject/DConfigProps.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import java.util.Optional;

/** Avaje-Config based implementation of PropertyRequiresPlugin. */
/**
* Avaje-Config based implementation of PropertyRequiresPlugin.
*/
final class DConfigProps implements PropertyRequiresPlugin {

@Override
Expand All @@ -22,9 +24,4 @@ public boolean contains(String property) {
public boolean equalTo(String property, String value) {
return value.equals(Config.getNullable(property));
}

@Override
public void set(String property, String value) {
Config.setProperty(property, value);
}
}
5 changes: 0 additions & 5 deletions inject/src/main/java/io/avaje/inject/DSystemProps.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,4 @@ public boolean contains(String property) {
public boolean equalTo(String property, String value) {
return value.equals(System.getProperty(property)) || value.equals(System.getenv(property));
}

@Override
public void set(String property, String value) {
System.setProperty(property, value);
}
}
8 changes: 5 additions & 3 deletions inject/src/main/java/io/avaje/inject/spi/Builder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.inject.spi;

import io.avaje.inject.BeanScope;
import io.avaje.lang.Nullable;
import jakarta.inject.Provider;

import java.lang.reflect.Type;
Expand All @@ -18,18 +19,19 @@ public interface Builder {
/**
* Create the root level Builder.
*
* @param profiles Explicit profiles used
* @param suppliedBeans The list of beans (typically test doubles) supplied when building the context.
* @param enrichBeans The list of classes we want to have with mockito spy enhancement
* @param parent The parent BeanScope
* @param parentOverride When false do not add beans that already exist on the parent
*/
@SuppressWarnings("rawtypes")
static Builder newBuilder(PropertyRequiresPlugin plugin, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans, BeanScope parent, boolean parentOverride) {
static Builder newBuilder(@Nullable String profiles, PropertyRequiresPlugin plugin, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans, BeanScope parent, boolean parentOverride) {
if (suppliedBeans.isEmpty() && enrichBeans.isEmpty()) {
// simple case, no mocks or spies
return new DBuilder(plugin, parent, parentOverride);
return new DBuilder(profiles, plugin, parent, parentOverride);
}
return new DBuilderExtn(plugin, parent, parentOverride, suppliedBeans, enrichBeans);
return new DBuilderExtn(profiles, plugin, parent, parentOverride, suppliedBeans, enrichBeans);
}

/**
Expand Down
25 changes: 18 additions & 7 deletions inject/src/main/java/io/avaje/inject/spi/DBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import io.avaje.inject.BeanEntry;
import io.avaje.inject.BeanScope;
import io.avaje.lang.Nullable;
import jakarta.inject.Provider;

import static java.util.stream.Collectors.toSet;

import java.lang.reflect.Type;
import java.util.*;
import java.util.function.Consumer;

import static io.avaje.inject.spi.DBeanScope.combine;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.toSet;

class DBuilder implements Builder {

Expand All @@ -37,14 +38,24 @@ class DBuilder implements Builder {

private DBeanScopeProxy beanScopeProxy;

DBuilder(PropertyRequiresPlugin propertyRequires, BeanScope parent, boolean parentOverride) {
DBuilder(@Nullable String profiles, PropertyRequiresPlugin propertyRequires, BeanScope parent, boolean parentOverride) {
this.propertyRequires = propertyRequires;
this.parent = parent;
this.parentOverride = parentOverride;
this.profiles =
propertyRequires.get("avaje.profiles").map(p -> p.split(",")).stream()
.flatMap(Arrays::stream)
.collect(toSet());
this.profiles = initProfiles(profiles, propertyRequires);
}

private Set<String> initProfiles(@Nullable String profiles, PropertyRequiresPlugin properties) {
if (profiles != null) {
return toProfiles(profiles);
}
return properties.get("avaje.profiles")
.map(this::toProfiles)
.orElse(emptySet());
}

private Set<String> toProfiles(String profiles) {
return Arrays.stream(profiles.split(",")).collect(toSet());
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions inject/src/main/java/io/avaje/inject/spi/DBuilderExtn.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.avaje.inject.BeanEntry;
import io.avaje.inject.BeanScope;
import io.avaje.lang.Nullable;

import java.lang.reflect.Type;
import java.util.HashMap;
Expand All @@ -19,8 +20,8 @@ final class DBuilderExtn extends DBuilder {
private final boolean hasSuppliedBeans;

@SuppressWarnings("rawtypes")
DBuilderExtn(PropertyRequiresPlugin plugin, BeanScope parent, boolean parentOverride, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans) {
super(plugin, parent, parentOverride);
DBuilderExtn(@Nullable String profiles, PropertyRequiresPlugin plugin, BeanScope parent, boolean parentOverride, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans) {
super(profiles, plugin, parent, parentOverride);
this.hasSuppliedBeans = (suppliedBeans != null && !suppliedBeans.isEmpty());
if (hasSuppliedBeans) {
beanMap.add(suppliedBeans);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,4 @@ default boolean notEqualTo(String property, String value) {
return !equalTo(property, value);
}

/** Set a property for wiring. */
default void set(String property, String value) {

throw new UnsupportedOperationException("Not Implemented");
}
}