Search in sources :

Example 6 with CacheManager

use of org.xwiki.cache.CacheManager in project xwiki-platform by xwiki.

the class ImagePluginTest method testCacheOfScaledAttachment.

@Test
public void testCacheOfScaledAttachment() throws Exception {
    XWikiContext xcontext = this.oldCore.getXWikiContext();
    XWikiAttachment attachment = Mockito.mock(XWikiAttachment.class);
    Mockito.when(attachment.getMimeType(xcontext)).thenReturn("image/png");
    InputStream attachmentInputStream = new ByteArrayInputStream(testPngImageContent);
    Mockito.when(attachment.getContentInputStream(xcontext)).thenReturn(attachmentInputStream);
    Mockito.when(attachment.clone()).thenReturn(attachment);
    XWikiAttachmentContent attachmentContent = Mockito.mock(XWikiAttachmentContent.class);
    Mockito.when(attachment.getAttachment_content()).thenReturn(attachmentContent);
    Mockito.when(attachmentContent.getContentInputStream()).thenReturn(attachmentInputStream);
    OutputStream attachmentOutputStream = Mockito.mock(OutputStream.class);
    Mockito.when(attachmentContent.getContentOutputStream()).thenReturn(attachmentOutputStream);
    CacheManager cacheManager = this.oldCore.getMocker().getInstance(CacheManager.class);
    Cache<Object> imageCache = Mockito.mock(Cache.class);
    Mockito.when(cacheManager.createNewLocalCache(ArgumentMatchers.any())).thenReturn(imageCache);
    XWikiServletRequest request = Mockito.mock(XWikiServletRequest.class);
    Mockito.when(request.getParameter("width")).thenReturn("30");
    Mockito.when(request.getParameter("height")).thenReturn("30");
    xcontext.setRequest(request);
    Image image = Mockito.mock(Image.class);
    Mockito.when(image.getWidth(null)).thenReturn(400);
    Mockito.when(image.getHeight(null)).thenReturn(300);
    Mockito.when(imageProcessor.readImage(attachmentInputStream)).thenReturn(image);
    RenderedImage renderedImage = Mockito.mock(RenderedImage.class);
    Mockito.when(imageProcessor.scaleImage(image, 30, 30)).thenReturn(renderedImage);
    XWikiAttachment scaled = plugin.downloadAttachment(attachment, xcontext);
    String cacheKey = "0;null;30;30;false;-1.0";
    Mockito.when(imageCache.get(cacheKey)).thenReturn(scaled);
    // Load again, this time from cache.
    Assert.assertSame(scaled, plugin.downloadAttachment(attachment, xcontext));
    Mockito.verify(imageProcessor, Mockito.times(1)).writeImage(renderedImage, "image/png", .5F, attachmentOutputStream);
    Mockito.verify(imageCache, Mockito.times(1)).set(cacheKey, attachment);
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Image(java.awt.Image) RenderedImage(java.awt.image.RenderedImage) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent) ByteArrayInputStream(java.io.ByteArrayInputStream) CacheManager(org.xwiki.cache.CacheManager) RenderedImage(java.awt.image.RenderedImage) Test(org.junit.Test)

Example 7 with CacheManager

use of org.xwiki.cache.CacheManager in project xwiki-platform by xwiki.

the class PageTest method setUpComponentsForPageTest.

/**
 * Set up components before Components declared in {@link org.xwiki.test.annotation.ComponentList} are handled.
 *
 * @throws Exception in case of errors
 */
@BeforeComponent
public void setUpComponentsForPageTest() throws Exception {
    mocker.registerMockComponent(JMXBeanRegistration.class);
    mocker.registerMockComponent(Environment.class);
    mocker.registerMockComponent(JobProgressManager.class);
    mocker.registerMockComponent(RenderingCache.class);
    mocker.registerMockComponent(EntityResourceActionLister.class);
    CacheManager cacheManager = mocker.registerMockComponent(CacheManager.class);
    when(cacheManager.createNewCache(any(CacheConfiguration.class))).thenReturn(mock(Cache.class));
}
Also used : CacheManager(org.xwiki.cache.CacheManager) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) RenderingCache(com.xpn.xwiki.internal.cache.rendering.RenderingCache) Cache(org.xwiki.cache.Cache) BeforeComponent(org.xwiki.test.annotation.BeforeComponent)

Example 8 with CacheManager

use of org.xwiki.cache.CacheManager in project xwiki-platform by xwiki.

the class FeedPlugin method initCache.

