Search in sources :

Example 16 with Provider

use of javax.inject.Provider in project xwiki-platform by xwiki.

the class AbstractBridgedXWikiComponentTestCase method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    // Statically store the component manager in {@link Utils} to be able to access it without
    // the context.
    Utils.setComponentManager(getComponentManager());
    this.context = new XWikiContext();
    this.context.setWikiId("xwiki");
    this.context.setMainXWiki("xwiki");
    // Make sure response.encodeURL() calls don't fail
    Mock xwikiResponse = mock(XWikiResponse.class);
    xwikiResponse.stubs().method("setLocale");
    xwikiResponse.stubs().method("encodeURL").will(new CustomStub("Implements XWikiResponse.encodeURL") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            return invocation.parameterValues.get(0);
        }
    });
    this.context.setResponse((XWikiResponse) xwikiResponse.proxy());
    // We need to initialize the Component Manager so that the components can be looked up
    getContext().put(ComponentManager.class.getName(), getComponentManager());
    // Bridge with old XWiki Context, required for old code.
    Execution execution = getComponentManager().getInstance(Execution.class);
    this.context.declareInExecutionContext(execution.getContext());
    XWikiStubContextProvider stubContextProvider = getComponentManager().getInstance(XWikiStubContextProvider.class);
    stubContextProvider.initialize(this.context);
    // Bridge with XWiki Context Provider, required by newer code.
    Mock mockContextProvider = mock(Provider.class);
    mockContextProvider.stubs().method("get").will(returnValue(this.context));
    DefaultComponentDescriptor<Provider<XWikiContext>> contextProviderDescriptor = new DefaultComponentDescriptor<Provider<XWikiContext>>();
    contextProviderDescriptor.setRoleType(new DefaultParameterizedType(null, Provider.class, XWikiContext.class));
    contextProviderDescriptor.setRoleHint("default");
    getComponentManager().registerComponent(contextProviderDescriptor, (Provider<XWikiContext>) mockContextProvider.proxy());
    // Since the oldcore module draws the Servlet Environment in its dependencies we need to ensure it's set up
    // correctly with a Servlet Context.
    ServletEnvironment environment = getComponentManager().getInstance(Environment.class);
    Mock mockServletContext = mock(ServletContext.class);
    environment.setServletContext((ServletContext) mockServletContext.proxy());
    mockServletContext.stubs().method("getResourceAsStream").will(returnValue(null));
    mockServletContext.stubs().method("getResource").will(returnValue(null));
    mockServletContext.stubs().method("getAttribute").with(eq("javax.servlet.context.tempdir")).will(returnValue(new File(System.getProperty("java.io.tmpdir"))));
    File testDirectory = new File("target/test-" + new Date().getTime());
    this.temporaryDirectory = new File(testDirectory, "temporary-dir");
    this.permanentDirectory = new File(testDirectory, "permanent-dir");
    environment.setTemporaryDirectory(this.temporaryDirectory);
    environment.setPermanentDirectory(this.permanentDirectory);
    Mock mockCoreConfiguration = registerMockComponent(CoreConfiguration.class);
    mockCoreConfiguration.stubs().method("getDefaultDocumentSyntax").will(returnValue(Syntax.XWIKI_1_0));
    this.mockWikiDescriptorManager = registerMockComponent(WikiDescriptorManager.class);
    this.mockWikiDescriptorManager.stubs().method("getCurrentWikiId").will(new CustomStub("Implements WikiDescriptorManager.getCurrentWikiId") {

        @Override
        public String invoke(Invocation invocation) throws Throwable {
            return getContext().getWikiId();
        }
    });
    this.mockWikiDescriptorManager.stubs().method("getMainWikiId").will(new CustomStub("Implements WikiDescriptorManager.getMainWikiId") {

        @Override
        public String invoke(Invocation invocation) throws Throwable {
            return getContext().getMainXWiki();
        }
    });
    // In order not to create a cyclic dependency we have the platform-rendering-xwiki module (which contains
    // XWikiWikiModel requires for oldcore testing) not depend on platform-rendering-configuration-default. As a
    // consequence we need to provide a mock ExtendedRenderingConfiguration component as otherwise injecting
    // WikiModel would fail (since XWikiWikiModel depends on ExtendedRenderingConfiguration).
    registerMockComponent(ExtendedRenderingConfiguration.class);
}
Also used : XWikiStubContextProvider(com.xpn.xwiki.util.XWikiStubContextProvider) Invocation(org.jmock.core.Invocation) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) XWikiContext(com.xpn.xwiki.XWikiContext) Mock(org.jmock.Mock) Date(java.util.Date) Provider(javax.inject.Provider) XWikiStubContextProvider(com.xpn.xwiki.util.XWikiStubContextProvider) Execution(org.xwiki.context.Execution) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) CustomStub(org.jmock.core.stub.CustomStub) ComponentManager(org.xwiki.component.manager.ComponentManager) ServletEnvironment(org.xwiki.environment.internal.ServletEnvironment) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) File(java.io.File)

