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

File URIClassLoaderTest.java

 

Code metrics

0
29
4
1
110
70
4
0.14
7.25
4
1

Classes

Class Line # Actions
URIClassLoaderTest 44 29 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.ByteArrayOutputStream;
24    import java.net.URI;
25    import java.net.URLStreamHandlerFactory;
26    import java.util.jar.JarEntry;
27    import java.util.jar.JarOutputStream;
28   
29    import org.junit.Assert;
30   
31    import org.jmock.Expectations;
32    import org.junit.Test;
33    import org.xwiki.bridge.DocumentAccessBridge;
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 URIClassLoader}.
41    *
42    * @version $Id: ac0c0d64e76ac0691fc06b0b2de24f9a598b55cb $
43    */
 
44    public class URIClassLoaderTest extends AbstractComponentTestCase
45    {
46    private AttachmentReferenceResolver<String> arf;
47   
48    private DocumentAccessBridge dab;
49   
 
50  1 toggle @SuppressWarnings("unchecked")
51    @Override
52    protected void registerComponents() throws Exception
53    {
54  1 super.registerComponents();
55   
56  1 this.arf = registerMockComponent(AttachmentReferenceResolver.TYPE_STRING, "current");
57  1 this.dab = registerMockComponent(DocumentAccessBridge.class);
58    }
59   
60    /**
61    * Verify that resource located in a URI with an attachmentjar protocol can be found.
62    */
 
63  1 toggle @Test
64    public void testFindResource() throws Exception
65    {
66  1 URLStreamHandlerFactory urlStreamHandlerFactory =
67    getComponentManager().getInstance(URLStreamHandlerFactory.class);
68  1 URIClassLoader cl =
69    new URIClassLoader(new URI[] {new URI("attachmentjar://page%40filename1"), new URI("http://some/url"),
70    new URI("attachmentjar://filename2")}, urlStreamHandlerFactory);
71   
72  1 Assert.assertEquals(3, cl.getURLs().length);
73  1 Assert.assertEquals("attachmentjar://page%40filename1", cl.getURLs()[0].toString());
74  1 Assert.assertEquals("http://some/url", cl.getURLs()[1].toString());
75  1 Assert.assertEquals("attachmentjar://filename2", cl.getURLs()[2].toString());
76   
77  1 final AttachmentReference attachmentName1 = new AttachmentReference("filename1",
78    new DocumentReference("wiki", "space", "page"));
79  1 final AttachmentReference attachmentName2 = new AttachmentReference("filename2",
80    new DocumentReference("wiki", "space", "page"));
81   
82  1 getMockery().checking(new Expectations()
83    {
 
84  1 toggle {
85  1 allowing(URIClassLoaderTest.this.arf).resolve("page@filename1");
86  1 will(returnValue(attachmentName1));
87  1 oneOf(URIClassLoaderTest.this.dab).getAttachmentContent(attachmentName1);
88  1 will(returnValue(new ByteArrayInputStream(createJarFile("/nomatch"))));
89  1 allowing(URIClassLoaderTest.this.arf).resolve("filename2");
90  1 will(returnValue(attachmentName2));
91  1 oneOf(URIClassLoaderTest.this.dab).getAttachmentContent(attachmentName2);
92  1 will(returnValue(new ByteArrayInputStream(createJarFile("/something"))));
93    }
94    });
95   
96  1 Assert.assertEquals("jar:attachmentjar://filename2!/something", cl.findResource("/something").toString());
97    }
98   
 
99  2 toggle private byte[] createJarFile(String resourceName) throws Exception
100    {
101  2 ByteArrayOutputStream baos = new ByteArrayOutputStream();
102  2 JarOutputStream jos = new JarOutputStream(baos);
103  2 JarEntry entry = new JarEntry(resourceName);
104  2 jos.putNextEntry(entry);
105  2 jos.write("whatever".getBytes("UTF-8"));
106  2 jos.closeEntry();
107  2 jos.close();
108  2 return baos.toByteArray();
109    }
110    }