public void initCache(int iCapacity, XWikiContext context) throws XWikiException {
    try {
        CacheConfiguration configuration = new CacheConfiguration();
        configuration.setConfigurationId("xwiki.plugin.feedcache");
        LRUEvictionConfiguration lru = new LRUEvictionConfiguration();
        lru.setMaxEntries(iCapacity);
        lru.setMaxIdle(this.refreshPeriod);
        configuration.put(LRUEvictionConfiguration.CONFIGURATIONID, lru);
        CacheManager cacheManager = Utils.getComponent(CacheManager.class);
        this.feedCache = cacheManager.createNewLocalCache(configuration);
    } catch (CacheException e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_CACHE, XWikiException.ERROR_CACHE_INITIALIZING, "Failed to create cache");
    }
}
Also used : CacheException(org.xwiki.cache.CacheException) LRUEvictionConfiguration(org.xwiki.cache.eviction.LRUEvictionConfiguration) CacheManager(org.xwiki.cache.CacheManager) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) XWikiException(com.xpn.xwiki.XWikiException)

Example 9 with CacheManager

use of org.xwiki.cache.CacheManager in project xwiki-platform by xwiki.

the class DefaultAuthorizationManagerIntegrationTest method initializeMocks.

@BeforeComponent
public void initializeMocks() throws Exception {
    cache = new TestCache<Object>();
    final CacheManager cacheManager = componentManager.registerMockComponent(CacheManager.class);
    when(cacheManager.createNewCache(any(CacheConfiguration.class))).thenReturn(cache);
    xWikiBridge = componentManager.registerMockComponent(XWikiBridge.class);
    userBridge = componentManager.registerMockComponent(UserBridge.class);
    securityEntryReader = componentManager.registerMockComponent(SecurityEntryReader.class);
    securityCacheRulesInvalidator = componentManager.registerMockComponent(SecurityCacheRulesInvalidator.class);
}
Also used : UserBridge(org.xwiki.security.internal.UserBridge) CacheManager(org.xwiki.cache.CacheManager) XWikiBridge(org.xwiki.security.internal.XWikiBridge) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) SecurityCacheRulesInvalidator(org.xwiki.security.authorization.cache.SecurityCacheRulesInvalidator) BeforeComponent(org.xwiki.test.annotation.BeforeComponent)

Example 10 with CacheManager

use of org.xwiki.cache.CacheManager in project xwiki-platform by xwiki.

the class DefaultSecurityCacheTest method configure.

@Before
public void configure() throws Exception {
    if (cache == null) {
        cache = new TestCache<Object>();
        final CacheManager cacheManager = securityCacheMocker.getInstance(CacheManager.class);
        when(cacheManager.createNewCache(any(CacheConfiguration.class))).thenReturn(cache);
    }
    XWikiBridge xwikiBridge = securityReferenceFactoryMocker.getInstance(XWikiBridge.class);
    when(xwikiBridge.getMainWikiReference()).thenReturn(new WikiReference("xwiki"));
    this.factory = securityReferenceFactoryMocker.getComponentUnderTest();
    this.securityCache = securityCacheMocker.getComponentUnderTest();
    aMissingParentRef = factory.newEntityReference(new SpaceReference("space", new WikiReference("missing")));
    aMissingEntityRef = factory.newEntityReference(new DocumentReference("missingPage", xspaceRef.getOriginalSpaceReference()));
    aMissingUserRef = factory.newUserReference(new DocumentReference("missingUser", xXWikiSpace.getOriginalSpaceReference()));
    aMissingGroupRef = factory.newGroupReference(new DocumentReference("missingGroup", xXWikiSpace.getOriginalSpaceReference()));
    aMissingWikiRef = factory.newEntityReference(new WikiReference("missingWiki"));
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) CacheManager(org.xwiki.cache.CacheManager) XWikiBridge(org.xwiki.security.internal.XWikiBridge) WikiReference(org.xwiki.model.reference.WikiReference) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Aggregations

CacheManager (org.xwiki.cache.CacheManager)12 CacheConfiguration (org.xwiki.cache.config.CacheConfiguration)10 Before (org.junit.Before)6 Cache (org.xwiki.cache.Cache)6 DocumentReference (org.xwiki.model.reference.DocumentReference)4 XWikiContext (com.xpn.xwiki.XWikiContext)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 BaseObject (com.xpn.xwiki.objects.BaseObject)3 Test (org.junit.Test)3 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)3 ConverterManager (org.xwiki.properties.ConverterManager)3 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)3 XWiki (com.xpn.xwiki.XWiki)2 CacheFactory (org.xwiki.cache.CacheFactory)2 WikiReference (org.xwiki.model.reference.WikiReference)2 XWikiBridge (org.xwiki.security.internal.XWikiBridge)2 BeforeComponent (org.xwiki.test.annotation.BeforeComponent)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiAttachmentContent (com.xpn.xwiki.doc.XWikiAttachmentContent)1