Example 17 with Provider

use of javax.inject.Provider in project xwiki-platform by xwiki.

the class SolrDocumentIteratorTest method configure.

@Before
public void configure() throws Exception {
    solr = mock(SolrInstance.class);
    Provider<SolrInstance> solrInstanceProvider = mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, SolrInstance.class));
    when(solrInstanceProvider.get()).thenReturn(solr);
    this.solrDocumentReferenceResolver = this.mocker.getInstance(new DefaultParameterizedType(null, DocumentReferenceResolver.class, SolrDocument.class));
}
Also used : DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) SolrInstance(org.xwiki.search.solr.internal.api.SolrInstance) Provider(javax.inject.Provider) Before(org.junit.Before)

Example 18 with Provider

use of javax.inject.Provider in project minijax by minijax.

the class MinijaxApplication method getParamProviders.

/**
 * Returns the param providers for a resource method.
 *
 * This is very similar to the logic used in building param providers for a normal
 * <code>@Inject</code> constructor, with one major difference.
 *
 * A resource method is allowed one special "entity" parameter representing the content body.
 * This entity parameter is handled by a <code>EntityProvider</code>.
 *
 * @param method The resource method.
 * @return The array of resource method param providers.
 */
private Provider<?>[] getParamProviders(final Method method) {
    final Class<?>[] paramClasses = method.getParameterTypes();
    final Type[] paramTypes = method.getGenericParameterTypes();
    final Annotation[][] annotations = method.getParameterAnnotations();
    final Provider<?>[] result = new Provider<?>[paramTypes.length];
    final Consumes consumes = method.getAnnotation(Consumes.class);
    final List<MediaType> consumesTypes = MediaTypeUtils.parseMediaTypes(consumes);
    boolean consumed = false;
    for (int i = 0; i < paramTypes.length; i++) {
        if (annotations[i].length == 0 && !consumed) {
            result[i] = new EntityProvider<>(paramClasses[i], paramTypes[i], annotations[i], consumesTypes);
            consumed = true;
        } else {
            result[i] = getInjector().getProvider(paramClasses[i], annotations[i]);
        }
    }
    return result;
}
Also used : Provider(javax.inject.Provider) EntityProvider(org.minijax.cdi.EntityProvider) MediaType(javax.ws.rs.core.MediaType) RuntimeType(javax.ws.rs.RuntimeType) Type(java.lang.reflect.Type) Consumes(javax.ws.rs.Consumes) MediaType(javax.ws.rs.core.MediaType)

Example 19 with Provider

use of javax.inject.Provider in project OA4MP by ncsa.

the class ClientLoader method createInstance.

