@Test
  public void getVelocityEngineWhenNoVelocityEngineInCache() throws Exception {
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    when(execution.getContext()).thenReturn(executionContext);

    XWikiContext xwikiContext = mock(XWikiContext.class);
    Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
    when(xcontextProvider.get()).thenReturn(xwikiContext);
    com.xpn.xwiki.XWiki xwiki = mock(com.xpn.xwiki.XWiki.class);
    when(xwikiContext.getWiki()).thenReturn(xwiki);
    when(xwiki.getSkin(any(XWikiContext.class))).thenReturn("testskin");

    VelocityFactory velocityFactory = this.mocker.getInstance(VelocityFactory.class);
    when(velocityFactory.hasVelocityEngine("default")).thenReturn(false);

    VelocityConfiguration velocityConfiguration =
        this.mocker.getInstance(VelocityConfiguration.class);
    when(velocityConfiguration.getProperties()).thenReturn(new Properties());

    VelocityEngine velocityEngine = mock(VelocityEngine.class);
    when(velocityFactory.createVelocityEngine(eq("default"), any(Properties.class)))
        .thenReturn(velocityEngine);

    Assert.assertSame(velocityEngine, this.mocker.getComponentUnderTest().getVelocityEngine());
  }
  /**
   * Tests that the Execution Context and the XWiki Context share the same reference of the Velocity
   * Context after a call to {@link VelocityManager#getVelocityContext()}. There is old code that
   * accesses the Velocity Context from the XWiki Context.
   */
  @Test
  public void getVelocityContextUpdatesXContext() throws Exception {
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    when(execution.getContext()).thenReturn(executionContext);

    ScriptContextManager scriptContextManager = this.mocker.getInstance(ScriptContextManager.class);
    ScriptContext scriptContext = new SimpleScriptContext();
    when(scriptContextManager.getScriptContext()).thenReturn(scriptContext);

    VelocityContext velocityContext = new VelocityContext();
    executionContext
        .newProperty("velocityContext")
        .initial(velocityContext)
        .inherited()
        .cloneValue()
        .declare();

    XWikiContext mockContext = mock(XWikiContext.class);
    Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
    when(xcontextProvider.get()).thenReturn(mockContext);

    this.mocker.getComponentUnderTest().getVelocityContext();

    verify(mockContext).put("vcontext", velocityContext);
  }
 @Before
 @Override
 public void setUp() throws Exception {
   super.setUp();
   executionContext = new ExecutionContext();
   Execution execution = getComponentManager().getInstance(Execution.class);
   execution.setContext(executionContext);
   Utils.setComponentManager(getComponentManager());
   legacyImpl = new XWikiRightServiceImpl();
   cachingImpl = new XWikiCachingRightService();
 }
  /**
   * We must ensure we clean the ThreadLocal variables located in the Container and Execution
   * components as otherwise we will have a potential memory leak.
   */
  protected void cleanupComponents() {
    @SuppressWarnings("deprecation")
    Container container = Utils.getComponent((Type) Container.class);
    container.removeRequest();
    container.removeResponse();
    container.removeSession();

    @SuppressWarnings("deprecation")
    Execution execution = Utils.getComponent((Type) Execution.class);
    execution.removeContext();
  }
Пример #5
0
  protected void cleanupComponents() {
    Container container = Utils.getComponent(Container.class);
    Execution execution = Utils.getComponent(Execution.class);

    // We must ensure we clean the ThreadLocal variables located in the Container and Execution
    // components as otherwise we will have a potential memory leak.
    container.removeRequest();
    container.removeResponse();
    container.removeSession();
    execution.removeContext();
  }
  public void shutdown() throws Exception {
    Execution execution = getComponentManager().getInstance(Execution.class);
    execution.removeContext();

    // Clean possible resources some components might hold
    this.componentManager.dispose();

    // Make sure we mark the component manager for garbage collection as otherwise each JUnit test
    // will
    // have an instance of the Component Manager (will all the components it's holding), leading to
    // out of memory errors when there are lots of tests...
    this.componentManager = null;
  }
