Search in sources :

Example 1 with DocumentReferenceResolver

use of org.xwiki.model.reference.DocumentReferenceResolver in project xwiki-platform by xwiki.

the class UntypedRecordableEventDescriptorComponentBuilderTest method setUp.

@Before
public void setUp() throws Exception {
    queryManager = mocker.registerMockComponent(QueryManager.class);
    modelBridge = mocker.registerMockComponent(ModelBridge.class);
    documentReferenceResolver = mocker.registerMockComponent(DocumentReferenceResolver.class);
    authorizationManager = mocker.registerMockComponent(AuthorizationManager.class);
    Query query = mock(Query.class);
    when(queryManager.createQuery(any(), any())).thenReturn(query);
    when(query.execute()).thenReturn(Arrays.asList("e1", "e2", "e3"));
    event1 = mock(DocumentReference.class);
    event2 = mock(DocumentReference.class);
    event3 = mock(DocumentReference.class);
    when(this.documentReferenceResolver.resolve("e1")).thenReturn(event1);
    when(this.documentReferenceResolver.resolve("e2")).thenReturn(event2);
    when(this.documentReferenceResolver.resolve("e3")).thenReturn(event3);
}
Also used : DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) Query(org.xwiki.query.Query) QueryManager(org.xwiki.query.QueryManager) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 2 with DocumentReferenceResolver

use of org.xwiki.model.reference.DocumentReferenceResolver in project xwiki-platform by xwiki.

the class GroupMimeMessageIteratorTest method createMessage.

@Test
public void createMessage() throws Exception {
    DocumentReference groupReference = new DocumentReference("xwiki", "XWiki", "Marketing");
    DocumentReference userReference1 = new DocumentReference("xwiki", "XWiki", "JohnDoe");
    DocumentReference userReference2 = new DocumentReference("xwiki", "XWiki", "JaneDoe");
    DocumentReference userReference3 = new DocumentReference("xwiki", "XWiki", "JonnieDoe");
    Session session = Session.getInstance(new Properties());
    MimeMessageFactory<MimeMessage> factory = new MimeMessageFactory<MimeMessage>() {

        @Override
        public MimeMessage createMessage(Object source, Map parameters) throws MessagingException {
            return new ExtendedMimeMessage();
        }
    };
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("parameters", Collections.EMPTY_MAP);
    parameters.put("session", session);
    DocumentAccessBridge accessBridge = mock(DocumentAccessBridge.class);
    when(accessBridge.getProperty(eq(groupReference), any(), eq(0), eq("member"))).thenReturn("XWiki.JohnDoe");
    when(accessBridge.getProperty(eq(groupReference), any(), eq(1), eq("member"))).thenReturn("XWiki.JaneDoe");
    when(accessBridge.getProperty(eq(groupReference), any(), eq(2), eq("member"))).thenReturn("XWiki.JonnieDoe");
    when(accessBridge.getProperty(eq(userReference1), any(), eq("email"))).thenReturn("john@doe.com");
    when(accessBridge.getProperty(eq(userReference2), any(), eq("email"))).thenReturn("jane@doe.com");
    when(accessBridge.getProperty(eq(userReference3), any(), eq("email"))).thenReturn("jannie@doe.com");
    Execution execution = mock(Execution.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(execution.getContext()).thenReturn(executionContext);
    XWikiContext xwikiContext = mock(XWikiContext.class);
    when(executionContext.getProperty("xwikicontext")).thenReturn(xwikiContext);
    XWiki xwiki = mock(XWiki.class);
    when(xwikiContext.getWiki()).thenReturn(xwiki);
    XWikiDocument document = mock(XWikiDocument.class);
    when(xwiki.getDocument(eq(groupReference), eq(xwikiContext))).thenReturn(document);
    BaseObject object = mock(BaseObject.class);
    when(document.getXObjects(any(EntityReference.class))).thenReturn(Arrays.asList(object, object, object));
    DocumentReferenceResolver<String> resolver = (DocumentReferenceResolver<String>) mock(DocumentReferenceResolver.class);
    when(resolver.resolve("XWiki.JohnDoe")).thenReturn(userReference1);
    when(resolver.resolve("XWiki.JaneDoe")).thenReturn(userReference2);
    when(resolver.resolve("XWiki.JonnieDoe")).thenReturn(userReference3);
    ComponentManager componentManager = mock(ComponentManager.class);
    when(componentManager.getInstance(eq(DocumentAccessBridge.class))).thenReturn(accessBridge);
    when(componentManager.getInstance(eq(Execution.class))).thenReturn(execution);
    when(componentManager.getInstance(eq(DocumentReferenceResolver.TYPE_STRING), eq("current"))).thenReturn(resolver);
    GroupMimeMessageIterator iterator = new GroupMimeMessageIterator(groupReference, factory, parameters, componentManager);
    assertTrue(iterator.hasNext());
    MimeMessage message1 = iterator.next();
    assertArrayEquals(message1.getRecipients(Message.RecipientType.TO), InternetAddress.parse("john@doe.com"));
    assertTrue(iterator.hasNext());
    MimeMessage message2 = iterator.next();
    assertArrayEquals(message2.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jane@doe.com"));
    assertTrue(iterator.hasNext());
    MimeMessage message3 = iterator.next();
    assertArrayEquals(message3.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jannie@doe.com"));
    assertFalse(iterator.hasNext());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) HashMap(java.util.HashMap) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) Properties(java.util.Properties) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) EntityReference(org.xwiki.model.reference.EntityReference) ComponentManager(org.xwiki.component.manager.ComponentManager) BaseObject(com.xpn.xwiki.objects.BaseObject) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session) Test(org.junit.Test)

