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

File ModelConverterTest.java

 

Code metrics

0
38
3
1
133
94
3
0.08
12.67
3
1

Classes

Class Line # Actions
ModelConverterTest 58 38 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.extension.maven.internal.converter;
21   
22    import java.net.URI;
23    import java.net.URISyntaxException;
24    import java.util.ArrayList;
25    import java.util.Arrays;
26    import java.util.List;
27   
28    import org.apache.commons.collections4.ListUtils;
29    import org.apache.maven.model.Dependency;
30    import org.apache.maven.model.Model;
31    import org.apache.maven.model.Repository;
32    import org.junit.Rule;
33    import org.junit.Test;
34    import org.xwiki.component.manager.ComponentLookupException;
35    import org.xwiki.extension.Extension;
36    import org.xwiki.extension.ExtensionDependency;
37    import org.xwiki.extension.ExtensionFeaturesInjector;
38    import org.xwiki.extension.ExtensionId;
39    import org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor;
40    import org.xwiki.properties.ConverterManager;
41    import org.xwiki.properties.internal.DefaultConverterManager;
42    import org.xwiki.test.annotation.AllComponents;
43    import org.xwiki.test.annotation.BeforeComponent;
44    import org.xwiki.test.mockito.MockitoComponentMockingRule;
45   
46    import static org.junit.Assert.assertEquals;
47    import static org.junit.Assert.assertNull;
48    import static org.junit.Assert.assertTrue;
49    import static org.mockito.ArgumentMatchers.any;
50    import static org.mockito.Mockito.when;
51   
52    /**
53    * Validate {@link ModelConverter} component.
54    *
55    * @version $Id: f2000e4f82da44381d66b5046b793c841916b166 $
56    */
57    @AllComponents
 
58    public class ModelConverterTest
59    {
60    private static final List<ExtensionId> INJECTED_FEATURES =
61    Arrays.asList(new ExtensionId("injectedfeature1", "injectedversion1"),
62    new ExtensionId("injectedfeature2", "injectedversion2"));
63   
64    @Rule
65    public MockitoComponentMockingRule<ConverterManager> mocker =
66    new MockitoComponentMockingRule<ConverterManager>(DefaultConverterManager.class);
67   
 
68  2 toggle @BeforeComponent
69    public void beforeComponent() throws Exception
70    {
71  2 ExtensionFeaturesInjector featureProvider =
72    this.mocker.registerMockComponent(ExtensionFeaturesInjector.class, "test");
73   
74  2 when(featureProvider.getFeatures(any(Extension.class))).thenReturn(INJECTED_FEATURES);
75    }
76   
 
77  1 toggle @Test
78    public void testConvertFromExtension() throws SecurityException, ComponentLookupException, URISyntaxException
79    {
80  1 Model model = new Model();
81   
82  1 model.setGroupId("groupid");
83  1 model.setArtifactId("artifactid");
84  1 model.setVersion("version");
85  1 model.addProperty(Extension.IKEYPREFIX + Extension.FIELD_CATEGORY, "category");
86  1 model.addProperty(Extension.IKEYPREFIX + Extension.FIELD_NAMESPACES,
87    "namespace1, namespace2,\r\n\t {root}, \"namespace3\", 'namespace4'");
88  1 model.addProperty(Extension.IKEYPREFIX + Extension.FIELD_FEATURES, "feature1/1.0");
89  1 Repository repository = new Repository();
90  1 repository.setId("repository-id");
91  1 repository.setUrl("http://url");
92  1 model.addRepository(repository);
93  1 Dependency dependency = new Dependency();
94  1 dependency.setGroupId("dgroupid");
95  1 dependency.setArtifactId("dartifactId");
96  1 dependency.setVersion("1.0");
97  1 dependency.setOptional(true);
98  1 model.addDependency(dependency);
99   
100  1 Extension extension = this.mocker.getComponentUnderTest().convert(Extension.class, model);
101   
102  1 assertEquals(model.getGroupId() + ':' + model.getArtifactId(), extension.getId().getId());
103  1 assertEquals(model.getVersion(), extension.getId().getVersion().getValue());
104  1 assertEquals(ListUtils.union(Arrays.asList(new ExtensionId("feature1", "1.0")), INJECTED_FEATURES),
105    new ArrayList<>(extension.getExtensionFeatures()));
106  1 assertEquals("category", extension.getCategory());
107  1 assertNull(extension.getProperty(Extension.IKEYPREFIX + Extension.FIELD_CATEGORY));
108  1 assertEquals(Arrays.asList("namespace1", "namespace2", "{root}", "namespace3", "namespace4"),
109    new ArrayList<>(extension.getAllowedNamespaces()));
110  1 assertNull(extension.getProperty(Extension.IKEYPREFIX + Extension.FIELD_NAMESPACES));
111  1 assertEquals(
112    Arrays.asList(new DefaultExtensionRepositoryDescriptor("repository-id", "maven", new URI("http://url"))),
113    extension.getRepositories());
114  1 assertEquals(1, extension.getDependencies().size());
115  1 ExtensionDependency extensionDependency = extension.getDependencies().iterator().next();
116  1 assertTrue(extensionDependency.isOptional());
117    }
118   
 
119  1 toggle @Test
120    public void testConvertFromExtensionAllowedOnRoot() throws SecurityException, ComponentLookupException
121    {
122  1 Model model = new Model();
123   
124  1 model.setGroupId("groupid");
125  1 model.setArtifactId("artifactid");
126  1 model.setVersion("version");
127  1 model.addProperty(Extension.IKEYPREFIX + Extension.FIELD_NAMESPACES, "{root}");
128   
129  1 Extension extension = this.mocker.getComponentUnderTest().convert(Extension.class, model);
130   
131  1 assertEquals(Arrays.asList("{root}"), new ArrayList<>(extension.getAllowedNamespaces()));
132    }
133    }