1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.store.serialization.xml.internal; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.ByteArrayOutputStream; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import org.apache.commons.io.IOUtils; |
27 |
|
import org.junit.Assert; |
28 |
|
import org.junit.Before; |
29 |
|
import org.junit.Test; |
30 |
|
|
31 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
@version |
37 |
|
@since |
38 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 3 |
Complexity Density: 0.11 |
|
39 |
|
public class AttachmentListMetadataSerializerTest |
40 |
|
{ |
41 |
|
private static final String TEST_CONTENT = |
42 |
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
43 |
|
+ "<attachment-list serializer=\"attachment-list-meta/1.0\">\n" |
44 |
|
+ " <attachment serializer=\"attachment-meta/1.0\">\n" |
45 |
|
+ " <filename>file1</filename>\n" |
46 |
|
+ " <filesize>10</filesize>\n" |
47 |
|
+ " <author>me</author>\n" |
48 |
|
+ " <version>1.1</version>\n" |
49 |
|
+ " <comment>something whitty</comment>\n" |
50 |
|
+ " <date>1293045632168</date>\n" |
51 |
|
+ " </attachment>\n" |
52 |
|
+ " <attachment serializer=\"attachment-meta/1.0\">\n" |
53 |
|
+ " <filename>file1</filename>\n" |
54 |
|
+ " <filesize>5</filesize>\n" |
55 |
|
+ " <author>you</author>\n" |
56 |
|
+ " <version>1.2</version>\n" |
57 |
|
+ " <comment>a comment</comment>\n" |
58 |
|
+ " <date>1293789456868</date>\n" |
59 |
|
+ " </attachment>\n" |
60 |
|
+ " <attachment serializer=\"attachment-meta/1.0\">\n" |
61 |
|
+ " <filename>file1</filename>\n" |
62 |
|
+ " <filesize>20</filesize>\n" |
63 |
|
+ " <author>them</author>\n" |
64 |
|
+ " <version>1.3</version>\n" |
65 |
|
+ " <comment>i saved it</comment>\n" |
66 |
|
+ " <date>1293012345668</date>\n" |
67 |
|
+ " </attachment>\n" |
68 |
|
+ "</attachment-list>"; |
69 |
|
|
70 |
|
private AttachmentListMetadataSerializer serializer; |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
2 |
@Before... |
73 |
|
public void setUp() |
74 |
|
{ |
75 |
2 |
this.serializer = new AttachmentListMetadataSerializer(new AttachmentMetadataSerializer()); |
76 |
|
} |
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
78 |
1 |
@Test... |
79 |
|
public void testParse() throws Exception |
80 |
|
{ |
81 |
1 |
final ByteArrayInputStream bais = new ByteArrayInputStream(TEST_CONTENT.getBytes("US-ASCII")); |
82 |
1 |
final List<XWikiAttachment> attachList = this.serializer.parse(bais); |
83 |
1 |
bais.close(); |
84 |
1 |
Assert.assertTrue("Attachment list was wrong size", 3 == attachList.size()); |
85 |
|
|
86 |
1 |
Assert.assertEquals("Attachment1 had wrong name", "file1", attachList.get(0).getFilename()); |
87 |
1 |
Assert.assertEquals("Attachment2 had wrong name", "file1", attachList.get(1).getFilename()); |
88 |
1 |
Assert.assertEquals("Attachment3 had wrong name", "file1", attachList.get(2).getFilename()); |
89 |
|
|
90 |
1 |
Assert.assertEquals("Attachment1 had wrong author", "me", attachList.get(0).getAuthor()); |
91 |
1 |
Assert.assertEquals("Attachment2 had wrong author", "you", attachList.get(1).getAuthor()); |
92 |
1 |
Assert.assertEquals("Attachment3 had wrong author", "them", attachList.get(2).getAuthor()); |
93 |
|
|
94 |
1 |
Assert.assertEquals("Attachment1 had wrong version", "1.1", attachList.get(0).getVersion()); |
95 |
1 |
Assert.assertEquals("Attachment2 had wrong version", "1.2", attachList.get(1).getVersion()); |
96 |
1 |
Assert.assertEquals("Attachment3 had wrong version", "1.3", attachList.get(2).getVersion()); |
97 |
|
|
98 |
1 |
Assert.assertEquals("Attachment1 had wrong comment", |
99 |
|
attachList.get(0).getComment(), |
100 |
|
"something whitty"); |
101 |
1 |
Assert.assertEquals("Attachment2 had wrong comment", "a comment", attachList.get(1).getComment()); |
102 |
1 |
Assert.assertEquals("Attachment3 had wrong comment", "i saved it", attachList.get(2).getComment()); |
103 |
|
|
104 |
|
|
105 |
1 |
Assert.assertEquals("Attachment1 had wrong date.", |
106 |
|
attachList.get(0).getDate().getTime() + "", |
107 |
|
"1293045632000"); |
108 |
1 |
Assert.assertEquals("Attachment2 had wrong date.", |
109 |
|
attachList.get(1).getDate().getTime() + "", |
110 |
|
"1293789456000"); |
111 |
1 |
Assert.assertEquals("Attachment3 had wrong date.", |
112 |
|
attachList.get(2).getDate().getTime() + "", |
113 |
|
"1293012345000"); |
114 |
|
} |
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
116 |
1 |
@Test... |
117 |
|
public void testParseSerialize() throws Exception |
118 |
|
{ |
119 |
1 |
final ByteArrayInputStream bais = new ByteArrayInputStream(TEST_CONTENT.getBytes("US-ASCII")); |
120 |
1 |
final List<XWikiAttachment> attachList = this.serializer.parse(bais); |
121 |
1 |
bais.close(); |
122 |
|
|
123 |
1 |
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
124 |
1 |
IOUtils.copy(this.serializer.serialize(attachList), baos); |
125 |
1 |
final String test = new String(baos.toByteArray(), "US-ASCII"); |
126 |
1 |
final String control = TEST_CONTENT.replaceAll("[0-9][0-9][0-9]</date>", "000</date>"); |
127 |
1 |
Assert.assertEquals("Parsing and serializing yields a different output.", control, test); |
128 |
|
} |
129 |
|
} |