Example 3 with DocumentReferenceResolver

use of org.xwiki.model.reference.DocumentReferenceResolver in project xwiki-platform by xwiki.

the class IntegrationTests method initialize.

@RenderingTestSuite.Initialized
public void initialize(MockitoComponentManager componentManager) throws Exception {
    // For performance reasons we mock some components to avoid having to draw all oldcore components
    // Macro Reference Resolver
    DocumentReferenceResolver<String> macroResolver = componentManager.registerMockComponent(new DefaultParameterizedType(null, DocumentReferenceResolver.class, String.class), "macro");
    DocumentReference referencedDocumentReference = new DocumentReference("Wiki", "Space", "Page");
    when(macroResolver.resolve(eq("Space.Page"), any(MacroBlock.class))).thenReturn(referencedDocumentReference);
    // Document Access Bridge mock
    // Simulate the XDOM of the referenced document
    DocumentAccessBridge dab = componentManager.registerMockComponent(DocumentAccessBridge.class);
    DocumentModelBridge dmb = mock(DocumentModelBridge.class);
    when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
    Parser parser = componentManager.getInstance(Parser.class, "xwiki/2.1");
    XDOM xdom = parser.parse(new StringReader("= heading1 =\n==heading2=="));
    when(dmb.getXDOM()).thenReturn(xdom);
}
Also used : DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) StringReader(java.io.StringReader) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Parser(org.xwiki.rendering.parser.Parser)

Example 4 with DocumentReferenceResolver

use of org.xwiki.model.reference.DocumentReferenceResolver in project xwiki-platform by xwiki.

the class BaseObjectReference method maybeParseName.

private void maybeParseName() {
    if (!this.nameParsed) {
        String name = getName();
        String classReferenceStr;
        String objectNumberStr;
        Matcher matcher = NUMBERPATTERN.matcher(name);
        if (matcher.find()) {
            if (matcher.group(1).length() % 2 == 0) {
                classReferenceStr = name.substring(0, matcher.end(1));
                objectNumberStr = matcher.group(2);
            } else {
                classReferenceStr = name;
                objectNumberStr = null;
            }
        } else {
            classReferenceStr = name;
            objectNumberStr = null;
        }
        this.xclassReference = Utils.<DocumentReferenceResolver<String>>getComponent(DocumentReferenceResolver.TYPE_STRING).resolve(classReferenceStr, getParent());
        if (objectNumberStr != null) {
            this.objectNumber = Integer.valueOf(objectNumberStr);
        }
        // Indicate that the name does not need to be parsed
        this.nameParsed = true;
    }
}
Also used : DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) Matcher(java.util.regex.Matcher)

Example 5 with DocumentReferenceResolver

use of org.xwiki.model.reference.DocumentReferenceResolver in project xwiki-platform by xwiki.

the class ModelScriptService method createDocumentReference.

/**
 * Create a Document Reference from a passed wiki, list of spaces and page names, which can be empty strings or null
 * in which case they are resolved against the Resolver having the hint passed as parameter. Valid hints are for
 * example "default", "current", "currentmixed".
 *
 * @param wiki the wiki reference name to use (can be empty or null)
 * @param spaces the spaces list to use (can be empty or null)
 * @param page the page reference name to use (can be empty or null)
 * @param hint the hint of the Resolver to use in case any parameter is empty or null
 * @return the typed Document Reference object or null if no Resolver with the passed hint could be found
 *
 * @since 7.2M2
 */
public DocumentReference createDocumentReference(String wiki, List<String> spaces, String page, String hint) {
    EntityReference reference = null;
    if (!StringUtils.isEmpty(wiki)) {
        reference = new EntityReference(wiki, EntityType.WIKI);
    }
    if (spaces != null && !spaces.isEmpty()) {
        for (String space : spaces) {
            reference = new EntityReference(space, EntityType.SPACE, reference);
        }
    }
    if (!StringUtils.isEmpty(page)) {
        reference = new EntityReference(page, EntityType.DOCUMENT, reference);
    }
    DocumentReference documentReference;
    try {
        DocumentReferenceResolver<EntityReference> resolver = this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, hint);
        documentReference = resolver.resolve(reference);
    } catch (ComponentLookupException e) {
        try {
            // Ensure backward compatibility with older scripts that use hints like "default/reference" because at
            // the time they were written we didn't have support for generic types in component role.
            DocumentReferenceResolver<EntityReference> drr = this.componentManager.getInstance(DocumentReferenceResolver.class, hint);
            documentReference = drr.resolve(reference);
            this.logger.warn("Deprecated usage of DocumentReferenceResolver with hint [{}]. " + "Please consider using a DocumentReferenceResolver that takes into account generic types.", hint);
        } catch (ComponentLookupException ex) {
            documentReference = null;
        }
    }
    return documentReference;
}
Also used : DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) EntityReference(org.xwiki.model.reference.EntityReference) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

DocumentReferenceResolver (org.xwiki.model.reference.DocumentReferenceResolver)5 DocumentReference (org.xwiki.model.reference.DocumentReference)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)2 EntityReference (org.xwiki.model.reference.EntityReference)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Matcher (java.util.regex.Matcher)1 Session (javax.mail.Session)1 MimeMessage (javax.mail.internet.MimeMessage)1 Before (org.junit.Before)1 Test (org.junit.Test)1 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1