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);
}
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));
}
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;
}
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);
}
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;
}
}
Aggregations