]> source.dussan.org Git - archiva.git/blob
3ab407a682c1dc8e6b352fd40eb975bfbbcd5827
[archiva.git] /
1 package org.apache.archiva.configuration;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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
36
37 import javax.inject.Inject;
38 import java.io.File;
39 import java.util.List;
40 import java.util.Map;
41 import org.apache.archiva.test.ArchivaSpringJUnit4ClassRunner;
42
43 /**
44  * Test the configuration store.
45  */
46 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
47 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
48 public class ArchivaConfigurationTest
49     extends TestCase
50 {
51
52     private Logger log = LoggerFactory.getLogger( getClass() );
53
54     @Inject
55     protected ApplicationContext applicationContext;
56
57     @Inject
58     FileTypes filetypes;
59
60     public static File getTestFile( String path )
61     {
62         return new File( FileUtil.getBasedir(), path );
63     }
64
65     protected <T> T lookup( Class<T> clazz, String hint )
66     {
67         return (T) applicationContext.getBean( "archivaConfiguration#" + hint, ArchivaConfiguration.class );
68     }
69
70
71     @Test
72     public void testGetConfigurationFromRegistryWithASingleNamedConfigurationResource()
73         throws Exception
74     {
75         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
76
77         Configuration configuration = archivaConfiguration.getConfiguration();
78         assertConfiguration( configuration, 2, 2, 2 );
79         assertEquals( "check network proxies", 1, configuration.getNetworkProxies().size() );
80
81         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
82
83         assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
84                       repository.getLocation() );
85         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
86         assertEquals( "check managed repositories", "internal", repository.getId() );
87         assertEquals( "check managed repositories", "default", repository.getLayout() );
88         assertTrue( "check managed repositories", repository.isScanned() );
89     }
90
91     @Test
92     public void testGetConfigurationFromDefaults()
93         throws Exception
94     {
95         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-defaults" );
96
97         Configuration configuration = archivaConfiguration.getConfiguration();
98         assertConfiguration( configuration, 2, 1, 1 );
99         assertEquals( "check network proxies", 0, configuration.getNetworkProxies().size() );
100
101         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
102
103         assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
104                       repository.getLocation() );
105         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
106         assertEquals( "check managed repositories", "internal", repository.getId() );
107         assertEquals( "check managed repositories", "default", repository.getLayout() );
108         assertTrue( "check managed repositories", repository.isScanned() );
109     }
110
111     // test for [MRM-789]
112     @Test
113     public void testGetConfigurationFromDefaultsWithDefaultRepoLocationAlreadyExisting()
114         throws Exception
115     {
116         File repo = new File( FileUtil.getBasedir(), "/target/test-classes/existing_snapshots" );
117         repo.mkdirs();
118
119         repo = new File( FileUtil.getBasedir(), "/target/test-classes/existing_internal" );
120         repo.mkdirs();
121
122         String existingTestDefaultArchivaConfigFile = FileUtils.readFileToString(
123             getTestFile( "target/test-classes/org/apache/archiva/configuration/test-default-archiva.xml" ) );
124         existingTestDefaultArchivaConfigFile =
125             StringUtils.replace( existingTestDefaultArchivaConfigFile, "${appserver.base}", FileUtil.getBasedir() );
126
127         File generatedTestDefaultArchivaConfigFile = new File( FileUtil.getBasedir(),
128                                                                "target/test-classes/org/apache/archiva/configuration/default-archiva.xml" );
129
130         FileUtils.writeStringToFile( generatedTestDefaultArchivaConfigFile, existingTestDefaultArchivaConfigFile,
131                                      null );
132
133         ArchivaConfiguration archivaConfiguration =
134             lookup( ArchivaConfiguration.class, "test-defaults-default-repo-location-exists" );
135
136         Configuration configuration = archivaConfiguration.getConfiguration();
137         assertConfiguration( configuration, 2, 1, 1 );
138
139         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
140         assertTrue( "check managed repositories", repository.getLocation().endsWith( "data/repositories/internal" ) );
141
142         generatedTestDefaultArchivaConfigFile.delete();
143         assertFalse( generatedTestDefaultArchivaConfigFile.exists() );
144     }
145
146
147     /**
148      * Ensures that the provided configuration matches the details present in the archiva-default.xml file.
149      */
150     private void assertConfiguration( Configuration configuration, int managedExpected, int remoteExpected,
151                                       int proxyConnectorExpected )
152         throws Exception
153     {
154
155         assertEquals( "check managed repositories: " + configuration.getManagedRepositories(), managedExpected,
156                       configuration.getManagedRepositories().size() );
157         assertEquals( "check remote repositories: " + configuration.getRemoteRepositories(), remoteExpected,
158                       configuration.getRemoteRepositories().size() );
159         assertEquals( "check proxy connectors:" + configuration.getProxyConnectors(), proxyConnectorExpected,
160                       configuration.getProxyConnectors().size() );
161
162         RepositoryScanningConfiguration repoScanning = configuration.getRepositoryScanning();
163         assertNotNull( "check repository scanning", repoScanning );
164         assertEquals( "check file types", 4, repoScanning.getFileTypes().size() );
165         assertEquals( "check known consumers", 9, repoScanning.getKnownContentConsumers().size() );
166         assertEquals( "check invalid consumers", 1, repoScanning.getInvalidContentConsumers().size() );
167
168         List<String> patterns = filetypes.getFileTypePatterns( "artifacts" );
169         assertNotNull( "check 'artifacts' file type", patterns );
170         assertEquals( "check 'artifacts' patterns", 13, patterns.size() );
171
172         WebappConfiguration webapp = configuration.getWebapp();
173         assertNotNull( "check webapp", webapp );
174
175         UserInterfaceOptions ui = webapp.getUi();
176         assertNotNull( "check webapp ui", ui );
177         assertTrue( "check showFindArtifacts", ui.isShowFindArtifacts() );
178         assertTrue( "check appletFindEnabled", ui.isAppletFindEnabled() );
179     }
180
181     @Test
182     public void testGetConfigurationFromRegistryWithTwoConfigurationResources()
183         throws Exception
184     {
185         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration-both" );
186
187         Configuration configuration = archivaConfiguration.getConfiguration();
188
189         // from base
190         assertEquals( "check repositories", 2, configuration.getManagedRepositories().size() );
191         assertEquals( "check repositories", 2, configuration.getRemoteRepositories().size() );
192         // from user
193         assertEquals( "check proxy connectors", 2, configuration.getProxyConnectors().size() );
194
195         WebappConfiguration webapp = configuration.getWebapp();
196         assertNotNull( "check webapp", webapp );
197
198         UserInterfaceOptions ui = webapp.getUi();
199         assertNotNull( "check webapp ui", ui );
200         // from base
201         assertFalse( "check showFindArtifacts", ui.isShowFindArtifacts() );
202         // from user
203         assertFalse( "check appletFindEnabled", ui.isAppletFindEnabled() );
204     }
205
206     @Test
207     public void testGetConfigurationSystemOverride()
208         throws Exception
209     {
210
211         System.setProperty( "org.apache.archiva.webapp.ui.appletFindEnabled", "false" );
212
213         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
214
215         archivaConfiguration.reload();
216
217         try
218         {
219             Configuration configuration = archivaConfiguration.getConfiguration();
220
221             assertFalse( "check boolean", configuration.getWebapp().getUi().isAppletFindEnabled() );
222         }
223         finally
224         {
225             System.getProperties().remove( "org.apache.archiva.webapp.ui.appletFindEnabled" );
226         }
227     }
228
229     @Test
230     public void testStoreConfiguration()
231         throws Exception
232     {
233         File file = getTestFile( "target/test/test-file.xml" );
234         file.delete();
235         assertFalse( file.exists() );
236
237         // TODO: remove with commons-configuration 1.4
238         //file.getParentFile().mkdirs();
239         //FileUtils.writeStringToFile( file, "<configuration/>", null );
240
241         DefaultArchivaConfiguration archivaConfiguration =
242             (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save" );
243
244         archivaConfiguration.reload();
245
246         Configuration configuration = new Configuration();
247         configuration.setVersion( "1" );
248         configuration.setWebapp( new WebappConfiguration() );
249         configuration.getWebapp().setUi( new UserInterfaceOptions() );
250         configuration.getWebapp().getUi().setAppletFindEnabled( false );
251
252         // add a change listener
253         MockControl control = createConfigurationListenerMockControl();
254         ConfigurationListener listener = (ConfigurationListener) control.getMock();
255         archivaConfiguration.addListener( listener );
256
257         listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
258         control.setVoidCallable();
259
260         control.replay();
261
262         archivaConfiguration.save( configuration );
263
264         control.verify();
265
266         assertTrue( "Check file exists", file.exists() );
267
268         // check it
269         configuration = archivaConfiguration.getConfiguration();
270         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
271
272         // read it back
273         archivaConfiguration = (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-read-saved" );
274
275         archivaConfiguration.reload();
276         configuration = archivaConfiguration.getConfiguration();
277         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
278     }
279
280     private static MockControl createConfigurationListenerMockControl()
281     {
282         return MockControl.createControl( ConfigurationListener.class );
283     }
284
285     @Test
286     public void testStoreConfigurationUser()
287         throws Exception
288     {
289         File baseFile = getTestFile( "target/test/test-file.xml" );
290         baseFile.delete();
291         assertFalse( baseFile.exists() );
292
293         File userFile = getTestFile( "target/test/test-file-user.xml" );
294         userFile.delete();
295         assertFalse( userFile.exists() );
296
297         userFile.getParentFile().mkdirs();
298         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
299
300         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user" );
301
302         Configuration configuration = new Configuration();
303         configuration.setWebapp( new WebappConfiguration() );
304         configuration.getWebapp().setUi( new UserInterfaceOptions() );
305         configuration.getWebapp().getUi().setAppletFindEnabled( false );
306
307         archivaConfiguration.save( configuration );
308
309         assertTrue( "Check file exists", userFile.exists() );
310         assertFalse( "Check file not created", baseFile.exists() );
311
312         // check it
313         configuration = archivaConfiguration.getConfiguration();
314         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
315     }
316
317     @Test
318     public void testStoreConfigurationLoadedFromDefaults()
319         throws Exception
320     {
321         File baseFile = getTestFile( "target/test/test-file.xml" );
322         baseFile.delete();
323         assertFalse( baseFile.exists() );
324
325         File userFile = getTestFile( "target/test/test-file-user.xml" );
326         userFile.delete();
327         assertFalse( userFile.exists() );
328
329         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-save-user-defaults" );
330
331         archivaConfiguration.reload();
332
333         Configuration configuration = new Configuration();
334         configuration.setWebapp( new WebappConfiguration() );
335         configuration.getWebapp().setUi( new UserInterfaceOptions() );
336         configuration.getWebapp().getUi().setAppletFindEnabled( false );
337
338         // add a change listener
339         MockControl control = createConfigurationListenerMockControl();
340         ConfigurationListener listener = (ConfigurationListener) control.getMock();
341         archivaConfiguration.addListener( listener );
342
343         listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
344
345         control.setVoidCallable( 1 );
346
347         control.replay();
348
349         archivaConfiguration.save( configuration );
350
351         control.verify();
352
353         assertTrue( "Check file exists", userFile.exists() );
354         assertFalse( "Check file not created", baseFile.exists() );
355
356         // check it
357         configuration = archivaConfiguration.getConfiguration();
358         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
359     }
360
361     @Test
362     public void testDefaultUserConfigFilename()
363         throws Exception
364     {
365         DefaultArchivaConfiguration archivaConfiguration =
366             (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "default" );
367
368         archivaConfiguration.reload();
369
370         assertEquals( System.getProperty( "user.home" ) + "/.m2/archiva.xml",
371                       archivaConfiguration.getUserConfigFilename() );
372         assertEquals( System.getProperty( "appserver.base", "${appserver.base}" ) + "/conf/archiva.xml",
373                       archivaConfiguration.getAltConfigFilename() );
374     }
375
376     @Test
377     public void testStoreConfigurationFallback()
378         throws Exception
379     {
380         File baseFile = getTestFile( "target/test/test-file.xml" );
381         baseFile.delete();
382         assertFalse( baseFile.exists() );
383
384         File userFile = getTestFile( "target/test/test-file-user.xml" );
385         userFile.delete();
386         assertFalse( userFile.exists() );
387
388         baseFile.getParentFile().mkdirs();
389         FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
390
391         ArchivaConfiguration archivaConfiguration =
392             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user-fallback" );
393
394         archivaConfiguration.reload();
395
396         Configuration configuration = new Configuration();
397         configuration.setWebapp( new WebappConfiguration() );
398         configuration.getWebapp().setUi( new UserInterfaceOptions() );
399         configuration.getWebapp().getUi().setAppletFindEnabled( false );
400
401         archivaConfiguration.save( configuration );
402
403         assertTrue( "Check file exists", baseFile.exists() );
404         assertFalse( "Check file not created", userFile.exists() );
405
406         // check it
407         configuration = archivaConfiguration.getConfiguration();
408         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
409     }
410
411     @Test
412     public void testStoreConfigurationFailsWhenReadFromBothLocationsNoLists()
413         throws Exception
414     {
415         File baseFile = getTestFile( "target/test/test-file.xml" );
416         baseFile.delete();
417         assertFalse( baseFile.exists() );
418
419         File userFile = getTestFile( "target/test/test-file-user.xml" );
420         userFile.delete();
421         assertFalse( userFile.exists() );
422
423         baseFile.getParentFile().mkdirs();
424         FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
425
426         userFile.getParentFile().mkdirs();
427         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
428
429         ArchivaConfiguration archivaConfiguration =
430             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
431
432         archivaConfiguration.reload();
433
434         Configuration configuration = archivaConfiguration.getConfiguration();
435         assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
436
437         configuration.getWebapp().getUi().setAppletFindEnabled( false );
438
439         archivaConfiguration.save( configuration );
440
441         assertTrue( "Check file exists", baseFile.exists() );
442         assertEquals( "Check base file is unchanged", "<configuration/>",
443                       FileUtils.readFileToString( baseFile, null ) );
444         assertTrue( "Check file exists", userFile.exists() );
445         assertFalse( "Check base file is changed",
446                      "<configuration/>".equals( FileUtils.readFileToString( userFile, null ) ) );
447
448         // check it
449         configuration = archivaConfiguration.getConfiguration();
450         assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
451     }
452
453     @Test
454     public void testStoreConfigurationFailsWhenReadFromBothLocationsUserHasLists()
455         throws Exception
456     {
457         File baseFile = getTestFile( "target/test/test-file.xml" );
458         baseFile.delete();
459         assertFalse( baseFile.exists() );
460
461         File userFile = getTestFile( "target/test/test-file-user.xml" );
462         userFile.delete();
463         assertFalse( userFile.exists() );
464
465         userFile.getParentFile().mkdirs();
466         FileUtils.copyFile( getTestFile( "src/test/conf/conf-user.xml" ), userFile );
467
468         baseFile.getParentFile().mkdirs();
469         FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
470
471         ArchivaConfiguration archivaConfiguration =
472             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
473
474         archivaConfiguration.reload();
475
476         Configuration configuration = archivaConfiguration.getConfiguration();
477         assertTrue( "check value", configuration.getWebapp().getUi().isShowFindArtifacts() );
478
479         configuration.getWebapp().getUi().setShowFindArtifacts( false );
480
481         archivaConfiguration.save( configuration );
482
483         assertTrue( "Check file exists", baseFile.exists() );
484         assertEquals( "Check base file is unchanged", "<configuration/>",
485                       FileUtils.readFileToString( baseFile, null ) );
486         assertTrue( "Check file exists", userFile.exists() );
487         assertFalse( "Check base file is changed",
488                      "<configuration/>".equals( FileUtils.readFileToString( userFile, null ) ) );
489
490         // check it
491         configuration = archivaConfiguration.getConfiguration();
492         assertFalse( "check value", configuration.getWebapp().getUi().isShowFindArtifacts() );
493     }
494
495     @Test
496     public void testStoreConfigurationFailsWhenReadFromBothLocationsAppserverHasLists()
497         throws Exception
498     {
499         File baseFile = getTestFile( "target/test/test-file.xml" );
500         baseFile.delete();
501         assertFalse( baseFile.exists() );
502
503         File userFile = getTestFile( "target/test/test-file-user.xml" );
504         userFile.delete();
505         assertFalse( userFile.exists() );
506
507         baseFile.getParentFile().mkdirs();
508         FileUtils.copyFile( getTestFile( "src/test/conf/conf-base.xml" ), baseFile );
509
510         userFile.getParentFile().mkdirs();
511         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
512
513         ArchivaConfiguration archivaConfiguration =
514             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
515
516         archivaConfiguration.reload();
517
518         Configuration configuration = archivaConfiguration.getConfiguration();
519         assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
520
521         configuration.getWebapp().getUi().setAppletFindEnabled( false );
522
523         try
524         {
525             archivaConfiguration.save( configuration );
526             fail( "Configuration saving should not succeed if it was loaded from two locations" );
527         }
528         catch ( IndeterminateConfigurationException e )
529         {
530             // check it was reverted
531             configuration = archivaConfiguration.getConfiguration();
532             assertTrue( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
533         }
534     }
535
536     @Test
537     public void testLoadConfigurationFromInvalidBothLocationsOnDisk()
538         throws Exception
539     {
540         ArchivaConfiguration archivaConfiguration =
541             lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-both" );
542         Configuration config = archivaConfiguration.getConfiguration();
543
544         try
545         {
546             archivaConfiguration.save( config );
547             fail( "Should have thrown a RegistryException because the configuration can't be saved." );
548         }
549         catch ( RegistryException e )
550         {
551             /* expected exception */
552         }
553     }
554
555     @Test
556     public void testLoadConfigurationFromInvalidUserLocationOnDisk()
557         throws Exception
558     {
559         File testConfDir = getTestFile( "target/test-appserver-base/conf/" );
560         testConfDir.mkdirs();
561
562         ArchivaConfiguration archivaConfiguration =
563             lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-user" );
564         Configuration config = archivaConfiguration.getConfiguration();
565         archivaConfiguration.save( config );
566         // No Exception == test passes.
567         // Expected Path is: Should not have thrown an exception.
568     }
569
570     @Test
571     public void testConfigurationUpgradeFrom09()
572         throws Exception
573     {
574         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-09" );
575
576         // we just use the defaults when upgrading from 0.9 at this point.
577         Configuration configuration = archivaConfiguration.getConfiguration();
578         // test-upgrade-09 contains a managed with id: local so it's 3 managed
579         assertConfiguration( configuration, 3, 1, 1 );
580         assertEquals( "check network proxies", 0, configuration.getNetworkProxies().size() );
581
582         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
583
584         assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
585                       repository.getLocation() );
586         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
587         assertEquals( "check managed repositories", "internal", repository.getId() );
588         assertEquals( "check managed repositories", "default", repository.getLayout() );
589         assertTrue( "check managed repositories", repository.isScanned() );
590     }
591
592     @Test
593     public void testConfigurationUpgradeFrom13()
594         throws Exception
595     {
596         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-1.3" );
597
598         // we just use the defaults when upgrading from 1.3 at this point.
599         Configuration configuration = archivaConfiguration.getConfiguration();
600         assertConfiguration( configuration, 2, 2, 2 );
601         assertEquals( "check network proxies", 0, configuration.getNetworkProxies().size() );
602
603         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
604
605         assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
606                       repository.getLocation() );
607         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
608         assertEquals( "check managed repositories", "internal", repository.getId() );
609         assertEquals( "check managed repositories", "default", repository.getLayout() );
610         assertTrue( "check managed repositories", repository.isScanned() );
611
612         log.info( "knowContentConsumers " + configuration.getRepositoryScanning().getKnownContentConsumers() );
613
614         assertFalse(
615             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "update-db-artifact" ) );
616         assertFalse( configuration.getRepositoryScanning().getKnownContentConsumers().contains(
617             "update-db-repository-metadata" ) );
618
619         assertTrue(
620             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "create-archiva-metadata" ) );
621
622         assertTrue(
623             configuration.getRepositoryScanning().getKnownContentConsumers().contains( "duplicate-artifacts" ) );
624     }
625
626     @Test
627     public void testAutoDetectV1()
628         throws Exception
629     {
630         // Setup the autodetect-v1.xml file in the target directory (so we can save/load it)
631         File userFile = getTestFile( "target/test-autodetect-v1/archiva-user.xml" );
632         userFile.delete();
633         assertFalse( userFile.exists() );
634
635         userFile.getParentFile().mkdirs();
636         FileUtils.copyFile( getTestFile( "src/test/conf/autodetect-v1.xml" ), userFile );
637
638         // Load the original (unconverted) archiva.xml
639         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-autodetect-v1" );
640
641         archivaConfiguration.reload();
642
643         Configuration configuration = archivaConfiguration.getConfiguration();
644         assertConfiguration( configuration, 2, 2, 2 );
645         assertEquals( "check network proxies", 1, configuration.getNetworkProxies().size() );
646
647         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
648
649         assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
650                       repository.getLocation() );
651         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
652         assertEquals( "check managed repositories", "internal", repository.getId() );
653         assertEquals( "check managed repositories", "default", repository.getLayout() );
654         assertTrue( "check managed repositories", repository.isScanned() );
655
656         // Test that only 1 set of repositories exist.
657         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositories().size() );
658         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositories().size() );
659         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories().size() );
660
661         // Save the file.
662         archivaConfiguration.save( configuration );
663
664         // Release existing
665         //release( archivaConfiguration );
666
667         // Reload.
668         archivaConfiguration = lookup( ArchivaConfiguration.class, "test-autodetect-v1" );
669         configuration = archivaConfiguration.getConfiguration();
670
671         // Test that only 1 set of repositories exist.
672         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositories().size() );
673         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositoriesAsMap().size() );
674         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositories().size() );
675         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositoriesAsMap().size() );
676         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories().size() );
677
678         String actualXML = FileUtils.readFileToString( userFile, null );
679         XMLAssert.assertXpathNotExists( "//configuration/repositories/repository", actualXML );
680         XMLAssert.assertXpathNotExists( "//configuration/repositories", actualXML );
681     }
682
683     @Test
684     public void testArchivaV1()
685         throws Exception
686     {
687         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-archiva-v1" );
688
689         Configuration configuration = archivaConfiguration.getConfiguration();
690         assertConfiguration( configuration, 2, 2, 2 );
691         assertEquals( "check network proxies", 1, configuration.getNetworkProxies().size() );
692
693         assertEquals( "check managed repositories", 2, configuration.getManagedRepositories().size() );
694         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories().size() );
695
696         Map<String, ManagedRepositoryConfiguration> map = configuration.getManagedRepositoriesAsMap();
697
698         ManagedRepositoryConfiguration repository = map.get( "internal" );
699         assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
700                       repository.getLocation() );
701         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName() );
702         assertEquals( "check managed repositories", "internal", repository.getId() );
703         assertEquals( "check managed repositories", "default", repository.getLayout() );
704         assertTrue( "check managed repositories", repository.isScanned() );
705         assertFalse( "check managed repositories", repository.isSnapshots() );
706
707         repository = map.get( "snapshots" );
708         assertEquals( "check managed repositories", "${appserver.base}/repositories/snapshots",
709                       repository.getLocation() );
710         assertEquals( "check managed repositories", "Archiva Managed Snapshot Repository", repository.getName() );
711         assertEquals( "check managed repositories", "snapshots", repository.getId() );
712         assertEquals( "check managed repositories", "default", repository.getLayout() );
713         assertFalse( "check managed repositories", repository.isScanned() );
714         assertTrue( "check managed repositories", repository.isSnapshots() );
715     }
716
717     @Test
718     public void testCronExpressionsWithComma()
719         throws Exception
720     {
721         File baseFile = getTestFile( "target/test/test-file.xml" );
722         baseFile.delete();
723         assertFalse( baseFile.exists() );
724
725         File userFile = getTestFile( "target/test/test-file-user.xml" );
726         userFile.delete();
727         assertFalse( userFile.exists() );
728
729         baseFile.getParentFile().mkdirs();
730         FileUtils.copyFile( getTestFile( "src/test/conf/escape-cron-expressions.xml" ), baseFile );
731
732         userFile.getParentFile().mkdirs();
733         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
734
735         final ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-cron-expressions" );
736
737         archivaConfiguration.reload();
738
739         Configuration configuration = archivaConfiguration.getConfiguration();
740
741         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories().get( 0 );
742
743         assertEquals( "check cron expression", "0 0,30 * * * ?", repository.getRefreshCronExpression().trim() );
744
745         // add a test listener to confirm it doesn't see the escaped format. We don't need to test the number of calls,
746         // etc. as it's done in other tests
747         archivaConfiguration.addListener( new ConfigurationListener()
748         {
749             public void configurationEvent( ConfigurationEvent event )
750             {
751                 assertEquals( ConfigurationEvent.SAVED, event.getType() );
752
753             }
754         } );
755
756         archivaConfiguration.save( configuration );
757
758         configuration = archivaConfiguration.getConfiguration();
759
760         // test for the escape character '\' showing up on repositories.jsp
761         repository.setRefreshCronExpression( "0 0,20 0 * * ?" );
762
763         archivaConfiguration.save( configuration );
764
765         repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( "snapshots" );
766
767         assertEquals( "check cron expression", "0 0,20 0 * * ?", repository.getRefreshCronExpression() );
768     }
769
770     @Test
771     public void testRemoveLastElements()
772         throws Exception
773     {
774         File baseFile = getTestFile( "target/test/test-file.xml" );
775         baseFile.delete();
776         assertFalse( baseFile.exists() );
777
778         File userFile = getTestFile( "target/test/test-file-user.xml" );
779         userFile.delete();
780         assertFalse( userFile.exists() );
781
782         baseFile.getParentFile().mkdirs();
783         FileUtils.copyFile( getTestFile( "src/test/conf/conf-single-list-elements.xml" ), baseFile );
784
785         userFile.getParentFile().mkdirs();
786         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
787
788         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-remove-central" );
789
790         archivaConfiguration.reload();
791
792         Configuration configuration = archivaConfiguration.getConfiguration();
793
794         RepositoryGroupConfiguration repositoryGroup = configuration.getRepositoryGroups().get( 0 );
795         assertNotNull( repositoryGroup );
796         configuration.removeRepositoryGroup( repositoryGroup );
797         assertTrue( configuration.getRepositoryGroups().isEmpty() );
798
799         RemoteRepositoryConfiguration repository = configuration.getRemoteRepositoriesAsMap().get( "central" );
800         assertNotNull( repository );
801         configuration.removeRemoteRepository( repository );
802         assertTrue( configuration.getRemoteRepositories().isEmpty() );
803
804         ManagedRepositoryConfiguration managedRepository =
805             configuration.getManagedRepositoriesAsMap().get( "snapshots" );
806         assertNotNull( managedRepository );
807         configuration.removeManagedRepository( managedRepository );
808         assertTrue( configuration.getManagedRepositories().isEmpty() );
809
810         ProxyConnectorConfiguration proxyConnector = configuration.getProxyConnectors().get( 0 );
811         assertNotNull( proxyConnector );
812         configuration.removeProxyConnector( proxyConnector );
813         assertTrue( configuration.getProxyConnectors().isEmpty() );
814
815         NetworkProxyConfiguration networkProxy = configuration.getNetworkProxiesAsMap().get( "proxy" );
816         assertNotNull( networkProxy );
817         configuration.removeNetworkProxy( networkProxy );
818         assertTrue( configuration.getNetworkProxies().isEmpty() );
819
820         LegacyArtifactPath path = configuration.getLegacyArtifactPaths().get( 0 );
821         assertNotNull( path );
822         configuration.removeLegacyArtifactPath( path );
823         assertTrue( configuration.getLegacyArtifactPaths().isEmpty() );
824
825         RepositoryScanningConfiguration scanning = configuration.getRepositoryScanning();
826         String consumer = scanning.getKnownContentConsumers().get( 0 );
827         assertNotNull( consumer );
828         scanning.removeKnownContentConsumer( consumer );
829         // default values
830         assertFalse( scanning.getKnownContentConsumers().isEmpty() );
831         consumer = scanning.getInvalidContentConsumers().get( 0 );
832         assertNotNull( consumer );
833         scanning.removeInvalidContentConsumer( consumer );
834         assertTrue( scanning.getInvalidContentConsumers().isEmpty() );
835
836         archivaConfiguration.save( configuration );
837
838         archivaConfiguration = lookup( ArchivaConfiguration.class, "test-read-saved" );
839         configuration = archivaConfiguration.getConfiguration();
840         assertNull( configuration.getRemoteRepositoriesAsMap().get( "central" ) );
841         assertTrue( configuration.getRepositoryGroups().isEmpty() );
842         assertNull( configuration.getManagedRepositoriesAsMap().get( "snapshots" ) );
843         assertTrue( configuration.getProxyConnectors().isEmpty() );
844         assertNull( configuration.getNetworkProxiesAsMap().get( "proxy" ) );
845         assertTrue( configuration.getLegacyArtifactPaths().isEmpty() );
846         scanning = configuration.getRepositoryScanning();
847         assertFalse( scanning.getKnownContentConsumers().isEmpty() );
848         assertTrue( scanning.getInvalidContentConsumers().isEmpty() );
849     }
850
851     /**
852      * [MRM-582] Remote Repositories with empty <username> and <password> fields shouldn't be created in configuration.
853      */
854     @Test
855     public void testGetConfigurationFixEmptyRemoteRepoUsernamePassword()
856         throws Exception
857     {
858         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-configuration" );
859
860         archivaConfiguration.reload();
861         Configuration configuration = archivaConfiguration.getConfiguration();
862         assertConfiguration( configuration, 2, 2, 2 );
863         assertEquals( "check remote repositories", 2, configuration.getRemoteRepositories().size() );
864
865         RemoteRepositoryConfiguration repository =
866             configuration.getRemoteRepositoriesAsMap().get( "maven2-repository.dev.java.net" );
867
868         assertEquals( "remote repository.url", "https://maven2-repository.dev.java.net/nonav/repository",
869                       repository.getUrl() );
870         assertEquals( "remote repository.name", "Java.net Repository for Maven 2", repository.getName() );
871         assertEquals( "remote repository.id", "maven2-repository.dev.java.net", repository.getId() );
872         assertEquals( "remote repository.layout", "default", repository.getLayout() );
873         assertNull( "remote repository.username == null", repository.getUsername() );
874         assertNull( "remote repository.password == null", repository.getPassword() );
875     }
876 }