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