1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.store.serialization.xml.internal

File AttachmentListMetadataSerializerTest.java

 

Code metrics

0
28
3
1
129
89
3
0.11
9.33
3
1

Classes

Class Line # Actions
AttachmentListMetadataSerializerTest 39 28 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 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.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    * Tests for AttachmentListMetadataSerializer
35    *
36    * @version $Id: b087ed1ea40bcca2e69b3d8ac822887c691a11eb $
37    * @since 3.0M2
38    */
 
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   
 
72  2 toggle @Before
73    public void setUp()
74    {
75  2 this.serializer = new AttachmentListMetadataSerializer(new AttachmentMetadataSerializer());
76    }
77   
 
78  1 toggle @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    // We drop milliseconds for consistency with the database so last 3 digits are 0.
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   
 
116  1 toggle @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    }