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