1 package org.apache.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 junit.framework.TestCase;
23 import org.apache.archiva.common.utils.FileUtil;
24 import org.apache.archiva.redback.components.registry.RegistryException;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.lang.StringUtils;
27 import org.custommonkey.xmlunit.XMLAssert;
28 import org.easymock.MockControl;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.test.context.ContextConfiguration;
35 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
37 import javax.inject.Inject;
39 import java.util.List;
43 * Test the configuration store.
45 @RunWith( SpringJUnit4ClassRunner.class )
46 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
47 public class ArchivaConfigurationTest
51 private Logger log = LoggerFactory.getLogger( getClass() );
54 protected ApplicationContext applicationContext;
59 public static File getTestFile( String path )
61 return new File( FileUtil.getBasedir(), path );
64 protected <T> T lookup( Class<T> clazz, String hint )
66 return (T) applicationContext.getBean( "archivaConfiguration#" + hint, ArchivaConfiguration.class );
71 public void testGetConfigurationFromRegistryWithASingleNamedConfigurationResource()
74 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
76 Configuration configuration = archivaConfiguration.getConfiguration();
77 assertConfiguration( configuration, 2, 2, 2 );
78 assertEquals( "check network proxies", 1, configuration.getNetworkProxies().size() );
80 ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
82 assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
83 repository.getLocation() );
84 assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
85 assertEquals( "check managed repositories", "internal", repository.getId() );
86 assertEquals( "check managed repositories", "default", repository.getLayout() );
87 assertTrue( "check managed repositories", repository.isScanned() );
91 public void testGetConfigurationFromDefaults()
94 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-defaults" );
96 Configuration configuration = archivaConfiguration.getConfiguration();
97 assertConfiguration( configuration, 2, 1, 1 );
98 assertEquals( "check network proxies", 0, configuration.getNetworkProxies().size() );
100 ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
102 assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
103 repository.getLocation() );
104 assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
105 assertEquals( "check managed repositories", "internal", repository.getId() );
106 assertEquals( "check managed repositories", "default", repository.getLayout() );
107 assertTrue( "check managed repositories", repository.isScanned() );
110 // test for [MRM-789]
112 public void testGetConfigurationFromDefaultsWithDefaultRepoLocationAlreadyExisting()
115 File repo = new File( FileUtil.getBasedir(), "/target/test-classes/existing_snapshots" );
118 repo = new File( FileUtil.getBasedir(), "/target/test-classes/existing_internal" );
121 String existingTestDefaultArchivaConfigFile = FileUtils.readFileToString(
122 getTestFile( "target/test-classes/org/apache/archiva/configuration/test-default-archiva.xml" ) );
123 existingTestDefaultArchivaConfigFile =
124 StringUtils.replace( existingTestDefaultArchivaConfigFile, "${appserver.base}", FileUtil.getBasedir() );
126 File generatedTestDefaultArchivaConfigFile = new File( FileUtil.getBasedir(),
127 "target/test-classes/org/apache/archiva/configuration/default-archiva.xml" );
129 FileUtils.writeStringToFile( generatedTestDefaultArchivaConfigFile, existingTestDefaultArchivaConfigFile,
132 ArchivaConfiguration archivaConfiguration =
133 lookup( ArchivaConfiguration.class, "test-defaults-default-repo-location-exists" );
135 Configuration configuration = archivaConfiguration.getConfiguration();
136 assertConfiguration( configuration, 2, 1, 1 );
138 ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
139 assertTrue( "check managed repositories", repository.getLocation().endsWith( "data/repositories/internal" ) );
141 generatedTestDefaultArchivaConfigFile.delete();
142 assertFalse( generatedTestDefaultArchivaConfigFile.exists() );
147 * Ensures that the provided configuration matches the details present in the archiva-default.xml file.
149 private void assertConfiguration( Configuration configuration, int managedExpected, int remoteExpected,
150 int proxyConnectorExpected )
154 assertEquals( "check managed repositories: " + configuration.getManagedRepositories(), managedExpected,
155 configuration.getManagedRepositories().size() );
156 assertEquals( "check remote repositories: " + configuration.getRemoteRepositories(), remoteExpected,
157 configuration.getRemoteRepositories().size() );
158 assertEquals( "check proxy connectors:" + configuration.getProxyConnectors(), proxyConnectorExpected,
159 configuration.getProxyConnectors().size() );
161 RepositoryScanningConfiguration repoScanning = configuration.getRepositoryScanning();
162 assertNotNull( "check repository scanning", repoScanning );
163 assertEquals( "check file types", 4, repoScanning.getFileTypes().size() );
164 assertEquals( "check known consumers", 9, repoScanning.getKnownContentConsumers().size() );
165 assertEquals( "check invalid consumers", 1, repoScanning.getInvalidContentConsumers().size() );
167 List<String> patterns = filetypes.getFileTypePatterns( "artifacts" );
168 assertNotNull( "check 'artifacts' file type", patterns );
169 assertEquals( "check 'artifacts' patterns", 13, patterns.size() );
171 WebappConfiguration webapp = configuration.getWebapp();
172 assertNotNull( "check webapp", webapp );
174 UserInterfaceOptions ui = webapp.getUi();
175 assertNotNull( "check webapp ui", ui );
176 assertTrue( "check showFindArtifacts", ui.isShowFindArtifacts() );
177 assertTrue( "check appletFindEnabled", ui.isAppletFindEnabled() );
181 public void testGetConfigurationFromRegistryWithTwoConfigurationResources()
184 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration-both" );
186 Configuration configuration = archivaConfiguration.getConfiguration();
189 assertEquals( "check repositories", 2, configuration.getManagedRepositories().size() );
190 assertEquals( "check repositories", 2, configuration.getRemoteRepositories().size() );
192 assertEquals( "check proxy connectors", 2, configuration.getProxyConnectors().size() );
194 WebappConfiguration webapp = configuration.getWebapp();
195 assertNotNull( "check webapp", webapp );
197 UserInterfaceOptions ui = webapp.getUi();
198 assertNotNull( "check webapp ui", ui );
200 assertFalse( "check showFindArtifacts", ui.isShowFindArtifacts() );
202 assertFalse( "check appletFindEnabled", ui.isAppletFindEnabled() );
206 public void testGetConfigurationSystemOverride()
210 System.setProperty( "org.apache.archiva.webapp.ui.appletFindEnabled", "false" );
212 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
214 archivaConfiguration.reload();
218 Configuration configuration = archivaConfiguration.getConfiguration();
220 assertFalse( "check boolean", configuration.getWebapp().getUi().isAppletFindEnabled() );
224 System.getProperties().remove( "org.apache.archiva.webapp.ui.appletFindEnabled" );
229 public void testStoreConfiguration()
232 File file = getTestFile( "target/test/test-file.xml" );
234 assertFalse( file.exists() );
236 // TODO: remove with commons-configuration 1.4
237 //file.getParentFile().mkdirs();
238 //FileUtils.writeStringToFile( file, "<configuration/>", null );
240 DefaultArchivaConfiguration archivaConfiguration =
241 (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save" );
243 archivaConfiguration.reload();
245 Configuration configuration = new Configuration();
246 configuration.setVersion( "1" );
247 configuration.setWebapp( new WebappConfiguration() );
248 configuration.getWebapp().setUi( new UserInterfaceOptions() );
249 configuration.getWebapp().getUi().setAppletFindEnabled( false );
251 // add a change listener
252 MockControl control = createConfigurationListenerMockControl();
253 ConfigurationListener listener = (ConfigurationListener) control.getMock();
254 archivaConfiguration.addListener( listener );
256 listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
257 control.setVoidCallable();
261 archivaConfiguration.save( configuration );
265 assertTrue( "Check file exists", file.exists() );
268 configuration = archivaConfiguration.getConfiguration();
269 assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
272 archivaConfiguration = (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-read-saved" );
274 archivaConfiguration.reload();
275 configuration = archivaConfiguration.getConfiguration();
276 assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
279 private static MockControl createConfigurationListenerMockControl()
281 return MockControl.createControl( ConfigurationListener.class );
285 public void testStoreConfigurationUser()
288 File baseFile = getTestFile( "target/test/test-file.xml" );
290 assertFalse( baseFile.exists() );
292 File userFile = getTestFile( "target/test/test-file-user.xml" );
294 assertFalse( userFile.exists() );
296 userFile.getParentFile().mkdirs();
297 FileUtils.writeStringToFile( userFile, "<configuration/>", null );
299 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
301 Configuration configuration = new Configuration();
302 configuration.setWebapp( new WebappConfiguration() );
303 configuration.getWebapp().setUi( new UserInterfaceOptions() );
304 configuration.getWebapp().getUi().setAppletFindEnabled( false );
306 archivaConfiguration.save( configuration );
308 assertTrue( "Check file exists", userFile.exists() );
309 assertFalse( "Check file not created", baseFile.exists() );
312 configuration = archivaConfiguration.getConfiguration();
313 assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
317 public void testStoreConfigurationLoadedFromDefaults()
320 File baseFile = getTestFile( "target/test/test-file.xml" );
322 assertFalse( baseFile.exists() );
324 File userFile = getTestFile( "target/test/test-file-user.xml" );
326 assertFalse( userFile.exists() );
328 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user-defaults" );
330 archivaConfiguration.reload();
332 Configuration configuration = new Configuration();
333 configuration.setWebapp( new WebappConfiguration() );
334 configuration.getWebapp().setUi( new UserInterfaceOptions() );
335 configuration.getWebapp().getUi().setAppletFindEnabled( false );
337 // add a change listener
338 MockControl control = createConfigurationListenerMockControl();
339 ConfigurationListener listener = (ConfigurationListener) control.getMock();
340 archivaConfiguration.addListener( listener );
342 listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
344 control.setVoidCallable( 1 );
348 archivaConfiguration.save( configuration );
352 assertTrue( "Check file exists", userFile.exists() );
353 assertFalse( "Check file not created", baseFile.exists() );
356 configuration = archivaConfiguration.getConfiguration();
357 assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
361 public void testDefaultUserConfigFilename()
364 DefaultArchivaConfiguration archivaConfiguration =
365 (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "default" );
367 archivaConfiguration.reload();
369 assertEquals( System.getProperty( "user.home" ) + "/.m2/archiva.xml",
370 archivaConfiguration.getUserConfigFilename() );
371 assertEquals( System.getProperty( "appserver.base", "${appserver.base}" ) + "/conf/archiva.xml",
372 archivaConfiguration.getAltConfigFilename() );
376 public void testStoreConfigurationFallback()
379 File baseFile = getTestFile( "target/test/test-file.xml" );
381 assertFalse( baseFile.exists() );
383 File userFile = getTestFile( "target/test/test-file-user.xml" );
385 assertFalse( userFile.exists() );
387 baseFile.getParentFile().mkdirs();
388 FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
390 ArchivaConfiguration archivaConfiguration =
391 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user-fallback" );
393 archivaConfiguration.reload();
395 Configuration configuration = new Configuration();
396 configuration.setWebapp( new WebappConfiguration() );
397 configuration.getWebapp().setUi( new UserInterfaceOptions() );
398 configuration.getWebapp().getUi().setAppletFindEnabled( false );
400 archivaConfiguration.save( configuration );
402 assertTrue( "Check file exists", baseFile.exists() );
403 assertFalse( "Check file not created", userFile.exists() );
406 configuration = archivaConfiguration.getConfiguration();
407 assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
411 public void testStoreConfigurationFailsWhenReadFromBothLocationsNoLists()
414 File baseFile = getTestFile( "target/test/test-file.xml" );
416 assertFalse( baseFile.exists() );
418 File userFile = getTestFile( "target/test/test-file-user.xml" );
420 assertFalse( userFile.exists() );
422 baseFile.getParentFile().mkdirs();
423 FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
425 userFile.getParentFile().mkdirs();
426 FileUtils.writeStringToFile( userFile, "<configuration/>", null );
428 ArchivaConfiguration archivaConfiguration =
429 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
431 archivaConfiguration.reload();
433 Configuration configuration = archivaConfiguration.getConfiguration();
434 assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
436 configuration.getWebapp().getUi().setAppletFindEnabled( false );
438 archivaConfiguration.save( configuration );
440 assertTrue( "Check file exists", baseFile.exists() );
441 assertEquals( "Check base file is unchanged", "<configuration/>",
442 FileUtils.readFileToString( baseFile, null ) );
443 assertTrue( "Check file exists", userFile.exists() );
444 assertFalse( "Check base file is changed",
445 "<configuration/>".equals( FileUtils.readFileToString( userFile, null ) ) );
448 configuration = archivaConfiguration.getConfiguration();
449 assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
453 public void testStoreConfigurationFailsWhenReadFromBothLocationsUserHasLists()
456 File baseFile = getTestFile( "target/test/test-file.xml" );
458 assertFalse( baseFile.exists() );
460 File userFile = getTestFile( "target/test/test-file-user.xml" );
462 assertFalse( userFile.exists() );
464 userFile.getParentFile().mkdirs();
465 FileUtils.copyFile( getTestFile( "src/test/conf/conf-user.xml" ), userFile );
467 baseFile.getParentFile().mkdirs();
468 FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
470 ArchivaConfiguration archivaConfiguration =
471 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
473 archivaConfiguration.reload();
475 Configuration configuration = archivaConfiguration.getConfiguration();
476 assertTrue( "check value", configuration.getWebapp().getUi().isShowFindArtifacts() );
478 configuration.getWebapp().getUi().setShowFindArtifacts( false );
480 archivaConfiguration.save( configuration );
482 assertTrue( "Check file exists", baseFile.exists() );
483 assertEquals( "Check base file is unchanged", "<configuration/>",
484 FileUtils.readFileToString( baseFile, null ) );
485 assertTrue( "Check file exists", userFile.exists() );
486 assertFalse( "Check base file is changed",
487 "<configuration/>".equals( FileUtils.readFileToString( userFile, null ) ) );
490 configuration = archivaConfiguration.getConfiguration();
491 assertFalse( "check value", configuration.getWebapp().getUi().isShowFindArtifacts() );
495 public void testStoreConfigurationFailsWhenReadFromBothLocationsAppserverHasLists()
498 File baseFile = getTestFile( "target/test/test-file.xml" );
500 assertFalse( baseFile.exists() );
502 File userFile = getTestFile( "target/test/test-file-user.xml" );
504 assertFalse( userFile.exists() );
506 baseFile.getParentFile().mkdirs();
507 FileUtils.copyFile( getTestFile( "src/test/conf/conf-base.xml" ), baseFile );
509 userFile.getParentFile().mkdirs();
510 FileUtils.writeStringToFile( userFile, "<configuration/>", null );
512 ArchivaConfiguration archivaConfiguration =
513 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
515 archivaConfiguration.reload();
517 Configuration configuration = archivaConfiguration.getConfiguration();
518 assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
520 configuration.getWebapp().getUi().setAppletFindEnabled( false );
524 archivaConfiguration.save( configuration );
525 fail( "Configuration saving should not succeed if it was loaded from two locations" );
527 catch ( IndeterminateConfigurationException e )
529 // check it was reverted
530 configuration = archivaConfiguration.getConfiguration();
531 assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
536 public void testLoadConfigurationFromInvalidBothLocationsOnDisk()
539 ArchivaConfiguration archivaConfiguration =
540 lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-both" );
541 Configuration config = archivaConfiguration.getConfiguration();
545 archivaConfiguration.save( config );
546 fail( "Should have thrown a RegistryException because the configuration can't be saved." );
548 catch ( RegistryException e )
550 /* expected exception */
555 public void testLoadConfigurationFromInvalidUserLocationOnDisk()
558 File testConfDir = getTestFile( "target/test-appserver-base/conf/" );
559 testConfDir.mkdirs();
561 ArchivaConfiguration archivaConfiguration =
562 lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-user" );
563 Configuration config = archivaConfiguration.getConfiguration();
564 archivaConfiguration.save( config );
565 // No Exception == test passes.
566 // Expected Path is: Should not have thrown an exception.
570 public void testConfigurationUpgradeFrom09()
573 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-09" );
575 // we just use the defaults when upgrading from 0.9 at this point.
576 Configuration configuration = archivaConfiguration.getConfiguration();
577 // test-upgrade-09 contains a managed with id: local so it's 3 managed
578 assertConfiguration( configuration, 3, 1, 1 );
579 assertEquals( "check network proxies", 0, configuration.getNetworkProxies().size() );
581 ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
583 assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
584 repository.getLocation() );
585 assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
586 assertEquals( "check managed repositories", "internal", repository.getId() );
587 assertEquals( "check managed repositories", "default", repository.getLayout() );
588 assertTrue( "check managed repositories", repository.isScanned() );
592 public void testConfigurationUpgradeFrom13()
595 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-1.3" );
597 // we just use the defaults when upgrading from 1.3 at this point.
598 Configuration configuration = archivaConfiguration.getConfiguration();
599 assertConfiguration( configuration, 2, 2, 2 );
600 assertEquals( "check network proxies", 0, configuration.getNetworkProxies().size() );
602 ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
604 assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
605 repository.getLocation() );
606 assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
607 assertEquals( "check managed repositories", "internal", repository.getId() );
608 assertEquals( "check managed repositories", "default", repository.getLayout() );
609 assertTrue( "check managed repositories", repository.isScanned() );
611 log.info( "knowContentConsumers " + configuration.getRepositoryScanning().getKnownContentConsumers() );
614 configuration.getRepositoryScanning().getKnownContentConsumers().contains( "update-db-artifact" ) );
615 assertFalse( configuration.getRepositoryScanning().getKnownContentConsumers().contains(
616 "update-db-repository-metadata" ) );
619 configuration.getRepositoryScanning().getKnownContentConsumers().contains( "create-archiva-metadata" ) );
622 configuration.getRepositoryScanning().getKnownContentConsumers().contains( "duplicate-artifacts" ) );
626 public void testAutoDetectV1()
629 // Setup the autodetect-v1.xml file in the target directory (so we can save/load it)
630 File userFile = getTestFile( "target/test-autodetect-v1/archiva-user.xml" );
632 assertFalse( userFile.exists() );
634 userFile.getParentFile().mkdirs();
635 FileUtils.copyFile( getTestFile( "src/test/conf/autodetect-v1.xml" ), userFile );
637 // Load the original (unconverted) archiva.xml
638 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-autodetect-v1" );
640 archivaConfiguration.reload();
642 Configuration configuration = archivaConfiguration.getConfiguration();
643 assertConfiguration( configuration, 2, 2, 2 );
644 assertEquals( "check network proxies", 1, configuration.getNetworkProxies().size() );
646 ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
648 assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
649 repository.getLocation() );
650 assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
651 assertEquals( "check managed repositories", "internal", repository.getId() );
652 assertEquals( "check managed repositories", "default", repository.getLayout() );
653 assertTrue( "check managed repositories", repository.isScanned() );
655 // Test that only 1 set of repositories exist.
656 assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositories().size() );
657 assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositories().size() );
658 assertEquals( "check v1 repositories size.", 0, configuration.getRepositories().size() );
661 archivaConfiguration.save( configuration );
664 //release( archivaConfiguration );
667 archivaConfiguration = lookup( ArchivaConfiguration.class, "test-autodetect-v1" );
668 configuration = archivaConfiguration.getConfiguration();
670 // Test that only 1 set of repositories exist.
671 assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositories().size() );
672 assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositoriesAsMap().size() );
673 assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositories().size() );
674 assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositoriesAsMap().size() );
675 assertEquals( "check v1 repositories size.", 0, configuration.getRepositories().size() );
677 String actualXML = FileUtils.readFileToString( userFile, null );
678 XMLAssert.assertXpathNotExists( "//configuration/repositories/repository", actualXML );
679 XMLAssert.assertXpathNotExists( "//configuration/repositories", actualXML );
683 public void testArchivaV1()
686 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-archiva-v1" );
688 Configuration configuration = archivaConfiguration.getConfiguration();
689 assertConfiguration( configuration, 2, 2, 2 );
690 assertEquals( "check network proxies", 1, configuration.getNetworkProxies().size() );
692 assertEquals( "check managed repositories", 2, configuration.getManagedRepositories().size() );
693 assertEquals( "check v1 repositories size.", 0, configuration.getRepositories().size() );
695 Map<String, ManagedRepositoryConfiguration> map = configuration.getManagedRepositoriesAsMap();
697 ManagedRepositoryConfiguration repository = map.get( "internal" );
698 assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
699 repository.getLocation() );
700 assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
701 assertEquals( "check managed repositories", "internal", repository.getId() );
702 assertEquals( "check managed repositories", "default", repository.getLayout() );
703 assertTrue( "check managed repositories", repository.isScanned() );
704 assertFalse( "check managed repositories", repository.isSnapshots() );
706 repository = map.get( "snapshots" );
707 assertEquals( "check managed repositories", "${appserver.base}/repositories/snapshots",
708 repository.getLocation() );
709 assertEquals( "check managed repositories", "Archiva Managed Snapshot Repository", repository.getName() );
710 assertEquals( "check managed repositories", "snapshots", repository.getId() );
711 assertEquals( "check managed repositories", "default", repository.getLayout() );
712 assertFalse( "check managed repositories", repository.isScanned() );
713 assertTrue( "check managed repositories", repository.isSnapshots() );
717 public void testCronExpressionsWithComma()
720 File baseFile = getTestFile( "target/test/test-file.xml" );
722 assertFalse( baseFile.exists() );
724 File userFile = getTestFile( "target/test/test-file-user.xml" );
726 assertFalse( userFile.exists() );
728 baseFile.getParentFile().mkdirs();
729 FileUtils.copyFile( getTestFile( "src/test/conf/escape-cron-expressions.xml" ), baseFile );
731 userFile.getParentFile().mkdirs();
732 FileUtils.writeStringToFile( userFile, "<configuration/>", null );
734 final ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-cron-expressions" );
736 archivaConfiguration.reload();
738 Configuration configuration = archivaConfiguration.getConfiguration();
740 ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
742 assertEquals( "check cron expression", "0 0,30 * * * ?", repository.getRefreshCronExpression().trim() );
744 // add a test listener to confirm it doesn't see the escaped format. We don't need to test the number of calls,
745 // etc. as it's done in other tests
746 archivaConfiguration.addListener( new ConfigurationListener()
748 public void configurationEvent( ConfigurationEvent event )
750 assertEquals( ConfigurationEvent.SAVED, event.getType() );
755 archivaConfiguration.save( configuration );
757 configuration = archivaConfiguration.getConfiguration();
759 // test for the escape character '\' showing up on repositories.jsp
760 repository.setRefreshCronExpression( "0 0,20 0 * * ?" );
762 archivaConfiguration.save( configuration );
764 repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( "snapshots" );
766 assertEquals( "check cron expression", "0 0,20 0 * * ?", repository.getRefreshCronExpression() );
770 public void testRemoveLastElements()
773 File baseFile = getTestFile( "target/test/test-file.xml" );
775 assertFalse( baseFile.exists() );
777 File userFile = getTestFile( "target/test/test-file-user.xml" );
779 assertFalse( userFile.exists() );
781 baseFile.getParentFile().mkdirs();
782 FileUtils.copyFile( getTestFile( "src/test/conf/conf-single-list-elements.xml" ), baseFile );
784 userFile.getParentFile().mkdirs();
785 FileUtils.writeStringToFile( userFile, "<configuration/>", null );
787 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-remove-central" );
789 archivaConfiguration.reload();
791 Configuration configuration = archivaConfiguration.getConfiguration();
793 RepositoryGroupConfiguration repositoryGroup = configuration.getRepositoryGroups().get( 0 );
794 assertNotNull( repositoryGroup );
795 configuration.removeRepositoryGroup( repositoryGroup );
796 assertTrue( configuration.getRepositoryGroups().isEmpty() );
798 RemoteRepositoryConfiguration repository = configuration.getRemoteRepositoriesAsMap().get( "central" );
799 assertNotNull( repository );
800 configuration.removeRemoteRepository( repository );
801 assertTrue( configuration.getRemoteRepositories().isEmpty() );
803 ManagedRepositoryConfiguration managedRepository =
804 configuration.getManagedRepositoriesAsMap().get( "snapshots" );
805 assertNotNull( managedRepository );
806 configuration.removeManagedRepository( managedRepository );
807 assertTrue( configuration.getManagedRepositories().isEmpty() );
809 ProxyConnectorConfiguration proxyConnector = configuration.getProxyConnectors().get( 0 );
810 assertNotNull( proxyConnector );
811 configuration.removeProxyConnector( proxyConnector );
812 assertTrue( configuration.getProxyConnectors().isEmpty() );
814 NetworkProxyConfiguration networkProxy = configuration.getNetworkProxiesAsMap().get( "proxy" );
815 assertNotNull( networkProxy );
816 configuration.removeNetworkProxy( networkProxy );
817 assertTrue( configuration.getNetworkProxies().isEmpty() );
819 LegacyArtifactPath path = configuration.getLegacyArtifactPaths().get( 0 );
820 assertNotNull( path );
821 configuration.removeLegacyArtifactPath( path );
822 assertTrue( configuration.getLegacyArtifactPaths().isEmpty() );
824 RepositoryScanningConfiguration scanning = configuration.getRepositoryScanning();
825 String consumer = scanning.getKnownContentConsumers().get( 0 );
826 assertNotNull( consumer );
827 scanning.removeKnownContentConsumer( consumer );
829 assertFalse( scanning.getKnownContentConsumers().isEmpty() );
830 consumer = scanning.getInvalidContentConsumers().get( 0 );
831 assertNotNull( consumer );
832 scanning.removeInvalidContentConsumer( consumer );
833 assertTrue( scanning.getInvalidContentConsumers().isEmpty() );
835 archivaConfiguration.save( configuration );
837 archivaConfiguration = lookup( ArchivaConfiguration.class, "test-read-saved" );
838 configuration = archivaConfiguration.getConfiguration();
839 assertNull( configuration.getRemoteRepositoriesAsMap().get( "central" ) );
840 assertTrue( configuration.getRepositoryGroups().isEmpty() );
841 assertNull( configuration.getManagedRepositoriesAsMap().get( "snapshots" ) );
842 assertTrue( configuration.getProxyConnectors().isEmpty() );
843 assertNull( configuration.getNetworkProxiesAsMap().get( "proxy" ) );
844 assertTrue( configuration.getLegacyArtifactPaths().isEmpty() );
845 scanning = configuration.getRepositoryScanning();
846 assertFalse( scanning.getKnownContentConsumers().isEmpty() );
847 assertTrue( scanning.getInvalidContentConsumers().isEmpty() );
851 * [MRM-582] Remote Repositories with empty <username> and <password> fields shouldn't be created in configuration.
854 public void testGetConfigurationFixEmptyRemoteRepoUsernamePassword()
857 ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
859 archivaConfiguration.reload();
860 Configuration configuration = archivaConfiguration.getConfiguration();
861 assertConfiguration( configuration, 2, 2, 2 );
862 assertEquals( "check remote repositories", 2, configuration.getRemoteRepositories().size() );
864 RemoteRepositoryConfiguration repository =
865 configuration.getRemoteRepositoriesAsMap().get( "maven2-repository.dev.java.net" );
867 assertEquals( "remote repository.url", "https://maven2-repository.dev.java.net/nonav/repository",
868 repository.getUrl() );
869 assertEquals( "remote repository.name", "Java.net Repository for Maven 2", repository.getName() );
870 assertEquals( "remote repository.id", "maven2-repository.dev.java.net", repository.getId() );
871 assertEquals( "remote repository.layout", "default", repository.getLayout() );
872 assertNull( "remote repository.username == null", repository.getUsername() );
873 assertNull( "remote repository.password == null", repository.getPassword() );