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

File AbstractExtensionTest.java

 

Code metrics

0
57
9
2
195
126
9
0.16
6.33
4.5
1

Classes

Class Line # Actions
AbstractExtensionTest 40 53 0% 7 0
1.0100%
AbstractExtensionTest.TestExtension 68 4 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 6 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.extension;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24   
25    import org.junit.jupiter.api.BeforeEach;
26    import org.junit.jupiter.api.Test;
27    import org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor;
28    import org.xwiki.extension.repository.ExtensionRepository;
29    import org.xwiki.extension.repository.ExtensionRepositoryDescriptor;
30    import org.xwiki.extension.version.internal.DefaultVersionConstraint;
31   
32    import static org.junit.jupiter.api.Assertions.assertEquals;
33    import static org.junit.jupiter.api.Assertions.assertNotEquals;
34    import static org.junit.jupiter.api.Assertions.assertSame;
35    import static org.mockito.Mockito.mock;
36   
37    /**
38    * @version $Id: b440730eeb538434d3e109b773c8391c9e819377 $
39    */
 
40    public class AbstractExtensionTest
41    {
42    public static final ExtensionRepositoryDescriptor DESCRIPTOR1 = new DefaultExtensionRepositoryDescriptor("id1");
43   
44    public static final ExtensionRepositoryDescriptor DESCRIPTOR2 = new DefaultExtensionRepositoryDescriptor("id2");
45   
46    public static final DefaultExtensionAuthor AUTHOR1 = new DefaultExtensionAuthor("name1", "http://url1");
47   
48    public static final DefaultExtensionAuthor AUTHOR2 = new DefaultExtensionAuthor("name1", "http://url2");
49   
50    public static final ExtensionLicense LICENSE1 = new ExtensionLicense("license1", Arrays.asList("content1"));
51   
52    public static final ExtensionLicense LICENSE2 = new ExtensionLicense("license2", Arrays.asList("content2"));
53   
54    public static final DefaultExtensionDependency DEPENDENCY1 =
55    new DefaultExtensionDependency("id1", new DefaultVersionConstraint("version1"));
56   
57    public static final DefaultExtensionDependency DEPENDENCY2 =
58    new DefaultExtensionDependency("id2", new DefaultVersionConstraint("version2"));
59   
60    private AbstractExtension extension;
61   
62    private ExtensionRepository repository;
63   
64    private ExtensionId id;
65   
66    private String type;
67   
 
68    public static class TestExtension extends AbstractExtension
69    {
 
70  37 toggle public TestExtension(ExtensionId id, String type, ExtensionId... features)
71    {
72  37 super(null, id, type);
73   
74  37 for (ExtensionId feature : features) {
75  12 addExtensionFeature(feature);
76    }
77    }
78   
 
79  2 toggle public TestExtension(Extension extension)
80    {
81  2 super(null, extension);
82    }
83    }
84   
 
85  25 toggle private AbstractExtension toExtension(String id, String version, ExtensionId... features)
86    {
87  25 return new TestExtension(new ExtensionId(id, version), "test", features);
88    }
89   
 
90  11 toggle private void assertCompareTo(int comparizon, Extension e1, Extension e2)
91    {
92  11 assertEquals(comparizon, e1.compareTo(e2));
93    }
94   
95    // Tests
96   
 
97  4 toggle @BeforeEach
98    public void beforeEach()
99    {
100  4 this.repository = mock(ExtensionRepository.class);
101  4 this.id = new ExtensionId("extesionid", "extensionversion");
102  4 this.type = "extensiontype";
103   
104  4 this.extension = new AbstractExtension(this.repository, this.id, this.type)
105    {
106    };
107    }
108   
 
109  1 toggle @Test
110    public void get()
111    {
112  1 assertSame(this.repository, this.extension.get("repository"));
113  1 assertEquals(this.id.getId(), this.extension.get("id"));
114  1 assertEquals(this.id.getVersion(), this.extension.get("version"));
115  1 assertEquals(this.type, this.extension.get("type"));
116    }
117   
 
118  1 toggle @Test
119    public void compareTo()
120    {
121  1 assertCompareTo(0, toExtension("id", "2.0"), toExtension("id", "2.0"));
122  1 assertCompareTo(-1, toExtension("id", "2.0"), toExtension("id", "3.0"));
123  1 assertCompareTo(1, toExtension("id", "2.0"), toExtension("id", "1.0"));
124   
125  1 assertCompareTo(0, toExtension("feature", "2.0"), toExtension("id", "2.0", new ExtensionId("feature", "2.0")));
126  1 assertCompareTo(-1, toExtension("feature", "2.0"), toExtension("id", "2.0", new ExtensionId("feature", "3.0")));
127  1 assertCompareTo(1, toExtension("feature", "2.0"), toExtension("id", "2.0", new ExtensionId("feature", "1.0")));
128   
129  1 assertCompareTo(0, toExtension("id", "2.0", new ExtensionId("feature", "2.0")), toExtension("feature", "2.0"));
130  1 assertCompareTo(-1, toExtension("id", "2.0", new ExtensionId("feature", "2.0")), toExtension("feature", "3.0"));
131  1 assertCompareTo(1, toExtension("id", "2.0", new ExtensionId("feature", "2.0")), toExtension("feature", "1.0"));
132   
133  1 assertCompareTo(-1, toExtension("id", "1.0"), toExtension("id2", "1.0"));
134   
135  1 assertCompareTo(-1, toExtension("id", "1.0"), null);
136    }
137   
 
138  1 toggle @Test
139    public void set()
140    {
141  1 AbstractExtension extension = toExtension("id", "version", new ExtensionId("feature", "featureversion"));
142   
143  1 extension.addAuthor(AUTHOR1);
144  1 extension.addAuthor(AUTHOR2);
145   
146  1 extension.addRepository(DESCRIPTOR1);
147  1 extension.addRepository(DESCRIPTOR2);
148   
149  1 extension.addLicense(LICENSE1);
150  1 extension.addLicense(LICENSE2);
151   
152  1 extension.addDependency(DEPENDENCY1);
153  1 extension.addDependency(DEPENDENCY2);
154   
155  1 extension.addManagedDependency(DEPENDENCY1);
156  1 extension.addManagedDependency(DEPENDENCY2);
157   
158  1 extension.addAllowedNamespace("namespace1");
159  1 extension.addAllowedNamespace("namespace2");
160   
161  1 AbstractExtension cloneExtension = new TestExtension(extension);
162   
163  1 assertEquals(extension, cloneExtension);
164  1 assertEquals(extension.getId(), cloneExtension.getId());
165  1 assertEquals(new ArrayList<>(extension.getExtensionFeatures()),
166    new ArrayList<>(cloneExtension.getExtensionFeatures()));
167  1 assertEquals(Arrays.asList(DESCRIPTOR1, DESCRIPTOR2), cloneExtension.getRepositories());
168  1 assertEquals(Arrays.asList(AUTHOR1, AUTHOR2), cloneExtension.getAuthors());
169  1 assertEquals(Arrays.asList(LICENSE1, LICENSE2), cloneExtension.getLicenses());
170  1 assertEquals(Arrays.asList(DEPENDENCY1, DEPENDENCY2), cloneExtension.getDependencies());
171  1 assertEquals(Arrays.asList(DEPENDENCY1, DEPENDENCY2), cloneExtension.getManagedDependencies());
172  1 assertEquals(Arrays.asList("namespace1", "namespace2"), new ArrayList<>(cloneExtension.getAllowedNamespaces()));
173    }
174   
 
175  1 toggle @Test
176    public void equals()
177    {
178  1 AbstractExtension extension = toExtension("id", "version");
179  1 AbstractExtension cloneExtension = new TestExtension(extension);
180   
181  1 assertEquals(extension, cloneExtension);
182   
183  1 cloneExtension.setCategory("othercategory");
184   
185  1 assertEquals(extension, cloneExtension);
186   
187  1 AbstractExtension extensionWithDifferentId = toExtension("otherid", "version");
188   
189  1 assertNotEquals(extension, extensionWithDifferentId);
190   
191  1 AbstractExtension extensionWithDifferentVersion = toExtension("id", "otherversion");
192   
193  1 assertNotEquals(extension, extensionWithDifferentVersion);
194    }
195    }