
com.fitbur.testify.TestNeedContainers Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2015 Sharmarke Aden.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fitbur.testify;
import static com.fitbur.guava.common.base.Preconditions.checkState;
import com.fitbur.guava.common.collect.Lists;
import com.fitbur.testify.di.ServiceLocator;
import com.fitbur.testify.need.NeedContainer;
import com.fitbur.testify.need.NeedContainerProvider;
import com.fitbur.testify.need.NeedContext;
import com.fitbur.testify.need.NeedDescriptor;
import com.fitbur.testify.need.NeedInstance;
import com.fitbur.testify.need.NeedScope;
import java.lang.annotation.Annotation;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.UUID;
import static java.util.stream.Collectors.toCollection;
/**
* A class for managing container based needs.
*
* @author saden
*/
public class TestNeedContainers {
private final TestContext testContext;
private final String name;
private final NeedScope scope;
private Set needContexts;
public TestNeedContainers(TestContext testContext,
String name,
NeedScope scope) {
this.testContext = testContext;
this.name = name;
this.scope = scope;
}
public void init() {
needContexts = new HashSet<>();
ServiceLoader serviceLoader = ServiceLoader.load(NeedContainerProvider.class);
ArrayList providers = Lists.newArrayList(serviceLoader);
providers.parallelStream().forEach(provider -> {
testContext.getAnnotations(NeedContainer.class).parallelStream().filter(p -> p.scope() == scope).map(p -> {
NeedDescriptor descriptor = new TestNeedDescriptor(testContext, name);
Object configuration = provider.configuration(descriptor);
testContext.getConfigMethod(configuration.getClass())
.map(m -> m.getMethod())
.ifPresent(m -> {
AccessController.doPrivileged((PrivilegedAction
© 2015 - 2025 Weber Informatics LLC | Privacy Policy