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

File DefaultExtensionRepositoryManagerTest.java

 

Code metrics

0
55
11
1
205
139
11
0.2
5
11
1

Classes

Class Line # Actions
DefaultExtensionRepositoryManagerTest 51 55 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 10 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.repository;
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.Collection;
27    import java.util.Iterator;
28   
29    import org.junit.Assert;
30    import org.junit.Before;
31    import org.junit.Rule;
32    import org.junit.Test;
33    import org.xwiki.component.manager.ComponentLookupException;
34    import org.xwiki.extension.DefaultExtensionDependency;
35    import org.xwiki.extension.Extension;
36    import org.xwiki.extension.ResolveException;
37    import org.xwiki.extension.repository.internal.DefaultExtensionRepositoryManager;
38    import org.xwiki.extension.version.internal.DefaultVersionConstraint;
39    import org.xwiki.test.mockito.MockitoComponentMockingRule;
40   
41    import static org.junit.Assert.assertSame;
42    import static org.mockito.ArgumentMatchers.same;
43    import static org.mockito.Mockito.mock;
44    import static org.mockito.Mockito.when;
45   
46    /**
47    * Unit tests for {@link DefaultExtensionRepositoryManager}.
48    *
49    * @version $Id: ca7d2044097e96b4616e5ed654ba239a4aa84757 $
50    */
 
