You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MockConfiguration.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package org.apache.archiva.maven.proxy;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.configuration.*;
  20. import org.apache.archiva.components.registry.Registry;
  21. import org.apache.archiva.components.registry.RegistryException;
  22. import org.apache.archiva.components.registry.RegistryListener;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.mockito.Mockito;
  25. import org.springframework.stereotype.Service;
  26. import javax.annotation.PostConstruct;
  27. import java.nio.file.Path;
  28. import java.nio.file.Paths;
  29. import java.util.*;
  30. import static org.mockito.Mockito.mock;
  31. /**
  32. * MockConfiguration
  33. *
  34. *
  35. */
  36. @Service( "archivaConfiguration#mock" )
  37. public class MockConfiguration
  38. implements ArchivaConfiguration
  39. {
  40. private Configuration configuration = new Configuration();
  41. private Set<RegistryListener> registryListeners = new HashSet<RegistryListener>();
  42. private Set<ConfigurationListener> configListeners = new HashSet<ConfigurationListener>();
  43. private Registry registryMock;
  44. public MockConfiguration()
  45. {
  46. registryMock = mock( Registry.class );
  47. }
  48. @PostConstruct
  49. public void initialize()
  50. throws Exception
  51. {
  52. configuration.setRepositoryScanning( new RepositoryScanningConfiguration()
  53. {
  54. @Override
  55. public List<FileType> getFileTypes()
  56. {
  57. FileType fileType = new FileType();
  58. fileType.setId( FileTypes.ARTIFACTS );
  59. fileType.setPatterns( Collections.singletonList( "**/*" ) );
  60. return Collections.singletonList( fileType );
  61. }
  62. } );
  63. ArchivaRuntimeConfiguration rt = new ArchivaRuntimeConfiguration();
  64. List<String> checksums = new ArrayList<>();
  65. checksums.add("MD5");
  66. checksums.add("SHA1");
  67. checksums.add("SHA256");
  68. rt.setChecksumTypes(checksums);
  69. configuration.setArchivaRuntimeConfiguration(rt);
  70. }
  71. @Override
  72. public void addChangeListener( org.apache.archiva.components.registry.RegistryListener listener )
  73. {
  74. registryListeners.add( listener );
  75. }
  76. @Override
  77. public void removeChangeListener( RegistryListener listener )
  78. {
  79. registryListeners.remove( listener );
  80. }
  81. @Override
  82. public Configuration getConfiguration()
  83. {
  84. return configuration;
  85. }
  86. @Override
  87. public void save( Configuration configuration )
  88. throws RegistryException
  89. {
  90. /* do nothing */
  91. }
  92. @Override
  93. public void save( Configuration configuration, String eventTag ) throws RegistryException, IndeterminateConfigurationException
  94. {
  95. // do nothing
  96. }
  97. public void triggerChange( String name, String value )
  98. {
  99. for ( org.apache.archiva.components.registry.RegistryListener listener : registryListeners )
  100. {
  101. try
  102. {
  103. listener.afterConfigurationChange( registryMock, name, value );
  104. }
  105. catch ( Exception e )
  106. {
  107. e.printStackTrace();
  108. }
  109. }
  110. for (ConfigurationListener listener : configListeners) {
  111. listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.CHANGED ) );
  112. }
  113. }
  114. @Override
  115. public void addListener( ConfigurationListener listener )
  116. {
  117. configListeners.add( listener );
  118. }
  119. @Override
  120. public void removeListener( ConfigurationListener listener )
  121. {
  122. configListeners.remove( listener );
  123. }
  124. @Override
  125. public boolean isDefaulted()
  126. {
  127. return false;
  128. }
  129. @Override
  130. public void reload()
  131. {
  132. // no op
  133. }
  134. @Override
  135. public Locale getDefaultLocale( )
  136. {
  137. return Locale.getDefault();
  138. }
  139. @Override
  140. public List<Locale.LanguageRange> getLanguagePriorities( )
  141. {
  142. return Locale.LanguageRange.parse( "en,fr,de" );
  143. }
  144. @Override
  145. public Path getAppServerBaseDir() {
  146. if (System.getProperties().containsKey("appserver.base")) {
  147. return Paths.get(System.getProperty("appserver.base"));
  148. } else {
  149. return Paths.get("");
  150. }
  151. }
  152. @Override
  153. public Path getRepositoryBaseDir() {
  154. return getDataDirectory().resolve("repositories");
  155. }
  156. @Override
  157. public Path getRemoteRepositoryBaseDir() {
  158. return getDataDirectory().resolve("remotes");
  159. }
  160. @Override
  161. public Path getRepositoryGroupBaseDir() {
  162. return getDataDirectory().resolve("groups");
  163. }
  164. @Override
  165. public Path getDataDirectory() {
  166. if (configuration!=null && StringUtils.isNotEmpty(configuration.getArchivaRuntimeConfiguration().getDataDirectory())) {
  167. return Paths.get(configuration.getArchivaRuntimeConfiguration().getDataDirectory());
  168. } else {
  169. return getAppServerBaseDir().resolve("data");
  170. }
  171. }
  172. @Override
  173. public Registry getRegistry( )
  174. {
  175. return null;
  176. }
  177. }