Search in sources :

Example 1 with CustomStub

use of org.jmock.core.stub.CustomStub in project intellij-plugins by JetBrains.

the class ClearHistoryCommandTest method testClearHistory.

public void testClearHistory() throws Exception {
    myIdeMock.expects(once()).method("askQuestion").with(eq(StringUtil.getMsg("ClearHistoryCommand.title")), eq(StringUtil.getMsg("ClearHistoryCommand.text"))).will(returnValue(true));
    myIdeMock.expects(once()).method("runLongProcess").will(new CustomStub("runner") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            ((IDEFacade.Process) invocation.parameterValues.get(1)).run(new NullProgressIndicator());
            return null;
        }
    });
    myIdeMock.expects(once()).method("runOnPooledThread").will(new CustomStub("foo") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            final FutureTask task = new FutureTask((Runnable) invocation.parameterValues.get(0), null);
            task.run();
            return task;
        }
    });
    myDispatcherMock.expects(once()).method("clearHistory");
    myCommand.execute();
}
Also used : Invocation(org.jmock.core.Invocation) FutureTask(java.util.concurrent.FutureTask) CustomStub(org.jmock.core.stub.CustomStub) NullProgressIndicator(jetbrains.communicator.ide.NullProgressIndicator) IDEFacade(jetbrains.communicator.ide.IDEFacade)

Example 2 with CustomStub

use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.

the class XWikiRightServiceImplTest method testHasAccessLevelWhithGuestUser.