51    public class DefaultExtensionRepositoryManagerTest
52    {
53    @Rule
54    public MockitoComponentMockingRule<ExtensionRepositoryManager> mocker =
55    new MockitoComponentMockingRule<ExtensionRepositoryManager>(DefaultExtensionRepositoryManager.class);
56   
57    private ExtensionRepository testRepository;
58   
59    private ExtensionRepositoryDescriptor descriptor;
60   
61    private ExtensionRepositoryDescriptor unsupportedDescriptor;
62   
63    private DefaultExtensionDependency dependency;
64   
65    private Extension testExtension;
66   
67    private ExtensionRepositoryFactory factory;
68   
 
69  10 toggle @Before
70    public void before() throws Exception
71    {
72  10 this.factory = this.mocker.registerMockComponent(ExtensionRepositoryFactory.class, "test");
73  10 this.testRepository = mock(ExtensionRepository.class);
74   
75  10 this.descriptor =
76    new DefaultExtensionRepositoryDescriptor("id", "test", new URI("http", "host", "/path", "fragment"));
77   
78  10 this.unsupportedDescriptor = new DefaultExtensionRepositoryDescriptor("unsupported", "unsupported",
79    new URI("http", "unsupported", "/unsupported", "unsupported"));
80   
81  10 when(factory.createRepository(same(this.descriptor))).thenReturn(this.testRepository);
82  10 when(this.testRepository.getDescriptor()).thenReturn(this.descriptor);
83   
84  10 this.dependency = new DefaultExtensionDependency("id", new DefaultVersionConstraint("version"));
85   
86  10 this.testExtension = mock(Extension.class);
87  10 when(this.testRepository.resolve(this.dependency)).thenReturn(testExtension);
88    }
89   
90    // Tests
91   
 
92  1 toggle @Test
93    public void addRepository() throws ExtensionRepositoryException, ComponentLookupException
94    {
95  1 ExtensionRepository repository = this.mocker.getComponentUnderTest().addRepository(this.descriptor);
96   
97  1 Assert.assertSame(this.testRepository, repository);
98    }
99   
 
100  1 toggle @Test(expected = ExtensionRepositoryException.class)
101    public void addRepository_unsuported() throws ComponentLookupException, ExtensionRepositoryException
102    {
103  1 this.mocker.getComponentUnderTest().addRepository(this.unsupportedDescriptor);
104    }
105   
 
106  1 toggle @Test
107    public void getRepository() throws ExtensionRepositoryException, ComponentLookupException
108    {
109  1 this.mocker.getComponentUnderTest().addRepository(this.descriptor);
110   
111  1 ExtensionRepository repository = this.mocker.getComponentUnderTest().getRepository("id");
112   
113  1 Assert.assertSame(this.testRepository, repository);
114    }
115   
 
116  1 toggle @Test
117    public void getRepository_doesnotexists() throws ComponentLookupException
118    {
119  1 ExtensionRepository repository = this.mocker.getComponentUnderTest().getRepository("id");
120   
121  1 Assert.assertNull(repository);
122    }
123   
 
124  1 toggle @Test
125    public void removeRepository() throws ExtensionRepositoryException, ComponentLookupException
126    {
127  1 this.mocker.getComponentUnderTest().addRepository(this.descriptor);
128   
129  1 this.mocker.getComponentUnderTest().removeRepository(this.descriptor.getId());
130   
131  1 ExtensionRepository repository = this.mocker.getComponentUnderTest().getRepository("id");
132   
133  1 Assert.assertNull(repository);
134    }
135   
 
136  1 toggle @Test
137    public void getRepositories() throws ExtensionRepositoryException, ComponentLookupException
138    {
139  1 this.mocker.getComponentUnderTest().addRepository(this.descriptor);
140   
141  1 Collection<ExtensionRepository> repositorties = this.mocker.getComponentUnderTest().getRepositories();
142   
143  1 Assert.assertEquals(Arrays.asList(this.testRepository), new ArrayList<ExtensionRepository>(repositorties));
144    }
145   
 
146  1 toggle @Test(expected = ResolveException.class)
147    public void resolveDependencyWithNoRegisteredRepository() throws ResolveException, ComponentLookupException
148    {
149  1 this.mocker.getComponentUnderTest().resolve(this.dependency);
150    }
151   
 
152  1 toggle @Test
153    public void resolveDependencyWithEmbeddedRepository() throws ResolveException, ComponentLookupException
154    {
155  1 this.dependency.addRepository(this.testRepository.getDescriptor());
156   
157  1 assertSame(this.testExtension, this.mocker.getComponentUnderTest().resolve(this.dependency));
158    }
159   
 
160  1 toggle @Test
161    public void resolveDependencyWithRegisteredRepository() throws ResolveException, ComponentLookupException
162    {
163  1 this.mocker.getComponentUnderTest().addRepository(this.testRepository);
164   
165  1 assertSame(this.testExtension, this.mocker.getComponentUnderTest().resolve(this.dependency));
166    }
167   
 
168  1 toggle @Test
169    public void repositoryOrder() throws URISyntaxException, ExtensionRepositoryException, ComponentLookupException
170    {
171  1 ExtensionRepositoryDescriptor repDescriptor0 =
172    new DefaultExtensionRepositoryDescriptor("id0", "test", new URI("http", "host", "/path", "fragment"));
173  1 ExtensionRepository rep0 = mock(ExtensionRepository.class, "repo0");
174  1 when(this.factory.createRepository(same(repDescriptor0))).thenReturn(rep0);
175  1 when(rep0.getDescriptor()).thenReturn(repDescriptor0);
176  1 ExtensionRepositoryDescriptor repDescriptor1 =
177    new DefaultExtensionRepositoryDescriptor("id1", "test", new URI("http", "host", "/path", "fragment"));
178  1 ExtensionRepository rep1 = mock(ExtensionRepository.class, "repo1");
179  1 when(this.factory.createRepository(same(repDescriptor1))).thenReturn(rep1);
180  1 when(rep1.getDescriptor()).thenReturn(repDescriptor1);
181  1 ExtensionRepositoryDescriptor repDescriptor2 =
182    new DefaultExtensionRepositoryDescriptor("id2", "test", new URI("http", "host", "/path", "fragment"));
183  1 ExtensionRepository rep2 = mock(ExtensionRepository.class, "repo2");
184  1 when(this.factory.createRepository(same(repDescriptor2))).thenReturn(rep2);
185  1 when(rep2.getDescriptor()).thenReturn(repDescriptor2);
186  1 ExtensionRepositoryDescriptor repDescriptor3 =
187    new DefaultExtensionRepositoryDescriptor("id3", "test", new URI("http", "host", "/path", "fragment"));
188  1 ExtensionRepository rep3 = mock(ExtensionRepository.class, "repo3");
189  1 when(this.factory.createRepository(same(repDescriptor3))).thenReturn(rep3);
190  1 when(rep3.getDescriptor()).thenReturn(repDescriptor3);
191   
192  1 this.mocker.getComponentUnderTest().addRepository(repDescriptor3, 3);
193  1 this.mocker.getComponentUnderTest().addRepository(repDescriptor2, 2);
194  1 this.mocker.getComponentUnderTest().addRepository(repDescriptor1, 1);
195  1 this.mocker.getComponentUnderTest().addRepository(repDescriptor0, 0);
196   
197  1 Collection<ExtensionRepository> repositories = this.mocker.getComponentUnderTest().getRepositories();
198  1 Iterator<ExtensionRepository> it = repositories.iterator();
199   
200  1 assertSame(rep0, it.next());
201  1 assertSame(rep1, it.next());
202  1 assertSame(rep2, it.next());
203  1 assertSame(rep3, it.next());
204    }
205    }