]> source.dussan.org Git - archiva.git/blob
2ecdc0dc668f3b5a841bdfdca72634ed8dcfdb62
[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.commons.io.FileUtils;
25 import org.apache.commons.lang.StringUtils;
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.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 =
310             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
311
312         Configuration configuration = new Configuration( );
313         configuration.setWebapp( new WebappConfiguration( ) );
314         configuration.getWebapp( ).setUi( new UserInterfaceOptions( ) );
315         configuration.getWebapp( ).getUi( ).setAppletFindEnabled( false );
316
317         archivaConfiguration.save( configuration );
318
319         assertTrue( "Check file exists", userFile.exists( ) );
320         assertFalse( "Check file not created", baseFile.exists( ) );
321
322         // check it
323         configuration = archivaConfiguration.getConfiguration( );
324         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
325     }
326
327     @Test
328     public void testStoreConfigurationLoadedFromDefaults( )
329         throws Exception
330     {
331         File baseFile = getTestFile( "target/test/test-file.xml" );
332         baseFile.delete( );
333         assertFalse( baseFile.exists( ) );
334
335         File userFile = getTestFile( "target/test/test-file-user.xml" );
336         userFile.delete( );
337         assertFalse( userFile.exists( ) );
338
339         ArchivaConfiguration archivaConfiguration =
340             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user-defaults" );
341
342         archivaConfiguration.reload( );
343
344         Configuration configuration = new Configuration( );
345         configuration.setWebapp( new WebappConfiguration( ) );
346         configuration.getWebapp( ).setUi( new UserInterfaceOptions( ) );
347         configuration.getWebapp( ).getUi( ).setAppletFindEnabled( false );
348
349         // add a change listener
350         MockControl control = createConfigurationListenerMockControl( );
351         ConfigurationListener listener = (ConfigurationListener) control.getMock( );
352         archivaConfiguration.addListener( listener );
353
354         listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
355
356         control.setVoidCallable( 1 );
357
358         control.replay( );
359
360         archivaConfiguration.save( configuration );
361
362         control.verify( );
363
364         assertTrue( "Check file exists", userFile.exists( ) );
365         assertFalse( "Check file not created", baseFile.exists( ) );
366
367         // check it
368         configuration = archivaConfiguration.getConfiguration( );
369         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
370     }
371
372     @Test
373     public void testDefaultUserConfigFilename( )
374         throws Exception
375     {
376         DefaultArchivaConfiguration archivaConfiguration =
377             (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class, "default" );
378
379         archivaConfiguration.reload( );
380
381         assertEquals( System.getProperty( "user.home" ) + "/.m2/archiva.xml",
382                       archivaConfiguration.getUserConfigFilename( ) );
383         assertEquals( System.getProperty( "appserver.base", "${appserver.base}" ) + "/conf/archiva.xml",
384                       archivaConfiguration.getAltConfigFilename( ) );
385     }
386
387     @Test
388     public void testStoreConfigurationFallback( )
389         throws Exception
390     {
391         File baseFile = getTestFile( "target/test/test-file.xml" );
392         baseFile.delete( );
393         assertFalse( baseFile.exists( ) );
394
395         File userFile = getTestFile( "target/test/test-file-user.xml" );
396         userFile.delete( );
397         assertFalse( userFile.exists( ) );
398
399         baseFile.getParentFile( ).mkdirs( );
400         FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
401
402         ArchivaConfiguration archivaConfiguration =
403             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user-fallback" );
404
405         archivaConfiguration.reload( );
406
407         Configuration configuration = new Configuration( );
408         configuration.setWebapp( new WebappConfiguration( ) );
409         configuration.getWebapp( ).setUi( new UserInterfaceOptions( ) );
410         configuration.getWebapp( ).getUi( ).setAppletFindEnabled( false );
411
412         archivaConfiguration.save( configuration );
413
414         assertTrue( "Check file exists", baseFile.exists( ) );
415         assertFalse( "Check file not created", userFile.exists( ) );
416
417         // check it
418         configuration = archivaConfiguration.getConfiguration( );
419         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
420     }
421
422     @Test
423     public void testStoreConfigurationFailsWhenReadFromBothLocationsNoLists( )
424         throws Exception
425     {
426         File baseFile = getTestFile( "target/test/test-file.xml" );
427         baseFile.delete( );
428         assertFalse( baseFile.exists( ) );
429
430         File userFile = getTestFile( "target/test/test-file-user.xml" );
431         userFile.delete( );
432         assertFalse( userFile.exists( ) );
433
434         baseFile.getParentFile( ).mkdirs( );
435         FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
436
437         userFile.getParentFile( ).mkdirs( );
438         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
439
440         ArchivaConfiguration archivaConfiguration =
441             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
442
443         archivaConfiguration.reload( );
444
445         Configuration configuration = archivaConfiguration.getConfiguration( );
446         assertTrue( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
447
448         configuration.getWebapp( ).getUi( ).setAppletFindEnabled( false );
449
450         archivaConfiguration.save( configuration );
451
452         assertTrue( "Check file exists", baseFile.exists( ) );
453         assertEquals( "Check base file is unchanged", "<configuration/>",
454                       FileUtils.readFileToString( baseFile, null ) );
455         assertTrue( "Check file exists", userFile.exists( ) );
456         assertFalse( "Check base file is changed",
457                      "<configuration/>".equals( FileUtils.readFileToString( userFile, null ) ) );
458
459         // check it
460         configuration = archivaConfiguration.getConfiguration( );
461         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
462     }
463
464     @Test
465     public void testStoreConfigurationFailsWhenReadFromBothLocationsUserHasLists( )
466         throws Exception
467     {
468         File baseFile = getTestFile( "target/test/test-file.xml" );
469         baseFile.delete( );
470         assertFalse( baseFile.exists( ) );
471
472         File userFile = getTestFile( "target/test/test-file-user.xml" );
473         userFile.delete( );
474         assertFalse( userFile.exists( ) );
475
476         userFile.getParentFile( ).mkdirs( );
477         FileUtils.copyFile( getTestFile( "src/test/conf/conf-user.xml" ), userFile );
478
479         baseFile.getParentFile( ).mkdirs( );
480         FileUtils.writeStringToFile( baseFile, "<configuration/>", null );
481
482         ArchivaConfiguration archivaConfiguration =
483             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
484
485         archivaConfiguration.reload( );
486
487         Configuration configuration = archivaConfiguration.getConfiguration( );
488         assertTrue( "check value", configuration.getWebapp( ).getUi( ).isShowFindArtifacts( ) );
489
490         configuration.getWebapp( ).getUi( ).setShowFindArtifacts( false );
491
492         archivaConfiguration.save( configuration );
493
494         assertTrue( "Check file exists", baseFile.exists( ) );
495         assertEquals( "Check base file is unchanged", "<configuration/>",
496                       FileUtils.readFileToString( baseFile, null ) );
497         assertTrue( "Check file exists", userFile.exists( ) );
498         assertFalse( "Check base file is changed",
499                      "<configuration/>".equals( FileUtils.readFileToString( userFile, null ) ) );
500
501         // check it
502         configuration = archivaConfiguration.getConfiguration( );
503         assertFalse( "check value", configuration.getWebapp( ).getUi( ).isShowFindArtifacts( ) );
504     }
505
506     @Test
507     public void testStoreConfigurationFailsWhenReadFromBothLocationsAppserverHasLists( )
508         throws Exception
509     {
510         File baseFile = getTestFile( "target/test/test-file.xml" );
511         baseFile.delete( );
512         assertFalse( baseFile.exists( ) );
513
514         File userFile = getTestFile( "target/test/test-file-user.xml" );
515         userFile.delete( );
516         assertFalse( userFile.exists( ) );
517
518         baseFile.getParentFile( ).mkdirs( );
519         FileUtils.copyFile( getTestFile( "src/test/conf/conf-base.xml" ), baseFile );
520
521         userFile.getParentFile( ).mkdirs( );
522         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
523
524         ArchivaConfiguration archivaConfiguration =
525             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-save-user" );
526
527         archivaConfiguration.reload( );
528
529         Configuration configuration = archivaConfiguration.getConfiguration( );
530         assertTrue( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
531
532         configuration.getWebapp( ).getUi( ).setAppletFindEnabled( false );
533
534         try
535         {
536             archivaConfiguration.save( configuration );
537             fail( "Configuration saving should not succeed if it was loaded from two locations" );
538         }
539         catch ( IndeterminateConfigurationException e )
540         {
541             // check it was reverted
542             configuration = archivaConfiguration.getConfiguration( );
543             assertTrue( "check value", configuration.getWebapp( ).getUi( ).isAppletFindEnabled( ) );
544         }
545     }
546
547     @Test
548     public void testLoadConfigurationFromInvalidBothLocationsOnDisk( )
549         throws Exception
550     {
551         ArchivaConfiguration archivaConfiguration =
552             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-both" );
553         Configuration config = archivaConfiguration.getConfiguration( );
554
555         try
556         {
557             archivaConfiguration.save( config );
558             fail( "Should have thrown a RegistryException because the configuration can't be saved." );
559         }
560         catch ( RegistryException e )
561         {
562             /* expected exception */
563         }
564     }
565
566     @Test
567     public void testLoadConfigurationFromInvalidUserLocationOnDisk( )
568         throws Exception
569     {
570         File testConfDir = getTestFile( "target/test-appserver-base/conf/" );
571         testConfDir.mkdirs( );
572
573         ArchivaConfiguration archivaConfiguration =
574             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-not-allowed-to-write-to-user" );
575         Configuration config = archivaConfiguration.getConfiguration( );
576         archivaConfiguration.save( config );
577         // No Exception == test passes.
578         // Expected Path is: Should not have thrown an exception.
579     }
580
581     @Test
582     public void testConfigurationUpgradeFrom09( )
583         throws Exception
584     {
585         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-09" );
586
587         // we just use the defaults when upgrading from 0.9 at this point.
588         Configuration configuration = archivaConfiguration.getConfiguration( );
589         // test-upgrade-09 contains a managed with id: local so it's 3 managed
590         assertConfiguration( configuration, 3, 1, 1 );
591         assertEquals( "check network proxies", 0, configuration.getNetworkProxies( ).size( ) );
592
593         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories( ).get( 0 );
594
595         assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
596                       repository.getLocation( ) );
597         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName( ) );
598         assertEquals( "check managed repositories", "internal", repository.getId( ) );
599         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
600         assertTrue( "check managed repositories", repository.isScanned( ) );
601     }
602
603     @Test
604     public void testConfigurationUpgradeFrom13( )
605         throws Exception
606     {
607         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-upgrade-1.3" );
608
609         // we just use the defaults when upgrading from 1.3 at this point.
610         Configuration configuration = archivaConfiguration.getConfiguration( );
611         assertConfiguration( configuration, 2, 2, 2 );
612         assertEquals( "check network proxies", 0, configuration.getNetworkProxies( ).size( ) );
613
614         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories( ).get( 0 );
615
616         assertEquals( "check managed repositories", "${appserver.base}/data/repositories/internal",
617                       repository.getLocation( ) );
618         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName( ) );
619         assertEquals( "check managed repositories", "internal", repository.getId( ) );
620         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
621         assertTrue( "check managed repositories", repository.isScanned( ) );
622
623         log.info( "knowContentConsumers " + configuration.getRepositoryScanning( ).getKnownContentConsumers( ) );
624
625         assertFalse(
626             configuration.getRepositoryScanning( ).getKnownContentConsumers( ).contains( "update-db-artifact" ) );
627         assertFalse( configuration.getRepositoryScanning( ).getKnownContentConsumers( ).contains(
628             "update-db-repository-metadata" ) );
629
630         assertTrue(
631             configuration.getRepositoryScanning( ).getKnownContentConsumers( ).contains( "create-archiva-metadata" ) );
632
633         assertTrue(
634             configuration.getRepositoryScanning( ).getKnownContentConsumers( ).contains( "duplicate-artifacts" ) );
635     }
636
637     @Test
638     public void testAutoDetectV1( )
639         throws Exception
640     {
641         // Setup the autodetect-v1.xml file in the target directory (so we can save/load it)
642         File userFile = getTestFile( "target/test-autodetect-v1/archiva-user.xml" );
643         userFile.delete( );
644         assertFalse( userFile.exists( ) );
645
646         userFile.getParentFile( ).mkdirs( );
647         FileUtils.copyFile( getTestFile( "src/test/conf/autodetect-v1.xml" ), userFile );
648
649         // Load the original (unconverted) archiva.xml
650         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-autodetect-v1" );
651
652         archivaConfiguration.reload( );
653
654         Configuration configuration = archivaConfiguration.getConfiguration( );
655         assertConfiguration( configuration, 2, 2, 2 );
656         assertEquals( "check network proxies", 1, configuration.getNetworkProxies( ).size( ) );
657
658         ManagedRepositoryConfiguration repository = configuration.getManagedRepositories( ).get( 0 );
659
660         assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
661                       repository.getLocation( ) );
662         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName( ) );
663         assertEquals( "check managed repositories", "internal", repository.getId( ) );
664         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
665         assertTrue( "check managed repositories", repository.isScanned( ) );
666
667         // Test that only 1 set of repositories exist.
668         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositories( ).size( ) );
669         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositories( ).size( ) );
670         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories( ).size( ) );
671
672         // Save the file.
673         archivaConfiguration.save( configuration );
674
675         // Release existing
676         //release( archivaConfiguration );
677
678         // Reload.
679         archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "test-autodetect-v1" );
680         configuration = archivaConfiguration.getConfiguration( );
681
682         // Test that only 1 set of repositories exist.
683         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositories( ).size( ) );
684         assertEquals( "check managed repositories size.", 2, configuration.getManagedRepositoriesAsMap( ).size( ) );
685         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositories( ).size( ) );
686         assertEquals( "check remote repositories size.", 2, configuration.getRemoteRepositoriesAsMap( ).size( ) );
687         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories( ).size( ) );
688
689         String actualXML = FileUtils.readFileToString( userFile, null );
690         XMLAssert.assertXpathNotExists( "//configuration/repositories/repository", actualXML );
691         XMLAssert.assertXpathNotExists( "//configuration/repositories", actualXML );
692     }
693
694     @Test
695     public void testArchivaV1( )
696         throws Exception
697     {
698         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-archiva-v1" );
699
700         Configuration configuration = archivaConfiguration.getConfiguration( );
701         assertConfiguration( configuration, 2, 2, 2 );
702         assertEquals( "check network proxies", 1, configuration.getNetworkProxies( ).size( ) );
703
704         assertEquals( "check managed repositories", 2, configuration.getManagedRepositories( ).size( ) );
705         assertEquals( "check v1 repositories size.", 0, configuration.getRepositories( ).size( ) );
706
707         Map<String, ManagedRepositoryConfiguration> map = configuration.getManagedRepositoriesAsMap( );
708
709         ManagedRepositoryConfiguration repository = map.get( "internal" );
710         assertEquals( "check managed repositories", "${appserver.base}/repositories/internal",
711                       repository.getLocation( ) );
712         assertEquals( "check managed repositories", "Archiva Managed Internal Repository", repository.getName( ) );
713         assertEquals( "check managed repositories", "internal", repository.getId( ) );
714         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
715         assertTrue( "check managed repositories", repository.isScanned( ) );
716         assertFalse( "check managed repositories", repository.isSnapshots( ) );
717
718         repository = map.get( "snapshots" );
719         assertEquals( "check managed repositories", "${appserver.base}/repositories/snapshots",
720                       repository.getLocation( ) );
721         assertEquals( "check managed repositories", "Archiva Managed Snapshot Repository", repository.getName( ) );
722         assertEquals( "check managed repositories", "snapshots", repository.getId( ) );
723         assertEquals( "check managed repositories", "default", repository.getLayout( ) );
724         assertFalse( "check managed repositories", repository.isScanned( ) );
725         assertTrue( "check managed repositories", repository.isSnapshots( ) );
726     }
727
728     @Test
729     public void testCronExpressionsWithComma( )
730         throws Exception
731     {
732         File baseFile = getTestFile( "target/test/test-file.xml" );
733         baseFile.delete( );
734         assertFalse( baseFile.exists( ) );
735
736         File userFile = getTestFile( "target/test/test-file-user.xml" );
737         userFile.delete( );
738         assertFalse( userFile.exists( ) );
739
740         baseFile.getParentFile( ).mkdirs( );
741         FileUtils.copyFile( getTestFile( "src/test/conf/escape-cron-expressions.xml" ), baseFile );
742
743         userFile.getParentFile( ).mkdirs( );
744         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
745
746         final ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-cron-expressions" );
747
748         archivaConfiguration.reload( );
749
750         Configuration configuration = archivaConfiguration.getConfiguration( );
751
752         ManagedRepositoryConfiguration repository =
753             (ManagedRepositoryConfiguration) configuration.getManagedRepositories( ).get( 0 );
754
755         assertEquals( "check cron expression", "0 0,30 * * * ?", repository.getRefreshCronExpression( ).trim( ) );
756
757         // add a test listener to confirm it doesn't see the escaped format. We don't need to test the number of calls,
758         // etc. as it's done in other tests
759         archivaConfiguration.addListener( new ConfigurationListener( )
760         {
761             public void configurationEvent( ConfigurationEvent event )
762             {
763                 assertEquals( ConfigurationEvent.SAVED, event.getType( ) );
764
765             }
766         } );
767
768         archivaConfiguration.save( configuration );
769
770         configuration = archivaConfiguration.getConfiguration( );
771
772         // test for the escape character '\' showing up on repositories.jsp
773         repository.setRefreshCronExpression( "0 0,20 0 * * ?" );
774
775         archivaConfiguration.save( configuration );
776
777         repository = archivaConfiguration.getConfiguration( ).findManagedRepositoryById( "snapshots" );
778
779         assertEquals( "check cron expression", "0 0,20 0 * * ?", repository.getRefreshCronExpression( ) );
780     }
781
782     @Test
783     public void testRemoveLastElements( )
784         throws Exception
785     {
786         File baseFile = getTestFile( "target/test/test-file.xml" );
787         baseFile.delete( );
788         assertFalse( baseFile.exists( ) );
789
790         File userFile = getTestFile( "target/test/test-file-user.xml" );
791         userFile.delete( );
792         assertFalse( userFile.exists( ) );
793
794         baseFile.getParentFile( ).mkdirs( );
795         FileUtils.copyFile( getTestFile( "src/test/conf/conf-single-list-elements.xml" ), baseFile );
796
797         userFile.getParentFile( ).mkdirs( );
798         FileUtils.writeStringToFile( userFile, "<configuration/>", null );
799
800         ArchivaConfiguration archivaConfiguration = lookup( ArchivaConfiguration.class, "test-remove-central" );
801
802         archivaConfiguration.reload( );
803
804         Configuration configuration = archivaConfiguration.getConfiguration( );
805
806         RepositoryGroupConfiguration repositoryGroup =
807             (RepositoryGroupConfiguration) configuration.getRepositoryGroups( ).get( 0 );
808         assertNotNull( repositoryGroup );
809         configuration.removeRepositoryGroup( repositoryGroup );
810         assertTrue( configuration.getRepositoryGroups( ).isEmpty( ) );
811
812         RemoteRepositoryConfiguration repository = configuration.getRemoteRepositoriesAsMap( ).get( "central" );
813         assertNotNull( repository );
814         configuration.removeRemoteRepository( repository );
815         assertTrue( configuration.getRemoteRepositories( ).isEmpty( ) );
816
817         ManagedRepositoryConfiguration managedRepository =
818             configuration.getManagedRepositoriesAsMap( ).get( "snapshots" );
819         assertNotNull( managedRepository );
820         configuration.removeManagedRepository( managedRepository );
821         assertTrue( configuration.getManagedRepositories( ).isEmpty( ) );
822
823         ProxyConnectorConfiguration proxyConnector = configuration.getProxyConnectors( ).get( 0 );
824         assertNotNull( proxyConnector );
825         configuration.removeProxyConnector( proxyConnector );
826         assertTrue( configuration.getProxyConnectors( ).isEmpty( ) );
827
828         NetworkProxyConfiguration networkProxy = configuration.getNetworkProxiesAsMap( ).get( "proxy" );
829         assertNotNull( networkProxy );
830         configuration.removeNetworkProxy( networkProxy );
831         assertTrue( configuration.getNetworkProxies( ).isEmpty( ) );
832
833         LegacyArtifactPath path = configuration.getLegacyArtifactPaths( ).get( 0 );
834         assertNotNull( path );
835         configuration.removeLegacyArtifactPath( path );
836         assertTrue( configuration.getLegacyArtifactPaths( ).isEmpty( ) );
837
838         RepositoryScanningConfiguration scanning = configuration.getRepositoryScanning( );
839         String consumer = scanning.getKnownContentConsumers( ).get( 0 );
840         assertNotNull( consumer );
841         scanning.removeKnownContentConsumer( consumer );
842         // default values
843         assertFalse( scanning.getKnownContentConsumers( ).isEmpty( ) );
844         consumer = scanning.getInvalidContentConsumers( ).get( 0 );
845         assertNotNull( consumer );
846         scanning.removeInvalidContentConsumer( consumer );
847         assertTrue( scanning.getInvalidContentConsumers( ).isEmpty( ) );
848
849         archivaConfiguration.save( configuration );
850
851         archivaConfiguration = lookup( ArchivaConfiguration.class, "test-read-saved" );
852         configuration = archivaConfiguration.getConfiguration( );
853         assertNull( configuration.getRemoteRepositoriesAsMap( ).get( "central" ) );
854         assertTrue( configuration.getRepositoryGroups( ).isEmpty( ) );
855         assertNull( configuration.getManagedRepositoriesAsMap( ).get( "snapshots" ) );
856         assertTrue( configuration.getProxyConnectors( ).isEmpty( ) );
857         assertNull( configuration.getNetworkProxiesAsMap( ).get( "proxy" ) );
858         assertTrue( configuration.getLegacyArtifactPaths( ).isEmpty( ) );
859         scanning = configuration.getRepositoryScanning( );
860         assertFalse( scanning.getKnownContentConsumers( ).isEmpty( ) );
861         assertTrue( scanning.getInvalidContentConsumers( ).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 = lookup( ArchivaConfiguration.class, "test-configuration" );
872
873         archivaConfiguration.reload( );
874         Configuration configuration = archivaConfiguration.getConfiguration( );
875         assertConfiguration( configuration, 2, 2, 2 );
876         assertEquals( "check remote repositories", 2, configuration.getRemoteRepositories( ).size( ) );
877
878         RemoteRepositoryConfiguration repository =
879             configuration.getRemoteRepositoriesAsMap( ).get( "maven2-repository.dev.java.net" );
880
881         assertEquals( "remote repository.url", "https://maven2-repository.dev.java.net/nonav/repository",
882                       repository.getUrl( ) );
883         assertEquals( "remote repository.name", "Java.net Repository for Maven 2", repository.getName( ) );
884         assertEquals( "remote repository.id", "maven2-repository.dev.java.net", repository.getId( ) );
885         assertEquals( "remote repository.layout", "default", repository.getLayout( ) );
886         assertNull( "remote repository.username == null", repository.getUsername( ) );
887         assertNull( "remote repository.password == null", repository.getPassword( ) );
888     }
889 }