1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.repository.local

File DefaultExtensionSerializerTest.java

 

Code metrics

6
61
2
1
165
115
5
0.08
30.5
2
2.5

Classes

Class Line # Actions
DefaultExtensionSerializerTest 53 61 0% 5 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.extension.repository.local;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.ByteArrayOutputStream;
24    import java.util.ArrayList;
25    import java.util.Arrays;
26    import java.util.Collections;
27    import java.util.Date;
28    import java.util.HashSet;
29   
30    import javax.xml.parsers.ParserConfigurationException;
31    import javax.xml.transform.TransformerException;
32   
33    import org.junit.jupiter.api.Test;
34    import org.xwiki.component.manager.ComponentLookupException;
35    import org.xwiki.extension.AbstractExtensionTest;
36    import org.xwiki.extension.DefaultExtensionIssueManagement;
37    import org.xwiki.extension.DefaultExtensionScm;
38    import org.xwiki.extension.DefaultExtensionScmConnection;
39    import org.xwiki.extension.ExtensionId;
40    import org.xwiki.extension.InvalidExtensionException;
41    import org.xwiki.extension.internal.ExtensionFactory;
42    import org.xwiki.extension.repository.internal.DefaultExtensionSerializer;
43    import org.xwiki.extension.repository.internal.local.DefaultLocalExtension;
44    import org.xwiki.test.annotation.ComponentList;
45    import org.xwiki.test.junit5.mockito.ComponentTest;
46    import org.xwiki.test.junit5.mockito.InjectMockComponents;
47   
48    import static org.junit.jupiter.api.Assertions.assertEquals;
49    import static org.junit.jupiter.api.Assertions.assertNull;
50   
51    @ComponentTest
52    @ComponentList(ExtensionFactory.class)
 
