1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.test

File MockitoRepositoryUtils.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
29
5
1
126
76
9
0.31
5.8
5
1.8

Classes

Class Line # Actions
MockitoRepositoryUtils 40 29 0% 9 1
0.9761904597.6%
 

Contributing tests

No tests hitting this source file were found.

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.test;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import org.mockito.Mockito;
26    import org.xwiki.component.annotation.ComponentAnnotationLoader;
27    import org.xwiki.component.descriptor.ComponentDescriptor;
28    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
29    import org.xwiki.configuration.internal.MemoryConfigurationSource;
30    import org.xwiki.environment.Environment;
31    import org.xwiki.extension.handler.ExtensionInitializer;
32    import org.xwiki.extension.repository.CoreExtensionRepository;
33    import org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor;
34    import org.xwiki.extension.repository.ExtensionRepositoryManager;
35    import org.xwiki.extension.version.internal.DefaultVersion;
36    import org.xwiki.test.mockito.MockitoComponentManagerRule;
37   
38    import static org.mockito.ArgumentMatchers.any;
39   
 
40    public class MockitoRepositoryUtils extends RepositoryUtils
41    {
42    protected final MockitoComponentManagerRule componentManager;
43   
44    private FileExtensionRepository remoteRepository;
45   
46    private ComponentAnnotationLoader componentLoader;
47   
 
48  160 toggle public MockitoRepositoryUtils(MockitoComponentManagerRule componentManager)
49    {
50  160 this.componentManager = componentManager;
51    }
52   
 
53  160 toggle @Override
54    public void setup() throws Exception
55    {
56  160 super.setup();
57   
58    // Disable default repositories
59  160 MemoryConfigurationSource memoryConfigurationSource = this.componentManager.registerMemoryConfigurationSource();
60  160 memoryConfigurationSource.setProperty("extension.repositories", Arrays.asList(""));
61   
62  160 final Environment environment = this.componentManager.registerMockComponent(Environment.class);
63  160 Mockito.when(environment.getPermanentDirectory()).thenReturn(getPermanentDirectory());
64  160 Mockito.when(environment.getTemporaryDirectory()).thenReturn(getTemporaryDirectory());
65  160 Mockito.when(environment.getResourceAsStream(any())).thenReturn(null);
66   
67  160 DefaultComponentDescriptor<Environment> dcd = new DefaultComponentDescriptor<Environment>();
68  160 dcd.setRoleType(Environment.class);
69  160 this.componentManager.registerComponent(dcd, environment);
70   
71    // add default test core extension
72  160 registerComponent(ConfigurableDefaultCoreExtensionRepository.class);
73  160 ((ConfigurableDefaultCoreExtensionRepository) this.componentManager.getInstance(CoreExtensionRepository.class))
74    .addExtensions("coreextension", new DefaultVersion("version"));
75   
76    // register repositories
77  160 ExtensionRepositoryManager repositoryManager =
78    this.componentManager.getInstance(ExtensionRepositoryManager.class);
79   
80    // light remote repository
81   
82  160 if (copyResourceFolder(getRemoteRepository(), "repository.remote") > 0) {
83  138 this.remoteRepository = new FileExtensionRepository(getRemoteRepository(), this.componentManager);
84  138 repositoryManager.addRepository(this.remoteRepository);
85    }
86   
87    // maven repositories
88   
89  160 if (getMavenRepository().exists()) {
90  20 repositoryManager.addRepository(new DefaultExtensionRepositoryDescriptor(MAVENREPOSITORY_ID, "maven",
91    getMavenRepository().toURI()));
92    }
93   
94  160 if (getMaven2Repository().exists()) {
95  20 repositoryManager.addRepository(new DefaultExtensionRepositoryDescriptor(MAVEN2REPOSITORY_ID, "maven",
96    getMaven2Repository().toURI()));
97    }
98   
99    // init
100   
101  160 this.componentManager.<ExtensionInitializer>getInstance(ExtensionInitializer.class).initialize();
102    }
103   
 
104  172 toggle public MockitoComponentManagerRule getComponentManager()
105    {
106  172 return this.componentManager;
107    }
108   
 
109  160 toggle public ComponentAnnotationLoader getComponentLoader()
110    {
111  160 if (this.componentLoader == null) {
112  160 this.componentLoader = new ComponentAnnotationLoader();
113    }
114   
115  160 return this.componentLoader;
116    }
117   
 
118  160 toggle private void registerComponent(Class<?> componentClass) throws Exception
119    {
120  160 List<ComponentDescriptor> descriptors = getComponentLoader().getComponentsDescriptors(componentClass);
121   
122  160 for (ComponentDescriptor<?> descriptor : descriptors) {
123  160 this.componentManager.registerComponent(descriptor);
124    }
125    }
126    }