1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.classloader.internal.protocol.attachmentjar

File AttachmentURLStreamHandlerTest.java

 

Code metrics

2
28
6
1
124
81
8
0.29
4.67
6
1.33

Classes

Class Line # Actions
AttachmentURLStreamHandlerTest 44 28 0% 8 2
0.944444494.4%
 

Contributing tests

This file is covered by 3 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 org.xwiki.classloader.internal.protocol.attachmentjar;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.InputStream;
24    import java.net.URL;
25    import java.net.URLConnection;
26    import java.net.URLStreamHandler;
27   
28    import org.apache.commons.io.IOUtils;
29    import org.jmock.Expectations;
30    import org.junit.Assert;
31    import org.junit.Test;
32    import org.xwiki.bridge.DocumentAccessBridge;
33    import org.xwiki.classloader.ExtendedURLStreamHandler;
34    import org.xwiki.model.reference.AttachmentReference;
35    import org.xwiki.model.reference.AttachmentReferenceResolver;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.test.jmock.AbstractComponentTestCase;
38   
39    /**
40    * Unit tests for {@link AttachmentURLStreamHandler}.
41    *
42    * @version $Id: 63c495c7206ea75905d99cd97bd3537c75124e19 $
43    */
 
44    public class AttachmentURLStreamHandlerTest extends AbstractComponentTestCase
45    {
46    private AttachmentReferenceResolver<String> arf;
47   
48    private DocumentAccessBridge dab;
49   
50    private ExtendedURLStreamHandler handler;
51   
 
52  3 toggle @Override
53    protected void registerComponents() throws Exception
54    {
55  3 super.registerComponents();
56   
57  3 this.arf = registerMockComponent(AttachmentReferenceResolver.TYPE_STRING, "current");
58  3 this.dab = registerMockComponent(DocumentAccessBridge.class);
59   
60  3 this.handler = getComponentManager().getInstance(ExtendedURLStreamHandler.class, "attachmentjar");
61    }
62   
 
63  1 toggle @Test
64    public void testInvalidAttachmentJarURL() throws Exception
65    {
66  1 URL url = new URL(null, "http://invalid/url", (URLStreamHandler) this.handler);
67   
68  1 try {
69  1 url.openConnection();
70  0 Assert.fail("Should have thrown an exception here");
71    } catch (RuntimeException expected) {
72  1 Assert.assertEquals("An attachment JAR URL should start with [attachmentjar://], got [http://invalid/url]",
73    expected.getMessage());
74    }
75    }
76   
 
77  1 toggle @Test
78    public void testAttachmentJarURL() throws Exception
79    {
80  1 URL url = new URL(null, "attachmentjar://Space.Page@filename", (URLStreamHandler) this.handler);
81   
82  1 final AttachmentReference attachmentReference = new AttachmentReference("filename",
83    new DocumentReference("wiki", "space", "page"));
84  1 getMockery().checking(new Expectations()
85    {
 
86  1 toggle {
87  1 oneOf(AttachmentURLStreamHandlerTest.this.arf).resolve("Space.Page@filename");
88  1 will(returnValue(attachmentReference));
89  1 oneOf(AttachmentURLStreamHandlerTest.this.dab).getAttachmentContent(attachmentReference);
90  1 will(returnValue(new ByteArrayInputStream("content".getBytes())));
91    }
92    });
93   
94  1 URLConnection connection = url.openConnection();
95  1 InputStream input = null;
96  1 try {
97  1 connection.connect();
98  1 input = connection.getInputStream();
99  1 Assert.assertEquals("content", IOUtils.toString(input));
100    } finally {
101  1 if (input != null) {
102  1 input.close();
103    }
104    }
105    }
106   
107    /**
108    * Verify that URL-encoded chars are decoded.
109    */
 
110  1 toggle @Test
111    public void testAttachmentJarURLWithEncodedChars() throws Exception
112    {
113  1 URL url = new URL(null, "attachmentjar://some%20page", (URLStreamHandler) this.handler);
114   
115  1 getMockery().checking(new Expectations()
116    {
 
117  1 toggle {
118  1 oneOf(AttachmentURLStreamHandlerTest.this.arf).resolve("some page");
119    }
120    });
121   
122  1 url.openConnection();
123    }
124    }