@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);
  }
 @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");
 }
 /** @return the deprecated xwiki context used to manipulate xwiki objects */
 private XWikiContext getXWikiContext() {
   return (XWikiContext) execution.getContext().getProperty("xwikicontext");
 }