Пример #7
0
  /**
   * Init provided {@link ExportURLFactory} and add rendered documents to ZIP stream.
   *
   * @param zos the ZIP output stream.
   * @param tempdir the directory where to copy attached files.
   * @param urlf the {@link com.xpn.xwiki.web.XWikiURLFactory} used to render the documents.
   * @param context the XWiki context.
   * @throws XWikiException error when render documents.
   * @throws IOException error when render documents.
   */
  private void renderDocuments(
      ZipOutputStream zos, File tempdir, ExportURLFactory urlf, XWikiContext context)
      throws XWikiException, IOException {
    ExecutionContextManager ecim = Utils.getComponent(ExecutionContextManager.class);
    Execution execution = Utils.getComponent(Execution.class);

    VelocityContext oldVelocityContext = (VelocityContext) context.get("vcontext");

    try {
      XWikiContext renderContext = (XWikiContext) context.clone();
      renderContext.put("action", "view");

      ExecutionContext ec = new ExecutionContext();

      // Bridge with old XWiki Context, required for old code.
      ec.setProperty("xwikicontext", renderContext);

      ecim.initialize(ec);

      // Push a clean new Execution Context since we don't want the main Execution Context to be
      // used for
      // rendering the HTML pages to export. It's cleaner to isolate it as we do. Note that the new
      // Execution Context automatically gets initialized with a new Velocity Context by
      // the VelocityRequestInitializer class.
      execution.pushContext(ec);

      VelocityManager velocityManager = Utils.getComponent(VelocityManager.class);

      // At this stage we have a clean Velocity Context
      VelocityContext vcontext = velocityManager.getVelocityContext();

      urlf.init(this.pages, tempdir, renderContext);
      renderContext.setURLFactory(urlf);

      for (String pageName : this.pages) {
        renderDocument(pageName, zos, renderContext, vcontext);
      }
    } catch (ExecutionContextException e) {
      throw new XWikiException(
          XWikiException.MODULE_XWIKI_EXPORT,
          XWikiException.ERROR_XWIKI_INIT_FAILED,
          "Failed to initialize Execution Context",
          e);
    } finally {
      // We must ensure that the new request we've used is removed so that the current
      // thread can continue to use its original Execution Context.
      execution.popContext();

      context.put("vcontext", oldVelocityContext);
    }
  }
 @Test
 public void getMembersWhenNoXWikiContext() throws Exception {
   Execution execution = this.componentManager.registerMockComponent(Execution.class);
   when(execution.getContext()).thenReturn(new ExecutionContext());
   DocumentReference userReference = new DocumentReference("userwiki", "XWiki", "userpage");
   try {
     new ReferenceUserIterator(userReference, null, execution).next();
   } catch (RuntimeException expected) {
     assertEquals(
         "Aborting member extraction from passed references [[userwiki:XWiki.userpage]] since no "
             + "XWiki Context was found",
         expected.getMessage());
   }
 }
  @Before
  public void setUp() throws Exception {
    Utils.setComponentManager(this.componentManager);

    Execution execution = this.componentManager.getInstance(Execution.class);
    ExecutionContext context = new ExecutionContext();

    this.xwiki = mock(XWiki.class);

    this.xwikiContext = new XWikiContext();
    this.xwikiContext.setDatabase("somewiki");
    this.xwikiContext.setWiki(this.xwiki);

    context.setProperty("xwikicontext", this.xwikiContext);

    final DocumentReference configDocReference =
        new DocumentReference("somewiki", "IRC", "IRCConfiguration");
    this.configDoc = mock(XWikiDocument.class);

    when(execution.getContext()).thenReturn(context);
    when(xwiki.getDocument(configDocReference, xwikiContext)).thenReturn(configDoc);
    when(configDoc.getDocumentReference()).thenReturn(configDocReference);
  }
  @Before
  public void setUp() throws Exception {
    EntityReferenceSerializer<String> localSerializer =
        mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
    EntityReferenceSerializer<String> serializer =
        mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "default");

    // No locale provided.
    documentReference = new DocumentReference("wiki", "space", "name");
    documentReferenceString = serializer.serialize(documentReference);
    documentReferenceLocalString = localSerializer.serialize(documentReference);

    language = "en";
    renderedContent = "content";
    title = "title";
    version = "1.1";
    hidden = false;
    date = new Date();
    creationDate = new Date();

    authorReference = new DocumentReference("wiki", "space", "author");
    authorString = serializer.serialize(authorReference);
    authorDisplay = "Au Thor";

    creatorReference = new DocumentReference("wiki", "space", "Creator");
    creatorString = serializer.serialize(creatorReference);
    creatorDisplay = "Crea Tor";

    // Mock

    mockContext = mock(XWikiContext.class);
    Execution mockExecution = mocker.getInstance(Execution.class);
    ExecutionContext mockExecutionContext = new ExecutionContext();
    mockExecutionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, mockContext);

    when(mockExecution.getContext()).thenReturn(mockExecutionContext);

    mockXWiki = mock(XWiki.class);
    mockDocument = mock(XWikiDocument.class);

    when(mockContext.getWiki()).thenReturn(mockXWiki);
    when(mockXWiki.getDocument(documentReference, mockContext)).thenReturn(mockDocument);

    when(mockDocument.getRealLanguage()).thenReturn(language);
    when(mockDocument.getTranslatedDocument(any(String.class), eq(mockContext)))
        .thenReturn(mockDocument);

    mockDab = mocker.getInstance(DocumentAccessBridge.class);
    when(mockDab.getDocument(documentReference)).thenReturn(mockDocument);

    BlockRenderer mockPlainRenderer = mocker.getInstance(BlockRenderer.class, "plain/1.0");
    doAnswer(
            new Answer<Object>() {
              public Object answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();

                WikiPrinter printer = (WikiPrinter) args[1];
                printer.print(renderedContent);

                return null;
              }
            })
        .when(mockPlainRenderer)
        .render(any(Block.class), any(WikiPrinter.class));

    when(mockDocument.getRenderedTitle(any(Syntax.class), eq(mockContext))).thenReturn(title);

    when(mockDocument.getVersion()).thenReturn(version);

    when(mockDocument.getAuthorReference()).thenReturn(authorReference);
    when(mockXWiki.getUserName(authorString, null, false, mockContext)).thenReturn(authorDisplay);

    when(mockDocument.getCreatorReference()).thenReturn(creatorReference);
    when(mockXWiki.getUserName(creatorString, null, false, mockContext)).thenReturn(creatorDisplay);

    when(mockDocument.getCreationDate()).thenReturn(creationDate);
    when(mockDocument.getContentUpdateDate()).thenReturn(date);

    when(mockDocument.isHidden()).thenReturn(hidden);
  }
 protected XWikiContext getContext() {
   return (XWikiContext) execution.getContext().getProperty("xwikicontext");
 }
Пример #12
0
 /** @return the deprecated xwiki context used to manipulate xwiki objects */
 private XWikiContext getXWikiContext() {
   return (XWikiContext) execution.getContext().getProperty("xwikicontext");
 }