1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
41 |
|
|
42 |
|
@version |
43 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (33) |
Complexity: 4 |
Complexity Density: 0.14 |
|
44 |
|
public class URIClassLoaderTest extends AbstractComponentTestCase |
45 |
|
{ |
46 |
|
private AttachmentReferenceResolver<String> arf; |
47 |
|
|
48 |
|
private DocumentAccessBridge dab; |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
50 |
1 |
@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 |
|
|
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
63 |
1 |
@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 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
84 |
1 |
{... |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
99 |
2 |
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 |
|
} |