public void testHasAccessLevelWhithGuestUser() throws XWikiException {
    final XWikiDocument doc = new XWikiDocument(new DocumentReference("wiki2", "Space", "Page"));
    final XWikiDocument preferences = new XWikiDocument(new DocumentReference("wiki2", "XWiki", "XWikiPreference"));
    BaseObject preferencesObject = new BaseObject();
    preferencesObject.setClassName("XWiki.XWikiGlobalRights");
    preferencesObject.setStringValue("levels", "view");
    preferencesObject.setIntValue("allow", 1);
    preferences.addXObject(preferencesObject);
    this.mockXWiki.stubs().method("getDocument").with(eq(XWIKIPREFERENCES_REFERENCE), ANYTHING).will(new CustomStub("Implements XWiki.getDocument") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            if (!getContext().getWikiId().equals("wiki2")) {
                new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiPreference"));
            }
            return preferences;
        }
    });
    this.mockXWiki.stubs().method("getDocument").with(eq(doc.getPrefixedFullName()), ANYTHING).will(returnValue(doc));
    this.mockXWiki.stubs().method("getXWikiPreference").with(eq("authenticate_view"), ANYTHING, ANYTHING).will(returnValue("false"));
    this.mockXWiki.stubs().method("getXWikiPreferenceAsInt").with(eq("authenticate_view"), ANYTHING, ANYTHING).will(returnValue(0));
    this.mockXWiki.stubs().method("getSpacePreference").with(eq("authenticate_view"), ANYTHING, ANYTHING).will(returnValue("false"));
    this.mockXWiki.stubs().method("getSpacePreferenceAsInt").with(eq("authenticate_view"), ANYTHING, ANYTHING).will(returnValue(0));
    this.mockGroupService.stubs().method("getAllGroupsReferencesForMember").with(eq(new DocumentReference("xwiki", "XWiki", XWikiRightService.GUEST_USER)), ANYTHING, ANYTHING, ANYTHING).will(returnValue(Collections.emptyList()));
    this.mockGroupService.stubs().method("getAllGroupsReferencesForMember").with(eq(new DocumentReference("wiki2", "XWiki", XWikiRightService.GUEST_USER)), ANYTHING, ANYTHING, ANYTHING).will(returnValue(Collections.emptyList()));
    getContext().setWikiId("wiki");
    assertFalse("Guest has wiew right on the document", this.rightService.hasAccessLevel("view", XWikiRightService.GUEST_USER_FULLNAME, doc.getPrefixedFullName(), true, getContext()));
    // direct user rights
    preferencesObject.setStringValue("users", XWikiRightService.GUEST_USER_FULLNAME);
    getContext().setWikiId("wiki");
    assertTrue("Guest does not have right on the document when tested from another wiki", this.rightService.hasAccessLevel("view", XWikiRightService.GUEST_USER_FULLNAME, doc.getPrefixedFullName(), true, getContext()));
    getContext().setWikiId(doc.getDatabase());
    assertTrue("Guest does not have right on the document when tested from the document wiki", this.rightService.hasAccessLevel("view", XWikiRightService.GUEST_USER_FULLNAME, doc.getPrefixedFullName(), true, getContext()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Invocation(org.jmock.core.Invocation) CustomStub(org.jmock.core.stub.CustomStub) BaseObject(com.xpn.xwiki.objects.BaseObject) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 3 with CustomStub

use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.

the class XWikiRightServiceImplTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    this.rightService = new XWikiRightServiceImpl();
    this.mockGroupService = mock(XWikiGroupService.class, new Class[] {}, new Object[] {});
    this.mockXWiki = mock(XWiki.class);
    this.mockXWiki.stubs().method("getGroupService").will(returnValue(this.mockGroupService.proxy()));
    this.mockXWiki.stubs().method("isReadOnly").will(returnValue(false));
    this.mockXWiki.stubs().method("getWikiOwner").will(returnValue(null));
    this.mockXWiki.stubs().method("getMaxRecursiveSpaceChecks").will(returnValue(0));
    this.mockXWiki.stubs().method("getDocument").with(ANYTHING, eq("WebPreferences"), ANYTHING).will(new CustomStub("Implements XWiki.getDocument") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            return new XWikiDocument(new DocumentReference(getContext().getWikiId(), (String) invocation.parameterValues.get(0), "WebPreferences"));
        }
    });
    // Called from MessageToolVelocityContextInitializer.
    this.mockXWiki.stubs().method("prepareResources");
    getContext().setWiki((XWiki) this.mockXWiki.proxy());
    this.user = new XWikiDocument(new DocumentReference("wiki", "XWiki", "user"));
    this.user.setNew(false);
    getContext().setWikiId(this.user.getWikiName());
    BaseObject userObject = new BaseObject();
    userObject.setClassName("XWiki.XWikiUser");
    this.user.addXObject(userObject);
    this.mockXWiki.stubs().method("getDocument").with(eq(this.user.getPrefixedFullName()), ANYTHING).will(returnValue(this.user));
    this.group = new XWikiDocument(new DocumentReference("wiki", "XWiki", "group"));
    this.group.setNew(false);
    getContext().setWikiId(this.group.getWikiName());
    BaseObject groupObject = new BaseObject();
    groupObject.setClassName("XWiki.XWikiGroups");
    groupObject.setStringValue("member", this.user.getFullName());
    this.group.addXObject(groupObject);
    this.mockXWiki.stubs().method("getDocument").with(eq(this.group.getPrefixedFullName()), ANYTHING).will(returnValue(this.group));
    this.group2 = new XWikiDocument(new DocumentReference("wiki2", "XWiki", "group2"));
    this.group2.setNew(false);
    getContext().setWikiId(this.group2.getWikiName());
    BaseObject group2Object = new BaseObject();
    group2Object.setClassName("XWiki.XWikiGroups");
    group2Object.setStringValue("member", this.user.getPrefixedFullName());
    this.group2.addXObject(groupObject);
    this.mockXWiki.stubs().method("getDocument").with(eq(this.group2.getPrefixedFullName()), ANYTHING).will(returnValue(this.group2));
    this.mockGroupService.stubs().method("getAllGroupsReferencesForMember").with(eq(this.user.getDocumentReference()), ANYTHING, ANYTHING, ANYTHING).will(new CustomStub("Implements XWikiGroupService.getAllGroupsReferencesForMember") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            XWikiContext context = (XWikiContext) invocation.parameterValues.get(3);
            if (context.getWikiId().equals(group.getWikiName())) {
                return Collections.singleton(group.getDocumentReference());
            } else if (context.getWikiId().equals(group2.getWikiName())) {
                return Collections.singleton(group2.getDocumentReference());
            } else {
                return Collections.emptyList();
            }
        }
    });
    this.mockGroupService.stubs().method("getAllGroupsReferencesForMember").with(eq(this.group.getDocumentReference()), ANYTHING, ANYTHING, ANYTHING).will(returnValue(Collections.emptyList()));
    this.mockGroupService.stubs().method("getAllGroupsReferencesForMember").with(eq(this.group2.getDocumentReference()), ANYTHING, ANYTHING, ANYTHING).will(returnValue(Collections.emptyList()));
}
Also used : Invocation(org.jmock.core.Invocation) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) CustomStub(org.jmock.core.stub.CustomStub) BaseObject(com.xpn.xwiki.objects.BaseObject) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiGroupService(com.xpn.xwiki.user.api.XWikiGroupService)

