1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.pdf.impl

File PDFURIResolverTest.java

 

Code metrics

0
20
2
1
83
47
2
0.1
10
2
1

Classes

Class Line # Actions
PDFURIResolverTest 47 20 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.pdf.impl;
21   
22    import java.io.ByteArrayInputStream;
23    import java.util.Collections;
24   
25    import javax.xml.transform.Source;
26    import javax.xml.transform.stream.StreamSource;
27   
28    import org.apache.commons.io.IOUtils;
29    import org.junit.Assert;
30    import org.junit.Test;
31    import org.xwiki.model.EntityType;
32    import org.xwiki.model.reference.AttachmentReference;
33    import org.xwiki.model.reference.DocumentReference;
34   
35    import com.xpn.xwiki.XWikiContext;
36    import com.xpn.xwiki.doc.XWikiAttachment;
37    import com.xpn.xwiki.doc.XWikiDocument;
38   
39    import static org.mockito.Mockito.*;
40   
41    /**
42    * Unit tests for {@link PDFURIResolver}.
43    *
44    * @version $Id: 7d82792eb56840ce30f216969f8a0218e18a1fd8 $
45    * @since 5.0RC1
46    */
 
47    public class PDFURIResolverTest
48    {
 
49  1 toggle @Test
50    public void resolveWhenNoMap() throws Exception
51    {
52  1 XWikiContext context = mock(XWikiContext.class);
53  1 PDFURIResolver resolver = new PDFURIResolver(context);
54  1 Source source = resolver.resolve("href", "base");
55  1 Assert.assertNull(source);
56    }
57   
 
58  1 toggle @Test
59    public void resolveWhenMapItemExists() throws Exception
60    {
61  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
62  1 AttachmentReference attachmentReference = new AttachmentReference("fileName", documentReference);
63  1 com.xpn.xwiki.XWiki xwiki = mock(com.xpn.xwiki.XWiki.class);
64  1 XWikiDocument document = mock(XWikiDocument.class);
65  1 XWikiAttachment attachment = mock(XWikiAttachment.class);
66   
67  1 XWikiContext context = mock(XWikiContext.class);
68    // Add an encoded space character to test that the URL is not decoded. The input URL (to be resolved) is
69    // expected to be encoded because the URL factory creates encoded URLs.
70  1 String url = "encoded+url";
71  1 when(context.get("pdfExportImageURLMap")).thenReturn(Collections.singletonMap(url, attachmentReference));
72  1 when(context.getWiki()).thenReturn(xwiki);
73  1 when(xwiki.getDocument(attachmentReference.extractReference(EntityType.DOCUMENT), context)).thenReturn(
74    document);
75  1 when(document.getAttachment("fileName")).thenReturn(attachment);
76  1 when(attachment.getContentInputStream(context)).thenReturn(new ByteArrayInputStream("content".getBytes()));
77   
78  1 PDFURIResolver resolver = new PDFURIResolver(context);
79  1 Source source = resolver.resolve(url, "base");
80  1 Assert.assertEquals(StreamSource.class, source.getClass());
81  1 Assert.assertEquals("content", IOUtils.readLines(((StreamSource) source).getInputStream()).get(0));
82    }
83    }