1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
41 |
|
|
42 |
|
@version |
43 |
|
|
|
|
| 94.4% |
Uncovered Elements: 2 (36) |
Complexity: 8 |
Complexity Density: 0.29 |
|
44 |
|
public class AttachmentURLStreamHandlerTest extends AbstractComponentTestCase |
45 |
|
{ |
46 |
|
private AttachmentReferenceResolver<String> arf; |
47 |
|
|
48 |
|
private DocumentAccessBridge dab; |
49 |
|
|
50 |
|
private ExtendedURLStreamHandler handler; |
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
52 |
3 |
@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 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
63 |
1 |
@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 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
1PASS
|
|
77 |
1 |
@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 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
86 |
1 |
{... |
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 |
|
|
109 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
110 |
1 |
@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 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
117 |
1 |
{... |
118 |
1 |
oneOf(AttachmentURLStreamHandlerTest.this.arf).resolve("some page"); |
119 |
|
} |
120 |
|
}); |
121 |
|
|
122 |
1 |
url.openConnection(); |
123 |
|
} |
124 |
|
} |