Example 4 with CustomStub

use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.

the class XWikiMessageToolTest method createMockDocumentWithTrans.

private Mock createMockDocumentWithTrans(long id, String name, String content, String transContent, boolean isNew) {
    Mock mockDocument = mock(XWikiDocument.class);
    final XWikiDocument document = (XWikiDocument) mockDocument.proxy();
    final XWikiDocument transdocument = createDocument(name, transContent, "fr", "", false);
    mockDocument.stubs().method("getTranslatedDocument").will(new CustomStub("Implements getTranslatedDocument") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            if (invocation.parameterValues.size() == 1) {
                XWikiContext context = (XWikiContext) invocation.parameterValues.get(0);
                String lang = context.getLanguage();
                if ("fr".equals(lang))
                    return transdocument;
                else
                    return document;
            } else {
                String lang = (String) invocation.parameterValues.get(0);
                if ("fr".equals(lang))
                    return transdocument;
                else
                    return document;
            }
        }
    });
    mockDocument.stubs().method("isNew").will(returnValue(isNew));
    mockDocument.stubs().method("getId").will(returnValue(Long.valueOf(id)));
    mockDocument.stubs().method("getDate").will(returnValue(new Date()));
    mockDocument.stubs().method("getContent").will(returnValue(content));
    mockDocument.stubs().method("getFullName").will(returnValue(name));
    mockDocument.stubs().method("getLanguage").will(returnValue(""));
    mockDocument.stubs().method("getDefaultLanguage").will(returnValue("en"));
    mockDocument.stubs().method("getRealLanguage").will(returnValue("en"));
    return mockDocument;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Invocation(org.jmock.core.Invocation) CustomStub(org.jmock.core.stub.CustomStub) XWikiContext(com.xpn.xwiki.XWikiContext) Mock(org.jmock.Mock) Date(java.util.Date)

Example 5 with CustomStub

use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.

the class XWikiTest method testGetPrefsClass.

/**
 * Tests that XWiki.XWikiPreferences page is not saved each time XWiki is initialized.
 *
 * @throws Exception when any exception occurs inside XWiki
 */
public void testGetPrefsClass() throws Exception {
    Mock mockStore = registerMockComponent(XWikiStoreInterface.class);
    this.xwiki.setStore((XWikiStoreInterface) mockStore.proxy());
    XWikiDocument prefsDoc = new XWikiDocument(new DocumentReference("xwiki", "XWiki", "XWikiPreferences"));
    final Map<DocumentReference, XWikiDocument> documents = new HashMap<DocumentReference, XWikiDocument>();
    documents.put(prefsDoc.getDocumentReference(), prefsDoc);
    mockStore.expects(atLeastOnce()).method("loadXWikiDoc").with(NOT_NULL, same(getContext())).will(new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
            if (!documents.containsKey(document.getDocumentReference())) {
                documents.put(document.getDocumentReference(), document);
            } else {
                document = documents.get(document.getDocumentReference());
            }
            return document;
        }
    });
    mockStore.expects(once()).method("saveXWikiDoc").with(same(prefsDoc), same(getContext()));
    this.xwiki.getPrefsClass(getContext());
    this.xwiki.getPrefsClass(getContext());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Invocation(org.jmock.core.Invocation) HashMap(java.util.HashMap) CustomStub(org.jmock.core.stub.CustomStub) BaseObject(com.xpn.xwiki.objects.BaseObject) Mock(org.jmock.Mock) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

Invocation (org.jmock.core.Invocation)18 CustomStub (org.jmock.core.stub.CustomStub)18 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)11 BaseObject (com.xpn.xwiki.objects.BaseObject)11 Mock (org.jmock.Mock)9 DocumentReference (org.xwiki.model.reference.DocumentReference)9 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)4 HashMap (java.util.HashMap)4 XWiki (com.xpn.xwiki.XWiki)3 XWikiConfig (com.xpn.xwiki.XWikiConfig)3 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)3 XWikiHibernateVersioningStore (com.xpn.xwiki.store.XWikiHibernateVersioningStore)3 XWikiServletRequestStub (com.xpn.xwiki.web.XWikiServletRequestStub)2 URL (java.net.URL)2 Date (java.util.Date)2 VelocityContext (org.apache.velocity.VelocityContext)2 Execution (org.xwiki.context.Execution)2 VelocityEngine (org.xwiki.velocity.VelocityEngine)2 VelocityManager (org.xwiki.velocity.VelocityManager)2