53    public class DefaultExtensionSerializerTest
54    {
55    @InjectMockComponents
56    private DefaultExtensionSerializer serializer;
57   
 
58  3 toggle private DefaultLocalExtension serializeAndUnserialize(DefaultLocalExtension extension)
59    throws ParserConfigurationException, TransformerException, InvalidExtensionException
60    {
61  3 ByteArrayOutputStream os = new ByteArrayOutputStream();
62   
63  3 this.serializer.saveExtensionDescriptor(extension, os);
64   
65  3 ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
66   
67  3 DefaultLocalExtension unserializedExtension = this.serializer.loadLocalExtensionDescriptor(null, is);
68   
69  3 assertEquals(extension, unserializedExtension);
70  3 assertEquals(extension.getDescription(), unserializedExtension.getDescription());
71  3 assertEquals(extension.getName(), unserializedExtension.getName());
72  3 assertEquals(extension.getCategory(), unserializedExtension.getCategory());
73  3 assertEquals(extension.getSummary(), unserializedExtension.getSummary());
74  3 assertEquals(extension.getWebSite(), unserializedExtension.getWebSite());
75  3 assertEquals(extension.getAuthors(), unserializedExtension.getAuthors());
76  3 assertEquals(new ArrayList<>(extension.getExtensionFeatures()),
77    new ArrayList<>(unserializedExtension.getExtensionFeatures()));
78  3 assertEquals(new ArrayList<>(extension.getFeatures()), new ArrayList<>(unserializedExtension.getFeatures()));
79  3 if (extension.getAllowedNamespaces() != null) {
80  1 assertEquals(new ArrayList<>(extension.getAllowedNamespaces()),
81    new ArrayList<>(unserializedExtension.getAllowedNamespaces()));
82    } else {
83  2 assertNull(unserializedExtension.getAllowedNamespaces());
84    }
85  3 assertEquals(new ArrayList<>(extension.getLicenses()), new ArrayList<>(unserializedExtension.getLicenses()));
86  3 assertEquals(extension.getScm(), unserializedExtension.getScm());
87  3 assertEquals(extension.getIssueManagement(), unserializedExtension.getIssueManagement());
88  3 assertEquals(extension.getProperties(), unserializedExtension.getProperties());
89   
90  3 assertEquals(extension.getDependencies().size(), unserializedExtension.getDependencies().size());
91  6 for (int i = 0; i < extension.getDependencies().size(); ++i) {
92  3 assertEquals(extension.getDependencies().get(i), unserializedExtension.getDependencies().get(i));
93  3 assertEquals(extension.getDependencies().get(i).getProperties(),
94    unserializedExtension.getDependencies().get(i).getProperties());
95    }
96   
97  3 assertEquals(extension.getManagedDependencies().size(), unserializedExtension.getManagedDependencies().size());
98  5 for (int i = 0; i < extension.getManagedDependencies().size(); ++i) {
99  2 assertEquals(extension.getManagedDependencies().get(i),
100    unserializedExtension.getManagedDependencies().get(i));
101  2 assertEquals(extension.getManagedDependencies().get(i).getProperties(),
102    unserializedExtension.getManagedDependencies().get(i).getProperties());
103    }
104   
105  3 return unserializedExtension;
106    }
107   
108    // Tests
109   
 
110  1 toggle @Test
111    public void testSerializeAndUnserialize()
112    throws ParserConfigurationException, TransformerException, InvalidExtensionException, ComponentLookupException
113    {
114  1 DefaultLocalExtension extension =
115    new DefaultLocalExtension(null, new ExtensionId("extensionid", "extensionversion"), "type");
116   
117    // Minimum extension
118  1 serializeAndUnserialize(extension);
119   
120  1 extension.addDependency(AbstractExtensionTest.DEPENDENCY1);
121   
122    // Minimum extension with minimum dependency
123  1 serializeAndUnserialize(extension);
124   
125  1 extension.addDependency(AbstractExtensionTest.DEPENDENCY2);
126   
127  1 AbstractExtensionTest.DEPENDENCY1
128    .setProperties(Collections.<String, Object>singletonMap("dependencykey", "dependencyvalue"));
129   
130  1 extension.addManagedDependency(AbstractExtensionTest.DEPENDENCY1);
131  1 extension.addManagedDependency(AbstractExtensionTest.DEPENDENCY2);
132   
133  1 extension.setDescription("description");
134  1 extension.setSummary("summary");
135  1 extension.setWebsite("website");
136  1 extension.setName("name");
137   
138  1 extension.setCategory("category");
139   
140  1 extension.putProperty("key1", "value1");
141  1 extension.putProperty("key2", true);
142  1 extension.putProperty("key3", false);
143  1 extension.putProperty("key4", 42);
144  1 extension.putProperty("key5", Arrays.asList("list1", "list2"));
145  1 extension.putProperty("key6", new HashSet<String>(Arrays.asList("list1", "list2")));
146  1 extension.putProperty("key7", Collections.singletonMap("key", "value"));
147  1 extension.putProperty("key8", Collections.singletonMap("key", Collections.singletonMap("subkey", "subvalue")));
148  1 extension.putProperty("key9", new Date(0));
149   
150  1 extension.addAuthor(AbstractExtensionTest.AUTHOR1);
151  1 extension.addAuthor(AbstractExtensionTest.AUTHOR2);
152  1 extension.addFeature("feature1");
153  1 extension.addExtensionFeature(new ExtensionId("feature2", "version"));
154  1 extension.addAllowedNamespace("namespae1");
155  1 extension.addLicense(AbstractExtensionTest.LICENSE1);
156  1 extension.addLicense(AbstractExtensionTest.LICENSE2);
157   
158  1 extension.setScm(new DefaultExtensionScm("url", new DefaultExtensionScmConnection("system", "path"),
159    new DefaultExtensionScmConnection("system2", "path2")));
160  1 extension.setIssueManagement(new DefaultExtensionIssueManagement("system", "url"));
161   
162    // Complete extension
163  1 serializeAndUnserialize(extension);
164    }
165    }