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.8KB

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