@Override
public T createInstance() {
    Provider<TokenForge> tokenForgeProvider = new Provider<TokenForge>() {

        @Override
        public TokenForge get() {
            return new OAuthTokenForge(getId());
        }
    };
    Provider<Client> clientProvider = new Provider<Client>() {

        @Override
        public Client get() {
            OAClient c = new OAClient(BasicIdentifier.newID(getId()));
            c.setSignatureMethod(OAuthConstants.RSA_SHA1);
            c.setCreationTS(new Date());
            return c;
        }
    };
    // sets constants specific to this protocol.
    HashMap<String, String> constants = new HashMap<String, String>();
    constants.put(CALLBACK_URI_KEY, OAuthConstants.OAUTH_CALLBACK);
    constants.put(ClientEnvironment.FORM_ENCODING, "UTF-8");
    constants.put(ClientEnvironment.TOKEN, OAuth.OAUTH_TOKEN);
    constants.put(ClientEnvironment.VERIFIER, OAuth.OAUTH_VERIFIER);
    return createInstance(tokenForgeProvider, clientProvider, constants);
}
Also used : OAuthTokenForge(edu.uiuc.ncsa.security.oauth_1_0a.OAuthTokenForge) TokenForge(edu.uiuc.ncsa.security.delegation.token.TokenForge) OAClient(edu.uiuc.ncsa.security.oauth_1_0a.client.OAClient) HashMap(java.util.HashMap) Client(edu.uiuc.ncsa.security.delegation.storage.Client) OAClient(edu.uiuc.ncsa.security.oauth_1_0a.client.OAClient) OAuthTokenForge(edu.uiuc.ncsa.security.oauth_1_0a.OAuthTokenForge) Date(java.util.Date) Provider(javax.inject.Provider) AssetProvider(edu.uiuc.ncsa.myproxy.oa4mp.client.storage.AssetProvider) DelegationServiceImplProvider(edu.uiuc.ncsa.security.oauth_1_0a.client.DelegationServiceImplProvider) OA4MPServiceProvider(edu.uiuc.ncsa.myproxy.oa4mp.client.OA4MPServiceProvider)

Example 20 with Provider

use of javax.inject.Provider in project actframework by actframework.

the class JsonParamValueLoader method findDefValProvider.

private static Provider findDefValProvider(BeanSpec beanSpec, DependencyInjector<?> injector) {
    final Class c = beanSpec.rawType();
    final StringValueResolverManager resolver = App.instance().resolverManager();
    if (c.isPrimitive()) {
        return new Provider() {

            @Override
            public Object get() {
                return resolver.resolve(null, c);
            }
        };
    } else if (Collection.class.isAssignableFrom(c) || Map.class.isAssignableFrom(c)) {
        return injector.getProvider(c);
    } else if (c.isArray()) {
        return new Provider() {

            @Override
            public Object get() {
                return Array.newInstance(c.getComponentType(), 0);
            }
        };
    } else {
        return NULL_PROVIDER;
    }
}
Also used : StringValueResolverManager(act.app.data.StringValueResolverManager) Provider(javax.inject.Provider)

Aggregations

Provider (javax.inject.Provider)53 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)23 Test (org.junit.Test)15 ComponentManager (org.xwiki.component.manager.ComponentManager)15 Before (org.junit.Before)11 HashMap (java.util.HashMap)9 File (java.io.File)7 FilesystemExportContext (org.xwiki.url.filesystem.FilesystemExportContext)7 ArrayList (java.util.ArrayList)6 XWikiContext (com.xpn.xwiki.XWikiContext)5 Map (java.util.Map)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 Maps (com.google.common.collect.Maps)4 Type (java.lang.reflect.Type)4 Query (org.graylog.plugins.views.search.Query)4 QueryResult (org.graylog.plugins.views.search.QueryResult)4 SearchJob (org.graylog.plugins.views.search.SearchJob)4 SearchType (org.graylog.plugins.views.search.SearchType)4 IndexLookup (org.graylog.plugins.views.search.elasticsearch.IndexLookup)4 QueryStringDecorators (org.graylog.plugins.views.search.elasticsearch.QueryStringDecorators)4