1 package org.apache.maven.archiva.configuration;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.codehaus.plexus.PlexusTestCase;
23 import org.codehaus.plexus.util.FileUtils;
26 import java.util.List;
29 * Test the configuration store.
31 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
33 public class ArchivaConfigurationTest
34 extends PlexusTestCase
36 public void testGetConfigurationFromRegistryWithASingleNamedConfigurationResource()
39 ArchivaConfiguration archivaConfiguration =
40 (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-configuration" );
42 Configuration configuration = archivaConfiguration.getConfiguration();
43 assertConfiguration( configuration );
44 assertEquals( "check network proxies", 1, configuration.getNetworkProxies().size() );
46 RepositoryConfiguration repository =
47 (RepositoryConfiguration) configuration.getRepositories().iterator().next();
49 assertEquals( "check managed repositories", "file://${appserver.base}/repositories/internal",
50 repository.getUrl() );
51 assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
52 assertEquals( "check managed repositories", "internal", repository.getId() );
53 assertEquals( "check managed repositories", "default", repository.getLayout() );
54 assertTrue( "check managed repositories", repository.isIndexed() );
57 public void testGetConfigurationFromDefaults()
60 ArchivaConfiguration archivaConfiguration =
61 (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-defaults" );
63 Configuration configuration = archivaConfiguration.getConfiguration();
64 assertConfiguration( configuration );
65 assertEquals( "check network proxies", 0, configuration.getNetworkProxies().size() );
67 RepositoryConfiguration repository =
68 (RepositoryConfiguration) configuration.getRepositories().iterator().next();
70 assertEquals( "check managed repositories", "file://${appserver.base}/data/repositories/internal",
71 repository.getUrl() );
72 assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
73 assertEquals( "check managed repositories", "internal", repository.getId() );
74 assertEquals( "check managed repositories", "default", repository.getLayout() );
75 assertTrue( "check managed repositories", repository.isIndexed() );
78 private void assertConfiguration( Configuration configuration )
81 FileTypes filetypes = (FileTypes) lookup( FileTypes.class.getName() );
83 assertEquals( "check repositories", 4, configuration.getRepositories().size() );
84 assertEquals( "check proxy connectors", 2, configuration.getProxyConnectors().size() );
86 RepositoryScanningConfiguration repoScanning = configuration.getRepositoryScanning();
87 assertNotNull( "check repository scanning", repoScanning );
88 assertEquals( "check file types", 4, repoScanning.getFileTypes().size() );
89 assertEquals( "check known consumers", 8, repoScanning.getKnownContentConsumers().size() );
90 assertEquals( "check invalid consumers", 1, repoScanning.getInvalidContentConsumers().size() );
92 List patterns = filetypes.getFileTypePatterns( "artifacts" );
93 assertNotNull( "check 'artifacts' file type", patterns );
94 assertEquals( "check 'artifacts' patterns", 13, patterns.size() );
96 DatabaseScanningConfiguration dbScanning = configuration.getDatabaseScanning();
97 assertNotNull( "check database scanning", dbScanning );
98 assertEquals( "check unprocessed consumers", 6, dbScanning.getUnprocessedConsumers().size() );
99 assertEquals( "check cleanup consumers", 3, dbScanning.getCleanupConsumers().size() );
101 WebappConfiguration webapp = configuration.getWebapp();
102 assertNotNull( "check webapp", webapp );
104 UserInterfaceOptions ui = webapp.getUi();
105 assertNotNull( "check webapp ui", ui );
106 assertTrue( "check showFindArtifacts", ui.isShowFindArtifacts() );
107 assertTrue( "check appletFindEnabled", ui.isAppletFindEnabled() );
110 public void testGetConfigurationFromRegistryWithTwoConfigurationResources()
113 ArchivaConfiguration archivaConfiguration =
114 (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-configuration-both" );
116 Configuration configuration = archivaConfiguration.getConfiguration();
119 assertEquals( "check repositories", 4, configuration.getRepositories().size() );
121 assertEquals( "check proxy connectors", 2, configuration.getProxyConnectors().size() );
123 WebappConfiguration webapp = configuration.getWebapp();
124 assertNotNull( "check webapp", webapp );
126 UserInterfaceOptions ui = webapp.getUi();
127 assertNotNull( "check webapp ui", ui );
129 assertFalse( "check showFindArtifacts", ui.isShowFindArtifacts() );
131 assertFalse( "check appletFindEnabled", ui.isAppletFindEnabled() );
134 public void testGetConfigurationSystemOverride()
137 ArchivaConfiguration archivaConfiguration =
138 (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-configuration" );
140 System.setProperty( "org.apache.maven.archiva.webapp.ui.appletFindEnabled", "false" );
144 Configuration configuration = archivaConfiguration.getConfiguration();
146 assertFalse( "check boolean", configuration.getWebapp().getUi().isAppletFindEnabled() );
150 System.getProperties().remove( "org.apache.maven.archiva.webapp.ui.appletFindEnabled" );
154 public void testStoreConfiguration()
157 File file = getTestFile( "target/test/test-file.xml" );
159 assertFalse( file.exists() );
161 // TODO: remove with commons-configuration 1.4
162 file.getParentFile().mkdirs();
163 FileUtils.fileWrite( file.getAbsolutePath(), "<configuration/>" );
165 ArchivaConfiguration archivaConfiguration =
166 (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-save" );
168 Configuration configuration = new Configuration();
169 configuration.setVersion( "1" );
170 configuration.setWebapp( new WebappConfiguration() );
171 configuration.getWebapp().setUi( new UserInterfaceOptions() );
172 configuration.getWebapp().getUi().setAppletFindEnabled( false );
174 archivaConfiguration.save( configuration );
176 assertTrue( "Check file exists", file.exists() );
179 configuration = archivaConfiguration.getConfiguration();
180 assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
183 archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-read-saved" );
184 configuration = archivaConfiguration.getConfiguration();
185 assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
188 public void testStoreConfigurationUser()
191 File baseFile = getTestFile( "target/test/test-file.xml" );
193 assertFalse( baseFile.exists() );
195 File userFile = getTestFile( "target/test/test-file-user.xml" );
197 assertFalse( userFile.exists() );
199 userFile.getParentFile().mkdirs();
200 FileUtils.fileWrite( userFile.getAbsolutePath(), "<configuration/>" );
202 ArchivaConfiguration archivaConfiguration =
203 (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-save-user" );
205 Configuration configuration = new Configuration();
206 configuration.setWebapp( new WebappConfiguration() );
207 configuration.getWebapp().setUi( new UserInterfaceOptions() );
208 configuration.getWebapp().getUi().setAppletFindEnabled( false );
210 archivaConfiguration.save( configuration );
212 assertTrue( "Check file exists", userFile.exists() );
213 assertFalse( "Check file not created", baseFile.exists() );
216 configuration = archivaConfiguration.getConfiguration();
217 assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
220 public void testStoreConfigurationLoadedFromDefaults()
223 File baseFile = getTestFile( "target/test/test-file.xml" );
225 assertFalse( baseFile.exists() );
227 File userFile = getTestFile( "target/test/test-file-user.xml" );
229 assertFalse( userFile.exists() );
231 ArchivaConfiguration archivaConfiguration =
232 (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-save-user" );
234 Configuration configuration = new Configuration();
235 configuration.setWebapp( new WebappConfiguration() );
236 configuration.getWebapp().setUi( new UserInterfaceOptions() );
237 configuration.getWebapp().getUi().setAppletFindEnabled( false );
239 archivaConfiguration.save( configuration );
241 assertTrue( "Check file exists", userFile.exists() );
242 assertFalse( "Check file not created", baseFile.exists() );
245 configuration = archivaConfiguration.getConfiguration();
246 assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
249 public void testDefaultUserConfigFilename()
252 DefaultArchivaConfiguration archivaConfiguration =
253 (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class.getName() );
255 assertEquals( System.getProperty( "user.home" ) + "/.m2/archiva.xml",
256 archivaConfiguration.getFilteredUserConfigFilename() );
259 public void testStoreConfigurationFallback()
262 File baseFile = getTestFile( "target/test/test-file.xml" );
264 assertFalse( baseFile.exists() );
266 File userFile = getTestFile( "target/test/test-file-user.xml" );
268 assertFalse( userFile.exists() );
270 baseFile.getParentFile().mkdirs();
271 FileUtils.fileWrite( baseFile.getAbsolutePath(), "<configuration/>" );
273 ArchivaConfiguration archivaConfiguration =
274 (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-save-user" );
276 Configuration configuration = new Configuration();
277 configuration.setWebapp( new WebappConfiguration() );
278 configuration.getWebapp().setUi( new UserInterfaceOptions() );
279 configuration.getWebapp().getUi().setAppletFindEnabled( false );
281 archivaConfiguration.save( configuration );
283 assertTrue( "Check file exists", baseFile.exists() );
284 assertFalse( "Check file not created", userFile.exists() );
287 configuration = archivaConfiguration.getConfiguration();
288 assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
291 public void testStoreConfigurationFailsWhenReadFromBothLocations()
294 File baseFile = getTestFile( "target/test/test-file.xml" );
296 assertFalse( baseFile.exists() );
298 File userFile = getTestFile( "target/test/test-file-user.xml" );
300 assertFalse( userFile.exists() );
302 baseFile.getParentFile().mkdirs();
303 FileUtils.fileWrite( baseFile.getAbsolutePath(), "<configuration/>" );
305 userFile.getParentFile().mkdirs();
306 FileUtils.fileWrite( userFile.getAbsolutePath(), "<configuration/>" );
308 ArchivaConfiguration archivaConfiguration =
309 (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-save-user" );
311 Configuration configuration = archivaConfiguration.getConfiguration();
312 assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
314 configuration.getWebapp().getUi().setAppletFindEnabled( false );
318 archivaConfiguration.save( configuration );
319 fail( "Configuration saving should not succeed if it was loaded from two locations" );
321 catch ( IndeterminateConfigurationException e )
323 // check it was reverted
324 configuration = archivaConfiguration.getConfiguration();
325 assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
329 public void testConfigurationUpgradeFrom09()
332 ArchivaConfiguration archivaConfiguration =
333 (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-upgrade-09" );
335 // we just use the defaults when upgrading from 0.9 at this point.
336 Configuration configuration = archivaConfiguration.getConfiguration();
337 assertConfiguration( configuration );
338 assertEquals( "check network proxies", 0, configuration.getNetworkProxies().size() );
340 RepositoryConfiguration repository =
341 (RepositoryConfiguration) configuration.getRepositories().iterator().next();
343 assertEquals( "check managed repositories", "file://${appserver.base}/data/repositories/internal",
344 repository.getUrl() );
345 assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
346 assertEquals( "check managed repositories", "internal", repository.getId() );
347 assertEquals( "check managed repositories", "default", repository.getLayout() );
348 assertTrue( "check managed repositories", repository.isIndexed() );
351 public void testAutoDetectV1()
354 ArchivaConfiguration archivaConfiguration =
355 (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-autodetect-v1" );
357 Configuration configuration = archivaConfiguration.getConfiguration();
358 assertConfiguration( configuration );
359 assertEquals( "check network proxies", 1, configuration.getNetworkProxies().size() );
361 RepositoryConfiguration repository =
362 (RepositoryConfiguration) configuration.getRepositories().iterator().next();
364 assertEquals( "check managed repositories", "file://${appserver.base}/repositories/internal",
365 repository.getUrl() );
366 assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
367 assertEquals( "check managed repositories", "internal", repository.getId() );
368 assertEquals( "check managed repositories", "default", repository.getLayout() );
369 assertTrue( "check managed repositories", repository.isIndexed() );