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